1 /******************************************************************************
2 * Copyright © 2014-2017 The SuperNET Developers. *
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. *
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 *
12 * Removal or modification of this copyright notice is prohibited. *
14 ******************************************************************************/
16 // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
18 int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base)
20 int32_t baseid; struct komodo_state *sp; int64_t netliability,maxallowed;
21 *available = *deposited = *issued = *withdrawn = *approved = *redeemed = 0;
22 if ( (baseid= komodo_baseid(base)) >= 0 )
24 if ( (sp= komodo_stateptrget(base)) != 0 )
26 *deposited = sp->deposited;
28 *withdrawn = sp->withdrawn;
29 *approved = sp->approved;
30 *redeemed = sp->redeemed;
31 netliability = (sp->deposited - sp->withdrawn) - sp->shorted;
32 maxallowed = komodo_maxallowed(baseid);
33 if ( netliability < maxallowed )
34 *available = (maxallowed - netliability);
35 //printf("%p %s %.8f %.8f %.8f %.8f %.8f\n",sp,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed));
37 } else printf("pax_fiatstatus cant get basesp.%s\n",base);
38 } // else printf("pax_fiatstatus illegal base.%s\n",base);
42 void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type)
45 memcpy(&buf[32],&vout,2);
49 struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type)
51 struct pax_transaction *pax; uint8_t buf[35];
52 pthread_mutex_lock(&komodo_mutex);
53 pax_keyset(buf,txid,vout,type);
54 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
55 pthread_mutex_unlock(&komodo_mutex);
59 struct pax_transaction *komodo_paxfinds(uint256 txid,uint16_t vout)
61 struct pax_transaction *pax; int32_t i; uint8_t types[] = { 'I', 'D', 'X', 'A', 'W' };
62 for (i=0; i<sizeof(types)/sizeof(*types); i++)
63 if ( (pax= komodo_paxfind(txid,vout,types[i])) != 0 )
68 struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark)
70 struct pax_transaction *pax; uint8_t buf[35];
71 pthread_mutex_lock(&komodo_mutex);
72 pax_keyset(buf,txid,vout,type);
73 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
76 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
80 memcpy(pax->buf,buf,sizeof(pax->buf));
81 HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
82 //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
86 pthread_mutex_unlock(&komodo_mutex);
90 void komodo_paxdelete(struct pax_transaction *pax)
92 return; // breaks when out of order
93 pthread_mutex_lock(&komodo_mutex);
94 HASH_DELETE(hh,PAX,pax);
95 pthread_mutex_unlock(&komodo_mutex);
98 void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,uint8_t type,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context
100 struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s;
101 if ( KOMODO_PAX == 0 )
103 sp = komodo_stateptr(str,dest);
104 pthread_mutex_lock(&komodo_mutex);
105 pax_keyset(buf,txid,vout,type);
106 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
109 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
113 memcpy(pax->buf,buf,sizeof(pax->buf));
114 HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
116 if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 )
118 int32_t i; for (i=0; i<32; i++)
119 printf("%02x",((uint8_t *)&txid)[i]);
120 printf(" v.%d [%s] kht.%d ht.%d create pax.%p symbol.%s source.%s\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax,symbol,source);
123 pthread_mutex_unlock(&komodo_mutex);
126 strcpy(pax->coinaddr,coinaddr);
128 pax->komodoshis = value;
130 strcpy(pax->symbol,symbol);
132 strcpy(pax->source,source);
133 if ( fiatoshis != 0 )
134 pax->fiatoshis = fiatoshis;
136 memcpy(pax->rmd160,rmd160,20);
138 pax->height = height;
139 if ( otherheight != 0 )
140 pax->otherheight = otherheight;
144 pax->marked = height;
145 //printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
149 int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax)
155 opretbuf[len++] = ((uint8_t *)&pax->txid)[i];
156 opretbuf[len++] = pax->vout & 0xff;
157 opretbuf[len++] = (pax->vout >> 8) & 0xff;
162 ((uint8_t *)&pax->txid)[i] = opretbuf[len++];
163 //for (i=0; i<32; i++)
164 // printf("%02x",((uint8_t *)&pax->txid)[31-i]);
165 pax->vout = opretbuf[len++];
166 pax->vout += ((uint32_t)opretbuf[len++] << 8);
167 //printf(" txid v.%d\n",pax->vout);
169 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis);
170 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
171 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height);
172 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight);
175 memcpy(&opretbuf[len],pax->rmd160,20), len += 20;
177 opretbuf[len++] = pax->source[i];
181 memcpy(pax->rmd160,&opretbuf[len],20), len += 20;
183 pax->source[i] = opretbuf[len++];
188 int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int64_t *srcvalues,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
190 struct pax_transaction p,*pax; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t type,rmd160[20]; uint64_t fiatoshis; char symbol[16];
191 if ( KOMODO_PAX == 0 )
193 incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
194 //41e77b91cb68dc2aa02fa88550eae6b6d44db676a7e935337b6d1392d9718f03cb0200305c90660400000000fbcbeb1f000000bde801006201000058e7945ad08ddba1eac9c9b6c8e1e97e8016a2d152
196 // 41e94d736ec69d88c08b5d238abeeca609c02357a8317e0d56c328bcb1c259be5d0200485bc80200000000404b4c000000000059470200b80b000061f22ba7d19fe29ac3baebd839af8b7127d1f9075553440046bb4cc7a3b5cd39dffe7206507a3482a00780e617f68b273cce9817ed69298d02001069ca1b0000000080f0fa02000000005b470200b90b000061f22ba7d19fe29ac3baebd839af8b7127d1f90755
198 //for (i=0; i<opretlen; i++)
199 // printf("%02x",opretbuf[i]);
200 //printf(" opretlen.%d (%s)\n",opretlen,base);
201 //printf(" opretlen.%d vs %d incr.%d (%d)\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 2),incr,opretlen/incr);
202 //if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
205 opretbuf++, opretlen--;
206 for (n=0; n<opretlen/incr; n++)
210 memset(&p,0,sizeof(p));
211 len += komodo_rwapproval(0,&opretbuf[len],&p);
212 if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 )
216 values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis;
217 srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis;
218 kmdheights[n] = p.height;
219 otherheights[n] = p.otherheight;
220 memcpy(&rmd160s[n * 20],p.rmd160,20);
221 baseids[n] = komodo_baseid(p.source);
225 bitcoin_address(coinaddr,60,&rmd160s[n * 20],20);
226 printf(">>>>>>> %s: (%s) fiat %.8f kmdheight.%d other.%d -> %s %.8f\n",type=='A'?"approvedA":"issuedX",baseids[n]>=0?CURRENCIES[baseids[n]]:"???",dstr(p.fiatoshis),kmdheights[n],otherheights[n],coinaddr,dstr(values[n]));
233 base[i] = opretbuf[opretlen-4+i];
236 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
237 //printf("%02x",((uint8_t *)&txids[n])[j]);
239 vouts[n] = opretbuf[len++];
240 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
241 baseids[n] = komodo_baseid(base);
242 if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 )
244 values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis;
245 srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis;
246 kmdheights[n] = pax->height;
247 otherheights[n] = pax->otherheight;
248 memcpy(&rmd160s[n * 20],pax->rmd160,20);
251 //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
257 int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t checkvalue,uint64_t seed)
260 if ( seed == 0 && checkvalue != 0 )
262 ratio = ((value << 6) / checkvalue);
263 if ( ratio >= 63 && ratio <= 65 )
267 if ( kmdheight >= 86150 )
268 printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio);
272 else if ( checkvalue != 0 )
274 ratio = ((value << 10) / checkvalue);
275 if ( ratio >= 1023 && ratio <= 1025 )
278 return(value != checkvalue);
281 uint64_t komodo_paxtotal()
283 struct pax_transaction *pax,*pax2,*tmp,*tmp2; char symbol[16],dest[16],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp;
284 if ( KOMODO_PAX == 0 )
286 if ( komodo_isrealtime(&ht) == 0 )
290 HASH_ITER(hh,PAX,pax,tmp)
292 if ( pax->marked != 0 )
294 if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' )
296 else str = pax->source;
297 basesp = komodo_stateptrget(str);
298 if ( basesp != 0 && pax->didstats == 0 )
300 if ( pax->type == 'I' && (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 )
302 if ( pax2->fiatoshis != 0 )
304 pax->komodoshis = pax2->komodoshis;
305 pax->fiatoshis = pax2->fiatoshis;
306 basesp->issued += pax->fiatoshis;
308 if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
309 printf("########### %p issued %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,str,dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight);
310 pax2->marked = pax->height;
311 pax->marked = pax->height;
314 else if ( pax->type == 'W' )
316 //bitcoin_address(coinaddr,addrtype,rmd160,20);
317 if ( (checktoshis= komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis)) != 0 )
319 if ( komodo_paxcmp(pax->source,pax->height,pax->komodoshis,checktoshis,seed) != 0 )
321 pax->marked = pax->height;
322 //printf("WITHDRAW.%s mark <- %d %.8f != %.8f\n",pax->source,pax->height,dstr(checktoshis),dstr(pax->komodoshis));
324 else if ( pax->validated == 0 )
326 pax->validated = pax->komodoshis = checktoshis;
327 //int32_t j; for (j=0; j<32; j++)
328 // printf("%02x",((uint8_t *)&pax->txid)[j]);
329 //if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
330 // printf(" v%d %p got WITHDRAW.%s kmd.%d ht.%d %.8f -> %.8f/%.8f\n",pax->vout,pax,pax->source,pax->height,pax->otherheight,dstr(pax->fiatoshis),dstr(pax->komodoshis),dstr(checktoshis));
337 komodo_stateptr(symbol,dest);
338 HASH_ITER(hh,PAX,pax,tmp)
341 if ( 0 && pax->type == 'A' )
342 printf("%p pax.%s <- %s marked.%d %.8f -> %.8f validated.%d approved.%d\n",pax,pax->symbol,pax->source,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0);
343 if ( pax->marked != 0 )
345 if ( strcmp(symbol,pax->symbol) == 0 || pax->type == 'A' )
347 if ( pax->marked == 0 )
349 if ( komodo_is_issuer() != 0 )
351 if ( pax->validated != 0 && pax->type == 'D' )
353 total += pax->fiatoshis;
357 else if ( pax->approved != 0 && pax->type == 'A' )
359 if ( pax->validated != 0 )
361 total += pax->komodoshis;
367 checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis);
368 //printf("paxtotal PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",pax->height,pax->symbol,(double)pax->fiatoshis/COIN,(double)pax->komodoshis/COIN,(double)checktoshis/COIN);
369 //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight);
370 if ( seed != 0 && checktoshis != 0 )
372 if ( checktoshis == pax->komodoshis )
374 total += pax->komodoshis;
375 pax->validated = pax->komodoshis;
377 } else pax->marked = pax->height;
381 if ( 0 && pax->ready != 0 )
382 printf("%p (%c) pax.%s marked.%d %.8f -> %.8f validated.%d approved.%d\n",pax,pax->type,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0);
386 //printf("paxtotal %.8f\n",dstr(total));
390 static int _paxorder(const void *a,const void *b)
392 #define pax_a (*(struct pax_transaction **)a)
393 #define pax_b (*(struct pax_transaction **)b)
395 aval = pax_a->fiatoshis + pax_a->komodoshis + pax_a->height;
396 bval = pax_b->fiatoshis + pax_b->komodoshis + pax_b->height;
399 else if ( bval < aval )
406 int32_t komodo_pending_withdraws(char *opretstr) // todo: enforce deterministic order
408 struct pax_transaction *pax,*pax2,*tmp,*paxes[64]; uint8_t opretbuf[16384]; int32_t i,n,ht,len=0; uint64_t total = 0;
409 if ( KOMODO_PAX == 0 )
411 if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
414 HASH_ITER(hh,PAX,pax,tmp)
416 if ( pax->type == 'W' )
418 if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'A')) != 0 )
420 if ( pax2->approved != 0 )
421 pax->approved = pax2->approved;
423 else if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'X')) != 0 )
424 pax->approved = pax->height;
425 //printf("pending_withdraw: pax %s marked.%u approved.%u validated.%llu\n",pax->symbol,pax->marked,pax->approved,(long long)pax->validated);
426 if ( pax->marked == 0 && pax->approved == 0 && pax->validated != 0 ) //strcmp((char *)"KMD",pax->symbol) == 0 &&
428 if ( n < sizeof(paxes)/sizeof(*paxes) )
431 //int32_t j; for (j=0; j<32; j++)
432 // printf("%02x",((uint8_t *)&pax->txid)[j]);
433 //printf(" %s.(kmdht.%d ht.%d marked.%u approved.%d validated %.8f) %.8f\n",pax->source,pax->height,pax->otherheight,pax->marked,pax->approved,dstr(pax->validated),dstr(pax->komodoshis));
441 opretbuf[len++] = 'A';
442 qsort(paxes,n,sizeof(*paxes),_paxorder);
445 if ( len < (sizeof(opretbuf)>>3)*7 )
446 len += komodo_rwapproval(1,&opretbuf[len],paxes[i]);
449 init_hexbytes_noT(opretstr,opretbuf,len);
451 //fprintf(stderr,"komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal()));
455 int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
457 struct pax_transaction *pax,*tmp; char symbol[16],dest[16]; uint8_t *script,opcode,opret[16384],data[16384]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t available,deposited,issued,withdrawn,approved,redeemed,mask;
458 if ( KOMODO_PAX == 0 )
460 struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD");
461 sp = komodo_stateptr(symbol,dest);
463 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
465 PENDING_KOMODO_TX = 0;
471 if ( komodo_isrealtime(&ht) != 0 )
477 printf("%s not realtime ht.%d\n",ASSETCHAINS_SYMBOL,ht);
484 if ( komodo_paxtotal() == 0 )
487 HASH_ITER(hh,PAX,pax,tmp)
489 if ( pax->type != 'D' && pax->type != 'A' )
492 #ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE
493 if ( kmdsp != 0 && (kmdsp->NOTARIZED_HEIGHT >= pax->height || kmdsp->CURRENT_HEIGHT > pax->height+30) ) // assumes same chain as notarize
494 pax->validated = pax->komodoshis; //kmdsp->NOTARIZED_HEIGHT;
495 else pax->validated = pax->ready = 0;
497 pax->validated = pax->komodoshis;
500 if ( ASSETCHAINS_SYMBOL[0] != 0 && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
502 if ( strcmp(ASSETCHAINS_SYMBOL,symbol) == 0 )
503 printf("miner.[%s]: skip %s %.8f when avail %.8f deposited %.8f, issued %.8f withdrawn %.8f approved %.8f redeemed %.8f\n",ASSETCHAINS_SYMBOL,symbol,dstr(pax->fiatoshis),dstr(available),dstr(deposited),dstr(issued),dstr(withdrawn),dstr(approved),dstr(redeemed));
506 /*printf("pax.%s marked.%d %.8f -> %.8f ready.%d validated.%d\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->ready!=0,pax->validated!=0);
507 if ( pax->marked != 0 || (pax->type != 'D' && pax->type != 'A') || pax->ready == 0 )
509 printf("reject 2\n");
512 if ( ASSETCHAINS_SYMBOL[0] != 0 && (strcmp(pax->symbol,symbol) != 0 || pax->validated == 0) )
514 //printf("pax->symbol.%s != %s or null pax->validated %.8f\n",pax->symbol,symbol,dstr(pax->validated));
517 if ( pax->ready == 0 )
519 if ( pax->type == 'A' && ASSETCHAINS_SYMBOL[0] == 0 )
523 if ( (baseid= komodo_baseid(pax->symbol)) < 0 || ((1LL << baseid) & sp->RTmask) == 0 )
525 printf("not RT for (%s) %llx baseid.%d %llx\n",pax->symbol,(long long)sp->RTmask,baseid,(long long)(1LL<<baseid));
531 //printf("redeem.%d? (%c) %p pax.%s marked.%d %.8f -> %.8f ready.%d validated.%d approved.%d\n",tokomodo,pax->type,pax,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->ready!=0,pax->validated!=0,pax->approved!=0);
532 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
533 printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
534 txNew->vout.resize(numvouts+1);
535 txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
536 txNew->vout[numvouts].scriptPubKey.resize(25);
537 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
541 memcpy(script,pax->rmd160,20), script += 20;
547 data[len++] = ((uint8_t *)&pax->txid)[i];
548 data[len++] = pax->vout & 0xff;
549 data[len++] = (pax->vout >> 8) & 0xff;
550 PENDING_KOMODO_TX += pax->fiatoshis;
554 len += komodo_rwapproval(1,&data[len],pax);
555 PENDING_KOMODO_TX += pax->komodoshis;
556 printf(" len.%d vout.%u DEPOSIT %.8f <- pax.%s pending ht %d %d %.8f | ",len,pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,pax->height,pax->otherheight,dstr(PENDING_KOMODO_TX));
558 if ( numvouts++ >= 64 )
564 strcpy(symbol,(char *)"KMD");
565 for (i=0; symbol[i]!=0; i++)
566 data[len++] = symbol[i];
568 for (i=0; i<len; i++)
569 printf("%02x",data[i]);
570 printf(" <- data[%d]\n",len);
571 opretlen = komodo_opreturnscript(opret,opcode,data,len);
572 txNew->vout.resize(numvouts+1);
573 txNew->vout[numvouts].nValue = 0;
574 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
575 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
576 memcpy(script,opret,opretlen);
578 printf("%02x",opret[i]);
579 printf(" <- opret, MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
585 int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
587 int32_t i,j,n,ht,num,opretlen,offset=1,errs=0,matched=0,kmdheights[64],otherheights[64]; uint256 hash,txids[64]; char symbol[16],base[16]; uint16_t vouts[64]; int8_t baseids[64]; uint8_t *script,opcode,rmd160s[64*20]; uint64_t available,deposited,issued,withdrawn,approved,redeemed; int64_t values[64],srcvalues[64]; struct pax_transaction *pax;
588 if ( KOMODO_PAX == 0 || komodo_isrealtime(&ht) == 0 )
590 memset(baseids,0xff,sizeof(baseids));
591 memset(values,0,sizeof(values));
592 memset(srcvalues,0,sizeof(srcvalues));
593 memset(rmd160s,0,sizeof(rmd160s));
594 memset(kmdheights,0,sizeof(kmdheights));
595 memset(otherheights,0,sizeof(otherheights));
596 n = block.vtx[0].vout.size();
597 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
598 if ( n <= 2 || script[0] != 0x6a )
600 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
601 if ( ASSETCHAINS_SYMBOL[0] == 0 )
603 //for (i=0; i<opretlen; i++)
604 // printf("%02x",script[i]);
605 //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
607 strcpy(symbol,(char *)"KMD");
611 strcpy(symbol,ASSETCHAINS_SYMBOL);
613 if ( komodo_baseid(symbol) < 0 )
615 if ( block.vtx[0].vout.size() != 1 )
617 printf("%s has more than one coinbase?\n",symbol);
623 if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
625 if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
627 for (i=1; i<n-1; i++)
629 if ( (pax= komodo_paxfinds(txids[i-1],vouts[i-1])) != 0 ) // finds... make sure right one
632 if ( opcode == 'I' && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
634 printf("checkdeposit.[%s]: skip %s %.8f when avail %.8f deposited %.8f, issued %.8f withdrawn %.8f approved %.8f redeemed %.8f\n",ASSETCHAINS_SYMBOL,symbol,dstr(pax->fiatoshis),dstr(available),dstr(deposited),dstr(issued),dstr(withdrawn),dstr(approved),dstr(redeemed));
637 if ( ((opcode == 'I' && (pax->fiatoshis == 0 || pax->fiatoshis == block.vtx[0].vout[i].nValue)) || (opcode == 'X' && (pax->komodoshis == 0 || pax->komodoshis == block.vtx[0].vout[i].nValue))) )
639 if ( pax->marked != 0 && height >= 80820 )
641 printf(">>>>>>>>>>> %c errs.%d i.%d match %.8f vs %.8f paxmarked.%d kht.%d ht.%d\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax->marked,pax->height,pax->otherheight);
642 if ( pax->komodoshis != 0 || pax->fiatoshis != 0 )
644 else matched++; // onetime init bypass
648 if ( opcode == 'X' && strcmp(ASSETCHAINS_SYMBOL,CURRENCIES[baseids[i]]) == 0 )
649 printf("check deposit validates %s %.8f -> %.8f\n",CURRENCIES[baseids[i]],dstr(srcvalues[i]),dstr(values[i]));
656 printf("%02x",((uint8_t *)&txids[i-1])[j]);
657 printf(" cant paxfind %c txid\n",opcode);
658 printf(">>>>>>>>>>> %c errs.%d i.%d match %.8f vs %.8f pax.%p\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax);
661 else if ( kmdheights[i-1] > 0 && otherheights[i-1] > 0 )
663 hash = block.GetHash();
665 printf("%02x",((uint8_t *)&hash)[j]);
666 printf(" kht.%d ht.%d %.8f %.8f blockhash couldnt find vout.[%d]\n",kmdheights[i-1],otherheights[i-1],dstr(values[i-1]),dstr(srcvalues[i]),i);
669 if ( ASSETCHAINS_SYMBOL[0] == 0 )
671 if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
673 printf("WOULD REJECT %s: ht.%d (%c) matched.%d vs num.%d tip.%d isRT.%d\n",symbol,height,opcode,matched,num,(int32_t)chainActive.Tip()->nHeight,komodo_isrealtime(&ht));
674 // can easily happen depending on order of loading
675 if ( height > 200000 )
677 printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
684 if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
686 printf("REJECT %s: ht.%d (%c) matched.%d vs num.%d tip.%d isRT.%d\n",symbol,height,opcode,matched,num,(int32_t)chainActive.Tip()->nHeight,komodo_isrealtime(&ht));
691 //printf("opretlen.%d num.%d\n",opretlen,num);
696 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
698 uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,kvheight,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fee,fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff;
699 const char *typestr = "unknown";
700 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 && opretbuf[0] != 'K' )
702 //printf("komodo_opreturn skip %s\n",ASSETCHAINS_SYMBOL);
703 return("assetchain");
705 //else if ( KOMODO_PAX == 0 )
707 memset(baseids,0xff,sizeof(baseids));
708 memset(values,0,sizeof(values));
709 memset(srcvalues,0,sizeof(srcvalues));
710 memset(rmd160s,0,sizeof(rmd160s));
711 memset(kmdheights,0,sizeof(kmdheights));
712 memset(otherheights,0,sizeof(otherheights));
713 tokomodo = (komodo_is_issuer() == 0);
714 if ( opretbuf[0] == 'K' && opretlen != 40 )
716 komodo_kvupdate(opretbuf,opretlen,value);
718 else if ( opretbuf[0] == 'D' )
721 if ( opretlen == 38 ) // any KMD tx
723 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
724 memset(base,0,sizeof(base));
725 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
726 bitcoin_address(coinaddr,addrtype,rmd160,20);
727 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
729 if ( kmdheight > 195000 || kmdheight <= height )
732 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
734 printf("(%s) (%s) kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",ASSETCHAINS_SYMBOL,base,kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
736 printf("%02x",((uint8_t *)&txid)[i]);
737 printf(" <- txid.v%u ",vout);
739 printf("%02x",pubkey33[i]);
740 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
742 if ( komodo_paxcmp(base,kmdheight,value,checktoshis,seed) == 0 )
744 if ( (pax= komodo_paxfind(txid,vout,'D')) == 0 )
746 if ( (basesp= komodo_stateptrget(base)) != 0 )
748 basesp->deposited += fiatoshis;
750 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
751 printf("########### %p deposited %s += %.8f kmdheight.%d %.8f\n",basesp,base,dstr(fiatoshis),kmdheight,dstr(value));
752 } else printf("cant get stateptr.(%s)\n",base);
753 komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0);
755 if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 )
757 pax->height = kmdheight;
758 pax->validated = value;
759 pax->komodoshis = value;
760 pax->fiatoshis = fiatoshis;
761 if ( didstats == 0 && pax->didstats == 0 )
763 if ( (basesp= komodo_stateptrget(base)) != 0 )
765 basesp->deposited += fiatoshis;
767 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
768 printf("########### %p depositedB %s += %.8f/%.8f kmdheight.%d/%d %.8f/%.8f\n",basesp,base,dstr(fiatoshis),dstr(pax->fiatoshis),kmdheight,pax->height,dstr(value),dstr(pax->komodoshis));
773 if ( (pax2= komodo_paxfind(txid,vout,'I')) != 0 )
775 pax2->fiatoshis = pax->fiatoshis;
776 pax2->komodoshis = pax->komodoshis;
777 pax->marked = pax2->marked = pax->height;
778 pax2->height = pax->height = height;
779 if ( pax2->didstats == 0 )
781 if ( (basesp= komodo_stateptrget(base)) != 0 )
783 basesp->issued += pax2->fiatoshis;
785 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
786 printf("########### %p issueda %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->komodoshis),pax2->otherheight);
792 else if ( seed != 0 && kmdheight > 182000 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
793 printf("pax %s deposit %.8f rejected kmdheight.%d %.8f KMD check %.8f seed.%llu\n",base,dstr(fiatoshis),kmdheight,dstr(value),dstr(checktoshis),(long long)seed);
794 } //else printf("paxdeposit height.%d vs kmdheight.%d\n",height,kmdheight);
797 else if ( opretbuf[0] == 'I' )
800 if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
802 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
806 if ( baseids[i] < 0 )
808 printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]);
811 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
812 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0);
813 komodo_paxmark(height,txids[i],vouts[i],'I',height);
814 if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 )
816 pax->type = opretbuf[0];
817 strcpy(pax->source,(char *)&opretbuf[opretlen-4]);
818 if ( (pax2= komodo_paxfind(txids[i],vouts[i],'D')) != 0 && pax2->fiatoshis != 0 && pax2->komodoshis != 0 )
821 pax->fiatoshis = pax2->fiatoshis;
822 pax->komodoshis = pax2->komodoshis;
823 pax->marked = pax2->marked = pax2->height;
824 if ( pax->didstats == 0 )
826 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
828 basesp->issued += pax->fiatoshis;
830 pax->height = pax2->height;
831 pax->otherheight = height;
832 if ( 0 && strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
833 printf("########### %p issuedb %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,CURRENCIES[baseids[i]],dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight);
838 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'I',height)) != 0 )
839 komodo_paxdelete(pax);
840 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'D',height)) != 0 )
841 komodo_paxdelete(pax);
843 } //else printf("opreturn none issued?\n");
846 else if ( opretbuf[0] == 'W' )//&& opretlen >= 38 )
848 if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 && strcmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]) == 0 )
850 for (i=0; i<opretlen; i++)
851 printf("%02x",opretbuf[i]);
852 printf(" [%s] reject obsolete withdraw request.%s\n",ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]);
856 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
857 memset(base,0,sizeof(base));
858 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
859 bitcoin_address(coinaddr,addrtype,rmd160,20);
860 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
861 typestr = "withdraw";
862 //printf(" [%s] WITHDRAW %s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s) len.%d\n",ASSETCHAINS_SYMBOL,base,kmdheight,height,dstr(checktoshis),dstr(komodoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr,opretlen);
864 //if ( komodo_paxcmp(base,kmdheight,komodoshis,checktoshis,seed) == 0 )
866 if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) )
868 if ( (basesp= komodo_stateptrget(base)) != 0 )
870 basesp->withdrawn += value;
872 if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
873 printf("########### %p withdrawn %s += %.8f check %.8f\n",basesp,base,dstr(value),dstr(checktoshis));
875 if ( 0 && strcmp(base,"RUB") == 0 && (pax == 0 || pax->approved == 0) )
876 printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
878 komodo_gateway_deposit(coinaddr,0,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0);
879 if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 )
881 pax->type = opretbuf[0];
882 strcpy(pax->source,base);
883 strcpy(pax->symbol,"KMD");
884 pax->height = kmdheight;
885 pax->otherheight = height;
886 pax->komodoshis = komodoshis;
888 } // else printf("withdraw %s paxcmp ht.%d %d error value %.8f -> %.8f vs %.8f\n",base,kmdheight,height,dstr(value),dstr(komodoshis),dstr(checktoshis));
889 // need to allocate pax
891 else if ( tokomodo != 0 && opretbuf[0] == 'A' )
894 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
896 for (i=0; i<opretlen; i++)
897 printf("%02x",opretbuf[i]);
898 printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
900 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
904 //for (j=0; j<32; j++)
905 // printf("%02x",((uint8_t *)&txids[i])[j]);
906 //printf(" v%d %.8f %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),dstr(srcvalues[i]),kmdheights[i],otherheights[i],baseids[i]);
907 if ( baseids[i] < 0 )
909 for (i=0; i<opretlen; i++)
910 printf("%02x",opretbuf[i]);
911 printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
912 //printf("baseids[%d] %d\n",i,baseids[i]);
913 if ( (pax= komodo_paxfind(txids[i],vouts[i],'W')) != 0 || (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
915 baseids[i] = komodo_baseid(pax->symbol);
916 printf("override neg1 with (%s)\n",pax->symbol);
918 if ( baseids[i] < 0 )
923 checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)values[i]);
924 //printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",kmdheights[i],CURRENCIES[baseids[i]],(double)values[i]/COIN,(double)srcvalues[i]/COIN,(double)checktoshis/COIN);
925 if ( srcvalues[i] == checktoshis )
927 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
929 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
930 komodo_gateway_deposit(coinaddr,srcvalues[i],CURRENCIES[baseids[i]],values[i],&rmd160s[i*20],txids[i],vouts[i],'A',kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]);
931 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
932 printf("unexpected null pax for approve\n");
933 else pax->validated = checktoshis;
934 if ( (pax2= komodo_paxfind(txids[i],vouts[i],'W')) != 0 )
935 pax2->approved = kmdheights[i];
936 komodo_paxmark(height,txids[i],vouts[i],'W',height);
937 //komodo_paxmark(height,txids[i],vouts[i],'A',height);
938 if ( values[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
940 basesp->approved += values[i];
942 printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),kmdheights[i],otherheights[i]);
944 //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i]));
946 else if ( pax->didstats == 0 && srcvalues[i] != 0 )
948 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
950 basesp->approved += values[i];
952 printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),kmdheights[i],otherheights[i]);
954 } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
955 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 )
957 pax->type = opretbuf[0];
958 pax->approved = kmdheights[i];
959 pax->validated = checktoshis;
962 //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
963 //printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax);
967 } //else printf("n.%d from opreturns\n",n);
968 //printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
970 else if ( opretbuf[0] == 'X' )
973 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
977 if ( baseids[i] < 0 )
979 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
980 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0);
981 komodo_paxmark(height,txids[i],vouts[i],'W',height);
982 komodo_paxmark(height,txids[i],vouts[i],'A',height);
983 komodo_paxmark(height,txids[i],vouts[i],'X',height);
984 if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
986 pax->type = opretbuf[0];
987 if ( height < 121842 ) // fields got switched around due to legacy issues and approves
988 value = srcvalues[i];
989 else value = values[i];
990 if ( baseids[i] >= 0 && value != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
992 basesp->redeemed += value;
994 if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
995 printf("ht.%d %.8f ########### %p redeemed %s += %.8f %.8f kht.%d ht.%d\n",height,dstr(value),basesp,CURRENCIES[baseids[i]],dstr(value),dstr(srcvalues[i]),kmdheights[i],otherheights[i]);
998 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'W',height)) != 0 )
999 komodo_paxdelete(pax);
1000 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'A',height)) != 0 )
1001 komodo_paxdelete(pax);
1002 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'X',height)) != 0 )
1003 komodo_paxdelete(pax);
1005 } //else printf("komodo_issued_opreturn returned %d\n",n);
1010 void komodo_passport_iteration()
1012 static long lastpos[34]; static char userpass[33][1024]; int32_t maxseconds = 7;
1013 FILE *fp; int32_t baseid,n,ht,isrealtime,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[16],dest[16]; uint32_t buf[3],starttime; cJSON *infoobj,*result; uint64_t RTmask = 0;
1014 //printf("PASSPORT.(%s)\n",ASSETCHAINS_SYMBOL);
1015 while ( KOMODO_INITDONE == 0 )
1017 fprintf(stderr,"[%s] PASSPORT iteration waiting for KOMODO_INITDONE\n",ASSETCHAINS_SYMBOL);
1020 refsp = komodo_stateptr(symbol,dest);
1021 if ( ASSETCHAINS_SYMBOL[0] == 0 )
1025 refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
1028 KOMODO_PASSPORT_INITDONE = 1;
1032 if ( KOMODO_PAX == 0 )
1034 KOMODO_PASSPORT_INITDONE = 1;
1037 starttime = (uint32_t)time(NULL);
1038 //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
1039 for (baseid=32; baseid>=0; baseid--)
1041 if ( time(NULL) >= starttime+maxseconds )
1045 base = (char *)CURRENCIES[baseid];
1046 if ( baseid+1 != refid )
1048 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
1049 komodo_nameset(symbol,dest,base);
1050 sp = komodo_stateptrget(symbol);
1052 if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 )
1054 fseek(fp,0,SEEK_END);
1055 if ( ftell(fp) > lastpos[baseid] )
1057 if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
1058 printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
1059 fseek(fp,lastpos[baseid],SEEK_SET);
1060 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 && n < 1000 )
1064 if ( time(NULL) < starttime+maxseconds )
1066 else printf("expire passport loop %s -> %s\n",ASSETCHAINS_SYMBOL,base);
1070 lastpos[baseid] = ftell(fp);
1071 if ( lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
1072 printf("from.(%s) lastpos[%s] %ld isrt.%d\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid],komodo_isrealtime(&ht));
1073 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
1075 } else printf("error.(%s) %p\n",fname,sp);
1076 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1077 if ( (fp= fopen(fname,"rb")) != 0 )
1079 if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
1081 sp->CURRENT_HEIGHT = buf[0];
1082 if ( buf[0] != 0 && buf[0] >= buf[1] && buf[2] > time(NULL)-300 )
1085 RTmask |= (1LL << baseid);
1086 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1087 } else if ( (time(NULL)-buf[2]) > 1200 )
1088 fprintf(stderr,"[%s]: %s not RT %u %u %d\n",ASSETCHAINS_SYMBOL,base,buf[0],buf[1],(int32_t)(time(NULL)-buf[2]));
1089 } //else fprintf(stderr,"%s size error RT\n",base);
1091 } //else fprintf(stderr,"%s open error RT\n",base);
1095 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1096 if ( (fp= fopen(fname,"wb")) != 0 )
1098 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
1099 buf[1] = (uint32_t)komodo_longestchain();
1100 if ( buf[0] != 0 && buf[0] == buf[1] )
1102 buf[2] = (uint32_t)time(NULL);
1103 RTmask |= (1LL << baseid);
1104 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1106 memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
1108 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
1109 fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
1111 } else fprintf(stderr,"%s create error RT\n",base);
1113 if ( sp != 0 && isrealtime == 0 )
1114 refsp->RTbufs[0][2] = 0;
1117 refsp->RTmask = RTmask;
1118 KOMODO_PASSPORT_INITDONE = 1;
1119 //printf("done PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);