]> Git Repo - VerusCoin.git/blob - src/komodo_gateway.h
Merge pull request #565 from jl777/kolo-assets-new
[VerusCoin.git] / src / komodo_gateway.h
1 /******************************************************************************
2  * Copyright © 2014-2017 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
16 // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
17 #include "komodo_defs.h"
18
19 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 {
21     int32_t baseid; struct komodo_state *sp; int64_t netliability,maxallowed,maxval;
22     *available = *deposited = *issued = *withdrawn = *approved = *redeemed = 0;
23     if ( (baseid= komodo_baseid(base)) >= 0 )
24     {
25         if ( (sp= komodo_stateptrget(base)) != 0 )
26         {
27             *deposited = sp->deposited;
28             *issued = sp->issued;
29             *withdrawn = sp->withdrawn;
30             *approved = sp->approved;
31             *redeemed = sp->redeemed;
32             maxval = sp->approved;
33             if ( sp->withdrawn > maxval )
34                 maxval = sp->withdrawn;
35             netliability = (sp->issued - maxval) - sp->shorted;
36             maxallowed = komodo_maxallowed(baseid);
37             if ( netliability < maxallowed )
38                 *available = (maxallowed - netliability);
39             //printf("%llu - %llu %s %.8f %.8f %.8f %.8f %.8f\n",(long long)maxallowed,(long long)netliability,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed));
40             return(0);
41         } else printf("pax_fiatstatus cant get basesp.%s\n",base);
42     } // else printf("pax_fiatstatus illegal base.%s\n",base);
43     return(-1);
44 }
45
46 void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type)
47 {
48     memcpy(buf,&txid,32);
49     memcpy(&buf[32],&vout,2);
50     buf[34] = type;
51 }
52
53 struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type)
54 {
55     struct pax_transaction *pax; uint8_t buf[35];
56     pthread_mutex_lock(&komodo_mutex);
57     pax_keyset(buf,txid,vout,type);
58     HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
59     pthread_mutex_unlock(&komodo_mutex);
60     return(pax);
61 }
62
63 struct pax_transaction *komodo_paxfinds(uint256 txid,uint16_t vout)
64 {
65     struct pax_transaction *pax; int32_t i; uint8_t types[] = { 'I', 'D', 'X', 'A', 'W' };
66     for (i=0; i<sizeof(types)/sizeof(*types); i++)
67         if ( (pax= komodo_paxfind(txid,vout,types[i])) != 0 )
68             return(pax);
69     return(0);
70 }
71
72 struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark)
73 {
74     struct pax_transaction *pax; uint8_t buf[35];
75     pthread_mutex_lock(&komodo_mutex);
76     pax_keyset(buf,txid,vout,type);
77     HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
78     if ( pax == 0 )
79     {
80         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
81         pax->txid = txid;
82         pax->vout = vout;
83         pax->type = type;
84         memcpy(pax->buf,buf,sizeof(pax->buf));
85         HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
86         //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
87     }
88     if ( pax != 0 )
89     {
90         pax->marked = mark;
91         //if ( height > 214700 || pax->height > 214700 )
92         //    printf("mark ht.%d %.8f %.8f\n",pax->height,dstr(pax->komodoshis),dstr(pax->fiatoshis));
93         
94     }
95     pthread_mutex_unlock(&komodo_mutex);
96     return(pax);
97 }
98
99 void komodo_paxdelete(struct pax_transaction *pax)
100 {
101     return; // breaks when out of order
102     pthread_mutex_lock(&komodo_mutex);
103     HASH_DELETE(hh,PAX,pax);
104     pthread_mutex_unlock(&komodo_mutex);
105 }
106
107 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
108 {
109     struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN],*s;
110     //if ( KOMODO_PAX == 0 )
111     //    return;
112     //if ( strcmp(symbol,ASSETCHAINS_SYMBOL) != 0 )
113     //    return;
114     sp = komodo_stateptr(str,dest);
115     pthread_mutex_lock(&komodo_mutex);
116     pax_keyset(buf,txid,vout,type);
117     HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
118     if ( pax == 0 )
119     {
120         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
121         pax->txid = txid;
122         pax->vout = vout;
123         pax->type = type;
124         memcpy(pax->buf,buf,sizeof(pax->buf));
125         HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
126         addflag = 1;
127         if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 )
128         {
129             int32_t i; for (i=0; i<32; i++)
130                 printf("%02x",((uint8_t *)&txid)[i]);
131             printf(" v.%d [%s] kht.%d ht.%d create pax.%p symbol.%s source.%s\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax,symbol,source);
132         }
133     }
134     pthread_mutex_unlock(&komodo_mutex);
135     if ( coinaddr != 0 )
136     {
137         strcpy(pax->coinaddr,coinaddr);
138         if ( value != 0 )
139             pax->komodoshis = value;
140         if ( symbol != 0 )
141             strcpy(pax->symbol,symbol);
142         if ( source != 0 )
143             strcpy(pax->source,source);
144         if ( fiatoshis != 0 )
145             pax->fiatoshis = fiatoshis;
146         if ( rmd160 != 0 )
147             memcpy(pax->rmd160,rmd160,20);
148         if ( height != 0 )
149             pax->height = height;
150         if ( otherheight != 0 )
151             pax->otherheight = otherheight;
152     }
153     else
154     {
155         pax->marked = height;
156         //printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
157     }
158 }
159
160 int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax)
161 {
162     int32_t i,len = 0;
163     if ( rwflag == 1 )
164     {
165         for (i=0; i<32; i++)
166             opretbuf[len++] = ((uint8_t *)&pax->txid)[i];
167         opretbuf[len++] = pax->vout & 0xff;
168         opretbuf[len++] = (pax->vout >> 8) & 0xff;
169     }
170     else
171     {
172         for (i=0; i<32; i++)
173             ((uint8_t *)&pax->txid)[i] = opretbuf[len++];
174         //for (i=0; i<32; i++)
175         //    printf("%02x",((uint8_t *)&pax->txid)[31-i]);
176         pax->vout = opretbuf[len++];
177         pax->vout += ((uint32_t)opretbuf[len++] << 8);
178         //printf(" txid v.%d\n",pax->vout);
179     }
180     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis);
181     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
182     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height);
183     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight);
184     if ( rwflag != 0 )
185     {
186         memcpy(&opretbuf[len],pax->rmd160,20), len += 20;
187         for (i=0; i<4; i++)
188             opretbuf[len++] = pax->source[i];
189     }
190     else
191     {
192         memcpy(pax->rmd160,&opretbuf[len],20), len += 20;
193         for (i=0; i<4; i++)
194             pax->source[i] = opretbuf[len++];
195     }
196     return(len);
197 }
198
199 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)
200 {
201     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[KOMODO_ASSETCHAIN_MAXLEN];
202     //if ( KOMODO_PAX == 0 )
203     //    return(0);
204     incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
205     //41e77b91cb68dc2aa02fa88550eae6b6d44db676a7e935337b6d1392d9718f03cb0200305c90660400000000fbcbeb1f000000bde801006201000058e7945ad08ddba1eac9c9b6c8e1e97e8016a2d152
206     
207     // 41e94d736ec69d88c08b5d238abeeca609c02357a8317e0d56c328bcb1c259be5d0200485bc80200000000404b4c000000000059470200b80b000061f22ba7d19fe29ac3baebd839af8b7127d1f9075553440046bb4cc7a3b5cd39dffe7206507a3482a00780e617f68b273cce9817ed69298d02001069ca1b0000000080f0fa02000000005b470200b90b000061f22ba7d19fe29ac3baebd839af8b7127d1f90755
208     
209     //for (i=0; i<opretlen; i++)
210     //    printf("%02x",opretbuf[i]);
211     //printf(" opretlen.%d (%s)\n",opretlen,base);
212     //printf(" opretlen.%d vs %d incr.%d (%d)\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 2),incr,opretlen/incr);
213     //if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
214     {
215         type = opretbuf[0];
216         opretbuf++, opretlen--;
217         for (n=0; n<opretlen/incr; n++)
218         {
219             if ( iskomodo != 0 )
220             {
221                 memset(&p,0,sizeof(p));
222                 len += komodo_rwapproval(0,&opretbuf[len],&p);
223                 if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 )
224                 {
225                     txids[n] = p.txid;
226                     vouts[n] = p.vout;
227                     values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis;
228                     srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis;
229                     kmdheights[n] = p.height;
230                     otherheights[n] = p.otherheight;
231                     memcpy(&rmd160s[n * 20],p.rmd160,20);
232                     baseids[n] = komodo_baseid(p.source);
233                     if ( 0 )
234                     {
235                         char coinaddr[64];
236                         bitcoin_address(coinaddr,60,&rmd160s[n * 20],20);
237                         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                     }
239                 }
240             }
241             else
242             {
243                 for (i=0; i<4; i++)
244                     base[i] = opretbuf[opretlen-4+i];
245                 for (j=0; j<32; j++)
246                 {
247                     ((uint8_t *)&txids[n])[j] = opretbuf[len++];
248                     //printf("%02x",((uint8_t *)&txids[n])[j]);
249                 }
250                 vouts[n] = opretbuf[len++];
251                 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
252                 baseids[n] = komodo_baseid(base);
253                 if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 )
254                 {
255                     values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis;
256                     srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis;
257                     kmdheights[n] = pax->height;
258                     otherheights[n] = pax->otherheight;
259                     memcpy(&rmd160s[n * 20],pax->rmd160,20);
260                 }
261             }
262             //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
263         }
264     }
265     return(n);
266 }
267
268 int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t checkvalue,uint64_t seed)
269 {
270     int32_t ratio;
271     if ( seed == 0 && checkvalue != 0 )
272     {
273         ratio = ((value << 6) / checkvalue);
274         if ( ratio >= 60 && ratio <= 67 )
275             return(0);
276         else
277         {
278             if ( ASSETCHAINS_SYMBOL[0] != 0 )
279                 printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio);
280             return(-1);
281         }
282     }
283     else if ( checkvalue != 0 )
284     {
285         ratio = ((value << 10) / checkvalue);
286         if ( ratio >= 1023 && ratio <= 1025 )
287             return(0);
288     }
289     return(value != checkvalue);
290 }
291
292 uint64_t komodo_paxtotal()
293 {
294     struct pax_transaction *pax,*pax2,*tmp,*tmp2; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp;
295     if ( KOMODO_PASSPORT_INITDONE == 0 ) //KOMODO_PAX == 0 ||
296         return(0);
297     if ( komodo_isrealtime(&ht) == 0 )
298         return(0);
299     else
300     {
301         HASH_ITER(hh,PAX,pax,tmp)
302         {
303             if ( pax->marked != 0 )
304                 continue;
305             if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' )
306                 str = pax->symbol;
307             else str = pax->source;
308             basesp = komodo_stateptrget(str);
309             if ( basesp != 0 && pax->didstats == 0 )
310             {
311                 if ( pax->type == 'I' && (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 )
312                 {
313                     if ( pax2->fiatoshis != 0 )
314                     {
315                         pax->komodoshis = pax2->komodoshis;
316                         pax->fiatoshis = pax2->fiatoshis;
317                         basesp->issued += pax->fiatoshis;
318                         pax->didstats = 1;
319                         if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
320                             printf("########### %p issued %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,str,dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight);
321                         pax2->marked = pax->height;
322                         pax->marked = pax->height;
323                     }
324                 }
325                 else if ( pax->type == 'W' )
326                 {
327                     //bitcoin_address(coinaddr,addrtype,rmd160,20);
328                     if ( (checktoshis= komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis)) != 0 )
329                     {
330                         if ( komodo_paxcmp(pax->source,pax->height,pax->komodoshis,checktoshis,seed) != 0 )
331                         {
332                             pax->marked = pax->height;
333                             //printf("WITHDRAW.%s mark <- %d %.8f != %.8f\n",pax->source,pax->height,dstr(checktoshis),dstr(pax->komodoshis));
334                         }
335                         else if ( pax->validated == 0 )
336                         {
337                             pax->validated = pax->komodoshis = checktoshis;
338                             //int32_t j; for (j=0; j<32; j++)
339                             //    printf("%02x",((uint8_t *)&pax->txid)[j]);
340                             //if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
341                             //    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                         }
343                     }
344                 }
345             }
346         }
347     }
348     komodo_stateptr(symbol,dest);
349     HASH_ITER(hh,PAX,pax,tmp)
350     {
351         pax->ready = 0;
352         if ( 0 && pax->type == 'A' )
353             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);
354         if ( pax->marked != 0 )
355             continue;
356         if ( strcmp(symbol,pax->symbol) == 0 || pax->type == 'A' )
357         {
358             if ( pax->marked == 0 )
359             {
360                 if ( komodo_is_issuer() != 0 )
361                 {
362                     if ( pax->validated != 0 && pax->type == 'D' )
363                     {
364                         total += pax->fiatoshis;
365                         pax->ready = 1;
366                     }
367                 }
368                 else if ( pax->approved != 0 && pax->type == 'A' )
369                 {
370                     if ( pax->validated != 0 )
371                     {
372                         total += pax->komodoshis;
373                         pax->ready = 1;
374                     }
375                     else
376                     {
377                         seed = 0;
378                         checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis);
379                         //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);
380                         //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight);
381                         if ( seed != 0 && checktoshis != 0 )
382                         {
383                             if ( checktoshis == pax->komodoshis )
384                             {
385                                 total += pax->komodoshis;
386                                 pax->validated = pax->komodoshis;
387                                 pax->ready = 1;
388                             } else pax->marked = pax->height;
389                         }
390                     }
391                 }
392                 if ( 0 && pax->ready != 0 )
393                     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);
394             }
395         }
396     }
397     //printf("paxtotal %.8f\n",dstr(total));
398     return(total);
399 }
400
401 static int _paxorder(const void *a,const void *b)
402 {
403 #define pax_a (*(struct pax_transaction **)a)
404 #define pax_b (*(struct pax_transaction **)b)
405     uint64_t aval,bval;
406     aval = pax_a->fiatoshis + pax_a->komodoshis + pax_a->height;
407     bval = pax_b->fiatoshis + pax_b->komodoshis + pax_b->height;
408         if ( bval > aval )
409                 return(-1);
410         else if ( bval < aval )
411                 return(1);
412         return(0);
413 #undef pax_a
414 #undef pax_b
415 }
416
417 int32_t komodo_pending_withdraws(char *opretstr) // todo: enforce deterministic order
418 {
419     struct pax_transaction *pax,*pax2,*tmp,*paxes[64]; uint8_t opretbuf[16384]; int32_t i,n,ht,len=0; uint64_t total = 0;
420     if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
421         return(0);
422     if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
423         return(0);
424     n = 0;
425     HASH_ITER(hh,PAX,pax,tmp)
426     {
427         if ( pax->type == 'W' )
428         {
429             if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'A')) != 0 )
430             {
431                 if ( pax2->approved != 0 )
432                     pax->approved = pax2->approved;
433             }
434             else if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'X')) != 0 )
435                 pax->approved = pax->height;
436             //printf("pending_withdraw: pax %s marked.%u approved.%u validated.%llu\n",pax->symbol,pax->marked,pax->approved,(long long)pax->validated);
437             if ( pax->marked == 0 && pax->approved == 0 && pax->validated != 0 ) //strcmp((char *)"KMD",pax->symbol) == 0 &&
438             {
439                 if ( n < sizeof(paxes)/sizeof(*paxes) )
440                 {
441                     paxes[n++] = pax;
442                     //int32_t j; for (j=0; j<32; j++)
443                     //    printf("%02x",((uint8_t *)&pax->txid)[j]);
444                     //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));
445                 }
446             }
447         }
448     }
449     opretstr[0] = 0;
450     if ( n > 0 )
451     {
452         opretbuf[len++] = 'A';
453         qsort(paxes,n,sizeof(*paxes),_paxorder);
454         for (i=0; i<n; i++)
455         {
456             if ( len < (sizeof(opretbuf)>>3)*7 )
457                 len += komodo_rwapproval(1,&opretbuf[len],paxes[i]);
458         }
459         if ( len > 0 )
460             init_hexbytes_noT(opretstr,opretbuf,len);
461     }
462     //fprintf(stderr,"komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal()));
463     return(len);
464 }
465
466 int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
467 {
468     struct pax_transaction *pax,*tmp; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; 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,sum = 0;
469     if ( KOMODO_PASSPORT_INITDONE == 0 )//KOMODO_PAX == 0 ||
470         return(0);
471     struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD");
472     sp = komodo_stateptr(symbol,dest);
473     strcpy(symbol,base);
474     if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
475         return(0);
476     PENDING_KOMODO_TX = 0;
477     for (i=0; i<3; i++)
478     {
479         if ( komodo_isrealtime(&ht) != 0 )
480             break;
481         sleep(1);
482     }
483     if ( i == 3 )
484     {
485         if ( tokomodo == 0 )
486             printf("%s not realtime ht.%d\n",ASSETCHAINS_SYMBOL,ht);
487         return(0);
488     }
489     if ( tokomodo == 0 )
490     {
491         opcode = 'I';
492     }
493     else
494     {
495         opcode = 'X';
496         if ( 1 || komodo_paxtotal() == 0 )
497             return(0);
498     }
499     HASH_ITER(hh,PAX,pax,tmp)
500     {
501         if ( pax->type != 'D' && pax->type != 'A' )
502             continue;
503         {
504 #ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE
505             if ( pax->height > 236000 )
506             {
507                 if ( kmdsp != 0 && kmdsp->NOTARIZED_HEIGHT >= pax->height )
508                     pax->validated = pax->komodoshis;
509                 else if ( kmdsp->CURRENT_HEIGHT > pax->height+30 )
510                     pax->validated = pax->ready = 0;
511             }
512             else
513             {
514                 if ( kmdsp != 0 && (kmdsp->NOTARIZED_HEIGHT >= pax->height || kmdsp->CURRENT_HEIGHT > pax->height+30) ) // assumes same chain as notarize
515                     pax->validated = pax->komodoshis;
516                 else pax->validated = pax->ready = 0;
517             }
518 #else
519             pax->validated = pax->komodoshis;
520 #endif
521         }
522         if ( ASSETCHAINS_SYMBOL[0] != 0 && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
523         {
524             //if ( pax->height > 214700 || strcmp(ASSETCHAINS_SYMBOL,symbol) == 0 )
525             //    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));
526             continue;
527         }
528         /*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);
529         if ( pax->marked != 0 || (pax->type != 'D' && pax->type != 'A') || pax->ready == 0 )
530         {
531             printf("reject 2\n");
532             continue;
533         }*/
534         if ( ASSETCHAINS_SYMBOL[0] != 0 && (strcmp(pax->symbol,symbol) != 0 || pax->validated == 0 || pax->ready == 0) )
535         {
536             if ( strcmp(pax->symbol,ASSETCHAINS_SYMBOL) == 0 )
537                 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);
538             pax->marked = pax->height;
539             continue;
540         }
541         if ( pax->ready == 0 )
542             continue;
543         if ( pax->type == 'A' && ASSETCHAINS_SYMBOL[0] == 0 )
544         {
545             if ( kmdsp != 0 )
546             {
547                 if ( (baseid= komodo_baseid(pax->symbol)) < 0 || ((1LL << baseid) & sp->RTmask) == 0 )
548                 {
549                     printf("not RT for (%s) %llx baseid.%d %llx\n",pax->symbol,(long long)sp->RTmask,baseid,(long long)(1LL<<baseid));
550                     continue;
551                 }
552             } else continue;
553         }
554
555         //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);
556         if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
557             printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
558         if ( opcode == 'I' )
559         {
560             sum += pax->fiatoshis;
561             if ( sum > available )
562                 break;
563         }
564         txNew->vout.resize(numvouts+1);
565         txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
566         txNew->vout[numvouts].scriptPubKey.resize(25);
567         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
568         *script++ = 0x76;
569         *script++ = 0xa9;
570         *script++ = 20;
571         memcpy(script,pax->rmd160,20), script += 20;
572         *script++ = 0x88;
573         *script++ = 0xac;
574         if ( tokomodo == 0 )
575         {
576             for (i=0; i<32; i++)
577                 data[len++] = ((uint8_t *)&pax->txid)[i];
578             data[len++] = pax->vout & 0xff;
579             data[len++] = (pax->vout >> 8) & 0xff;
580             PENDING_KOMODO_TX += pax->fiatoshis;
581         }
582         else
583         {
584             len += komodo_rwapproval(1,&data[len],pax);
585             PENDING_KOMODO_TX += pax->komodoshis;
586             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));
587         }
588         if ( numvouts++ >= 64 || sum > COIN )
589             break;
590     }
591     if ( numvouts > 1 )
592     {
593         if ( tokomodo != 0 )
594             strcpy(symbol,(char *)"KMD");
595         for (i=0; symbol[i]!=0; i++)
596             data[len++] = symbol[i];
597         data[len++] = 0;
598         for (i=0; i<len; i++)
599             printf("%02x",data[i]);
600         printf(" <- data[%d]\n",len);
601         opretlen = komodo_opreturnscript(opret,opcode,data,len);
602         txNew->vout.resize(numvouts+1);
603         txNew->vout[numvouts].nValue = 0;
604         txNew->vout[numvouts].scriptPubKey.resize(opretlen);
605         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
606         memcpy(script,opret,opretlen);
607         for (i=0; i<8; i++)
608             printf("%02x",opret[i]);
609         printf(" <- opret, MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
610         return(1);
611     }
612     return(0);
613 }
614
615 const char *banned_txids[] =
616 {
617     "78cb4e21245c26b015b888b14c4f5096e18137d2741a6de9734d62b07014dfca", //233559
618     "00697be658e05561febdee1aafe368b821ca33fbb89b7027365e3d77b5dfede5", //234172
619     "e909465788b32047c472d73e882d79a92b0d550f90be008f76e1edaee6d742ea", //234187
620     "f56c6873748a327d0b92b8108f8ec8505a2843a541b1926022883678fb24f9dc", //234188
621     "abf08be07d8f5b3a433ddcca7ef539e79a3571632efd6d0294ec0492442a0204", //234213
622     "3b854b996cc982fba8c06e76cf507ae7eed52ab92663f4c0d7d10b3ed879c3b0", //234367
623     "fa9e474c2cda3cb4127881a40eb3f682feaba3f3328307d518589024a6032cc4", //234635
624     "ca746fa13e0113c4c0969937ea2c66de036d20274efad4ce114f6b699f1bc0f3", //234662
625     "43ce88438de4973f21b1388ffe66e68fda592da38c6ef939be10bb1b86387041", //234697
626     "0aeb748de82f209cd5ff7d3a06f65543904c4c17387c9d87c65fd44b14ad8f8c", //234899
627     "bbd3a3d9b14730991e1066bd7c626ca270acac4127131afe25f877a5a886eb25", //235252
628     "fa9943525f2e6c32cbc243294b08187e314d83a2870830180380c3c12a9fd33c", //235253
629     "a01671c8775328a41304e31a6693bbd35e9acbab28ab117f729eaba9cb769461", //235265
630     "2ef49d2d27946ad7c5d5e4ab5c089696762ff04e855f8ab48e83bdf0cc68726d", //235295
631     "c85dcffb16d5a45bd239021ad33443414d60224760f11d535ae2063e5709efee", //235296
632     // all vouts banned
633     "c4ea1462c207547cd6fb6a4155ca6d042b22170d29801a465db5c09fec55b19d", //246748
634     "305dc96d8bc23a69d3db955e03a6a87c1832673470c32fe25473a46cc473c7d1", //247204
635 };
636
637 int32_t komodo_bannedset(int32_t *indallvoutsp,uint256 *array,int32_t max)
638 {
639     int32_t i;
640     if ( sizeof(banned_txids)/sizeof(*banned_txids) > max )
641     {
642         fprintf(stderr,"komodo_bannedset: buffer too small %ld vs %d\n",sizeof(banned_txids)/sizeof(*banned_txids),max);
643         exit(-1);
644     }
645     for (i=0; i<sizeof(banned_txids)/sizeof(*banned_txids); i++)
646         array[i] = uint256S(banned_txids[i]);
647     *indallvoutsp = i-2;
648     return(i);
649 }
650
651 void komodo_passport_iteration();
652
653 uint64_t komodo_commission(const CBlock &block)
654 {
655     int32_t i,j,n=0,txn_count; uint64_t total = 0;
656     txn_count = block.vtx.size();
657     for (i=0; i<txn_count; i++)
658     {
659         n = block.vtx[i].vout.size();
660         for (j=0; j<n; j++)
661         {
662             //fprintf(stderr,"(%d %.8f).%d ",i,dstr(block.vtx[i].vout[j].nValue),j);
663             if ( i != 0 || j != 1 )
664             {
665                 total += block.vtx[i].vout[j].nValue;
666             }
667         }
668     }
669     //fprintf(stderr,"txn.%d n.%d commission total %.8f -> %.8f\n",txn_count,n,dstr(total),dstr((total * ASSETCHAINS_COMMISSION) / COIN));
670     return((total * ASSETCHAINS_COMMISSION) / COIN);
671 }
672
673 uint32_t komodo_stake(int32_t validateflag,arith_uint256 bnTarget,int32_t nHeight,uint256 txid,int32_t vout,uint32_t blocktime,uint32_t prevtime,char *destaddr)
674 {
675     CBlockIndex *pindex; uint8_t hashbuf[128]; char address[64]; bits256 addrhash; arith_uint256 hashval; uint256 hash,pasthash; int64_t diff=0; int32_t segid,minage,i,iter=0; uint32_t txtime,winner = 0; uint64_t value,coinage,supply = ASSETCHAINS_SUPPLY + nHeight*ASSETCHAINS_REWARD/SATOSHIDEN;
676     txtime = komodo_txtime(&value,txid,vout,address);
677     if ( value == 0 || txtime == 0 )
678         return(0);
679     if ( (minage= nHeight*3) > 6000 )
680         minage = 6000;
681     if ( blocktime > txtime+minage && (pindex= komodo_chainactive(nHeight>200?nHeight-200:1)) != 0 )
682     {
683         vcalc_sha256(0,(uint8_t *)&addrhash,(uint8_t *)address,(int32_t)strlen(address));
684         segid = ((nHeight + addrhash.uints[0]) & 0x3f);
685         pasthash = pindex->GetBlockHash();
686         memcpy(hashbuf,&pasthash,sizeof(pasthash));
687         memcpy(&hashbuf[sizeof(pasthash)],&addrhash,sizeof(addrhash));
688         vcalc_sha256(0,(uint8_t *)&hash,hashbuf,(int32_t)sizeof(uint256)*2);
689         //fprintf(stderr,"(%s) vs. (%s) %s %.8f txtime.%u\n",address,destaddr,hash.ToString().c_str(),dstr(value),txtime);
690         for (iter=0; iter<3600; iter++)
691         {
692             diff = (iter + blocktime - txtime - minage);
693             if ( diff > 3600*24 )
694                 break;
695             coinage = (value * diff) * ((diff >> 16) + 1);
696             hashval = arith_uint256(supply * 64) * (UintToArith256(hash) / arith_uint256(coinage+1));
697             if ( hashval <= bnTarget )
698             {
699                 winner = 1;
700                 if ( validateflag == 0 )
701                 {
702                     blocktime += iter;
703                     blocktime += segid * 2;
704                 }
705                 break;
706             }
707             if ( validateflag != 0 )
708             {
709                 for (i=31; i>=24; i--)
710                     fprintf(stderr,"%02x",((uint8_t *)&hashval)[i]);
711                 fprintf(stderr," vs target ");
712                 for (i=31; i>=24; i--)
713                     fprintf(stderr,"%02x",((uint8_t *)&bnTarget)[i]);
714                 fprintf(stderr," segid.%d iter.%d winner.%d coinage.%llu %d ht.%d gap.%d %.8f diff.%d\n",segid,iter,winner,(long long)coinage,(int32_t)(blocktime - txtime),nHeight,(int32_t)(blocktime - prevtime),dstr(value),(int32_t)diff);
715                 break;
716             }
717         }
718         //fprintf(stderr,"iterated until i.%d winner.%d\n",i,winner);
719         if ( 0 )
720         {
721             for (i=31; i>=24; i--)
722                 fprintf(stderr,"%02x",((uint8_t *)&hashval)[i]);
723             fprintf(stderr," vs ");
724             for (i=31; i>=24; i--)
725                 fprintf(stderr,"%02x",((uint8_t *)&bnTarget)[i]);
726             fprintf(stderr," segid.%d iter.%d winner.%d coinage.%llu %d ht.%d t.%u %.8f diff.%d\n",segid,iter,winner,(long long)coinage,(int32_t)(blocktime - txtime),nHeight,blocktime,dstr(value),(int32_t)diff);
727         }
728     }
729     if ( nHeight < 2 )
730         return(blocktime);
731     return(blocktime * winner);
732 }
733
734 #define KOMODO_POWMINMULT 16
735 arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t height,int32_t goalperc)
736 {
737     CBlockIndex *pindex; arith_uint256 bnTarget,hashval,sum,ave; bool fNegative,fOverflow; int32_t i,n,ht,percPoS,diff;
738     *percPoSp = percPoS = 0;
739     sum = arith_uint256(0);
740     ave = sum;
741     for (i=n=0; i<100; i++)
742     {
743         ht = height - 100 + i;
744         if ( (pindex= komodo_chainactive(ht)) != 0 )
745         {
746             bnTarget.SetCompact(pindex->nBits,&fNegative,&fOverflow);
747             bnTarget = (bnTarget / arith_uint256(KOMODO_POWMINMULT));
748             hashval = UintToArith256(pindex->GetBlockHash());
749             if ( hashval <= bnTarget ) // PoW is never as easy as PoS/64, some PoS will be counted as PoW
750             {
751                 sum += hashval;
752                 n++;
753             } else percPoS++;
754         }
755     }
756     *percPoSp = percPoS;
757     target = (target / arith_uint256(KOMODO_POWMINMULT));
758     if ( n > 0 )
759     {
760         ave = (sum / arith_uint256(n));
761         if ( ave > target )
762             ave = target;
763     } else return(target);
764     if ( percPoS < goalperc ) // increase PoW diff -> lower bnTarget
765     {
766         bnTarget = (ave * arith_uint256(goalperc)) / arith_uint256(percPoS + goalperc);
767         if ( 1 )
768         {
769             for (i=31; i>=24; i--)
770                 fprintf(stderr,"%02x",((uint8_t *)&ave)[i]);
771             fprintf(stderr," increase diff -> ");
772             for (i=31; i>=24; i--)
773                 fprintf(stderr,"%02x",((uint8_t *)&bnTarget)[i]);
774             fprintf(stderr," floor diff ");
775             for (i=31; i>=24; i--)
776                 fprintf(stderr,"%02x",((uint8_t *)&target)[i]);
777             fprintf(stderr," ht.%d percPoS.%d vs goal.%d -> diff %d\n",height,percPoS,goalperc,goalperc - percPoS);
778         }
779     }
780     else if ( percPoS > goalperc ) // decrease PoW diff -> raise bnTarget
781     {
782         bnTarget = ((ave * arith_uint256(goalperc)) + (target * arith_uint256(percPoS))) / arith_uint256(percPoS + goalperc);
783        if ( 1 )
784        {
785            for (i=31; i>=24; i--)
786                fprintf(stderr,"%02x",((uint8_t *)&ave)[i]);
787            fprintf(stderr," decrease diff -> ");
788            for (i=31; i>=24; i--)
789                fprintf(stderr,"%02x",((uint8_t *)&bnTarget)[i]);
790            fprintf(stderr," floor diff ");
791            for (i=31; i>=24; i--)
792                fprintf(stderr,"%02x",((uint8_t *)&target)[i]);
793            fprintf(stderr," ht.%d percPoS.%d vs goal.%d -> diff %d\n",height,percPoS,goalperc,goalperc - percPoS);
794        }
795     }
796     else bnTarget = ave; // recent ave is perfect
797     return(bnTarget);
798 }
799
800 int32_t komodo_check_deposit(int32_t height,const CBlock& block,uint32_t prevtime) // verify above block is valid pax pricing
801 {
802     static uint256 array[64]; static int32_t numbanned,indallvouts;
803     int32_t i,j,k,n,ht,baseid,txn_count,activation,num,opretlen,offset=1,errs=0,matched=0,kmdheights[256],otherheights[256]; uint256 hash,txids[256]; char symbol[KOMODO_ASSETCHAIN_MAXLEN],base[KOMODO_ASSETCHAIN_MAXLEN]; uint16_t vouts[256]; int8_t baseids[256]; uint8_t *script,opcode,rmd160s[256*20]; uint64_t total,subsidy,available,deposited,issued,withdrawn,approved,redeemed,checktoshis,seed; int64_t values[256],srcvalues[256]; struct pax_transaction *pax; struct komodo_state *sp;
804     activation = 235300;
805     if ( *(int32_t *)&array[0] == 0 )
806         numbanned = komodo_bannedset(&indallvouts,array,(int32_t)(sizeof(array)/sizeof(*array)));
807     memset(baseids,0xff,sizeof(baseids));
808     memset(values,0,sizeof(values));
809     memset(srcvalues,0,sizeof(srcvalues));
810     memset(rmd160s,0,sizeof(rmd160s));
811     memset(kmdheights,0,sizeof(kmdheights));
812     memset(otherheights,0,sizeof(otherheights));
813     txn_count = block.vtx.size();
814     if ( ASSETCHAINS_SYMBOL[0] == 0 )
815     {
816         for (i=0; i<txn_count; i++)
817         {
818             n = block.vtx[i].vin.size();
819             for (j=0; j<n; j++)
820             {
821                 for (k=0; k<numbanned; k++)
822                 {
823                     if ( block.vtx[i].vin[j].prevout.hash == array[k] && (block.vtx[i].vin[j].prevout.n == 1 || k >= indallvouts)  )
824                     {
825                         printf("banned tx.%d being used at ht.%d txi.%d vini.%d\n",k,height,i,j);
826                         return(-1);
827                     }
828                 }
829             }
830         }
831     }
832     n = block.vtx[0].vout.size();
833     script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
834     //if ( n <= 2 || script[0] != 0x6a )
835     {
836         int64_t val,prevtotal = 0; int32_t overflow = 0;
837         total = 0;
838         for (i=1; i<n; i++)
839         {
840             if ( (val= block.vtx[0].vout[i].nValue) < 0 || val >= MAX_MONEY )
841             {
842                 overflow = 1;
843                 break;
844             }
845             total += val;
846             if ( total < prevtotal || (val != 0 && total == prevtotal) )
847             {
848                 overflow = 1;
849                 break;
850             }
851             prevtotal = total;
852         }
853         if ( ASSETCHAINS_SYMBOL[0] == 0 )
854         {
855             if ( overflow != 0 || total > COIN/10 )
856             {
857                 //fprintf(stderr,">>>>>>>> <<<<<<<<<< ht.%d illegal nonz output %.8f n.%d\n",height,dstr(block.vtx[0].vout[1].nValue),n);
858                 if ( height >= activation )
859                     return(-1);
860             }
861             else if ( block.nBits == KOMODO_MINDIFF_NBITS && total > 0 ) // to deal with fee stealing
862             {
863                 fprintf(stderr,"notary mined ht.%d with extra %.8f\n",height,dstr(total));
864                 if ( height > KOMODO_NOTARIES_HEIGHT1 )
865                     return(-1);
866             }
867         }
868         else
869         {
870             if ( ASSETCHAINS_STAKED != 0 && height >= 2 )
871             {
872                 arith_uint256 bnTarget,hashval; int32_t PoSperc; bool fNegative,fOverflow; CBlockIndex *previndex; uint32_t eligible,isPoS = 0;
873                 bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
874                 if ( txn_count > 1 )
875                 {
876                     if ( prevtime == 0 )
877                     {
878                         if ( (previndex= mapBlockIndex[block.hashPrevBlock]) != 0 )
879                             prevtime = (uint32_t)previndex->nTime;
880                     }
881                     eligible = komodo_stake(1,bnTarget,height,block.vtx[txn_count-1].vin[0].prevout.hash,block.vtx[txn_count-1].vin[0].prevout.n,block.nTime,prevtime,(char *)"");
882                     if ( eligible == 0 || eligible > block.nTime )
883                     {
884                         fprintf(stderr,"PoS failure ht.%d eligible.%u vs blocktime.%u, lag.%d -> check to see if it is PoW block\n",height,eligible,(uint32_t)block.nTime,(int32_t)(eligible - block.nTime));
885                     } else isPoS = 1;
886                 }
887                 if ( isPoS == 0 && height > 100 )
888                 {
889                     if ( ASSETCHAINS_STAKED == 100 )
890                     {
891                         fprintf(stderr,"ht.%d 100%% PoS after height 100 rule violated for -ac_staking=100\n",height);
892                         return(-1);
893                     }
894                     // check PoW
895                     bnTarget = komodo_PoWtarget(&PoSperc,bnTarget,height,ASSETCHAINS_STAKED);
896                     hashval = UintToArith256(block.GetHash());
897                     if ( hashval > bnTarget )
898                     {
899                         /*for (i=31; i>=0; i--)
900                             fprintf(stderr,"%02x",((uint8_t *)&hashval)[i]);
901                         fprintf(stderr," > ");
902                         for (i=31; i>=0; i--)
903                             fprintf(stderr,"%02x",((uint8_t *)&bnTarget)[i]);
904                         fprintf(stderr," ht.%d PoW diff violation PoSperc.%d vs goalperc.%d\n",height,PoSperc,(int32_t)ASSETCHAINS_STAKED);*/
905                         return(-1);
906                     }
907                 }
908             }
909             if ( ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 && ASSETCHAINS_COMMISSION != 0 && block.vtx[0].vout.size() > 1 )
910             {
911                 script = (uint8_t *)block.vtx[0].vout[1].scriptPubKey.data();
912                 if ( script[0] != 33 || script[34] != OP_CHECKSIG || memcmp(script+1,ASSETCHAINS_OVERRIDE_PUBKEY33,33) != 0 )
913                     return(-1);
914                 if ( (checktoshis = komodo_commission(block)) != 0 )
915                 {
916                     if ( block.vtx[0].vout[1].nValue != checktoshis )
917                     {
918                         fprintf(stderr,"checktoshis %.8f vs actual vout[1] %.8f\n",dstr(checktoshis),dstr(block.vtx[0].vout[1].nValue));
919                         return(-1);
920                     } else return(0);
921                 }
922             }
923             if ( overflow != 0 || total > 0 )
924                 return(-1);
925         }
926         return(0);
927     }
928 /*
929     //fprintf(stderr,"ht.%d n.%d nValue %.8f (%d %d %d)\n",height,n,dstr(block.vtx[0].vout[1].nValue),KOMODO_PAX,komodo_isrealtime(&ht),KOMODO_PASSPORT_INITDONE);
930     offset += komodo_scriptitemlen(&opretlen,&script[offset]);
931     //printf("offset.%d opretlen.%d [%02x %02x %02x %02x]\n",offset,opretlen,script[0],script[1],script[2],script[3]);
932     if ( ASSETCHAINS_SYMBOL[0] == 0 )
933     {
934         opcode = 'X';
935         if ( height >= 235300 )
936             return(-1);
937         strcpy(symbol,(char *)"KMD");
938         if ( komodo_isrealtime(&ht) == 0 || KOMODO_PASSPORT_INITDONE == 0 ) // init time already in DB
939             return(0);
940     }
941     else
942     {
943         strcpy(symbol,ASSETCHAINS_SYMBOL);
944         opcode = 'I';
945         if ( (baseid= komodo_baseid(symbol)) < 0 )
946         {
947             if ( block.vtx[0].vout.size() != 1 )
948             {
949                 printf("%s has more than one coinbase?\n",symbol);
950                 return(-1);
951             }
952             return(0);
953         }
954         while ( KOMODO_PASSPORT_INITDONE == 0 )
955             komodo_passport_iteration();
956         komodo_passport_iteration();
957         // grandfathering preexisting
958         if ( height < 777 )
959             return(0);
960         if ( baseid == USD ) // 6820 in balance calcs
961         {
962             if ( height <= 6821 || height <= 2000 || height == 2968 || height == 2960 || height == 2649 || height == 3275 || height == 3282 || height == 3328 || height == 3468 )
963             return(0);
964         }
965         else if ( baseid == EUR )
966         {
967             if ( height < 1200 || height == 1408 || height == 1111 || height == 1094 || height == 1092 || height == 1080 || height == 1053 || height == 1005 || height == 822 || height == 821 || height == 810 || height == 797 || height == 790 || height == 787 || height == 785 || height == 1313 || height == 1288 || height == 1263 || height == 1262 || height <= 211 || height == 210 || height == 188 || height == 185 || height == 182 || height == 181 || height == 171 || height == 170 || height == 169 || height == 168 || height == 167 || height == 166 || height == 165 || height == 164 || height == 163 || height == 162 )
968                 return(0);
969         }
970         else if ( baseid == JPY )
971         {
972             if ( height == 1038 || height == 1039 || height == 1040 || height == 1041 || height == 1014 || height == 998 || height == 973 || height == 139 || height == 971 || height == 141 || height == 816 || height == 814 || height == 803 || height == 142 || height == 782 || height == 145 || height == 181 || height == 186 || height == 192 || height == 190 || height == 189 || height == 255 || height == 218 || height == 233 || height == 259 || height == 278 || height == 361 || height == 367 || height == 733 || height == 688 || height == 468 )
973                 return(0);
974         }
975         else if ( baseid == GBP )
976         {
977             if ( height == 1025 || height == 1026 || height == 1027 || height == 1028 || height == 997 || height == 970 || height == 953 || height == 951 || height == 803 || height == 800 || height == 786 || height == 123 || height == 771 || height == 137 || height == 139 || height == 140 || height == 146 || height == 194 || height == 200 || height == 202 || height == 203 || height == 205 || height == 210 || height == 216 || height == 271 || height == 233 || height == 294 )
978                 return(0);
979         }
980         else if ( baseid == AUD )
981         {
982             if ( height == 1012 || height == 1013 || height == 1014 || height == 985 || height == 958 || height == 936 || height == 933 || height == 790 || height == 788 || height == 778 || height == 124 || height == 777 || height == 122 || height == 140 || height == 142 || height == 143 || height == 186 || height == 196 || height == 198 || height == 201 || height == 210 || height == 192 )
983                 return(0);
984         }
985         else if ( baseid == CAD )
986         {
987             if ( height < 1000 || height == 836 || height == 781 || height == 779 || height == 798 || height == 794 || height == 1181 || height == 1179 || height == 1159 || height == 1134 || height == 1132 || height == 121 || height == 970 || height == 119 || height == 299 || height == 302 || height == 303 || height == 306 || height == 308 || height == 355 || height == 362 || height == 367 || height == 363 || height == 364 || height == 380 || height == 373 || height == 390 || height == 437 || height == 777 )
988                 return(0);
989         }
990         else if ( baseid == CHF )
991         {
992             if ( height == 987 || height == 988 || height == 989 || height == 960 || height == 931 || height == 914 || height == 122 || height == 912 || height == 141 || height == 780 || height == 145 || height == 777 || height == 151 || height == 775 || height == 765 || height == 195 || height == 764 || height == 737 || height == 194 || height == 193 || height == 198 || height == 207 || height == 216 || height == 227 || height == 256 || height == 260 || height == 276 || height == 289 || height == 292 || height == 349 )
993                 return(0);
994         }
995         else if ( baseid == NZD )
996         {
997             if ( height < 1000 || height == 787 || height == 786 || height == 784 || height == 785 || height == 777 || height == 1113 || height == 1083 || height == 1067 || height == 265 || height == 1064 || height == 920 || height == 913 || height == 124 || height == 911 || height == 903 || height == 900 || height == 879 || height == 122 || height == 266 || height == 269 || height == 270 || height == 278 || height == 307 || height == 318 || height == 322 || height == 335 || height == 395 || height == 406 || height == 314 )
998                 return(0);
999         }
1000         else if ( baseid == CNY )
1001         {
1002             if ( height == 987 || height == 988 || height == 989 || height == 982 || height == 957 || height == 931 || height == 119 || height == 929 || height == 134 || height == 782 || height == 137 || height == 775 || height == 143 || height == 772 || height == 186 || height == 763 || height == 761 || height == 734 || height == 183 || height == 190 || height == 192 || height == 211 || height == 203 || height == 226 || height == 263 || height == 264 || height == 277 || height == 342 || height == 613 || height == 525 )
1003                 return(0);
1004         }
1005         else if ( baseid == RUB )
1006         {
1007             if ( height == 819 || height == 812 || height == 810 || height == 802 || height == 801 || height == 1074 || height == 1040 || height == 1014 || height == 163 || height == 1013 || height == 877 || height == 867 || height == 863 || height == 834 || height == 158 || height == 126 || height == 106 || height == 105 || height == 174 || height == 175 || height == 176 || height == 177 || height == 181 || height == 183 || height == 184 || height == 187 )
1008                 return(0);
1009         }
1010         else if ( baseid == MXN )
1011         {
1012             if ( height == 1079 || height == 1080 || height == 1081 || height == 1069 || height == 1048 || height == 1028 || height == 121 || height == 1027 || height == 146 || height == 894 || height == 891 || height == 148 || height == 882 || height == 879 || height == 151 || height == 856 || height == 158 || height == 822 || height == 197 || height == 203 || height == 207 || height == 209 || height == 213 || height == 217 || height == 218 || height == 226 || height == 283 || height == 302 || height == 319 || height == 318 || height == 569 )
1013                 return(0);
1014         }
1015         else if ( baseid == BRL )
1016         {
1017             if ( height == 1003 || height == 1004 || height == 1005 || height == 972 || height == 943 || height == 922 || height == 118 || height == 920 || height == 145 || height == 787 || height == 783 || height == 147 || height == 781 || height == 774 || height == 148 || height == 771 || height == 154 || height == 751 || height == 156 || height == 188 || height == 193 || height == 195 || height == 199 || height == 203 || height == 225 || height == 230 || height == 262 || height == 266 || height == 284 || height == 300 || height == 640 )
1018                 return(0);
1019         }
1020         else if ( baseid == INR )
1021         {
1022             if ( height == 998 || height == 999 || height == 1000 || height == 788 || height == 786 || height == 775 || height == 753 || height == 125 || height == 153 || height == 155 || height == 159 || height == 204 || height == 206 || height == 211 || height == 217 || height == 220 || height == 229 || height == 265 || height == 292 || height == 300 )
1023                 return(0);
1024         }
1025         else if ( baseid == PLN )
1026         {
1027             if ( height == 1029 || height == 1030 || height == 1031 || height == 1009 || height == 984 || height == 960 || height == 959 || height == 798 || height == 794 || height == 792 || height == 782 || height == 775 || height == 755 || height == 122 || height == 120 || height == 158 || height == 160 || height == 163 || height == 225 || height == 217 || height == 219 || height == 229 || height == 234 || height == 286 || height == 231 || height == 292 )
1028                 return(0);
1029         }
1030         else if ( baseid == HKD )
1031         {
1032             if ( height == 1013 || height == 1014 || height == 1015 || height == 1002 || height == 979 || height == 947 || height == 123 || height == 946 || height == 153 || height == 811 || height == 808 || height == 806 || height == 155 || height == 795 || height == 157 || height == 773 || height == 205 || height == 208 || height == 210 || height == 211 || height == 215 || height == 221 || height == 231 || height == 255 || height == 273 || height == 293 || height == 310 || height == 654 )
1033                 return(0);
1034         }
1035         else if ( baseid == TRY )
1036         {
1037             if ( height == 1022 || height == 1023 || height == 1051 || height == 991 || height == 974 || height == 949 || height == 141 || height == 948 || height == 783 || height == 153 || height == 781 || height == 779 || height == 776 || height == 768 || height == 765 || height == 121 || height == 142 || height == 144 || height == 143 || height == 149 || height == 204 || height == 210 || height == 216 || height == 229 || height == 280 || height == 145 )
1038                 return(0);
1039         }
1040         else if ( baseid == NOK )
1041         {
1042             if ( height == 987 || height == 988 || height == 989 || height == 962 || height == 911 || height == 907 || height == 125 || height == 765 || height == 150 || height == 762 || height == 760 || height == 753 || height == 152 || height == 751 || height == 151 || height == 154 || height == 160 || height == 195 || height == 203 || height == 207 || height == 211 || height == 220 || height == 236 || height == 253 || height == 278 || height == 283 )
1043                 return(0);
1044         }
1045         else if ( baseid == ZAR )
1046         {
1047             if ( height == 979 || height == 980 || height == 981 || height == 956 || height == 937 || height == 906 || height == 905 || height == 122 || height == 756 || height == 754 || height == 752 || height == 749 || height == 742 || height == 739 || height == 120 || height == 151 || height == 153 || height == 157 || height == 159 || height == 206 || height == 213 || height == 216 || height == 222 || height == 277 || height == 287 || height == 215 )
1048                 return(0);
1049         }
1050         else if ( baseid == SEK )
1051         {
1052             if ( height == 960 || height == 961 || height == 962 || height == 943 || height == 925 || height == 896 || height == 147 || height == 894 || height == 759 || height == 751 || height == 749 || height == 742 || height == 740 || height == 716 || height == 123 || height == 148 || height == 150 || height == 151 || height == 157 || height == 199 || height == 200 || height == 201 || height == 206 || height == 256 || height == 257 || height == 268 )
1053                 return(0);
1054         }
1055         else if ( baseid == CZK )
1056         {
1057             if ( height < 1000 || height == 1084 || height == 1085 || height == 1086 || height == 778 || height == 1079 || height == 1054 || height == 1032 || height == 121 || height == 1030 || height == 182 || height == 876 || height == 873 || height == 871 || height == 184 || height == 862 || height == 185 || height == 861 || height == 186 || height == 838 || height == 238 || height == 242 || height == 246 || height == 248 || height == 250 || height == 251 || height == 256 || height == 282 || height == 322 || height == 343 || height == 433 || height == 323 || height == 276 )
1058                 return(0);
1059         }
1060         else if ( baseid == HUF )
1061         {
1062             if ( height < 2200 || height == 1531 || height == 1528 || height == 1530 || height == 1451 || height == 1448 || height == 1442 || height == 1418 || height == 1407 || height == 1406 || height == 1392 || height == 1391 || height == 1387 || height == 1383 || height == 1335 || height == 1337 || height == 1338 || height == 1649 || height == 2378 || height == 2376 || height == 2371 || height == 1330 || height == 2326 || height == 1344 || height == 2305 || height == 2304 || height == 2171 || height == 2168 || height == 2166 || height == 2156 || height == 2135 || height == 1343 || height == 1342 || height == 1341 || height == 1340 || height == 1339 || height == 1336 || height == 1334 || height == 1333 || height == 1346 || height == 1400 || height == 1525 || height == 1509 )
1063                 return(0);
1064         }
1065         else if ( baseid == DKK )
1066         {
1067             if ( height == 1241 || height == 945 || height == 898 || height == 894 || height == 892 || height == 866 || height == 857 || height == 853 || height == 834 || height == 812 || height == 792 || height == 784 || height == 778 || height == 783 || height == 1214 || height == 1186 || height == 1158 || height == 126 || height == 1156 || height == 329 || height == 1021 || height == 1016 || height == 1014 || height == 333 || height == 1003 || height == 1002 || height == 978 || height == 332 || height == 337 || height == 381 || height == 390 || height == 386 || height == 401 || height == 389 || height == 396 || height == 428 || height == 473 || height == 490 || height == 493 )
1068                 return(0);
1069         }
1070         else if ( baseid == ILS )
1071         {
1072             if ( height == 1017 || height == 1018 || height == 1019 || height == 1010 || height == 985 || height == 954 || height == 120 || height == 952 || height == 143 || height == 819 || height == 817 || height == 808 || height == 145 || height == 786 || height == 155 || height == 753 || height == 151 || height == 149 || height == 199 || height == 193 || height == 201 || height == 209 || height == 284 || height == 305 || height == 676 || height == 702 || height == 406 )
1073                 return(0);
1074         }
1075         else if ( baseid == MYR )
1076         {
1077             if ( height == 1020 || height == 1021 || height == 1022 || height == 1015 || height == 984 || height == 950 || height == 949 || height == 823 || height == 820 || height == 818 || height == 812 || height == 809 || height == 126 || height == 789 || height == 144 || height == 746 || height == 146 || height == 147 || height == 148 || height == 150 || height == 205 || height == 210 || height == 215 || height == 219 || height == 222 || height == 290 || height == 242 || height == 312 )
1078                 return(0);
1079         }
1080         else if ( baseid == PHP )
1081         {
1082             if ( height == 859 || height == 817 || height == 812 || height == 811 || height == 792 || height == 779 || height == 778 || height == 781 || height == 1154 || height == 1114 || height == 1077 || height == 1076 || height == 931 || height == 927 || height == 925 || height == 918 || height == 891 || height == 127 || height == 125 || height == 272 || height == 275 || height == 276 || height == 278 || height == 328 || height == 330 || height == 335 || height == 344 || height == 315 )
1083                 return(0);
1084         }
1085         else if ( baseid == KRW )
1086         {
1087             if ( height == 1021 || height == 1022 || height == 1023 || height == 1010 || height == 950 || height == 916 || height == 138 || height == 914 || height == 140 || height == 785 || height == 782 || height == 781 || height == 142 || height == 775 || height == 148 || height == 772 || height == 751 || height == 147 || height == 146 || height == 159 || height == 175 || height == 181 || height == 184 || height == 186 || height == 188 || height == 196 || height == 209 || height == 238 )
1088                 return(0);
1089         }
1090         else if ( baseid == RON )
1091         {
1092             if ( height == 1008 || height == 1009 || height == 1010 || height == 1003 || height == 973 || height == 940 || height == 939 || height == 806 || height == 802 || height == 800 || height == 793 || height == 789 || height == 768 || height == 123 || height == 141 || height == 143 || height == 144 || height == 150 || height == 197 || height == 200 || height == 202 || height == 217 || height == 240 || height == 276 )
1093                 return(0);
1094         }
1095         else if ( baseid == SGD )
1096         {
1097             if ( height == 1016 || height == 1017 || height == 1018 || height == 1010 || height == 984 || height == 951 || height == 950 || height == 801 || height == 798 || height == 792 || height == 788 || height == 122 || height == 769 || height == 120 || height == 158 || height == 160 || height == 164 || height == 169 || height == 229 || height == 234 || height == 246 || height == 297 || height == 299 || height == 316 || height == 233 )
1098                 return(0);
1099         }
1100         else if ( baseid == THB )
1101         {
1102             if ( height == 977 || height == 978 || height == 968 || height == 933 || height == 898 || height == 897 || height == 788 || height == 786 || height == 779 || height == 777 || height == 757 || height == 119 || height == 140 || height == 142 || height == 146 || height == 152 || height == 196 || height == 200 || height == 214 || height == 260 || height == 263 )
1103                 return(0);
1104         }
1105         else if ( baseid == BGN )
1106         {
1107             if ( height == 1044 || height == 1045 || height == 1046 || height == 1030 || height == 995 || height == 960 || height == 959 || height == 830 || height == 822 || height == 819 || height == 810 || height == 809 || height == 781 || height == 124 || height == 122 || height == 189 || height == 193 || height == 200 || height == 194 || height == 250 || height == 256 || height == 260 || height == 280 )
1108                 return(0);
1109         }
1110         else if ( baseid == HRK )
1111         {
1112             if ( height == 1027 || height == 1028 || height == 1029 || height == 1022 || height == 999 || height == 969 || height == 967 || height == 832 || height == 829 || height == 827 || height == 817 || height == 792 || height == 754 || height == 125 || height == 123 || height == 184 || height == 186 || height == 187 || height == 227 || height == 230 || height == 235 || height == 240 || height == 242 || height == 261 || height == 336 )
1113                 return(0);
1114         }
1115         else if ( baseid == IDR )
1116         {
1117             if ( height == 836 || height == 1036 || height == 837 || height == 720 || height == 715 || height == 714 || height == 696 || height == 695 || height == 694 || height == 693 || height == 119 || height == 689 || height == 120 || height == 669 || height == 123 || height == 662 || height == 122 || height == 634 || height == 121 || height == 609 || height == 124 || height == 174 || height == 603 || height == 595 || height == 166 || height == 176 || height == 589 || height == 172 || height == 587 || height == 179 || height == 239 || height == 175 || height == 245 || height == 260 )
1118                 return(0);
1119         }
1120     }
1121     if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
1122     {
1123         if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
1124         {
1125             for (i=1; i<n-1; i++)
1126             {
1127                 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && (sp= komodo_stateptrget(ASSETCHAINS_SYMBOL)) != 0 && strcmp(CURRENCIES[baseids[i-1]],ASSETCHAINS_SYMBOL) == 0 )
1128                 {
1129                     int32_t rtflag = 0;
1130                     while ( (sp->RTmask & (1LL << 32)) == 0 )
1131                     {
1132                         if ( rtflag == 0 )
1133                             printf("[%s] skip checkdeposit.%s not RT %llx\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseids[i-1]],(long long)sp->RTmask);
1134                         sleep(60);
1135                         rtflag = 1;
1136                         //matched++;
1137                         //continue;
1138                     }
1139                     if ( rtflag != 0 )
1140                         printf("[%s] %s reached RT\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseids[i-1]]);
1141                 }
1142                 if ( (pax= komodo_paxfinds(txids[i-1],vouts[i-1])) != 0 ) // finds... make sure right one
1143                 {
1144                     pax->type = opcode;
1145                     if ( opcode == 'I' && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
1146                     {
1147                         printf("checkdeposit.[%s.%d]: skip %s %.8f when avail %.8f deposited %.8f, issued %.8f withdrawn %.8f approved %.8f redeemed %.8f\n",ASSETCHAINS_SYMBOL,height,symbol,dstr(pax->fiatoshis),dstr(available),dstr(deposited),dstr(issued),dstr(withdrawn),dstr(approved),dstr(redeemed));
1148                         return(-1);
1149                     }
1150                     if ( pax->fiatoshis == block.vtx[0].vout[i].nValue )
1151                     {
1152                         matched++;
1153                         if ( pax->marked != 0 && height >= 80820 )
1154                         {
1155                             printf(">>>>>>>>>>> %c errs.%d i.%d match %.8f vs %.8f paxmarked.%d kht.%d ht.%d [%s].%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,ASSETCHAINS_SYMBOL,height);
1156                         }
1157                         else
1158                         {
1159                         }
1160                         if ( strcmp(ASSETCHAINS_SYMBOL,CURRENCIES[baseids[i-1]]) == 0 )
1161                         {
1162                             //check deposit validates AUD.1047 [3] 0.14585530 -> 0.01000000 (0.14585530 0.01000000 0.01000000)
1163                             char destaddr[64],coinaddr[64]; uint8_t pubkey33[33];
1164                             checktoshis = PAX_fiatdest(&seed,0,destaddr,pubkey33,coinaddr,pax->height,CURRENCIES[baseids[i-1]],pax->fiatoshis);
1165                             //checktoshis = komodo_paxprice(&seed,pax->height,CURRENCIES[baseids[i-1]],(char *)"KMD",(uint64_t)pax->komodoshis);
1166                             if ( komodo_paxcmp(CURRENCIES[baseids[i-1]],pax->height,pax->komodoshis,checktoshis,seed) < 0 )
1167                             {
1168                                 printf("paxcmp FAIL when check deposit validates %s.%d [%d] %.8f -> %.8f (%.8f %.8f %.8f)\n",CURRENCIES[baseids[i-1]],height,i,dstr(srcvalues[i-1]),dstr(values[i-1]),dstr(pax->komodoshis),dstr(pax->fiatoshis),dstr(block.vtx[0].vout[i].nValue));
1169                                 return(-1);
1170                             } //else printf("check deposit validates %s.%d [%d] %.8f -> %.8f (%.8f %.8f %.8f)\n",CURRENCIES[baseids[i-1]],height,i,dstr(srcvalues[i-1]),dstr(values[i-1]),dstr(pax->komodoshis),dstr(pax->fiatoshis),dstr(block.vtx[0].vout[i].nValue));
1171                         }
1172                     }
1173                     else if ( strcmp(ASSETCHAINS_SYMBOL,CURRENCIES[baseids[i-1]]) == 0 )
1174                     {
1175                         for (j=0; j<32; j++)
1176                             printf("%02x",((uint8_t *)&txids[i-1])[j]);
1177                         printf(" cant paxfind %c txid [%d]\n",opcode,height);
1178                         printf(">>>>>>>>>>> %c errs.%d i.%d match %.8f vs %.8f pax.%p [%s] ht.%d\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax,ASSETCHAINS_SYMBOL,height);
1179                         return(-1);
1180                     }
1181                 }
1182                 else //if ( kmdheights[i-1] > 0 && otherheights[i-1] > 0 )
1183                 {
1184                     hash = block.GetHash();
1185                     for (j=0; j<n; j++)
1186                         printf("%.8f ",dstr(block.vtx[0].vout[j].nValue));
1187                     printf("vout values\n");
1188                     for (j=0; j<32; j++)
1189                         printf("%02x",((uint8_t *)&txids[i-1])[j]);
1190                     printf(" kht.%d ht.%d %.8f %.8f couldnt find vout.[%d of %d] ht.%d %s for [%s]\n",kmdheights[i-1],otherheights[i-1],dstr(values[i-1]),dstr(srcvalues[i]),i,n,height,ASSETCHAINS_SYMBOL,CURRENCIES[baseids[i-1]]);
1191                     if ( ASSETCHAINS_SYMBOL[0] != 0 || height >= activation )
1192                         return(-1);
1193                 }
1194             }
1195             if ( ASSETCHAINS_SYMBOL[0] == 0 )
1196             {
1197                 //if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
1198                 //{
1199                    // 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));
1200                     // can easily happen depending on order of loading
1201                     //if ( height > 200000 )
1202                     //{
1203                     //    printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
1204                     //    return(-1);
1205                     //}
1206                 //} // disabled 'X' path
1207             }
1208             else
1209             {
1210                 if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
1211                 {
1212                     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));
1213                     return(-1);
1214                 }
1215             }
1216         }
1217         else
1218         {
1219             for (i=0; i<n; i++)
1220                 printf("%.8f ",dstr(block.vtx[0].vout[i].nValue));
1221             printf("no opreturn entries to check ht.%d %s\n",height,ASSETCHAINS_SYMBOL);
1222             if ( ASSETCHAINS_SYMBOL[0] != 0 || height >= activation )
1223                 return(-1);
1224         }
1225         //printf("opretlen.%d num.%d\n",opretlen,num);
1226     }
1227     else
1228     {
1229         for (i=0; i<opretlen&&i<100; i++)
1230             printf("%02x",script[i]);
1231         printf(" height.%d checkdeposit n.%d [%02x] [%c] %d opcode.%d len.%d ",height,n,script[0],script[offset],script[offset],opcode,opretlen);
1232         printf("not proper vout with opreturn format %s ht.%d cmp.%d %d\n",ASSETCHAINS_SYMBOL,height,script[offset] == opcode,(int32_t)block.vtx[0].vout[n-1].scriptPubKey.size());
1233         return(-1);
1234     }
1235     return(0);*/
1236 }
1237
1238 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
1239 {
1240     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; 
1241     const char *typestr = "unknown";
1242     if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 && opretbuf[0] != 'K' )
1243     {
1244         //printf("komodo_opreturn skip %s\n",ASSETCHAINS_SYMBOL);
1245         return("assetchain");
1246     }
1247     memset(baseids,0xff,sizeof(baseids));
1248     memset(values,0,sizeof(values));
1249     memset(srcvalues,0,sizeof(srcvalues));
1250     memset(rmd160s,0,sizeof(rmd160s));
1251     memset(kmdheights,0,sizeof(kmdheights));
1252     memset(otherheights,0,sizeof(otherheights));
1253     tokomodo = (komodo_is_issuer() == 0);
1254     if ( opretbuf[0] == 'K' && opretlen != 40 )
1255     {
1256         komodo_kvupdate(opretbuf,opretlen,value);
1257         return("kv");
1258     }
1259     else if ( ASSETCHAINS_SYMBOL[0] == 0 && KOMODO_PAX == 0 )
1260         return("nopax");
1261     if ( opretbuf[0] == 'D' )
1262     {
1263         tokomodo = 0;
1264         if ( opretlen == 38 ) // any KMD tx
1265         {
1266             iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
1267             memset(base,0,sizeof(base));
1268             PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
1269             bitcoin_address(coinaddr,addrtype,rmd160,20);
1270             checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
1271             if ( komodo_paxcmp(base,kmdheight,value,checktoshis,kmdheight < 225000 ? seed : 0) != 0 )
1272                 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,height,base,fiatoshis);
1273             typestr = "deposit";
1274             if ( 0 && strcmp("NOK",base) == 0 )
1275             {
1276                 printf("[%s] %s paxdeposit height.%d vs kmdheight.%d\n",ASSETCHAINS_SYMBOL,base,height,kmdheight);
1277                 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);
1278                 for (i=0; i<32; i++)
1279                     printf("%02x",((uint8_t *)&txid)[i]);
1280                 printf(" <- txid.v%u ",vout);
1281                 for (i=0; i<33; i++)
1282                     printf("%02x",pubkey33[i]);
1283                 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
1284             }
1285             if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 && (kmdheight > 195000 || kmdheight <= height) )
1286             {
1287                 didstats = 0;
1288                 if ( komodo_paxcmp(base,kmdheight,value,checktoshis,kmdheight < 225000 ? seed : 0) == 0 )
1289                 {
1290                     if ( (pax= komodo_paxfind(txid,vout,'D')) == 0 )
1291                     {
1292                         if ( (basesp= komodo_stateptrget(base)) != 0 )
1293                         {
1294                             basesp->deposited += fiatoshis;
1295                             didstats = 1;
1296                             if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
1297                                 printf("########### %p deposited %s += %.8f kmdheight.%d %.8f\n",basesp,base,dstr(fiatoshis),kmdheight,dstr(value));
1298                         } else printf("cant get stateptr.(%s)\n",base);
1299                         komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0);
1300                     }
1301                     if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 )
1302                     {
1303                         pax->height = kmdheight;
1304                         pax->validated = value;
1305                         pax->komodoshis = value;
1306                         pax->fiatoshis = fiatoshis;
1307                         if ( didstats == 0 && pax->didstats == 0 )
1308                         {
1309                             if ( (basesp= komodo_stateptrget(base)) != 0 )
1310                             {
1311                                 basesp->deposited += fiatoshis;
1312                                 didstats = 1;
1313                                 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
1314                                     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));
1315                             }
1316                         } //
1317                         if ( didstats != 0 )
1318                             pax->didstats = 1;
1319                         if ( (pax2= komodo_paxfind(txid,vout,'I')) != 0 )
1320                         {
1321                             pax2->fiatoshis = pax->fiatoshis;
1322                             pax2->komodoshis = pax->komodoshis;
1323                             pax->marked = pax2->marked = pax->height;
1324                             pax2->height = pax->height = height;
1325                             if ( pax2->didstats == 0 )
1326                             {
1327                                 if ( (basesp= komodo_stateptrget(base)) != 0 )
1328                                 {
1329                                     basesp->issued += pax2->fiatoshis;
1330                                     pax2->didstats = 1;
1331                                     if ( 0 && strcmp(base,"USD") == 0 )
1332                                         printf("########### %p issueda %s += %.8f kmdheight.%d %.8f other.%d [%d]\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->komodoshis),pax2->otherheight,height);
1333                                 }
1334                             }
1335                         }
1336                     }
1337                 }
1338                 else
1339                 {
1340                     if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 )
1341                         pax->marked = checktoshis;
1342                     if ( kmdheight > 238000 && (kmdheight > 214700 || strcmp(base,ASSETCHAINS_SYMBOL) == 0) ) //seed != 0 &&
1343                         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);
1344                 }
1345             } //else printf("[%s] %s paxdeposit height.%d vs kmdheight.%d\n",ASSETCHAINS_SYMBOL,base,height,kmdheight);
1346         } //else printf("unsupported size.%d for opreturn D\n",opretlen);
1347     }
1348     else if ( opretbuf[0] == 'I' )
1349     {
1350         tokomodo = 0;
1351         if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 && strncmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4],3) == 0 )
1352         {
1353             if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
1354             {
1355                 for (i=0; i<n; i++)
1356                 {
1357                     if ( baseids[i] < 0 )
1358                     {
1359                         static uint32_t counter;
1360                         if ( counter++ < 0 )
1361                             printf("%d of %d illegal baseid.%d, this can be ignored\n",i,n,baseids[i]);
1362                         continue;
1363                     }
1364                     bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
1365                     komodo_gateway_deposit(coinaddr,0,ASSETCHAINS_SYMBOL,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0);
1366                     komodo_paxmark(height,txids[i],vouts[i],'I',height);
1367                     if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 )
1368                     {
1369                         pax->type = opretbuf[0];
1370                         strcpy(pax->source,(char *)&opretbuf[opretlen-4]);
1371                         if ( (pax2= komodo_paxfind(txids[i],vouts[i],'D')) != 0 && pax2->fiatoshis != 0 && pax2->komodoshis != 0 )
1372                         {
1373                             // realtime path?
1374                             pax->fiatoshis = pax2->fiatoshis;
1375                             pax->komodoshis = pax2->komodoshis;
1376                             pax->marked = pax2->marked = pax2->height;
1377                             if ( pax->didstats == 0 )
1378                             {
1379                                 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
1380                                 {
1381                                     basesp->issued += pax->fiatoshis;
1382                                     pax->didstats = 1;
1383                                     pax->height = pax2->height;
1384                                     pax->otherheight = height;
1385                                     if ( 1 && strcmp(CURRENCIES[baseids[i]],"USD") == 0 )
1386                                         printf("########### %p issuedb %s += %.8f kmdheight.%d %.8f other.%d [%d]\n",basesp,CURRENCIES[baseids[i]],dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight,height);
1387                                 }
1388                             }
1389                         }
1390                     }
1391                     if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'I',height)) != 0 )
1392                         komodo_paxdelete(pax);
1393                     if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'D',height)) != 0 )
1394                         komodo_paxdelete(pax);
1395                 }
1396             } //else printf("opreturn none issued?\n");
1397         }
1398     }
1399     else if ( height < 236000 && opretbuf[0] == 'W' && strncmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4],3) == 0 )//&& opretlen >= 38 )
1400     {
1401         if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 && strcmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]) == 0 )
1402         {
1403             for (i=0; i<opretlen; i++)
1404                 printf("%02x",opretbuf[i]);
1405             printf(" [%s] reject obsolete withdraw request.%s\n",ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]);
1406             return(typestr);
1407         }
1408         tokomodo = 1;
1409         iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
1410         memset(base,0,sizeof(base));
1411         PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
1412         bitcoin_address(coinaddr,addrtype,rmd160,20);
1413         checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
1414         typestr = "withdraw";
1415         //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);
1416         didstats = 0;
1417         //if ( komodo_paxcmp(base,kmdheight,komodoshis,checktoshis,seed) == 0 )
1418         {
1419             if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) )
1420             {
1421                 if ( (basesp= komodo_stateptrget(base)) != 0 )
1422                 {
1423                     basesp->withdrawn += value;
1424                     didstats = 1;
1425                     if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
1426                         printf("########### %p withdrawn %s += %.8f check %.8f\n",basesp,base,dstr(value),dstr(checktoshis));
1427                 }
1428                 if ( 0 && strcmp(base,"RUB") == 0 && (pax == 0 || pax->approved == 0) )
1429                     printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
1430             }
1431             komodo_gateway_deposit(coinaddr,0,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0);
1432             if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 )
1433             {
1434                 pax->type = opretbuf[0];
1435                 strcpy(pax->source,base);
1436                 strcpy(pax->symbol,"KMD");
1437                 pax->height = kmdheight;
1438                 pax->otherheight = height;
1439                 pax->komodoshis = komodoshis;
1440             }
1441         } // else printf("withdraw %s paxcmp ht.%d %d error value %.8f -> %.8f vs %.8f\n",base,kmdheight,height,dstr(value),dstr(komodoshis),dstr(checktoshis));
1442         // need to allocate pax
1443     }
1444     else if ( height < 236000 && tokomodo != 0 && opretbuf[0] == 'A' && ASSETCHAINS_SYMBOL[0] == 0 )
1445     {
1446         tokomodo = 1;
1447         if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
1448         {
1449             for (i=0; i<opretlen; i++)
1450                 printf("%02x",opretbuf[i]);
1451             printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
1452         }
1453         if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
1454         {
1455             for (i=0; i<n; i++)
1456             {
1457                 //for (j=0; j<32; j++)
1458                 //    printf("%02x",((uint8_t *)&txids[i])[j]);
1459                 //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]);
1460                 if ( baseids[i] < 0 )
1461                 {
1462                     for (i=0; i<opretlen; i++)
1463                         printf("%02x",opretbuf[i]);
1464                     printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
1465                     //printf("baseids[%d] %d\n",i,baseids[i]);
1466                     if ( (pax= komodo_paxfind(txids[i],vouts[i],'W')) != 0 || (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
1467                     {
1468                         baseids[i] = komodo_baseid(pax->symbol);
1469                         printf("override neg1 with (%s)\n",pax->symbol);
1470                     }
1471                     if ( baseids[i] < 0 )
1472                         continue;
1473                 }
1474                 didstats = 0;
1475                 seed = 0;
1476                 checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)values[i]);
1477                 //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);
1478                 if ( srcvalues[i] == checktoshis )
1479                 {
1480                     if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
1481                     {
1482                         bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
1483                         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]);
1484                         if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
1485                             printf("unexpected null pax for approve\n");
1486                         else pax->validated = checktoshis;
1487                         if ( (pax2= komodo_paxfind(txids[i],vouts[i],'W')) != 0 )
1488                             pax2->approved = kmdheights[i];
1489                         komodo_paxmark(height,txids[i],vouts[i],'W',height);
1490                         //komodo_paxmark(height,txids[i],vouts[i],'A',height);
1491                         if ( values[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
1492                         {
1493                             basesp->approved += values[i];
1494                             didstats = 1;
1495                             //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]);
1496                         }
1497                         //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i]));
1498                     }
1499                     else if ( pax->didstats == 0 && srcvalues[i] != 0 )
1500                     {
1501                         if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
1502                         {
1503                             basesp->approved += values[i];
1504                             didstats = 1;
1505                             //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]);
1506                         }
1507                     } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
1508                     if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 )
1509                     {
1510                         pax->type = opretbuf[0];
1511                         pax->approved = kmdheights[i];
1512                         pax->validated = checktoshis;
1513                         if ( didstats != 0 )
1514                             pax->didstats = 1;
1515                         //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
1516                         //printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax);
1517                     }
1518                 }
1519             }
1520         } //else printf("n.%d from opreturns\n",n);
1521         //printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
1522     }
1523     else if ( height < 236000 && opretbuf[0] == 'X' && ASSETCHAINS_SYMBOL[0] == 0 )
1524     {
1525         tokomodo = 1;
1526         if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
1527         {
1528             for (i=0; i<n; i++)
1529             {
1530                 if ( baseids[i] < 0 )
1531                     continue;
1532                 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
1533                 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0);
1534                 komodo_paxmark(height,txids[i],vouts[i],'W',height);
1535                 komodo_paxmark(height,txids[i],vouts[i],'A',height);
1536                 komodo_paxmark(height,txids[i],vouts[i],'X',height);
1537                 if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
1538                 {
1539                     pax->type = opretbuf[0];
1540                     if ( height < 121842 ) // fields got switched around due to legacy issues and approves
1541                         value = srcvalues[i];
1542                     else value = values[i];
1543                     if ( baseids[i] >= 0 && value != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
1544                     {
1545                         basesp->redeemed += value;
1546                         pax->didstats = 1;
1547                         if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
1548                             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]);
1549                     }
1550                 }
1551                 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'W',height)) != 0 )
1552                     komodo_paxdelete(pax);
1553                 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'A',height)) != 0 )
1554                     komodo_paxdelete(pax);
1555                 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'X',height)) != 0 )
1556                     komodo_paxdelete(pax);
1557             }
1558         } //else printf("komodo_issued_opreturn returned %d\n",n);
1559     }
1560     return(typestr);
1561 }
1562
1563 int32_t komodo_parsestatefiledata(struct komodo_state *sp,uint8_t *filedata,long *fposp,long datalen,char *symbol,char *dest);
1564
1565 void komodo_stateind_set(struct komodo_state *sp,uint32_t *inds,int32_t n,uint8_t *filedata,long datalen,char *symbol,char *dest)
1566 {
1567     uint8_t func; long lastK,lastT,lastN,lastV,fpos,lastfpos; int32_t i,count,doissue,iter,numn,numv,numN,numV,numR; uint32_t tmp,prevpos100,offset;
1568     count = numR = numN = numV = numn = numv = 0;
1569     lastK = lastT = lastN = lastV = -1;
1570     for (iter=0; iter<2; iter++)
1571     {
1572         for (lastfpos=fpos=prevpos100=i=0; i<n; i++)
1573         {
1574             tmp = inds[i];
1575             if ( (i % 100) == 0 )
1576                 prevpos100 = tmp;
1577             else
1578             {
1579                 func = (tmp & 0xff);
1580                 offset = (tmp >> 8);
1581                 fpos = prevpos100 + offset;
1582                 if ( lastfpos >= datalen || (filedata[lastfpos] != func && func != 0) )
1583                     printf("i.%d/n.%d lastfpos.%ld >= datalen.%ld or [%d] != func.%d\n",i,n,lastfpos,datalen,filedata[lastfpos],func);
1584                 else if ( iter == 0 )
1585                 {
1586                     switch ( func )
1587                     {
1588                         default: case 'P': case 'U': case 'D':
1589                             inds[i] &= 0xffffff00;
1590                             break;
1591                         case 'K':
1592                             lastK = lastfpos;
1593                             inds[i] &= 0xffffff00;
1594                             break;
1595                         case 'T':
1596                             lastT = lastfpos;
1597                             inds[i] &= 0xffffff00;
1598                             break;
1599                         case 'N':
1600                             lastN = lastfpos;
1601                             numN++;
1602                             break;
1603                         case 'V':
1604                             lastV = lastfpos;
1605                             numV++;
1606                             break;
1607                         case 'R':
1608                             numR++;
1609                             break;
1610                     }
1611                 }
1612                 else
1613                 {
1614                     doissue = 0;
1615                     if ( func == 'K' )
1616                     {
1617                         if ( lastK == lastfpos )
1618                             doissue = 1;
1619                     }
1620                     else if ( func == 'T' )
1621                     {
1622                         if ( lastT == lastfpos )
1623                             doissue = 1;
1624                     }
1625                     else if ( func == 'N' )
1626                     {
1627                         if ( numn > numN-128 )
1628                             doissue = 1;
1629                         numn++;
1630                     }
1631                     else if ( func == 'V' )
1632                     {
1633                         if ( KOMODO_PAX != 0 && numv > numV-1440 )
1634                             doissue = 1;
1635                         numv++;
1636                     }
1637                     else if ( func == 'R' )
1638                         doissue = 1;
1639                     if ( doissue != 0 )
1640                     {
1641                         //printf("issue %c total.%d lastfpos.%ld\n",func,count,lastfpos);
1642                         komodo_parsestatefiledata(sp,filedata,&lastfpos,datalen,symbol,dest);
1643                         count++;
1644                     }
1645                 }
1646             }
1647             lastfpos = fpos;
1648         }
1649     }
1650     printf("numR.%d numV.%d numN.%d count.%d\n",numR,numV,numN,count);
1651     /*else if ( func == 'K' ) // KMD height: stop after 1st
1652     else if ( func == 'T' ) // KMD height+timestamp: stop after 1st
1653         
1654     else if ( func == 'N' ) // notarization, scan backwards 1440+ blocks;
1655     else if ( func == 'V' ) // price feed: can stop after 1440+
1656     else if ( func == 'R' ) // opreturn:*/
1657 }
1658
1659 void *OS_loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep)
1660 {
1661     FILE *fp;
1662     long  filesize,buflen = *allocsizep;
1663     uint8_t *buf = *bufp;
1664     *lenp = 0;
1665     if ( (fp= fopen(fname,"rb")) != 0 )
1666     {
1667         fseek(fp,0,SEEK_END);
1668         filesize = ftell(fp);
1669         if ( filesize == 0 )
1670         {
1671             fclose(fp);
1672             *lenp = 0;
1673             printf("OS_loadfile null size.(%s)\n",fname);
1674             return(0);
1675         }
1676         if ( filesize > buflen )
1677         {
1678             *allocsizep = filesize;
1679             *bufp = buf = (uint8_t *)realloc(buf,(long)*allocsizep+64);
1680         }
1681         rewind(fp);
1682         if ( buf == 0 )
1683             printf("Null buf ???\n");
1684         else
1685         {
1686             if ( fread(buf,1,(long)filesize,fp) != (unsigned long)filesize )
1687                 printf("error reading filesize.%ld\n",(long)filesize);
1688             buf[filesize] = 0;
1689         }
1690         fclose(fp);
1691         *lenp = filesize;
1692         //printf("loaded.(%s)\n",buf);
1693     } //else printf("OS_loadfile couldnt load.(%s)\n",fname);
1694     return(buf);
1695 }
1696
1697 uint8_t *OS_fileptr(long *allocsizep,char *fname)
1698 {
1699     long filesize = 0; uint8_t *buf = 0; void *retptr;
1700     *allocsizep = 0;
1701     retptr = OS_loadfile(fname,&buf,&filesize,allocsizep);
1702     return((uint8_t *)retptr);
1703 }
1704
1705 long komodo_stateind_validate(struct komodo_state *sp,char *indfname,uint8_t *filedata,long datalen,uint32_t *prevpos100p,uint32_t *indcounterp,char *symbol,char *dest)
1706 {
1707     FILE *fp; long fsize,lastfpos=0,fpos=0; uint8_t *inds,func; int32_t i,n; uint32_t offset,tmp,prevpos100 = 0;
1708     *indcounterp = *prevpos100p = 0;
1709     if ( (inds= OS_fileptr(&fsize,indfname)) != 0 )
1710     {
1711         lastfpos = 0;
1712         fprintf(stderr,"inds.%p validate %s fsize.%ld datalen.%ld n.%ld lastfpos.%ld\n",inds,indfname,fsize,datalen,fsize / sizeof(uint32_t),lastfpos);
1713         if ( (fsize % sizeof(uint32_t)) == 0 )
1714         {
1715             n = (int32_t)(fsize / sizeof(uint32_t));
1716             for (i=0; i<n; i++)
1717             {
1718                 memcpy(&tmp,&inds[i * sizeof(uint32_t)],sizeof(uint32_t));
1719                 if ( 0 && i > n-10 )
1720                     printf("%d: tmp.%08x [%c] prevpos100.%u\n",i,tmp,tmp&0xff,prevpos100);
1721                 if ( (i % 100) == 0 )
1722                     prevpos100 = tmp;
1723                 else
1724                 {
1725                     func = (tmp & 0xff);
1726                     offset = (tmp >> 8);
1727                     fpos = prevpos100 + offset;
1728                     if ( lastfpos >= datalen || filedata[lastfpos] != func )
1729                     {
1730                         printf("validate.%d error (%u %d) prev100 %u -> fpos.%ld datalen.%ld [%d] (%c) vs (%c) lastfpos.%ld\n",i,offset,func,prevpos100,fpos,datalen,lastfpos < datalen ? filedata[lastfpos] : -1,func,filedata[lastfpos],lastfpos);
1731                         return(-1);
1732                     }
1733                 }
1734                 lastfpos = fpos;
1735             }
1736             *indcounterp = n;
1737             *prevpos100p = prevpos100;
1738             if ( sp != 0 )
1739                 komodo_stateind_set(sp,(uint32_t *)inds,n,filedata,fpos,symbol,dest);
1740             //printf("free inds.%p %s validated[%d] fpos.%ld datalen.%ld, offset %ld vs fsize.%ld\n",inds,indfname,i,fpos,datalen,i * sizeof(uint32_t),fsize);
1741             free(inds);
1742             return(fpos);
1743         } else printf("wrong filesize %s %ld\n",indfname,fsize);
1744     }
1745     free(inds);
1746     fprintf(stderr,"indvalidate return -1\n");
1747     return(-1);
1748 }
1749
1750 long komodo_indfile_update(FILE *indfp,uint32_t *prevpos100p,long lastfpos,long newfpos,uint8_t func,uint32_t *indcounterp)
1751 {
1752     uint32_t tmp;
1753     if ( indfp != 0 )
1754     {
1755         tmp = ((uint32_t)(newfpos - *prevpos100p) << 8) | (func & 0xff);
1756         if ( ftell(indfp)/sizeof(uint32_t) != *indcounterp )
1757             printf("indfp fpos %ld -> ind.%ld vs counter.%u\n",ftell(indfp),ftell(indfp)/sizeof(uint32_t),*indcounterp);
1758         //fprintf(stderr,"ftell.%ld indcounter.%u lastfpos.%ld newfpos.%ld func.%02x\n",ftell(indfp),*indcounterp,lastfpos,newfpos,func);
1759         fwrite(&tmp,1,sizeof(tmp),indfp), (*indcounterp)++;
1760         if ( (*indcounterp % 100) == 0 )
1761         {
1762             *prevpos100p = (uint32_t)newfpos;
1763             fwrite(prevpos100p,1,sizeof(*prevpos100p),indfp), (*indcounterp)++;
1764         }
1765     }
1766     return(newfpos);
1767 }
1768
1769 int32_t komodo_faststateinit(struct komodo_state *sp,char *fname,char *symbol,char *dest)
1770 {
1771     FILE *indfp; char indfname[1024]; uint8_t *filedata; long validated=-1,datalen,fpos,lastfpos; uint32_t tmp,prevpos100,indcounter,starttime; int32_t func,finished = 0;
1772     starttime = (uint32_t)time(NULL);
1773     safecopy(indfname,fname,sizeof(indfname)-4);
1774     strcat(indfname,".ind");
1775     if ( (filedata= OS_fileptr(&datalen,fname)) != 0 )
1776     {
1777         if ( 1 )//datalen >= (1LL << 32) || GetArg("-genind",0) != 0 || (validated= komodo_stateind_validate(0,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest)) < 0 )
1778         {
1779             lastfpos = fpos = 0;
1780             indcounter = prevpos100 = 0;
1781             if ( (indfp= fopen(indfname,"wb")) != 0 )
1782                 fwrite(&prevpos100,1,sizeof(prevpos100),indfp), indcounter++;
1783             fprintf(stderr,"processing %s %ldKB, validated.%ld\n",fname,datalen/1024,validated);
1784             while ( (func= komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest)) >= 0 )
1785             {
1786                 lastfpos = komodo_indfile_update(indfp,&prevpos100,lastfpos,fpos,func,&indcounter);
1787             }
1788             if ( indfp != 0 )
1789             {
1790                 fclose(indfp);
1791                 if ( (fpos= komodo_stateind_validate(0,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest)) < 0 )
1792                     printf("unexpected komodostate.ind validate failure %s datalen.%ld\n",indfname,datalen);
1793                 else printf("%s validated fpos.%ld\n",indfname,fpos);
1794             }
1795             finished = 1;
1796             fprintf(stderr,"took %d seconds to process %s %ldKB\n",(int32_t)(time(NULL)-starttime),fname,datalen/1024);
1797         }
1798         else if ( validated > 0 )
1799         {
1800             if ( (indfp= fopen(indfname,"rb+")) != 0 )
1801             {
1802                 lastfpos = fpos = validated;
1803                 fprintf(stderr,"datalen.%ld validated %ld -> indcounter %u, prevpos100 %u offset.%ld\n",datalen,validated,indcounter,prevpos100,indcounter * sizeof(uint32_t));
1804                 if ( fpos < datalen )
1805                 {
1806                     fseek(indfp,indcounter * sizeof(uint32_t),SEEK_SET);
1807                     if ( ftell(indfp) == indcounter * sizeof(uint32_t) )
1808                     {
1809                         while ( (func= komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest)) >= 0 )
1810                         {
1811                             lastfpos = komodo_indfile_update(indfp,&prevpos100,lastfpos,fpos,func,&indcounter);
1812                             if ( lastfpos != fpos )
1813                                 fprintf(stderr,"unexpected lastfpos.%ld != %ld\n",lastfpos,fpos);
1814                         }
1815                     }
1816                     fclose(indfp);
1817                 }
1818                 if ( komodo_stateind_validate(sp,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest) < 0 )
1819                     printf("unexpected komodostate.ind validate failure %s datalen.%ld\n",indfname,datalen);
1820                 else
1821                 {
1822                     printf("%s validated updated from validated.%ld to %ld new.[%ld] -> indcounter %u, prevpos100 %u offset.%ld | elapsed %d seconds\n",indfname,validated,fpos,fpos-validated,indcounter,prevpos100,indcounter * sizeof(uint32_t),(int32_t)(time(NULL) - starttime));
1823                     finished = 1;
1824                 }
1825             }
1826         } else printf("komodo_faststateinit unexpected case\n");
1827         free(filedata);
1828         return(finished == 1);
1829     }
1830     return(-1);
1831 }
1832
1833 void komodo_passport_iteration()
1834 {
1835     static long lastpos[34]; static char userpass[33][1024]; static uint32_t lasttime,callcounter;
1836     int32_t maxseconds = 10;
1837     FILE *fp; uint8_t *filedata; long fpos,datalen,lastfpos; int32_t baseid,limit,n,ht,isrealtime,expired,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; uint32_t buf[3],starttime; cJSON *infoobj,*result; uint64_t RTmask = 0;
1838     expired = 0;
1839     while ( KOMODO_INITDONE == 0 )
1840     {
1841         fprintf(stderr,"[%s] PASSPORT iteration waiting for KOMODO_INITDONE\n",ASSETCHAINS_SYMBOL);
1842         sleep(3);
1843     }
1844     refsp = komodo_stateptr(symbol,dest);
1845     if ( ASSETCHAINS_SYMBOL[0] == 0 )
1846     {
1847         refid = 33;
1848         limit = 10000000;
1849         jumblr_iteration();
1850     }
1851     else
1852     {
1853         limit = 10000000;
1854         refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
1855         if ( refid == 0 )
1856         {
1857             KOMODO_PASSPORT_INITDONE = 1;
1858             return;
1859         }
1860     }
1861     /*if ( KOMODO_PAX == 0 )
1862     {
1863         KOMODO_PASSPORT_INITDONE = 1;
1864         return;
1865     }*/
1866     starttime = (uint32_t)time(NULL);
1867     if ( callcounter++ < 1 )
1868         limit = 10000;
1869     lasttime = starttime;
1870     for (baseid=32; baseid>=0; baseid--)
1871     {
1872         if ( time(NULL) >= starttime+maxseconds )
1873             break;
1874         sp = 0;
1875         isrealtime = 0;
1876         base = (char *)CURRENCIES[baseid];
1877         //printf("PASSPORT %s baseid+1 %d refid.%d\n",ASSETCHAINS_SYMBOL,baseid+1,refid);
1878         if ( baseid+1 != refid ) // only need to import state from a different coin
1879         {
1880             if ( baseid == 32 ) // only care about KMD's state
1881             {
1882                 refsp->RTmask &= ~(1LL << baseid);
1883                 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
1884                 komodo_nameset(symbol,dest,base);
1885                 sp = komodo_stateptrget(symbol);
1886                 n = 0;
1887                 if ( lastpos[baseid] == 0 && (filedata= OS_fileptr(&datalen,fname)) != 0 )
1888                 {
1889                     fpos = 0;
1890                     fprintf(stderr,"%s processing %s %ldKB\n",ASSETCHAINS_SYMBOL,fname,datalen/1024);
1891                     while ( komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest) >= 0 )
1892                         lastfpos = fpos;
1893                     fprintf(stderr,"%s took %d seconds to process %s %ldKB\n",ASSETCHAINS_SYMBOL,(int32_t)(time(NULL)-starttime),fname,datalen/1024);
1894                     lastpos[baseid] = lastfpos;
1895                     free(filedata), filedata = 0;
1896                     datalen = 0;
1897                 }
1898                 else if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 )
1899                 {
1900                     fseek(fp,0,SEEK_END);
1901                     //fprintf(stderr,"couldnt OS_fileptr(%s), freading %ldKB\n",fname,ftell(fp)/1024);
1902                     if ( ftell(fp) > lastpos[baseid] )
1903                     {
1904                         if ( ASSETCHAINS_SYMBOL[0] != 0 )
1905                             printf("%s passport refid.%d %s fname.(%s) base.%s %ld %ld\n",ASSETCHAINS_SYMBOL,refid,symbol,fname,base,ftell(fp),lastpos[baseid]);
1906                         fseek(fp,lastpos[baseid],SEEK_SET);
1907                         while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 && n < limit )
1908                         {
1909                             if ( n == limit-1 )
1910                             {
1911                                 if ( time(NULL) < starttime+maxseconds )
1912                                     n = 0;
1913                                 else
1914                                 {
1915                                     //printf("expire passport loop %s -> %s at %ld\n",ASSETCHAINS_SYMBOL,base,lastpos[baseid]);
1916                                     expired++;
1917                                 }
1918                             }
1919                             n++;
1920                         }
1921                         lastpos[baseid] = ftell(fp);
1922                         if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
1923                             printf("from.(%s) lastpos[%s] %ld isrt.%d\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid],komodo_isrealtime(&ht));
1924                     } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
1925                     fclose(fp);
1926                 } else fprintf(stderr,"load error.(%s) %p\n",fname,sp);
1927                 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1928                 if ( (fp= fopen(fname,"rb")) != 0 )
1929                 {
1930                     if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
1931                     {
1932                         sp->CURRENT_HEIGHT = buf[0];
1933                         if ( buf[0] != 0 && buf[0] >= buf[1] && buf[2] > time(NULL)-60 )
1934                         {
1935                             isrealtime = 1;
1936                             RTmask |= (1LL << baseid);
1937                             memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1938                         }
1939                         else if ( KOMODO_PAX != 0 && (time(NULL)-buf[2]) > 60 && ASSETCHAINS_SYMBOL[0] != 0 )
1940                             fprintf(stderr,"[%s]: %s not RT %u %u %d\n",ASSETCHAINS_SYMBOL,base,buf[0],buf[1],(int32_t)(time(NULL)-buf[2]));
1941                     } //else fprintf(stderr,"%s size error RT\n",base);
1942                     fclose(fp);
1943                 } //else fprintf(stderr,"%s open error RT\n",base);
1944             }
1945         }
1946         else
1947         {
1948             refsp->RTmask &= ~(1LL << baseid);
1949             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1950             if ( (fp= fopen(fname,"wb")) != 0 )
1951             {
1952                 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
1953                 buf[1] = (uint32_t)komodo_longestchain();
1954                 if ( buf[0] != 0 && buf[0] == buf[1] )
1955                 {
1956                     buf[2] = (uint32_t)time(NULL);
1957                     RTmask |= (1LL << baseid);
1958                     memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1959                     if ( refid != 0 )
1960                         memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
1961                 }
1962                 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
1963                     fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
1964                 fclose(fp);
1965             } else fprintf(stderr,"%s create error RT\n",base);
1966         }
1967         if ( sp != 0 && isrealtime == 0 )
1968             refsp->RTbufs[0][2] = 0;
1969     }
1970     komodo_paxtotal();
1971     refsp->RTmask |= RTmask;
1972     if ( expired == 0 && KOMODO_PASSPORT_INITDONE == 0 )
1973     {
1974         KOMODO_PASSPORT_INITDONE = 1;
1975         printf("done PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
1976     }
1977 }
1978
This page took 0.139085 seconds and 4 git commands to generate.