]> Git Repo - VerusCoin.git/blob - src/komodo_gateway.h
Test
[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
18 int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base)
19 {
20     int32_t baseid; struct komodo_state *sp; int64_t netliability,maxallowed;
21     *available = *deposited = *issued = *withdrawn = *approved = *redeemed = 0;
22     if ( (baseid= komodo_baseid(base)) >= 0 )
23     {
24         if ( (sp= komodo_stateptrget(base)) != 0 )
25         {
26             *deposited = sp->deposited;
27             *issued = sp->issued;
28             *withdrawn = sp->withdrawn;
29             *approved = sp->approved;
30             *redeemed = sp->redeemed;
31             netliability = (sp->deposited - sp->withdrawn) - sp->shorted;
32             maxallowed = komodo_maxallowed(baseid);
33             if ( netliability < maxallowed )
34                 *available = (maxallowed - netliability);
35             //printf("%p %s %.8f %.8f %.8f %.8f %.8f\n",sp,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed));
36             return(0);
37         } else printf("pax_fiatstatus cant get basesp.%s\n",base);
38     } // else printf("pax_fiatstatus illegal base.%s\n",base);
39     return(-1);
40 }
41
42 void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type)
43 {
44     memcpy(buf,&txid,32);
45     memcpy(&buf[32],&vout,2);
46     buf[34] = type;
47 }
48
49 struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type)
50 {
51     struct pax_transaction *pax; uint8_t buf[35];
52     pthread_mutex_lock(&komodo_mutex);
53     pax_keyset(buf,txid,vout,type);
54     HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
55     pthread_mutex_unlock(&komodo_mutex);
56     return(pax);
57 }
58
59 struct pax_transaction *komodo_paxfinds(uint256 txid,uint16_t vout)
60 {
61     struct pax_transaction *pax; int32_t i; uint8_t types[] = { 'I', 'D', 'X', 'A', 'W' };
62     for (i=0; i<sizeof(types)/sizeof(*types); i++)
63         if ( (pax= komodo_paxfind(txid,vout,types[i])) != 0 )
64             return(pax);
65     return(0);
66 }
67
68 struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark)
69 {
70     struct pax_transaction *pax; uint8_t buf[35];
71     pthread_mutex_lock(&komodo_mutex);
72     pax_keyset(buf,txid,vout,type);
73     HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
74     if ( pax == 0 )
75     {
76         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
77         pax->txid = txid;
78         pax->vout = vout;
79         pax->type = type;
80         memcpy(pax->buf,buf,sizeof(pax->buf));
81         HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
82         //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
83     }
84     if ( pax != 0 )
85     {
86         pax->marked = mark;
87         if ( height > 214700 || pax->height > 214700 )
88             printf("mark ht.%d %.8f %.8f\n",pax->height,dstr(pax->komodoshis),dstr(pax->fiatoshis));
89         
90     }
91     pthread_mutex_unlock(&komodo_mutex);
92     return(pax);
93 }
94
95 void komodo_paxdelete(struct pax_transaction *pax)
96 {
97     return; // breaks when out of order
98     pthread_mutex_lock(&komodo_mutex);
99     HASH_DELETE(hh,PAX,pax);
100     pthread_mutex_unlock(&komodo_mutex);
101 }
102
103 void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,uint8_t type,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context
104 {
105     struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s;
106     if ( KOMODO_PAX == 0 )
107         return;
108     sp = komodo_stateptr(str,dest);
109     pthread_mutex_lock(&komodo_mutex);
110     pax_keyset(buf,txid,vout,type);
111     HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
112     if ( pax == 0 )
113     {
114         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
115         pax->txid = txid;
116         pax->vout = vout;
117         pax->type = type;
118         memcpy(pax->buf,buf,sizeof(pax->buf));
119         HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
120         addflag = 1;
121         if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 )
122         {
123             int32_t i; for (i=0; i<32; i++)
124                 printf("%02x",((uint8_t *)&txid)[i]);
125             printf(" v.%d [%s] kht.%d ht.%d create pax.%p symbol.%s source.%s\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax,symbol,source);
126         }
127     }
128     pthread_mutex_unlock(&komodo_mutex);
129     if ( coinaddr != 0 )
130     {
131         strcpy(pax->coinaddr,coinaddr);
132         if ( value != 0 )
133             pax->komodoshis = value;
134         if ( symbol != 0 )
135             strcpy(pax->symbol,symbol);
136         if ( source != 0 )
137             strcpy(pax->source,source);
138         if ( fiatoshis != 0 )
139             pax->fiatoshis = fiatoshis;
140         if ( rmd160 != 0 )
141             memcpy(pax->rmd160,rmd160,20);
142         if ( height != 0 )
143             pax->height = height;
144         if ( otherheight != 0 )
145             pax->otherheight = otherheight;
146     }
147     else
148     {
149         pax->marked = height;
150         //printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
151     }
152 }
153
154 int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax)
155 {
156     int32_t i,len = 0;
157     if ( rwflag == 1 )
158     {
159         for (i=0; i<32; i++)
160             opretbuf[len++] = ((uint8_t *)&pax->txid)[i];
161         opretbuf[len++] = pax->vout & 0xff;
162         opretbuf[len++] = (pax->vout >> 8) & 0xff;
163     }
164     else
165     {
166         for (i=0; i<32; i++)
167             ((uint8_t *)&pax->txid)[i] = opretbuf[len++];
168         //for (i=0; i<32; i++)
169         //    printf("%02x",((uint8_t *)&pax->txid)[31-i]);
170         pax->vout = opretbuf[len++];
171         pax->vout += ((uint32_t)opretbuf[len++] << 8);
172         //printf(" txid v.%d\n",pax->vout);
173     }
174     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis);
175     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
176     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height);
177     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight);
178     if ( rwflag != 0 )
179     {
180         memcpy(&opretbuf[len],pax->rmd160,20), len += 20;
181         for (i=0; i<4; i++)
182             opretbuf[len++] = pax->source[i];
183     }
184     else
185     {
186         memcpy(pax->rmd160,&opretbuf[len],20), len += 20;
187         for (i=0; i<4; i++)
188             pax->source[i] = opretbuf[len++];
189     }
190     return(len);
191 }
192
193 int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int64_t *srcvalues,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
194 {
195     struct pax_transaction p,*pax; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t type,rmd160[20]; uint64_t fiatoshis; char symbol[16];
196     if ( KOMODO_PAX == 0 )
197         return(0);
198     incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
199     //41e77b91cb68dc2aa02fa88550eae6b6d44db676a7e935337b6d1392d9718f03cb0200305c90660400000000fbcbeb1f000000bde801006201000058e7945ad08ddba1eac9c9b6c8e1e97e8016a2d152
200     
201     // 41e94d736ec69d88c08b5d238abeeca609c02357a8317e0d56c328bcb1c259be5d0200485bc80200000000404b4c000000000059470200b80b000061f22ba7d19fe29ac3baebd839af8b7127d1f9075553440046bb4cc7a3b5cd39dffe7206507a3482a00780e617f68b273cce9817ed69298d02001069ca1b0000000080f0fa02000000005b470200b90b000061f22ba7d19fe29ac3baebd839af8b7127d1f90755
202     
203     //for (i=0; i<opretlen; i++)
204     //    printf("%02x",opretbuf[i]);
205     //printf(" opretlen.%d (%s)\n",opretlen,base);
206     //printf(" opretlen.%d vs %d incr.%d (%d)\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 2),incr,opretlen/incr);
207     //if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
208     {
209         type = opretbuf[0];
210         opretbuf++, opretlen--;
211         for (n=0; n<opretlen/incr; n++)
212         {
213             if ( iskomodo != 0 )
214             {
215                 memset(&p,0,sizeof(p));
216                 len += komodo_rwapproval(0,&opretbuf[len],&p);
217                 if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 )
218                 {
219                     txids[n] = p.txid;
220                     vouts[n] = p.vout;
221                     values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis;
222                     srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis;
223                     kmdheights[n] = p.height;
224                     otherheights[n] = p.otherheight;
225                     memcpy(&rmd160s[n * 20],p.rmd160,20);
226                     baseids[n] = komodo_baseid(p.source);
227                     if ( 0 )
228                     {
229                         char coinaddr[64];
230                         bitcoin_address(coinaddr,60,&rmd160s[n * 20],20);
231                         printf(">>>>>>> %s: (%s) fiat %.8f kmdheight.%d other.%d -> %s %.8f\n",type=='A'?"approvedA":"issuedX",baseids[n]>=0?CURRENCIES[baseids[n]]:"???",dstr(p.fiatoshis),kmdheights[n],otherheights[n],coinaddr,dstr(values[n]));
232                     }
233                 }
234             }
235             else
236             {
237                 for (i=0; i<4; i++)
238                     base[i] = opretbuf[opretlen-4+i];
239                 for (j=0; j<32; j++)
240                 {
241                     ((uint8_t *)&txids[n])[j] = opretbuf[len++];
242                     //printf("%02x",((uint8_t *)&txids[n])[j]);
243                 }
244                 vouts[n] = opretbuf[len++];
245                 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
246                 baseids[n] = komodo_baseid(base);
247                 if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 )
248                 {
249                     values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis;
250                     srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis;
251                     kmdheights[n] = pax->height;
252                     otherheights[n] = pax->otherheight;
253                     memcpy(&rmd160s[n * 20],pax->rmd160,20);
254                 }
255             }
256             //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
257         }
258     }
259     return(n);
260 }
261
262 int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t checkvalue,uint64_t seed)
263 {
264     int32_t ratio;
265     if ( seed == 0 && checkvalue != 0 )
266     {
267         ratio = ((value << 6) / checkvalue);
268         if ( ratio >= 63 && ratio <= 65 )
269             return(0);
270         else
271         {
272             if ( kmdheight >= 86150 )
273                 printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio);
274             return(-1);
275         }
276     }
277     else if ( checkvalue != 0 )
278     {
279         ratio = ((value << 10) / checkvalue);
280         if ( ratio >= 1023 && ratio <= 1025 )
281             return(0);
282     }
283     return(value != checkvalue);
284 }
285
286 uint64_t komodo_paxtotal()
287 {
288     struct pax_transaction *pax,*pax2,*tmp,*tmp2; char symbol[16],dest[16],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp;
289     if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
290         return(0);
291     if ( komodo_isrealtime(&ht) == 0 )
292         return(0);
293     else
294     {
295         HASH_ITER(hh,PAX,pax,tmp)
296         {
297             if ( pax->marked != 0 )
298                 continue;
299             if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' )
300                 str = pax->symbol;
301             else str = pax->source;
302             basesp = komodo_stateptrget(str);
303             if ( basesp != 0 && pax->didstats == 0 )
304             {
305                 if ( pax->type == 'I' && (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 )
306                 {
307                     if ( pax2->fiatoshis != 0 )
308                     {
309                         pax->komodoshis = pax2->komodoshis;
310                         pax->fiatoshis = pax2->fiatoshis;
311                         basesp->issued += pax->fiatoshis;
312                         pax->didstats = 1;
313                         if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
314                             printf("########### %p issued %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,str,dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight);
315                         pax2->marked = pax->height;
316                         pax->marked = pax->height;
317                     }
318                 }
319                 else if ( pax->type == 'W' )
320                 {
321                     //bitcoin_address(coinaddr,addrtype,rmd160,20);
322                     if ( (checktoshis= komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis)) != 0 )
323                     {
324                         if ( komodo_paxcmp(pax->source,pax->height,pax->komodoshis,checktoshis,seed) != 0 )
325                         {
326                             pax->marked = pax->height;
327                             //printf("WITHDRAW.%s mark <- %d %.8f != %.8f\n",pax->source,pax->height,dstr(checktoshis),dstr(pax->komodoshis));
328                         }
329                         else if ( pax->validated == 0 )
330                         {
331                             pax->validated = pax->komodoshis = checktoshis;
332                             //int32_t j; for (j=0; j<32; j++)
333                             //    printf("%02x",((uint8_t *)&pax->txid)[j]);
334                             //if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
335                             //    printf(" v%d %p got WITHDRAW.%s kmd.%d ht.%d %.8f -> %.8f/%.8f\n",pax->vout,pax,pax->source,pax->height,pax->otherheight,dstr(pax->fiatoshis),dstr(pax->komodoshis),dstr(checktoshis));
336                         }
337                     }
338                 }
339             }
340         }
341     }
342     komodo_stateptr(symbol,dest);
343     HASH_ITER(hh,PAX,pax,tmp)
344     {
345         pax->ready = 0;
346         if ( 0 && pax->type == 'A' )
347             printf("%p pax.%s <- %s marked.%d %.8f -> %.8f validated.%d approved.%d\n",pax,pax->symbol,pax->source,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0);
348         if ( pax->marked != 0 )
349             continue;
350         if ( strcmp(symbol,pax->symbol) == 0 || pax->type == 'A' )
351         {
352             if ( pax->marked == 0 )
353             {
354                 if ( komodo_is_issuer() != 0 )
355                 {
356                     if ( pax->validated != 0 && pax->type == 'D' )
357                     {
358                         total += pax->fiatoshis;
359                         pax->ready = 1;
360                     }
361                 }
362                 else if ( pax->approved != 0 && pax->type == 'A' )
363                 {
364                     if ( pax->validated != 0 )
365                     {
366                         total += pax->komodoshis;
367                         pax->ready = 1;
368                     }
369                     else
370                     {
371                         seed = 0;
372                         checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis);
373                         //printf("paxtotal PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",pax->height,pax->symbol,(double)pax->fiatoshis/COIN,(double)pax->komodoshis/COIN,(double)checktoshis/COIN);
374                         //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight);
375                         if ( seed != 0 && checktoshis != 0 )
376                         {
377                             if ( checktoshis == pax->komodoshis )
378                             {
379                                 total += pax->komodoshis;
380                                 pax->validated = pax->komodoshis;
381                                 pax->ready = 1;
382                             } else pax->marked = pax->height;
383                         }
384                     }
385                 }
386                 if ( 0 && pax->ready != 0 )
387                     printf("%p (%c) pax.%s marked.%d %.8f -> %.8f validated.%d approved.%d ready.%d ht.%d\n",pax,pax->type,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0,pax->ready,pax->height);
388             }
389         }
390     }
391     //printf("paxtotal %.8f\n",dstr(total));
392     return(total);
393 }
394
395 static int _paxorder(const void *a,const void *b)
396 {
397 #define pax_a (*(struct pax_transaction **)a)
398 #define pax_b (*(struct pax_transaction **)b)
399     uint64_t aval,bval;
400     aval = pax_a->fiatoshis + pax_a->komodoshis + pax_a->height;
401     bval = pax_b->fiatoshis + pax_b->komodoshis + pax_b->height;
402         if ( bval > aval )
403                 return(-1);
404         else if ( bval < aval )
405                 return(1);
406         return(0);
407 #undef pax_a
408 #undef pax_b
409 }
410
411 int32_t komodo_pending_withdraws(char *opretstr) // todo: enforce deterministic order
412 {
413     struct pax_transaction *pax,*pax2,*tmp,*paxes[64]; uint8_t opretbuf[16384]; int32_t i,n,ht,len=0; uint64_t total = 0;
414     if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
415         return(0);
416     if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
417         return(0);
418     n = 0;
419     HASH_ITER(hh,PAX,pax,tmp)
420     {
421         if ( pax->type == 'W' )
422         {
423             if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'A')) != 0 )
424             {
425                 if ( pax2->approved != 0 )
426                     pax->approved = pax2->approved;
427             }
428             else if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'X')) != 0 )
429                 pax->approved = pax->height;
430             //printf("pending_withdraw: pax %s marked.%u approved.%u validated.%llu\n",pax->symbol,pax->marked,pax->approved,(long long)pax->validated);
431             if ( pax->marked == 0 && pax->approved == 0 && pax->validated != 0 ) //strcmp((char *)"KMD",pax->symbol) == 0 &&
432             {
433                 if ( n < sizeof(paxes)/sizeof(*paxes) )
434                 {
435                     paxes[n++] = pax;
436                     //int32_t j; for (j=0; j<32; j++)
437                     //    printf("%02x",((uint8_t *)&pax->txid)[j]);
438                     //printf(" %s.(kmdht.%d ht.%d marked.%u approved.%d validated %.8f) %.8f\n",pax->source,pax->height,pax->otherheight,pax->marked,pax->approved,dstr(pax->validated),dstr(pax->komodoshis));
439                 }
440             }
441         }
442     }
443     opretstr[0] = 0;
444     if ( n > 0 )
445     {
446         opretbuf[len++] = 'A';
447         qsort(paxes,n,sizeof(*paxes),_paxorder);
448         for (i=0; i<n; i++)
449         {
450             if ( len < (sizeof(opretbuf)>>3)*7 )
451                 len += komodo_rwapproval(1,&opretbuf[len],paxes[i]);
452         }
453         if ( len > 0 )
454             init_hexbytes_noT(opretstr,opretbuf,len);
455     }
456     //fprintf(stderr,"komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal()));
457     return(len);
458 }
459
460 int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
461 {
462     struct pax_transaction *pax,*tmp; char symbol[16],dest[16]; uint8_t *script,opcode,opret[16384],data[16384]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t available,deposited,issued,withdrawn,approved,redeemed,mask;
463     if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
464         return(0);
465     struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD");
466     sp = komodo_stateptr(symbol,dest);
467     strcpy(symbol,base);
468     if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
469         return(0);
470     PENDING_KOMODO_TX = 0;
471     for (i=0; i<3; i++)
472     {
473         if ( komodo_isrealtime(&ht) != 0 )
474             break;
475         sleep(1);
476     }
477     if ( i == 3 )
478     {
479         printf("%s not realtime ht.%d\n",ASSETCHAINS_SYMBOL,ht);
480         return(0);
481     }
482     if ( tokomodo == 0 )
483     {
484         opcode = 'I';
485     }
486     else
487     {
488         opcode = 'X';
489         if ( komodo_paxtotal() == 0 )
490             return(0);
491     }
492     HASH_ITER(hh,PAX,pax,tmp)
493     {
494         if ( pax->type != 'D' && pax->type != 'A' )
495             continue;
496         {
497 #ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE
498             if ( kmdsp != 0 && (kmdsp->NOTARIZED_HEIGHT >= pax->height || kmdsp->CURRENT_HEIGHT > pax->height+30) ) // assumes same chain as notarize
499                 pax->validated = pax->komodoshis; //kmdsp->NOTARIZED_HEIGHT;
500             else pax->validated = pax->ready = 0;
501 #else
502             pax->validated = pax->komodoshis;
503 #endif
504         }
505         if ( ASSETCHAINS_SYMBOL[0] != 0 && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
506         {
507             if ( pax->height > 214700 || strcmp(ASSETCHAINS_SYMBOL,symbol) == 0 )
508                 printf("miner.[%s]: skip %s %.8f when avail %.8f deposited %.8f, issued %.8f withdrawn %.8f approved %.8f redeemed %.8f\n",ASSETCHAINS_SYMBOL,symbol,dstr(pax->fiatoshis),dstr(available),dstr(deposited),dstr(issued),dstr(withdrawn),dstr(approved),dstr(redeemed));
509             continue;
510         }
511         /*printf("pax.%s marked.%d %.8f -> %.8f ready.%d validated.%d\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->ready!=0,pax->validated!=0);
512         if ( pax->marked != 0 || (pax->type != 'D' && pax->type != 'A') || pax->ready == 0 )
513         {
514             printf("reject 2\n");
515             continue;
516         }*/
517         if ( ASSETCHAINS_SYMBOL[0] != 0 && (strcmp(pax->symbol,symbol) != 0 || pax->validated == 0 || pax->ready == 0) )
518         {
519             if ( strcmp(pax->symbol,ASSETCHAINS_SYMBOL) == 0 )
520                 printf("pax->symbol.%s != %s or null pax->validated %.8f ready.%d ht.(%d %d)\n",pax->symbol,symbol,dstr(pax->validated),pax->ready,kmdsp->CURRENT_HEIGHT,pax->height);
521             continue;
522         }
523         if ( pax->ready == 0 )
524             continue;
525         if ( pax->type == 'A' && ASSETCHAINS_SYMBOL[0] == 0 )
526         {
527             if ( kmdsp != 0 )
528             {
529                 if ( (baseid= komodo_baseid(pax->symbol)) < 0 || ((1LL << baseid) & sp->RTmask) == 0 )
530                 {
531                     printf("not RT for (%s) %llx baseid.%d %llx\n",pax->symbol,(long long)sp->RTmask,baseid,(long long)(1LL<<baseid));
532                     continue;
533                 }
534             } else continue;
535         }
536
537         //printf("redeem.%d? (%c) %p pax.%s marked.%d %.8f -> %.8f ready.%d validated.%d approved.%d\n",tokomodo,pax->type,pax,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->ready!=0,pax->validated!=0,pax->approved!=0);
538         if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
539             printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
540         txNew->vout.resize(numvouts+1);
541         txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
542         txNew->vout[numvouts].scriptPubKey.resize(25);
543         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
544         *script++ = 0x76;
545         *script++ = 0xa9;
546         *script++ = 20;
547         memcpy(script,pax->rmd160,20), script += 20;
548         *script++ = 0x88;
549         *script++ = 0xac;
550         if ( tokomodo == 0 )
551         {
552             for (i=0; i<32; i++)
553                 data[len++] = ((uint8_t *)&pax->txid)[i];
554             data[len++] = pax->vout & 0xff;
555             data[len++] = (pax->vout >> 8) & 0xff;
556             PENDING_KOMODO_TX += pax->fiatoshis;
557         }
558         else
559         {
560             len += komodo_rwapproval(1,&data[len],pax);
561             PENDING_KOMODO_TX += pax->komodoshis;
562             printf(" len.%d vout.%u DEPOSIT %.8f <- pax.%s pending ht %d %d %.8f | ",len,pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,pax->height,pax->otherheight,dstr(PENDING_KOMODO_TX));
563         }
564         if ( numvouts++ >= 64 )
565             break;
566     }
567     if ( numvouts > 1 )
568     {
569         if ( tokomodo != 0 )
570             strcpy(symbol,(char *)"KMD");
571         for (i=0; symbol[i]!=0; i++)
572             data[len++] = symbol[i];
573         data[len++] = 0;
574         for (i=0; i<len; i++)
575             printf("%02x",data[i]);
576         printf(" <- data[%d]\n",len);
577         opretlen = komodo_opreturnscript(opret,opcode,data,len);
578         txNew->vout.resize(numvouts+1);
579         txNew->vout[numvouts].nValue = 0;
580         txNew->vout[numvouts].scriptPubKey.resize(opretlen);
581         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
582         memcpy(script,opret,opretlen);
583         for (i=0; i<8; i++)
584             printf("%02x",opret[i]);
585         printf(" <- opret, MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
586         return(1);
587     }
588     return(0);
589 }
590
591 int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
592 {
593     int32_t i,j,n,ht,num,opretlen,offset=1,errs=0,matched=0,kmdheights[64],otherheights[64]; uint256 hash,txids[64]; char symbol[16],base[16]; uint16_t vouts[64]; int8_t baseids[64]; uint8_t *script,opcode,rmd160s[64*20]; uint64_t available,deposited,issued,withdrawn,approved,redeemed; int64_t values[64],srcvalues[64]; struct pax_transaction *pax; struct komodo_state *sp;
594     if ( KOMODO_PAX == 0 || komodo_isrealtime(&ht) == 0 || KOMODO_PASSPORT_INITDONE == 0 )
595         return(0);
596     memset(baseids,0xff,sizeof(baseids));
597     memset(values,0,sizeof(values));
598     memset(srcvalues,0,sizeof(srcvalues));
599     memset(rmd160s,0,sizeof(rmd160s));
600     memset(kmdheights,0,sizeof(kmdheights));
601     memset(otherheights,0,sizeof(otherheights));
602     n = block.vtx[0].vout.size();
603     script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
604     if ( n <= 2 || script[0] != 0x6a )
605         return(0);
606     offset += komodo_scriptitemlen(&opretlen,&script[offset]);
607     if ( ASSETCHAINS_SYMBOL[0] == 0 )
608     {
609         //for (i=0; i<opretlen; i++)
610         //    printf("%02x",script[i]);
611         //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
612         opcode = 'X';
613         strcpy(symbol,(char *)"KMD");
614     }
615     else
616     {
617         strcpy(symbol,ASSETCHAINS_SYMBOL);
618         opcode = 'I';
619         if ( komodo_baseid(symbol) < 0 )
620         {
621             if ( block.vtx[0].vout.size() != 1 )
622             {
623                 printf("%s has more than one coinbase?\n",symbol);
624                 return(-1);
625             }
626             return(0);
627         }
628     }
629     if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
630     {
631         if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
632         {
633             for (i=1; i<n-1; i++)
634             {
635                 if ( (sp= komodo_stateptrget(CURRENCIES[baseids[i-1]])) != 0 && (sp->RTmask & (1LL << baseids[i-1])) == 0 )
636                 {
637                     printf("skip checkdeposit.%s not RT\n",CURRENCIES[baseids[i-1]]);
638                     matched++;
639                     continue;
640                 }
641                 if ( (pax= komodo_paxfinds(txids[i-1],vouts[i-1])) != 0 ) // finds... make sure right one
642                 {
643                     pax->type = opcode;
644                     if ( opcode == 'I' && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
645                     {
646                         printf("checkdeposit.[%s]: skip %s %.8f when avail %.8f deposited %.8f, issued %.8f withdrawn %.8f approved %.8f redeemed %.8f\n",ASSETCHAINS_SYMBOL,symbol,dstr(pax->fiatoshis),dstr(available),dstr(deposited),dstr(issued),dstr(withdrawn),dstr(approved),dstr(redeemed));
647                         continue;
648                     }
649                     if ( ((opcode == 'I' && (pax->fiatoshis == 0 || pax->fiatoshis == block.vtx[0].vout[i].nValue)) || (opcode == 'X' && (pax->komodoshis == 0 || pax->komodoshis == block.vtx[0].vout[i].nValue))) )
650                     {
651                         if ( pax->marked != 0 && height >= 80820 )
652                         {
653                             printf(">>>>>>>>>>> %c errs.%d i.%d match %.8f vs %.8f paxmarked.%d kht.%d ht.%d\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax->marked,pax->height,pax->otherheight);
654                             if ( pax->komodoshis != 0 || pax->fiatoshis != 0 )
655                                 errs++;
656                             else matched++; // onetime init bypass
657                         }
658                         else
659                         {
660                             if ( opcode == 'X' && strcmp(ASSETCHAINS_SYMBOL,CURRENCIES[baseids[i]]) == 0 )
661                                 printf("check deposit validates %s %.8f -> %.8f\n",CURRENCIES[baseids[i]],dstr(srcvalues[i]),dstr(values[i]));
662                             matched++;
663                         }
664                     }
665                     else
666                     {
667                         for (j=0; j<32; j++)
668                             printf("%02x",((uint8_t *)&txids[i-1])[j]);
669                         printf(" cant paxfind %c txid\n",opcode);
670                         printf(">>>>>>>>>>> %c errs.%d i.%d match %.8f vs %.8f pax.%p\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax);
671                     }
672                 }
673                 else if ( kmdheights[i-1] > 0 && otherheights[i-1] > 0 )
674                 {
675                     hash = block.GetHash();
676                     for (j=0; j<32; j++)
677                         printf("%02x",((uint8_t *)&hash)[j]);
678                     printf(" kht.%d ht.%d %.8f %.8f blockhash couldnt find vout.[%d]\n",kmdheights[i-1],otherheights[i-1],dstr(values[i-1]),dstr(srcvalues[i]),i);
679                 }
680             }
681             if ( ASSETCHAINS_SYMBOL[0] == 0 )
682             {
683                 if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
684                 {
685                     printf("WOULD REJECT %s: ht.%d (%c) matched.%d vs num.%d tip.%d isRT.%d\n",symbol,height,opcode,matched,num,(int32_t)chainActive.Tip()->nHeight,komodo_isrealtime(&ht));
686                     // can easily happen depending on order of loading
687                     if ( height > 200000 )
688                     {
689                         printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
690                         return(-1);
691                     }
692                 }
693             }
694             else
695             {
696                 if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
697                 {
698                     printf("REJECT %s: ht.%d (%c) matched.%d vs num.%d tip.%d isRT.%d\n",symbol,height,opcode,matched,num,(int32_t)chainActive.Tip()->nHeight,komodo_isrealtime(&ht));
699                     return(-1);
700                 }
701             }
702         }
703         //printf("opretlen.%d num.%d\n",opretlen,num);
704     }
705     return(0);
706 }
707
708 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
709 {
710     uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,kvheight,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fee,fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff; 
711     const char *typestr = "unknown";
712     if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 && opretbuf[0] != 'K' )
713     {
714         //printf("komodo_opreturn skip %s\n",ASSETCHAINS_SYMBOL);
715         return("assetchain");
716     }
717     //else if ( KOMODO_PAX == 0 )
718     //    return("nopax");
719     memset(baseids,0xff,sizeof(baseids));
720     memset(values,0,sizeof(values));
721     memset(srcvalues,0,sizeof(srcvalues));
722     memset(rmd160s,0,sizeof(rmd160s));
723     memset(kmdheights,0,sizeof(kmdheights));
724     memset(otherheights,0,sizeof(otherheights));
725     tokomodo = (komodo_is_issuer() == 0);
726     if ( opretbuf[0] == 'K' && opretlen != 40 )
727     {
728         komodo_kvupdate(opretbuf,opretlen,value);
729     }
730     else if ( opretbuf[0] == 'D' )
731     {
732         tokomodo = 0;
733         if ( opretlen == 38 ) // any KMD tx
734         {
735             iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
736             memset(base,0,sizeof(base));
737             PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
738             bitcoin_address(coinaddr,addrtype,rmd160,20);
739             checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
740             typestr = "deposit";
741             if ( kmdheight > 195000 || kmdheight <= height )
742             {
743                 didstats = 0;
744                 if ( 0 && kmdheight > 214700 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
745                 {
746                     printf("(%s) (%s) kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",ASSETCHAINS_SYMBOL,base,kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
747                     for (i=0; i<32; i++)
748                         printf("%02x",((uint8_t *)&txid)[i]);
749                     printf(" <- txid.v%u ",vout);
750                     for (i=0; i<33; i++)
751                         printf("%02x",pubkey33[i]);
752                     printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
753                 }
754                 if ( komodo_paxcmp(base,kmdheight,value,checktoshis,kmdheight < 225000 ? seed : 0) == 0 )
755                 {
756                     if ( (pax= komodo_paxfind(txid,vout,'D')) == 0 )
757                     {
758                         if ( (basesp= komodo_stateptrget(base)) != 0 )
759                         {
760                             basesp->deposited += fiatoshis;
761                             didstats = 1;
762                             if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
763                                 printf("########### %p deposited %s += %.8f kmdheight.%d %.8f\n",basesp,base,dstr(fiatoshis),kmdheight,dstr(value));
764                         } else printf("cant get stateptr.(%s)\n",base);
765                         komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0);
766                     }
767                     if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 )
768                     {
769                         pax->height = kmdheight;
770                         pax->validated = value;
771                         pax->komodoshis = value;
772                         pax->fiatoshis = fiatoshis;
773                         if ( didstats == 0 && pax->didstats == 0 )
774                         {
775                             if ( (basesp= komodo_stateptrget(base)) != 0 )
776                             {
777                                 basesp->deposited += fiatoshis;
778                                 didstats = 1;
779                                 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
780                                     printf("########### %p depositedB %s += %.8f/%.8f kmdheight.%d/%d %.8f/%.8f\n",basesp,base,dstr(fiatoshis),dstr(pax->fiatoshis),kmdheight,pax->height,dstr(value),dstr(pax->komodoshis));
781                             }
782                         }
783                         if ( didstats != 0 )
784                             pax->didstats = 1;
785                         if ( (pax2= komodo_paxfind(txid,vout,'I')) != 0 )
786                         {
787                             pax2->fiatoshis = pax->fiatoshis;
788                             pax2->komodoshis = pax->komodoshis;
789                             pax->marked = pax2->marked = pax->height;
790                             pax2->height = pax->height = height;
791                             if ( pax2->didstats == 0 )
792                             {
793                                 if ( (basesp= komodo_stateptrget(base)) != 0 )
794                                 {
795                                     basesp->issued += pax2->fiatoshis;
796                                     pax2->didstats = 1;
797                                     if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
798                                         printf("########### %p issueda %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->komodoshis),pax2->otherheight);
799                                 }
800                             }
801                         }
802                     }
803                 }
804                 else if ( kmdheight > 182000 && (kmdheight > 214700 || strcmp(base,ASSETCHAINS_SYMBOL) == 0) ) //seed != 0 &&
805                     printf("pax %s deposit %.8f rejected kmdheight.%d %.8f KMD check %.8f seed.%llu\n",base,dstr(fiatoshis),kmdheight,dstr(value),dstr(checktoshis),(long long)seed);
806             } //else printf("paxdeposit height.%d vs kmdheight.%d\n",height,kmdheight);
807         }
808     }
809     else if ( opretbuf[0] == 'I' )
810     {
811         tokomodo = 0;
812         if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
813         {
814             if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
815             {
816                 for (i=0; i<n; i++)
817                 {
818                     if ( baseids[i] < 0 )
819                     {
820                         printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]);
821                         continue;
822                     }
823                     bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
824                     komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0);
825                     komodo_paxmark(height,txids[i],vouts[i],'I',height);
826                     if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 )
827                     {
828                         pax->type = opretbuf[0];
829                         strcpy(pax->source,(char *)&opretbuf[opretlen-4]);
830                         if ( (pax2= komodo_paxfind(txids[i],vouts[i],'D')) != 0 && pax2->fiatoshis != 0 && pax2->komodoshis != 0 )
831                         {
832                             // realtime path?
833                             pax->fiatoshis = pax2->fiatoshis;
834                             pax->komodoshis = pax2->komodoshis;
835                             pax->marked = pax2->marked = pax2->height;
836                             if ( pax->didstats == 0 )
837                             {
838                                 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
839                                 {
840                                     basesp->issued += pax->fiatoshis;
841                                     pax->didstats = 1;
842                                     pax->height = pax2->height;
843                                     pax->otherheight = height;
844                                     if ( 0 && strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
845                                         printf("########### %p issuedb %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,CURRENCIES[baseids[i]],dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight);
846                                 }
847                             }
848                         }
849                     }
850                     if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'I',height)) != 0 )
851                         komodo_paxdelete(pax);
852                     if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'D',height)) != 0 )
853                         komodo_paxdelete(pax);
854                 }
855             } //else printf("opreturn none issued?\n");
856         }
857     }
858     else if ( opretbuf[0] == 'W' )//&& opretlen >= 38 )
859     {
860         if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 && strcmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]) == 0 )
861         {
862             for (i=0; i<opretlen; i++)
863                 printf("%02x",opretbuf[i]);
864             printf(" [%s] reject obsolete withdraw request.%s\n",ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]);
865             return(typestr);
866         }
867         tokomodo = 1;
868         iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
869         memset(base,0,sizeof(base));
870         PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
871         bitcoin_address(coinaddr,addrtype,rmd160,20);
872         checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
873         typestr = "withdraw";
874         //printf(" [%s] WITHDRAW %s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s) len.%d\n",ASSETCHAINS_SYMBOL,base,kmdheight,height,dstr(checktoshis),dstr(komodoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr,opretlen);
875         didstats = 0;
876         //if ( komodo_paxcmp(base,kmdheight,komodoshis,checktoshis,seed) == 0 )
877         {
878             if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) )
879             {
880                 if ( (basesp= komodo_stateptrget(base)) != 0 )
881                 {
882                     basesp->withdrawn += value;
883                     didstats = 1;
884                     if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
885                         printf("########### %p withdrawn %s += %.8f check %.8f\n",basesp,base,dstr(value),dstr(checktoshis));
886                 }
887                 if ( 0 && strcmp(base,"RUB") == 0 && (pax == 0 || pax->approved == 0) )
888                     printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
889             }
890             komodo_gateway_deposit(coinaddr,0,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0);
891             if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 )
892             {
893                 pax->type = opretbuf[0];
894                 strcpy(pax->source,base);
895                 strcpy(pax->symbol,"KMD");
896                 pax->height = kmdheight;
897                 pax->otherheight = height;
898                 pax->komodoshis = komodoshis;
899             }
900         } // else printf("withdraw %s paxcmp ht.%d %d error value %.8f -> %.8f vs %.8f\n",base,kmdheight,height,dstr(value),dstr(komodoshis),dstr(checktoshis));
901         // need to allocate pax
902     }
903     else if ( tokomodo != 0 && opretbuf[0] == 'A' )
904     {
905         tokomodo = 1;
906         if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
907         {
908             for (i=0; i<opretlen; i++)
909                 printf("%02x",opretbuf[i]);
910             printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
911         }
912         if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
913         {
914             for (i=0; i<n; i++)
915             {
916                 //for (j=0; j<32; j++)
917                 //    printf("%02x",((uint8_t *)&txids[i])[j]);
918                 //printf(" v%d %.8f %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),dstr(srcvalues[i]),kmdheights[i],otherheights[i],baseids[i]);
919                 if ( baseids[i] < 0 )
920                 {
921                     for (i=0; i<opretlen; i++)
922                         printf("%02x",opretbuf[i]);
923                     printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
924                     //printf("baseids[%d] %d\n",i,baseids[i]);
925                     if ( (pax= komodo_paxfind(txids[i],vouts[i],'W')) != 0 || (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
926                     {
927                         baseids[i] = komodo_baseid(pax->symbol);
928                         printf("override neg1 with (%s)\n",pax->symbol);
929                     }
930                     if ( baseids[i] < 0 )
931                         continue;
932                 }
933                 didstats = 0;
934                 seed = 0;
935                 checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)values[i]);
936                 //printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",kmdheights[i],CURRENCIES[baseids[i]],(double)values[i]/COIN,(double)srcvalues[i]/COIN,(double)checktoshis/COIN);
937                 if ( srcvalues[i] == checktoshis )
938                 {
939                     if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
940                     {
941                         bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
942                         komodo_gateway_deposit(coinaddr,srcvalues[i],CURRENCIES[baseids[i]],values[i],&rmd160s[i*20],txids[i],vouts[i],'A',kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]);
943                         if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
944                             printf("unexpected null pax for approve\n");
945                         else pax->validated = checktoshis;
946                         if ( (pax2= komodo_paxfind(txids[i],vouts[i],'W')) != 0 )
947                             pax2->approved = kmdheights[i];
948                         komodo_paxmark(height,txids[i],vouts[i],'W',height);
949                         //komodo_paxmark(height,txids[i],vouts[i],'A',height);
950                         if ( values[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
951                         {
952                             basesp->approved += values[i];
953                             didstats = 1;
954                             printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),kmdheights[i],otherheights[i]);
955                         }
956                         //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i]));
957                     }
958                     else if ( pax->didstats == 0 && srcvalues[i] != 0 )
959                     {
960                         if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
961                         {
962                             basesp->approved += values[i];
963                             didstats = 1;
964                             printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),kmdheights[i],otherheights[i]);
965                         }
966                     } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
967                     if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 )
968                     {
969                         pax->type = opretbuf[0];
970                         pax->approved = kmdheights[i];
971                         pax->validated = checktoshis;
972                         if ( didstats != 0 )
973                             pax->didstats = 1;
974                         //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
975                         //printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax);
976                     }
977                 }
978             }
979         } //else printf("n.%d from opreturns\n",n);
980         //printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
981     }
982     else if ( opretbuf[0] == 'X' )
983     {
984         tokomodo = 1;
985         if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
986         {
987             for (i=0; i<n; i++)
988             {
989                 if ( baseids[i] < 0 )
990                     continue;
991                 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
992                 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0);
993                 komodo_paxmark(height,txids[i],vouts[i],'W',height);
994                 komodo_paxmark(height,txids[i],vouts[i],'A',height);
995                 komodo_paxmark(height,txids[i],vouts[i],'X',height);
996                 if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
997                 {
998                     pax->type = opretbuf[0];
999                     if ( height < 121842 ) // fields got switched around due to legacy issues and approves
1000                         value = srcvalues[i];
1001                     else value = values[i];
1002                     if ( baseids[i] >= 0 && value != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
1003                     {
1004                         basesp->redeemed += value;
1005                         pax->didstats = 1;
1006                         if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
1007                             printf("ht.%d %.8f ########### %p redeemed %s += %.8f %.8f kht.%d ht.%d\n",height,dstr(value),basesp,CURRENCIES[baseids[i]],dstr(value),dstr(srcvalues[i]),kmdheights[i],otherheights[i]);
1008                     }
1009                 }
1010                 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'W',height)) != 0 )
1011                     komodo_paxdelete(pax);
1012                 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'A',height)) != 0 )
1013                     komodo_paxdelete(pax);
1014                 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'X',height)) != 0 )
1015                     komodo_paxdelete(pax);
1016             }
1017         } //else printf("komodo_issued_opreturn returned %d\n",n);
1018     }
1019     return(typestr);
1020 }
1021
1022 void komodo_passport_iteration()
1023 {
1024     static long lastpos[34]; static char userpass[33][1024]; int32_t maxseconds = 7;
1025     FILE *fp; int32_t baseid,n,ht,isrealtime,expired,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[16],dest[16]; uint32_t buf[3],starttime; cJSON *infoobj,*result; uint64_t RTmask = 0;
1026     //printf("PASSPORT.(%s)\n",ASSETCHAINS_SYMBOL);
1027     expired = 0;
1028     while ( KOMODO_INITDONE == 0 )
1029     {
1030         fprintf(stderr,"[%s] PASSPORT iteration waiting for KOMODO_INITDONE\n",ASSETCHAINS_SYMBOL);
1031         sleep(3);
1032     }
1033     refsp = komodo_stateptr(symbol,dest);
1034     if ( ASSETCHAINS_SYMBOL[0] == 0 )
1035         refid = 33;
1036     else
1037     {
1038         refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
1039         if ( refid == 0 )
1040         {
1041             KOMODO_PASSPORT_INITDONE = 1;
1042             return;
1043         }
1044     }
1045     if ( KOMODO_PAX == 0 )
1046     {
1047         KOMODO_PASSPORT_INITDONE = 1;
1048         return;
1049     }
1050     starttime = (uint32_t)time(NULL);
1051     //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
1052     for (baseid=32; baseid>=0; baseid--)
1053     {
1054         if ( time(NULL) >= starttime+maxseconds )
1055             break;
1056         sp = 0;
1057         isrealtime = 0;
1058         base = (char *)CURRENCIES[baseid];
1059         if ( baseid+1 != refid )
1060         {
1061             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
1062             komodo_nameset(symbol,dest,base);
1063             sp = komodo_stateptrget(symbol);
1064             n = 0;
1065             if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 )
1066             {
1067                 fseek(fp,0,SEEK_END);
1068                 if ( ftell(fp) > lastpos[baseid] )
1069                 {
1070                     if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
1071                         printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
1072                     fseek(fp,lastpos[baseid],SEEK_SET);
1073                     while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 && n < 1000 )
1074                     {
1075                         if ( n == 999 )
1076                         {
1077                             if ( time(NULL) < starttime+maxseconds )
1078                                 n = 0;
1079                             else
1080                             {
1081                                 //printf("expire passport loop %s -> %s at %ld\n",ASSETCHAINS_SYMBOL,base,lastpos[baseid]);
1082                                 expired++;
1083                             }
1084                         }
1085                         n++;
1086                     }
1087                     lastpos[baseid] = ftell(fp);
1088                     if ( lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
1089                         printf("from.(%s) lastpos[%s] %ld isrt.%d\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid],komodo_isrealtime(&ht));
1090                 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
1091                 fclose(fp);
1092             } else printf("error.(%s) %p\n",fname,sp);
1093             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1094             if ( (fp= fopen(fname,"rb")) != 0 )
1095             {
1096                 if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
1097                 {
1098                     sp->CURRENT_HEIGHT = buf[0];
1099                     if ( buf[0] != 0 && buf[0] >= buf[1] && buf[2] > time(NULL)-300 )
1100                     {
1101                         isrealtime = 1;
1102                         RTmask |= (1LL << baseid);
1103                         memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1104                     } else if ( (time(NULL)-buf[2]) > 1200 )
1105                         fprintf(stderr,"[%s]: %s not RT %u %u %d\n",ASSETCHAINS_SYMBOL,base,buf[0],buf[1],(int32_t)(time(NULL)-buf[2]));
1106                 } //else fprintf(stderr,"%s size error RT\n",base);
1107                 fclose(fp);
1108             } //else fprintf(stderr,"%s open error RT\n",base);
1109         }
1110         else
1111         {
1112             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1113             if ( (fp= fopen(fname,"wb")) != 0 )
1114             {
1115                 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
1116                 buf[1] = (uint32_t)komodo_longestchain();
1117                 if ( buf[0] != 0 && buf[0] == buf[1] )
1118                 {
1119                     buf[2] = (uint32_t)time(NULL);
1120                     RTmask |= (1LL << baseid);
1121                     memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1122                     if ( refid != 0 )
1123                         memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
1124                 }
1125                 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
1126                     fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
1127                 fclose(fp);
1128             } else fprintf(stderr,"%s create error RT\n",base);
1129         }
1130         if ( sp != 0 && isrealtime == 0 )
1131             refsp->RTbufs[0][2] = 0;
1132     }
1133     komodo_paxtotal();
1134     refsp->RTmask = RTmask;
1135     if ( expired == 0 && KOMODO_PASSPORT_INITDONE == 0 )
1136     {
1137         KOMODO_PASSPORT_INITDONE = 1;
1138         printf("done PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
1139     }
1140 }
1141
This page took 0.090199 seconds and 4 git commands to generate.