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