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);
87 if ( height > 214700 || pax->height > 214700 )
88 printf("mark ht.%d %.8f %.8f\n",pax->height,dstr(pax->komodoshis),dstr(pax->fiatoshis));
91 pthread_mutex_unlock(&komodo_mutex);
95 void komodo_paxdelete(struct pax_transaction *pax)
97 return; // breaks when out of order
98 pthread_mutex_lock(&komodo_mutex);
99 HASH_DELETE(hh,PAX,pax);
100 pthread_mutex_unlock(&komodo_mutex);
103 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
105 struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s;
106 if ( KOMODO_PAX == 0 )
108 sp = komodo_stateptr(str,dest);
109 pthread_mutex_lock(&komodo_mutex);
110 pax_keyset(buf,txid,vout,type);
111 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
114 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
118 memcpy(pax->buf,buf,sizeof(pax->buf));
119 HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
121 if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 )
123 int32_t i; for (i=0; i<32; i++)
124 printf("%02x",((uint8_t *)&txid)[i]);
125 printf(" v.%d [%s] kht.%d ht.%d create pax.%p symbol.%s source.%s\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax,symbol,source);
128 pthread_mutex_unlock(&komodo_mutex);
131 strcpy(pax->coinaddr,coinaddr);
133 pax->komodoshis = value;
135 strcpy(pax->symbol,symbol);
137 strcpy(pax->source,source);
138 if ( fiatoshis != 0 )
139 pax->fiatoshis = fiatoshis;
141 memcpy(pax->rmd160,rmd160,20);
143 pax->height = height;
144 if ( otherheight != 0 )
145 pax->otherheight = otherheight;
149 pax->marked = height;
150 //printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
154 int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax)
160 opretbuf[len++] = ((uint8_t *)&pax->txid)[i];
161 opretbuf[len++] = pax->vout & 0xff;
162 opretbuf[len++] = (pax->vout >> 8) & 0xff;
167 ((uint8_t *)&pax->txid)[i] = opretbuf[len++];
168 //for (i=0; i<32; i++)
169 // printf("%02x",((uint8_t *)&pax->txid)[31-i]);
170 pax->vout = opretbuf[len++];
171 pax->vout += ((uint32_t)opretbuf[len++] << 8);
172 //printf(" txid v.%d\n",pax->vout);
174 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis);
175 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
176 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height);
177 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight);
180 memcpy(&opretbuf[len],pax->rmd160,20), len += 20;
182 opretbuf[len++] = pax->source[i];
186 memcpy(pax->rmd160,&opretbuf[len],20), len += 20;
188 pax->source[i] = opretbuf[len++];
193 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)
195 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];
196 if ( KOMODO_PAX == 0 )
198 incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
199 //41e77b91cb68dc2aa02fa88550eae6b6d44db676a7e935337b6d1392d9718f03cb0200305c90660400000000fbcbeb1f000000bde801006201000058e7945ad08ddba1eac9c9b6c8e1e97e8016a2d152
201 // 41e94d736ec69d88c08b5d238abeeca609c02357a8317e0d56c328bcb1c259be5d0200485bc80200000000404b4c000000000059470200b80b000061f22ba7d19fe29ac3baebd839af8b7127d1f9075553440046bb4cc7a3b5cd39dffe7206507a3482a00780e617f68b273cce9817ed69298d02001069ca1b0000000080f0fa02000000005b470200b90b000061f22ba7d19fe29ac3baebd839af8b7127d1f90755
203 //for (i=0; i<opretlen; i++)
204 // printf("%02x",opretbuf[i]);
205 //printf(" opretlen.%d (%s)\n",opretlen,base);
206 //printf(" opretlen.%d vs %d incr.%d (%d)\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 2),incr,opretlen/incr);
207 //if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
210 opretbuf++, opretlen--;
211 for (n=0; n<opretlen/incr; n++)
215 memset(&p,0,sizeof(p));
216 len += komodo_rwapproval(0,&opretbuf[len],&p);
217 if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 )
221 values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis;
222 srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis;
223 kmdheights[n] = p.height;
224 otherheights[n] = p.otherheight;
225 memcpy(&rmd160s[n * 20],p.rmd160,20);
226 baseids[n] = komodo_baseid(p.source);
230 bitcoin_address(coinaddr,60,&rmd160s[n * 20],20);
231 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]));
238 base[i] = opretbuf[opretlen-4+i];
241 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
242 //printf("%02x",((uint8_t *)&txids[n])[j]);
244 vouts[n] = opretbuf[len++];
245 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
246 baseids[n] = komodo_baseid(base);
247 if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 )
249 values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis;
250 srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis;
251 kmdheights[n] = pax->height;
252 otherheights[n] = pax->otherheight;
253 memcpy(&rmd160s[n * 20],pax->rmd160,20);
256 //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
262 int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t checkvalue,uint64_t seed)
265 if ( seed == 0 && checkvalue != 0 )
267 ratio = ((value << 6) / checkvalue);
268 if ( ratio >= 63 && ratio <= 65 )
272 if ( kmdheight >= 86150 )
273 printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio);
277 else if ( checkvalue != 0 )
279 ratio = ((value << 10) / checkvalue);
280 if ( ratio >= 1023 && ratio <= 1025 )
283 return(value != checkvalue);
286 uint64_t komodo_paxtotal()
288 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;
289 if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
291 if ( komodo_isrealtime(&ht) == 0 )
295 HASH_ITER(hh,PAX,pax,tmp)
297 if ( pax->marked != 0 )
299 if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' )
301 else str = pax->source;
302 basesp = komodo_stateptrget(str);
303 if ( basesp != 0 && pax->didstats == 0 )
305 if ( pax->type == 'I' && (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 )
307 if ( pax2->fiatoshis != 0 )
309 pax->komodoshis = pax2->komodoshis;
310 pax->fiatoshis = pax2->fiatoshis;
311 basesp->issued += pax->fiatoshis;
313 if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
314 printf("########### %p issued %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,str,dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight);
315 pax2->marked = pax->height;
316 pax->marked = pax->height;
319 else if ( pax->type == 'W' )
321 //bitcoin_address(coinaddr,addrtype,rmd160,20);
322 if ( (checktoshis= komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis)) != 0 )
324 if ( komodo_paxcmp(pax->source,pax->height,pax->komodoshis,checktoshis,seed) != 0 )
326 pax->marked = pax->height;
327 //printf("WITHDRAW.%s mark <- %d %.8f != %.8f\n",pax->source,pax->height,dstr(checktoshis),dstr(pax->komodoshis));
329 else if ( pax->validated == 0 )
331 pax->validated = pax->komodoshis = checktoshis;
332 //int32_t j; for (j=0; j<32; j++)
333 // printf("%02x",((uint8_t *)&pax->txid)[j]);
334 //if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
335 // 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));
342 komodo_stateptr(symbol,dest);
343 HASH_ITER(hh,PAX,pax,tmp)
346 if ( 0 && pax->type == 'A' )
347 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);
348 if ( pax->marked != 0 )
350 if ( strcmp(symbol,pax->symbol) == 0 || pax->type == 'A' )
352 if ( pax->marked == 0 )
354 if ( komodo_is_issuer() != 0 )
356 if ( pax->validated != 0 && pax->type == 'D' )
358 total += pax->fiatoshis;
362 else if ( pax->approved != 0 && pax->type == 'A' )
364 if ( pax->validated != 0 )
366 total += pax->komodoshis;
372 checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis);
373 //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);
374 //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight);
375 if ( seed != 0 && checktoshis != 0 )
377 if ( checktoshis == pax->komodoshis )
379 total += pax->komodoshis;
380 pax->validated = pax->komodoshis;
382 } else pax->marked = pax->height;
386 if ( 0 && pax->ready != 0 )
387 printf("%p (%c) pax.%s marked.%d %.8f -> %.8f validated.%d approved.%d ready.%d ht.%d\n",pax,pax->type,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0,pax->ready,pax->height);
391 //printf("paxtotal %.8f\n",dstr(total));
395 static int _paxorder(const void *a,const void *b)
397 #define pax_a (*(struct pax_transaction **)a)
398 #define pax_b (*(struct pax_transaction **)b)
400 aval = pax_a->fiatoshis + pax_a->komodoshis + pax_a->height;
401 bval = pax_b->fiatoshis + pax_b->komodoshis + pax_b->height;
404 else if ( bval < aval )
411 int32_t komodo_pending_withdraws(char *opretstr) // todo: enforce deterministic order
413 struct pax_transaction *pax,*pax2,*tmp,*paxes[64]; uint8_t opretbuf[16384]; int32_t i,n,ht,len=0; uint64_t total = 0;
414 if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
416 if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
419 HASH_ITER(hh,PAX,pax,tmp)
421 if ( pax->type == 'W' )
423 if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'A')) != 0 )
425 if ( pax2->approved != 0 )
426 pax->approved = pax2->approved;
428 else if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'X')) != 0 )
429 pax->approved = pax->height;
430 //printf("pending_withdraw: pax %s marked.%u approved.%u validated.%llu\n",pax->symbol,pax->marked,pax->approved,(long long)pax->validated);
431 if ( pax->marked == 0 && pax->approved == 0 && pax->validated != 0 ) //strcmp((char *)"KMD",pax->symbol) == 0 &&
433 if ( n < sizeof(paxes)/sizeof(*paxes) )
436 //int32_t j; for (j=0; j<32; j++)
437 // printf("%02x",((uint8_t *)&pax->txid)[j]);
438 //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));
446 opretbuf[len++] = 'A';
447 qsort(paxes,n,sizeof(*paxes),_paxorder);
450 if ( len < (sizeof(opretbuf)>>3)*7 )
451 len += komodo_rwapproval(1,&opretbuf[len],paxes[i]);
454 init_hexbytes_noT(opretstr,opretbuf,len);
456 //fprintf(stderr,"komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal()));
460 int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
462 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;
463 if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
465 struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD");
466 sp = komodo_stateptr(symbol,dest);
468 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
470 PENDING_KOMODO_TX = 0;
473 if ( komodo_isrealtime(&ht) != 0 )
479 printf("%s not realtime ht.%d\n",ASSETCHAINS_SYMBOL,ht);
489 if ( komodo_paxtotal() == 0 )
492 HASH_ITER(hh,PAX,pax,tmp)
494 if ( pax->type != 'D' && pax->type != 'A' )
497 #ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE
498 if ( kmdsp != 0 && (kmdsp->NOTARIZED_HEIGHT >= pax->height || kmdsp->CURRENT_HEIGHT > pax->height+30) ) // assumes same chain as notarize
499 pax->validated = pax->komodoshis; //kmdsp->NOTARIZED_HEIGHT;
500 else pax->validated = pax->ready = 0;
502 pax->validated = pax->komodoshis;
505 if ( ASSETCHAINS_SYMBOL[0] != 0 && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
507 if ( pax->height > 214700 || strcmp(ASSETCHAINS_SYMBOL,symbol) == 0 )
508 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));
511 /*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);
512 if ( pax->marked != 0 || (pax->type != 'D' && pax->type != 'A') || pax->ready == 0 )
514 printf("reject 2\n");
517 if ( ASSETCHAINS_SYMBOL[0] != 0 && (strcmp(pax->symbol,symbol) != 0 || pax->validated == 0 || pax->ready == 0) )
519 if ( strcmp(pax->symbol,ASSETCHAINS_SYMBOL) == 0 )
520 printf("pax->symbol.%s != %s or null pax->validated %.8f ready.%d ht.(%d %d)\n",pax->symbol,symbol,dstr(pax->validated),pax->ready,kmdsp->CURRENT_HEIGHT,pax->height);
523 if ( pax->ready == 0 )
525 if ( pax->type == 'A' && ASSETCHAINS_SYMBOL[0] == 0 )
529 if ( (baseid= komodo_baseid(pax->symbol)) < 0 || ((1LL << baseid) & sp->RTmask) == 0 )
531 printf("not RT for (%s) %llx baseid.%d %llx\n",pax->symbol,(long long)sp->RTmask,baseid,(long long)(1LL<<baseid));
537 //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);
538 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
539 printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
540 txNew->vout.resize(numvouts+1);
541 txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
542 txNew->vout[numvouts].scriptPubKey.resize(25);
543 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
547 memcpy(script,pax->rmd160,20), script += 20;
553 data[len++] = ((uint8_t *)&pax->txid)[i];
554 data[len++] = pax->vout & 0xff;
555 data[len++] = (pax->vout >> 8) & 0xff;
556 PENDING_KOMODO_TX += pax->fiatoshis;
560 len += komodo_rwapproval(1,&data[len],pax);
561 PENDING_KOMODO_TX += pax->komodoshis;
562 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));
564 if ( numvouts++ >= 64 )
570 strcpy(symbol,(char *)"KMD");
571 for (i=0; symbol[i]!=0; i++)
572 data[len++] = symbol[i];
574 for (i=0; i<len; i++)
575 printf("%02x",data[i]);
576 printf(" <- data[%d]\n",len);
577 opretlen = komodo_opreturnscript(opret,opcode,data,len);
578 txNew->vout.resize(numvouts+1);
579 txNew->vout[numvouts].nValue = 0;
580 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
581 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
582 memcpy(script,opret,opretlen);
584 printf("%02x",opret[i]);
585 printf(" <- opret, MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
591 int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
593 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; struct komodo_state *sp;
594 if ( KOMODO_PAX == 0 || komodo_isrealtime(&ht) == 0 || KOMODO_PASSPORT_INITDONE == 0 )
596 memset(baseids,0xff,sizeof(baseids));
597 memset(values,0,sizeof(values));
598 memset(srcvalues,0,sizeof(srcvalues));
599 memset(rmd160s,0,sizeof(rmd160s));
600 memset(kmdheights,0,sizeof(kmdheights));
601 memset(otherheights,0,sizeof(otherheights));
602 n = block.vtx[0].vout.size();
603 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
604 if ( n <= 2 || script[0] != 0x6a )
606 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
607 if ( ASSETCHAINS_SYMBOL[0] == 0 )
609 //for (i=0; i<opretlen; i++)
610 // printf("%02x",script[i]);
611 //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
613 strcpy(symbol,(char *)"KMD");
617 strcpy(symbol,ASSETCHAINS_SYMBOL);
619 if ( komodo_baseid(symbol) < 0 )
621 if ( block.vtx[0].vout.size() != 1 )
623 printf("%s has more than one coinbase?\n",symbol);
629 if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
631 if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
633 for (i=1; i<n-1; i++)
635 if ( (sp= komodo_stateptrget(CURRENCIES[baseids[i-1]])) != 0 && (sp->RTmask & (1LL << baseids[i-1])) == 0 )
637 printf("skip checkdeposit.%s not RT\n",CURRENCIES[baseids[i-1]]);
641 if ( (pax= komodo_paxfinds(txids[i-1],vouts[i-1])) != 0 ) // finds... make sure right one
644 if ( opcode == 'I' && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
646 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));
649 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))) )
651 if ( pax->marked != 0 && height >= 80820 )
653 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);
654 if ( pax->komodoshis != 0 || pax->fiatoshis != 0 )
656 else matched++; // onetime init bypass
660 if ( opcode == 'X' && strcmp(ASSETCHAINS_SYMBOL,CURRENCIES[baseids[i]]) == 0 )
661 printf("check deposit validates %s %.8f -> %.8f\n",CURRENCIES[baseids[i]],dstr(srcvalues[i]),dstr(values[i]));
668 printf("%02x",((uint8_t *)&txids[i-1])[j]);
669 printf(" cant paxfind %c txid\n",opcode);
670 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);
673 else if ( kmdheights[i-1] > 0 && otherheights[i-1] > 0 )
675 hash = block.GetHash();
677 printf("%02x",((uint8_t *)&hash)[j]);
678 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);
681 if ( ASSETCHAINS_SYMBOL[0] == 0 )
683 if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
685 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));
686 // can easily happen depending on order of loading
687 if ( height > 200000 )
689 printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
696 if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
698 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));
703 //printf("opretlen.%d num.%d\n",opretlen,num);
708 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
710 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;
711 const char *typestr = "unknown";
712 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 && opretbuf[0] != 'K' )
714 //printf("komodo_opreturn skip %s\n",ASSETCHAINS_SYMBOL);
715 return("assetchain");
717 //else if ( KOMODO_PAX == 0 )
719 memset(baseids,0xff,sizeof(baseids));
720 memset(values,0,sizeof(values));
721 memset(srcvalues,0,sizeof(srcvalues));
722 memset(rmd160s,0,sizeof(rmd160s));
723 memset(kmdheights,0,sizeof(kmdheights));
724 memset(otherheights,0,sizeof(otherheights));
725 tokomodo = (komodo_is_issuer() == 0);
726 if ( opretbuf[0] == 'K' && opretlen != 40 )
728 komodo_kvupdate(opretbuf,opretlen,value);
730 else if ( opretbuf[0] == 'D' )
733 if ( opretlen == 38 ) // any KMD tx
735 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
736 memset(base,0,sizeof(base));
737 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
738 bitcoin_address(coinaddr,addrtype,rmd160,20);
739 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
741 if ( kmdheight > 195000 || kmdheight <= height )
744 if ( 0 && kmdheight > 214700 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
746 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);
748 printf("%02x",((uint8_t *)&txid)[i]);
749 printf(" <- txid.v%u ",vout);
751 printf("%02x",pubkey33[i]);
752 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
754 if ( komodo_paxcmp(base,kmdheight,value,checktoshis,kmdheight < 225000 ? seed : 0) == 0 )
756 if ( (pax= komodo_paxfind(txid,vout,'D')) == 0 )
758 if ( (basesp= komodo_stateptrget(base)) != 0 )
760 basesp->deposited += fiatoshis;
762 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
763 printf("########### %p deposited %s += %.8f kmdheight.%d %.8f\n",basesp,base,dstr(fiatoshis),kmdheight,dstr(value));
764 } else printf("cant get stateptr.(%s)\n",base);
765 komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0);
767 if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 )
769 pax->height = kmdheight;
770 pax->validated = value;
771 pax->komodoshis = value;
772 pax->fiatoshis = fiatoshis;
773 if ( didstats == 0 && pax->didstats == 0 )
775 if ( (basesp= komodo_stateptrget(base)) != 0 )
777 basesp->deposited += fiatoshis;
779 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
780 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));
785 if ( (pax2= komodo_paxfind(txid,vout,'I')) != 0 )
787 pax2->fiatoshis = pax->fiatoshis;
788 pax2->komodoshis = pax->komodoshis;
789 pax->marked = pax2->marked = pax->height;
790 pax2->height = pax->height = height;
791 if ( pax2->didstats == 0 )
793 if ( (basesp= komodo_stateptrget(base)) != 0 )
795 basesp->issued += pax2->fiatoshis;
797 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
798 printf("########### %p issueda %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->komodoshis),pax2->otherheight);
804 else if ( kmdheight > 182000 && (kmdheight > 214700 || strcmp(base,ASSETCHAINS_SYMBOL) == 0) ) //seed != 0 &&
805 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);
806 } //else printf("paxdeposit height.%d vs kmdheight.%d\n",height,kmdheight);
809 else if ( opretbuf[0] == 'I' )
812 if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
814 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
818 if ( baseids[i] < 0 )
820 printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]);
823 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
824 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0);
825 komodo_paxmark(height,txids[i],vouts[i],'I',height);
826 if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 )
828 pax->type = opretbuf[0];
829 strcpy(pax->source,(char *)&opretbuf[opretlen-4]);
830 if ( (pax2= komodo_paxfind(txids[i],vouts[i],'D')) != 0 && pax2->fiatoshis != 0 && pax2->komodoshis != 0 )
833 pax->fiatoshis = pax2->fiatoshis;
834 pax->komodoshis = pax2->komodoshis;
835 pax->marked = pax2->marked = pax2->height;
836 if ( pax->didstats == 0 )
838 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
840 basesp->issued += pax->fiatoshis;
842 pax->height = pax2->height;
843 pax->otherheight = height;
844 if ( 0 && strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
845 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);
850 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'I',height)) != 0 )
851 komodo_paxdelete(pax);
852 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'D',height)) != 0 )
853 komodo_paxdelete(pax);
855 } //else printf("opreturn none issued?\n");
858 else if ( opretbuf[0] == 'W' )//&& opretlen >= 38 )
860 if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 && strcmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]) == 0 )
862 for (i=0; i<opretlen; i++)
863 printf("%02x",opretbuf[i]);
864 printf(" [%s] reject obsolete withdraw request.%s\n",ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]);
868 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
869 memset(base,0,sizeof(base));
870 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
871 bitcoin_address(coinaddr,addrtype,rmd160,20);
872 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
873 typestr = "withdraw";
874 //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);
876 //if ( komodo_paxcmp(base,kmdheight,komodoshis,checktoshis,seed) == 0 )
878 if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) )
880 if ( (basesp= komodo_stateptrget(base)) != 0 )
882 basesp->withdrawn += value;
884 if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
885 printf("########### %p withdrawn %s += %.8f check %.8f\n",basesp,base,dstr(value),dstr(checktoshis));
887 if ( 0 && strcmp(base,"RUB") == 0 && (pax == 0 || pax->approved == 0) )
888 printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
890 komodo_gateway_deposit(coinaddr,0,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0);
891 if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 )
893 pax->type = opretbuf[0];
894 strcpy(pax->source,base);
895 strcpy(pax->symbol,"KMD");
896 pax->height = kmdheight;
897 pax->otherheight = height;
898 pax->komodoshis = komodoshis;
900 } // else printf("withdraw %s paxcmp ht.%d %d error value %.8f -> %.8f vs %.8f\n",base,kmdheight,height,dstr(value),dstr(komodoshis),dstr(checktoshis));
901 // need to allocate pax
903 else if ( tokomodo != 0 && opretbuf[0] == 'A' )
906 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
908 for (i=0; i<opretlen; i++)
909 printf("%02x",opretbuf[i]);
910 printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
912 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
916 //for (j=0; j<32; j++)
917 // printf("%02x",((uint8_t *)&txids[i])[j]);
918 //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]);
919 if ( baseids[i] < 0 )
921 for (i=0; i<opretlen; i++)
922 printf("%02x",opretbuf[i]);
923 printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
924 //printf("baseids[%d] %d\n",i,baseids[i]);
925 if ( (pax= komodo_paxfind(txids[i],vouts[i],'W')) != 0 || (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
927 baseids[i] = komodo_baseid(pax->symbol);
928 printf("override neg1 with (%s)\n",pax->symbol);
930 if ( baseids[i] < 0 )
935 checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)values[i]);
936 //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);
937 if ( srcvalues[i] == checktoshis )
939 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
941 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
942 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]);
943 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
944 printf("unexpected null pax for approve\n");
945 else pax->validated = checktoshis;
946 if ( (pax2= komodo_paxfind(txids[i],vouts[i],'W')) != 0 )
947 pax2->approved = kmdheights[i];
948 komodo_paxmark(height,txids[i],vouts[i],'W',height);
949 //komodo_paxmark(height,txids[i],vouts[i],'A',height);
950 if ( values[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
952 basesp->approved += values[i];
954 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]);
956 //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i]));
958 else if ( pax->didstats == 0 && srcvalues[i] != 0 )
960 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
962 basesp->approved += values[i];
964 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]);
966 } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
967 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 )
969 pax->type = opretbuf[0];
970 pax->approved = kmdheights[i];
971 pax->validated = checktoshis;
974 //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
975 //printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax);
979 } //else printf("n.%d from opreturns\n",n);
980 //printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
982 else if ( opretbuf[0] == 'X' )
985 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
989 if ( baseids[i] < 0 )
991 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
992 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0);
993 komodo_paxmark(height,txids[i],vouts[i],'W',height);
994 komodo_paxmark(height,txids[i],vouts[i],'A',height);
995 komodo_paxmark(height,txids[i],vouts[i],'X',height);
996 if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
998 pax->type = opretbuf[0];
999 if ( height < 121842 ) // fields got switched around due to legacy issues and approves
1000 value = srcvalues[i];
1001 else value = values[i];
1002 if ( baseids[i] >= 0 && value != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
1004 basesp->redeemed += value;
1006 if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
1007 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]);
1010 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'W',height)) != 0 )
1011 komodo_paxdelete(pax);
1012 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'A',height)) != 0 )
1013 komodo_paxdelete(pax);
1014 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'X',height)) != 0 )
1015 komodo_paxdelete(pax);
1017 } //else printf("komodo_issued_opreturn returned %d\n",n);
1022 void komodo_passport_iteration()
1024 static long lastpos[34]; static char userpass[33][1024]; int32_t maxseconds = 7;
1025 FILE *fp; int32_t baseid,n,ht,isrealtime,expired,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;
1026 //printf("PASSPORT.(%s)\n",ASSETCHAINS_SYMBOL);
1028 while ( KOMODO_INITDONE == 0 )
1030 fprintf(stderr,"[%s] PASSPORT iteration waiting for KOMODO_INITDONE\n",ASSETCHAINS_SYMBOL);
1033 refsp = komodo_stateptr(symbol,dest);
1034 if ( ASSETCHAINS_SYMBOL[0] == 0 )
1038 refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
1041 KOMODO_PASSPORT_INITDONE = 1;
1045 if ( KOMODO_PAX == 0 )
1047 KOMODO_PASSPORT_INITDONE = 1;
1050 starttime = (uint32_t)time(NULL);
1051 //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
1052 for (baseid=32; baseid>=0; baseid--)
1054 if ( time(NULL) >= starttime+maxseconds )
1058 base = (char *)CURRENCIES[baseid];
1059 if ( baseid+1 != refid )
1061 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
1062 komodo_nameset(symbol,dest,base);
1063 sp = komodo_stateptrget(symbol);
1065 if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 )
1067 fseek(fp,0,SEEK_END);
1068 if ( ftell(fp) > lastpos[baseid] )
1070 if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
1071 printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
1072 fseek(fp,lastpos[baseid],SEEK_SET);
1073 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 && n < 1000 )
1077 if ( time(NULL) < starttime+maxseconds )
1081 //printf("expire passport loop %s -> %s at %ld\n",ASSETCHAINS_SYMBOL,base,lastpos[baseid]);
1087 lastpos[baseid] = ftell(fp);
1088 if ( lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
1089 printf("from.(%s) lastpos[%s] %ld isrt.%d\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid],komodo_isrealtime(&ht));
1090 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
1092 } else printf("error.(%s) %p\n",fname,sp);
1093 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1094 if ( (fp= fopen(fname,"rb")) != 0 )
1096 if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
1098 sp->CURRENT_HEIGHT = buf[0];
1099 if ( buf[0] != 0 && buf[0] >= buf[1] && buf[2] > time(NULL)-300 )
1102 RTmask |= (1LL << baseid);
1103 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1104 } else if ( (time(NULL)-buf[2]) > 1200 )
1105 fprintf(stderr,"[%s]: %s not RT %u %u %d\n",ASSETCHAINS_SYMBOL,base,buf[0],buf[1],(int32_t)(time(NULL)-buf[2]));
1106 } //else fprintf(stderr,"%s size error RT\n",base);
1108 } //else fprintf(stderr,"%s open error RT\n",base);
1112 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1113 if ( (fp= fopen(fname,"wb")) != 0 )
1115 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
1116 buf[1] = (uint32_t)komodo_longestchain();
1117 if ( buf[0] != 0 && buf[0] == buf[1] )
1119 buf[2] = (uint32_t)time(NULL);
1120 RTmask |= (1LL << baseid);
1121 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1123 memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
1125 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
1126 fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
1128 } else fprintf(stderr,"%s create error RT\n",base);
1130 if ( sp != 0 && isrealtime == 0 )
1131 refsp->RTbufs[0][2] = 0;
1134 refsp->RTmask = RTmask;
1135 if ( expired == 0 && KOMODO_PASSPORT_INITDONE == 0 )
1137 KOMODO_PASSPORT_INITDONE = 1;
1138 printf("done PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);