test
[VerusCoin.git] / src / komodo_gateway.h
CommitLineData
9b0e1808 1/******************************************************************************
2 * Copyright © 2014-2016 The SuperNET Developers. *
3 * *
4 * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
5 * the top-level directory of this distribution for the individual copyright *
6 * holder information and the developer policies on copyright and licensing. *
7 * *
8 * Unless otherwise agreed in a custom licensing agreement, no part of the *
9 * SuperNET software, including this file may be copied, modified, propagated *
10 * or distributed except according to the terms contained in the LICENSE file *
11 * *
12 * Removal or modification of this copyright notice is prohibited. *
13 * *
14 ******************************************************************************/
15
f38345e9 16// paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
f38345e9 17
429dabb5 18struct pax_transaction
64bb0834 19{
6210f328 20 UT_hash_handle hh;
429dabb5 21 uint256 txid;
22 uint64_t komodoshis,fiatoshis;
d95563be 23 int32_t marked,height,otherheight;
429dabb5 24 uint16_t vout;
8ec26042 25 char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag;
6210f328 26} *PAX;
27
77be6cd9 28uint64_t komodo_paxtotal()
53c47c90 29{
e5430f52 30 struct pax_transaction *pax,*tmp; uint64_t total = 0;
c169f64b 31 HASH_ITER(hh,PAX,pax,tmp)
32 {
33 if ( pax->marked == 0 )
34 {
35 if ( komodo_is_issuer() != 0 )
36 total += pax->fiatoshis;
37 else total += pax->komodoshis;
38 }
39 }
53c47c90 40 return(total);
2ab43fd2 41}
42
6210f328 43struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout)
44{
45 struct pax_transaction *pax;
46 pthread_mutex_lock(&komodo_mutex);
47 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
48 if ( pax != 0 )
49 memcpy(space,pax,sizeof(*pax));
50 pthread_mutex_unlock(&komodo_mutex);
51 return(pax);
52}
53
b5bf65f5 54struct pax_transaction *komodo_paxmark(int32_t height,struct pax_transaction *space,uint256 txid,uint16_t vout,int32_t mark)
6210f328 55{
56 struct pax_transaction *pax;
57 pthread_mutex_lock(&komodo_mutex);
58 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
2ab43fd2 59 if ( pax == 0 )
60 {
61 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
62 pax->txid = txid;
63 pax->vout = vout;
64 HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
32d525eb 65 //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
2ab43fd2 66 }
6210f328 67 if ( pax != 0 )
68 {
69 pax->marked = mark;
f1e6bb01 70 //int32_t i; for (i=0; i<32; i++)
71 // printf("%02x",((uint8_t *)&txid)[i]);
72 //printf(" paxmark.ht %d vout%d\n",mark,vout);
6210f328 73 memcpy(space,pax,sizeof(*pax));
74 }
75 pthread_mutex_unlock(&komodo_mutex);
76 return(pax);
77}
429dabb5 78
1fc75a27 79void komodo_gateway_deposit(char *coinaddr,uint64_t value,int32_t shortflag,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height,int32_t otherheight) // assetchain context
d43c7a93 80{
b2d64a0f 81 struct pax_transaction *pax; int32_t addflag = 0;
d43c7a93 82 pthread_mutex_lock(&komodo_mutex);
83 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
84 if ( pax == 0 )
b2d64a0f 85 {
d43c7a93 86 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
8d52488f 87 pax->txid = txid;
88 pax->vout = vout;
89 HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
7961668b 90 addflag = 1;
1fc75a27 91 if ( ASSETCHAINS_SYMBOL[0] == 0 )
92 {
93 int32_t i; for (i=0; i<32; i++)
94 printf("%02x",((uint8_t *)&txid)[i]);
95 printf(" v.%d [%s] kht.%d ht.%d create pax.%p\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax);
96 }
b2d64a0f 97 }
8d52488f 98 pthread_mutex_unlock(&komodo_mutex);
d43c7a93 99 if ( coinaddr != 0 )
100 {
101 strcpy(pax->coinaddr,coinaddr);
102 pax->komodoshis = value;
103 pax->shortflag = shortflag;
104 strcpy(pax->symbol,symbol);
105 pax->fiatoshis = fiatoshis;
106 memcpy(pax->rmd160,rmd160,20);
107 pax->height = height;
1fc75a27 108 pax->otherheight = otherheight;
d43c7a93 109 if ( pax->marked == 0 )
7961668b 110 {
111 if ( addflag != 0 )
00cf544e 112 printf("[%s] addflag.%d ADD DEPOSIT %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,symbol,dstr(fiatoshis),coinaddr,height,otherheight,dstr(komodo_paxtotal()));
7961668b 113 }
0dca8d4c 114 //else printf("%p MARKED.%d DEPOSIT %s %.8f -> %s TO PAX ht.%d otherht.%d\n",pax,pax->marked,symbol,dstr(fiatoshis),coinaddr,height,otherheight);
d43c7a93 115 }
116 else
117 {
118 pax->marked = height;
1fc75a27 119 printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
d43c7a93 120 }
d43c7a93 121}
122
05d91eb2 123int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uint16_t *vouts,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
63b289ad 124{
e2c35fcd 125 int32_t i,n=0,j,len;
63b289ad 126 if ( opretbuf[opretlen-5] == '-' )
127 *shortflagp = 1;
128 else *shortflagp = 0;
129 for (i=0; i<4; i++)
130 base[i] = opretbuf[opretlen-4+i];
0e3472c1 131 if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) // shortflag
63b289ad 132 {
133 opretbuf++, opretlen--;
134 for (n=len=0; n<opretlen/34; n++)
135 {
136 for (j=0; j<32; j++)
137 {
138 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
1938746c 139 //printf("%02x",((uint8_t *)&txids[n])[j]);
63b289ad 140 }
141 vouts[n] = opretbuf[len++];
142 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
1938746c 143 //printf(" issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
05d91eb2 144 if ( iskomodo != 0 )
145 {
146 uint64_t fiatoshis; int32_t height,otherheight,shortflag; char symbol[16];
147 len += iguana_rwnum(0,&opretbuf[len],sizeof(fiatoshis),&fiatoshis);
148 len += iguana_rwnum(0,&opretbuf[len],sizeof(height),&height);
149 len += iguana_rwnum(0,&opretbuf[len],sizeof(otherheight),&otherheight);
150 if ( opretbuf[len] == '-' )
151 shortflag = 1, len++;
152 else shortflag = 0;
153 for (i=0; opretbuf[len+i]!=0&&i<3; i++)
154 symbol[i] = opretbuf[len+i];
155 symbol[i] = 0;
156 printf(">>>>>>> iskomodo X: (%s) fiat %.8f kmdheight.%d other.%d short.%d\n",symbol,dstr(fiatoshis),height,otherheight,shortflag);
157 }
63b289ad 158 }
159 }
160 return(n);
161}
162
a3f23ee2 163void komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *symbol,int32_t tokomodo)
429dabb5 164{
77be6cd9 165 struct pax_transaction *pax,*tmp; uint8_t *script,opcode,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1;
429dabb5 166 PENDING_KOMODO_TX = 0;
a3f23ee2 167 if ( tokomodo == 0 )
77be6cd9 168 opcode = 'I';
169 else opcode = 'X';
c169f64b 170 HASH_ITER(hh,PAX,pax,tmp)
429dabb5 171 {
e5430f52 172 if ( pax->marked != 0 )
173 continue;
7ca711ba 174 if ( ASSETCHAINS_SYMBOL[0] != 0 )
175 printf("pax.%p marked.%d %.8f -> %.8f\n",pax,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
e5430f52 176 txNew->vout.resize(numvouts+1);
1fc75a27 177 txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
e5430f52 178 txNew->vout[numvouts].scriptPubKey.resize(25);
179 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
180 *script++ = 0x76;
181 *script++ = 0xa9;
182 *script++ = 20;
183 memcpy(script,pax->rmd160,20), script += 20;
184 *script++ = 0x88;
185 *script++ = 0xac;
186 for (i=0; i<32; i++)
187 {
1938746c 188 //printf("%02x",((uint8_t *)&pax->txid)[i]);
e5430f52 189 data[len++] = ((uint8_t *)&pax->txid)[i];
429dabb5 190 }
e5430f52 191 data[len++] = pax->vout & 0xff;
192 data[len++] = (pax->vout >> 8) & 0xff;
a3f23ee2 193 if ( tokomodo == 0 )
e5430f52 194 PENDING_KOMODO_TX += pax->fiatoshis;
fbc4209b 195 else
196 {
1fc75a27 197 //[{"prev_hash":"5d5c9a49489b558de9e84f991f996dedaae6b9d0f157f82b2fec64662476d5cf","prev_vout":2,"EUR":0.10000000,"fiat":"EUR","kmdheight":57930,"height":153,"KMD":0.78329000,"address":"RDhEGYScNQYetCyG75Kf8Fg61UWPdwc1C5","rmd160":"306c507eea639e7220b3069ed9f49f3bc97eaca1"}]
198 len += iguana_rwnum(1,&data[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
199 len += iguana_rwnum(1,&data[len],sizeof(pax->height),&pax->height);
200 len += iguana_rwnum(1,&data[len],sizeof(pax->otherheight),&pax->otherheight);
201 if ( pax->shortflag != 0 )
202 data[len++] = '-';
203 for (i=0; pax->symbol[i]!=0&&i<3; i++)
204 data[len++] = pax->symbol[i];
205 data[len++] = 0;
fbc4209b 206 PENDING_KOMODO_TX += pax->komodoshis;
05d91eb2 207 printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX));
fbc4209b 208 }
e5430f52 209 if ( numvouts++ >= 64 )
210 break;
429dabb5 211 }
212 if ( numvouts > 1 )
213 {
9ef47e29 214 if ( shortflag != 0 )
a5ad8f02 215 data[len++] = '-';
0e3472c1 216 if ( tokomodo != 0 )
217 strcpy(symbol,"KMD");
9ef47e29 218 for (i=0; symbol[i]!=0; i++)
219 data[len++] = symbol[i];
a5ad8f02 220 data[len++] = 0;
77be6cd9 221 opretlen = komodo_opreturnscript(opret,opcode,data,len);
50824530 222 txNew->vout.resize(numvouts+1);
223 txNew->vout[numvouts].nValue = 0;
224 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
225 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
429dabb5 226 memcpy(script,opret,opretlen);
23d955d4 227 printf("MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
9ef47e29 228 }
429dabb5 229}
230
8f64a3a9 231int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
557d9a23 232{
81041ab2 233 int32_t i,j,n,num,opretlen,offset=1,errs=0,matched=0; uint256 hash,txids[64]; uint8_t shortflag; char symbol[16],base[16]; uint16_t vouts[64]; uint8_t *script,opcode; struct pax_transaction *pax,space;
557d9a23 234 n = block.vtx[0].vout.size();
235 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
12d47153 236 if ( n <= 2 || script[0] != 0x6a )
237 return(0);
238 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
77be6cd9 239 if ( ASSETCHAINS_SYMBOL[0] == 0 )
240 {
76aa815e 241 for (i=0; i<opretlen; i++)
242 printf("%02x",script[i]);
243 printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
77be6cd9 244 opcode = 'X';
245 strcpy(symbol,"KMD");
246 }
247 else
248 {
249 strcpy(symbol,ASSETCHAINS_SYMBOL);
250 opcode = 'I';
251 }
252 if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
557d9a23 253 {
05d91eb2 254 if ( (num= komodo_issued_opreturn(&shortflag,base,txids,vouts,&script[offset],opretlen,opcode == 'X')) > 0 )
557d9a23 255 {
256 for (i=1; i<n-1; i++)
257 {
f10b8e2e 258 if ( (pax= komodo_paxfind(&space,txids[i-1],vouts[i-1])) != 0 )
5a2c5b6f 259 {
f10b8e2e 260 if ( ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) )
261 {
262 if ( pax->marked != 0 )
263 errs++;
264 else matched++;
05d91eb2 265 if ( opcode == 'X' )
266 printf("errs.%d i.%d match %.8f == %.8f\n",errs,i,dstr(pax != 0 ? pax->fiatoshis:-1),dstr(block.vtx[0].vout[i].nValue));
f10b8e2e 267 }
268 else
269 {
270 hash = block.GetHash();
05d91eb2 271 if ( opcode == 'X' )
272 {
273 for (j=0; j<32; j++)
274 printf("%02x",((uint8_t *)&hash)[j]);
275 printf(" ht.%d blockhash couldnt find vout.[%d]\n",height,i);
276 }
f10b8e2e 277 }
5a2c5b6f 278 }
cb040aa1 279 else
280 {
05d91eb2 281 if ( opcode == 'X' )
282 {
283 for (j=0; j<32; j++)
284 printf("%02x",((uint8_t *)&txids[i-1])[j]);
285 printf(" cant paxfind txid\n");
286 }
cb040aa1 287 }
f6b5fa43 288 komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height);
557d9a23 289 }
81041ab2 290 if ( matched != num )
291 {
0dca8d4c 292 // can easily happen depending on order of loading
81041ab2 293 }
557d9a23 294 }
b98053e2 295 //printf("opretlen.%d num.%d\n",opretlen,num);
557d9a23 296 }
297 return(0);
298}
299
429dabb5 300const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout)
301{
15b7788a 302 uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo,kmdheight; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; double diff; uint64_t seed; int64_t fiatoshis,checktoshis; const char *typestr = "unknown";
95db8239 303 tokomodo = (komodo_is_issuer() == 0);
1938746c 304 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
ad7b6271 305 {
306 for (i=0; i<opretlen; i++)
307 printf("%02x",opretbuf[i]);
308 printf(" opret[%c] else path tokomodo.%d ht.%d\n",opretbuf[0],tokomodo,height);
309 }
0a611c1e 310 if ( opretbuf[0] == 'D' )
64bb0834 311 {
05a37d87 312 if ( opretlen == 38 ) // any KMD tx
64bb0834 313 {
05a37d87 314 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
abdd8672 315 memset(base,0,sizeof(base));
316 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
317 if ( fiatoshis < 0 )
318 fiatoshis = -fiatoshis;
319 bitcoin_address(coinaddr,addrtype,rmd160,20);
320 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
321 typestr = "deposit";
15b7788a 322 printf("kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
323 diff = ((double)value / checktoshis) - 1.;
324 if ( diff < 0. )
325 diff = -diff;
05a37d87 326 if ( kmdheight <= height )
6e576848 327 {
15b7788a 328 if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 && shortflag == ASSETCHAINS_SHORTFLAG )
05a37d87 329 {
330 if ( shortflag == 0 )
331 {
332 for (i=0; i<32; i++)
333 printf("%02x",((uint8_t *)&txid)[i]);
334 printf(" <- txid.v%u ",vout);
335 for (i=0; i<33; i++)
336 printf("%02x",pubkey33[i]);
337 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
15b7788a 338 if ( value >= checktoshis || (seed == 0 && diff < .01) )
05a37d87 339 {
340 if ( komodo_paxfind(&space,txid,vout) == 0 )
31e168df 341 {
1fc75a27 342 komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,kmdheight,height);
31e168df 343 } else printf("duplicate deposit\n");
05a37d87 344 }
345 }
346 else // short
755ead98 347 {
05a37d87 348 for (i=0; i<opretlen; i++)
349 printf("%02x",opretbuf[i]);
350 printf(" opret[%c] tokomodo.%d value %.8f vs check %.8f\n",opretbuf[0],tokomodo,dstr(value),dstr(checktoshis));
15b7788a 351 if ( value <= checktoshis || (seed == 0 && diff < .01) )
05a37d87 352 {
353
354 }
755ead98 355 }
8e3430ee 356 }
8bc1bcad 357 }
64bb0834 358 }
359 }
402d5759 360 else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
429dabb5 361 {
755ead98 362 if ( tokomodo == 0 && opretbuf[0] == 'I' ) // assetchain coinbase
429dabb5 363 {
05d91eb2 364 if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen,0)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG )
429dabb5 365 {
63b289ad 366 for (i=0; i<n; i++)
ec35af90 367 {
1938746c 368 //for (j=0; j<32; j++)
369 // printf("%02x",((uint8_t *)&txids[i])[j]);
b5bf65f5 370 if ( komodo_paxmark(height,&space,txids[i],vouts[i],height) == 0 )
31e168df 371 {
1fc75a27 372 komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height,0);
31e168df 373 }
f6b5fa43 374 else
375 {
1938746c 376 //printf(" duplicate issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen);
f6b5fa43 377 }
ec35af90 378 }
429dabb5 379 }
429dabb5 380 }
0920fed9 381 else if ( tokomodo != 0 && opretbuf[0] == 'X' )
9ef47e29 382 {
383 // verify and update limits
384 }
429dabb5 385 }
64bb0834 386 return(typestr);
387}
388
eae0d2e5 389void komodo_gateway_voutupdate(char *symbol,int32_t isspecial,int32_t height,int32_t txi,bits256 txid,int32_t vout,int32_t numvouts,uint64_t value,uint8_t *script,int32_t len)
f38345e9 390{
eae0d2e5 391 int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr;
57abdbaf 392 typestr = "unknown";
eae0d2e5 393 memcpy(&utxid,&txid,sizeof(utxid));
64bb0834 394 if ( script[offset++] == 0x6a )
654330ab 395 {
b673d264 396 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
557d9a23 397 if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 )
654330ab 398 typestr = "notarized";
7eca18ee 399 else if ( txi == 0 && vout == 1 && opretlen == 149 )
9b9adadc 400 {
654330ab 401 typestr = "pricefeed";
e7e817f7 402 komodo_paxpricefeed(height,&script[offset],opretlen);
0765bc71 403 //printf("height.%d pricefeed len.%d\n",height,opretlen);
9b9adadc 404 }
eae0d2e5 405 else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout);
654330ab 406 }
755ead98 407 else if ( numvouts >= KOMODO_MINRATIFY )
654330ab 408 typestr = "ratify";
f38345e9 409}
410
c59a3beb 411int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
c42b46db 412{
ee0744d2 413 char *retstr,params[256],*hexstr; uint8_t script[10000]; cJSON *oldpub,*newpub,*json,*result,*vouts,*item,*sobj; int32_t vout,n,len,isspecial,retval = -1; uint64_t value; bits256 txid;
f38345e9 414 sprintf(params,"[\"%s\", 1]",txidstr);
415 if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 )
c42b46db 416 {
f38345e9 417 if ( (json= cJSON_Parse(retstr)) != 0 )
c42b46db 418 {
26fa1236 419 if ( (result= jobj(json,(char *)"result")) != 0 )
2c6bdd83 420 {
ee0744d2 421 oldpub = jobj(result,(char *)"vpub_old");
422 newpub = jobj(result,(char *)"vpub_new");
c59a3beb 423 retval = 0;
ee0744d2 424 if ( oldpub == 0 && newpub == 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
2c6bdd83 425 {
26fa1236 426 isspecial = 0;
427 txid = jbits256(result,(char *)"txid");
428 for (vout=0; vout<n; vout++)
2c6bdd83 429 {
26fa1236 430 item = jitem(vouts,vout);
431 value = SATOSHIDEN * jdouble(item,(char *)"value");
432 if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
2c6bdd83 433 {
26fa1236 434 if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
f38345e9 435 {
26fa1236 436 len = (int32_t)strlen(hexstr) >> 1;
437 if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) )
438 isspecial = 1;
439 else if ( len <= sizeof(script) )
440 {
441 decode_hex(script,len,hexstr);
442 komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len);
443 }
f38345e9 444 }
2c6bdd83 445 }
446 }
2c6bdd83 447 }
a10aafcc 448 } else printf("error getting txids.(%s) %p\n",retstr,result);
f38345e9 449 free_json(json);
450 }
451 free(retstr);
c42b46db 452 }
c59a3beb 453 return(retval);
c42b46db 454}
455
c59a3beb 456int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
9b0e1808 457{
d4c346d6 458 char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2;
f38345e9 459 sprintf(params,"[%d]",height);
460 if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 )
461 {
e14a749a 462 if ( (result= cJSON_Parse(retstr)) != 0 )
f38345e9 463 {
f686d848 464 if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
f38345e9 465 {
e14a749a 466 sprintf(params,"[\"%s\"]",txidstr);
467 if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
f38345e9 468 {
f686d848 469 //printf("getblock.(%s)\n",retstr2);
e14a749a 470 if ( (json= cJSON_Parse(retstr2)) != 0 )
f38345e9 471 {
f686d848 472 if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
e14a749a 473 {
474 for (i=0; i<n; i++)
c59a3beb 475 if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
476 break;
477 if ( i == n )
478 retval = 0;
4f593916 479 else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n);
d4c346d6 480 } else printf("cant get result.%p or tx.%p\n",result,tx);
e14a749a 481 free_json(json);
d4c346d6 482 } else printf("cant parse2.(%s)\n",retstr2);
e14a749a 483 free(retstr2);
d4c346d6 484 } else printf("error getblock %s\n",params);
e14a749a 485 } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
486 free_json(result);
d4c346d6 487 } else printf("couldnt parse.(%s)\n",retstr);
f38345e9 488 free(retstr);
d4c346d6 489 } else printf("error from getblockhash %d\n",height);
c59a3beb 490 return(retval);
9b0e1808 491}
492
f38345e9 493void komodo_gateway_iteration(char *symbol)
494{
2b84e06c 495 char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
12546443 496 if ( KMDHEIGHT <= 0 )
497 KMDHEIGHT = 1;
2d657c5b 498 KOMODO_REALTIME = 0;
f38345e9 499 if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
500 {
501 if ( (infoobj= cJSON_Parse(retstr)) != 0 )
502 {
830fc774 503 if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
f38345e9 504 {
908b761f 505 //printf("gateway KMDHEIGHT.%d kmdheight.%d\n",KMDHEIGHT,kmdheight);
d360ffce 506 for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
f38345e9 507 {
755ead98 508 if ( (KMDHEIGHT % 10) == 0 )
2b84e06c 509 {
755ead98 510 if ( (KMDHEIGHT % 100) == 0 )
511 fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT);
2b84e06c 512 memset(&zero,0,sizeof(zero));
429dabb5 513 komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0);
2b84e06c 514 }
c59a3beb 515 if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
d4c346d6 516 {
517 printf("error KMDHEIGHT %d\n",KMDHEIGHT);
c59a3beb 518 break;
d4c346d6 519 }
4563911d 520 usleep(10000);
f38345e9 521 }
1fdfe03c 522 if ( KMDHEIGHT >= kmdheight )
523 KOMODO_REALTIME = (uint32_t)time(NULL);
f38345e9 524 }
525 free_json(infoobj);
526 }
f38345e9 527 free(retstr);
bb7f7473 528 }
529 else
530 {
a55c8425 531 printf("error from %s\n",symbol);
bb7f7473 532 sleep(30);
533 }
3f083c25 534}
535
536void komodo_iteration(char *symbol)
537{
1fc75a27 538 char *retstr,*base,*coinaddr,*txidstr,cmd[512]; uint64_t value,fiatoshis; cJSON *array,*item; int32_t i,n,vout,shortflag,height,fiatheight; uint256 txid; uint8_t rmd160[20],addrtype;
77be6cd9 539 if ( ASSETCHAINS_SYMBOL[0] == 0 )
540 {
3f083c25 541 sprintf(cmd,"{\"agent\":\"dpow\",\"method\":\"pending\",\"fiat\":\"%s\"}",symbol);
542 if ( (retstr= issue_curl(cmd)) != 0 )
77be6cd9 543 {
544 if ( (array= cJSON_Parse(retstr)) != 0 )
545 {
546 if ( (n= cJSON_GetArraySize(array)) > 0 )
547 {
548 for (i=0; i<n; i++)
549 {
550 item = jitem(array,i);
51508fed 551 coinaddr = jstr(item,(char *)"address");
552 value = jdouble(item,(char *)"KMD") * COIN;
1fc75a27 553 base = jstr(item,(char *)"fiat");
51508fed 554 shortflag = juint(item,(char *)"short");
555 vout = jint(item,(char *)"prev_vout");
1fc75a27 556 height = jint(item,(char *)"kmdheight");
557 fiatheight = jint(item,(char *)"height");
51508fed 558 txidstr = jstr(item,(char *)"prev_hash");
77be6cd9 559 if ( coinaddr != 0 && base != 0 && value > 0 && height > 0 )
560 {
561 fiatoshis = jdouble(item,base) * COIN;
562 decode_hex((uint8_t *)&txid,sizeof(txid),txidstr);
563 bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
1fc75a27 564 komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height,fiatheight);
77be6cd9 565 }
566 }
567 }
568 }
7961668b 569 //printf("retstr.(%s)\n",retstr);
77be6cd9 570 free(retstr);
571 }
572 }
f38345e9 573}
This page took 0.157005 seconds and 4 git commands to generate.