]> Git Repo - VerusCoin.git/blob - src/komodo_gateway.h
test
[VerusCoin.git] / src / komodo_gateway.h
1 /******************************************************************************
2  * Copyright © 2014-2016 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 *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base)
19 {
20     int32_t baseid; struct komodo_state *sp;
21     if ( (baseid= komodo_baseid(base)) >= 0 )
22     {
23         if ( (sp= komodo_stateptrget(base)) != 0 )
24         {
25             *deposited = sp->deposited;
26             *issued = sp->issued;
27             *withdrawn = sp->withdrawn;
28             *approved = sp->approved;
29             *redeemed = sp->redeemed;
30             //printf("%p %s %.8f %.8f %.8f %.8f %.8f\n",sp,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed));
31             return(0);
32         } else printf("pax_fiatstatus cant get basesp.%s\n",base);
33     } else printf("pax_fiatstatus illegal base.%s\n",base);
34     return(-1);
35 }
36
37 struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout)
38 {
39     struct pax_transaction *pax;
40     pthread_mutex_lock(&komodo_mutex);
41     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
42     pthread_mutex_unlock(&komodo_mutex);
43     return(pax);
44 }
45
46 struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,int32_t mark)
47 {
48     struct pax_transaction *pax;
49     pthread_mutex_lock(&komodo_mutex);
50     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
51     if ( pax == 0 )
52     {
53         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
54         pax->txid = txid;
55         pax->vout = vout;
56         HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
57         //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
58     }
59     if ( pax != 0 )
60     {
61         pax->marked = mark;
62         //int32_t i; for (i=0; i<32; i++)
63         //    printf("%02x",((uint8_t *)&txid)[i]);
64         //printf(" paxmark.ht %d vout%d\n",mark,vout);
65     }
66     pthread_mutex_unlock(&komodo_mutex);
67     return(pax);
68 }
69
70 void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context
71 {
72     struct pax_transaction *pax; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s;
73     sp = komodo_stateptr(str,dest);
74     pthread_mutex_lock(&komodo_mutex);
75     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
76     if ( pax == 0 )
77     {
78         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
79         pax->txid = txid;
80         pax->vout = vout;
81         HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
82         addflag = 1;
83         if ( ASSETCHAINS_SYMBOL[0] == 0 )
84         {
85             int32_t i; for (i=0; i<32; i++)
86                 printf("%02x",((uint8_t *)&txid)[i]);
87             printf(" v.%d [%s] kht.%d ht.%d create pax.%p symbol.%s source.%s\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax,symbol,source);
88         }
89     }
90     pthread_mutex_unlock(&komodo_mutex);
91     if ( coinaddr != 0 )
92     {
93         strcpy(pax->coinaddr,coinaddr);
94         if ( value != 0 )
95             pax->komodoshis = value;
96         if ( symbol != 0 )
97             strcpy(pax->symbol,symbol);
98         if ( source != 0 )
99             strcpy(pax->source,source);
100         if ( fiatoshis != 0 )
101             pax->fiatoshis = fiatoshis;
102         if ( rmd160 != 0 )
103             memcpy(pax->rmd160,rmd160,20);
104         if ( height != 0 )
105             pax->height = height;
106         if ( otherheight != 0 )
107             pax->otherheight = otherheight;
108         if ( pax->marked == 0 )
109         {
110             if ( addflag != 0 )
111             {
112                 if ( (pax->approved= approved) != 0 )
113                     s = (char *)"APPROVED";
114                 else s = (char *)((ASSETCHAINS_SYMBOL[0] == 0) ? "WITHDRAW" : "DEPOSIT");
115                 printf("[%s] addflag.%d ADD %s/%s %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,s,symbol,source,dstr(ASSETCHAINS_SYMBOL[0]==0?pax->komodoshis:pax->fiatoshis),pax->coinaddr,pax->height,pax->otherheight,dstr(komodo_paxtotal()));
116             }
117         }
118         //else printf("%p MARKED.%d DEPOSIT %s %.8f -> %s TO PAX ht.%d otherht.%d\n",pax,pax->marked,symbol,dstr(fiatoshis),coinaddr,height,otherheight);
119     }
120     else
121     {
122         pax->marked = height;
123         printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
124     }
125 }
126
127 int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax)
128 {
129     int32_t i,len = 0;
130     if ( rwflag == 1 )
131     {
132         for (i=0; i<32; i++)
133             opretbuf[len++] = ((uint8_t *)&pax->txid)[i];
134         opretbuf[len++] = pax->vout & 0xff;
135         opretbuf[len++] = (pax->vout >> 8) & 0xff;
136     }
137     else
138     {
139         for (i=0; i<32; i++)
140             ((uint8_t *)&pax->txid)[i] = opretbuf[len++];
141         for (i=0; i<32; i++)
142             printf("%02x",((uint8_t *)&pax->txid)[31-i]);
143         pax->vout = opretbuf[len++];
144         pax->vout += ((uint32_t)opretbuf[len++] << 8);
145         printf(" txid v.%d\n",pax->vout);
146     }
147     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis);
148     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
149     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height);
150     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight);
151     if ( rwflag != 0 )
152     {
153         memcpy(&opretbuf[len],pax->rmd160,20), len += 20;
154         for (i=0; i<4; i++)
155             opretbuf[len++] = pax->source[i];
156     }
157     else
158     {
159         memcpy(pax->rmd160,&opretbuf[len],20), len += 20;
160         for (i=0; i<4; i++)
161             pax->source[i] = opretbuf[len++];
162     }
163     return(len);
164 }
165
166 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)
167 {
168     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];
169     incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
170     for (i=0; i<4; i++)
171         base[i] = opretbuf[opretlen-4+i];
172     for (i=0; i<opretlen; i++)
173         printf("%02x",opretbuf[i]);
174     printf(" opretlen.%d (%s)\n",opretlen,base);
175     //printf(" opretlen.%d vs %d incr.%d\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4),incr);
176     if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
177     {
178         type = opretbuf[0];
179         opretbuf++, opretlen--;
180         for (n=0; n<opretlen/incr; n++)
181         {
182             if ( iskomodo != 0 )
183             {
184                 memset(&p,0,sizeof(p));
185                 len += komodo_rwapproval(0,&opretbuf[len],&p);
186                 if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 )
187                 {
188                     txids[n] = p.txid;
189                     vouts[n] = p.vout;
190                     values[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.komodoshis : p.fiatoshis;
191                     srcvalues[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.fiatoshis : p.komodoshis;
192                     kmdheights[n] = p.height;
193                     otherheights[n] = p.otherheight;
194                     memcpy(&rmd160s[n * 20],p.rmd160,20);
195                     baseids[n] = komodo_baseid(p.source);
196                     {
197                         char coinaddr[64];
198                         bitcoin_address(coinaddr,60,&rmd160s[n * 20],20);
199                         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]));
200                     }
201                 }
202             }
203             else
204             {
205                 for (j=0; j<32; j++)
206                 {
207                     ((uint8_t *)&txids[n])[j] = opretbuf[len++];
208                     //printf("%02x",((uint8_t *)&txids[n])[j]);
209                 }
210                 vouts[n] = opretbuf[len++];
211                 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
212                 baseids[n] = komodo_baseid(base);
213                 if ( (pax= komodo_paxfind(txids[n],vouts[n])) != 0 )
214                 {
215                     values[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? pax->komodoshis : pax->fiatoshis;
216                     srcvalues[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? pax->fiatoshis : pax->komodoshis;
217                     kmdheights[n] = pax->height;
218                     otherheights[n] = pax->otherheight;
219                     memcpy(&rmd160s[n * 20],pax->rmd160,20);
220                 }
221             }
222             //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
223         }
224     }
225     return(n);
226 }
227
228 uint64_t komodo_paxtotal()
229 {
230     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;
231     if ( komodo_isrealtime(&ht) == 0 )
232         return(0);
233     else
234     {
235         HASH_ITER(hh,PAX,pax,tmp)
236         {
237             //if ( pax->didstats == 0 && pax->type == 'I' )
238             {
239                 //for (i=0; i<32; i++)
240                 //    printf("%02x",((uint8_t *)&pax->txid)[i]);
241                 if ( pax->type == 'X' || pax->type == 'A' || pax->type == 'D' || pax->type == 'I' )
242                     str = pax->symbol;
243                 else str = pax->source;
244                 basesp = komodo_stateptrget(str);
245                 if ( komodo_baseid(str) >= 0 && pax->didstats == 0 && pax->fiatoshis != 0 )
246                 {
247                     if ( basesp != 0 )
248                     {
249                         if ( pax->type == 'I' )
250                         {
251                             basesp->issued += pax->fiatoshis;
252                             pax->didstats = 1;
253                             printf("Iset dstats %.8f += %.8f\n",dstr(basesp->issued),dstr(pax->fiatoshis));
254                         }
255                     }
256                 }
257                 printf(" stats.%d type.%c (%s) k.%d %.8f h.%d %.8f I.%.8f X.%.8f\n",pax->didstats,pax->type,str,pax->height,dstr(pax->komodoshis),pax->otherheight,dstr(pax->fiatoshis),dstr(basesp->issued),dstr(basesp->redeemed));
258             }
259         }
260     }
261     komodo_stateptr(symbol,dest);
262     HASH_ITER(hh,PAX,pax,tmp)
263     {
264         //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
265         if ( strcmp(symbol,pax->symbol) == 0 )
266         {
267             if ( pax->marked == 0 )
268             {
269                 if ( komodo_is_issuer() != 0 )
270                     total += pax->fiatoshis;
271                 else if ( pax->approved != 0 )
272                 {
273                     if ( pax->validated != 0 )
274                         total += pax->komodoshis;
275                     else
276                     {
277                         seed = 0;
278                         checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis);
279                         //printf("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);
280                         //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight);
281                         if ( seed != 0 )
282                         {
283                             if ( checktoshis >= pax->komodoshis )
284                             {
285                                 total += pax->komodoshis;
286                                 pax->validated = pax->komodoshis;
287                             } else pax->marked = pax->height;
288                         }
289                     }
290                 }
291             }
292         }
293     }
294     //printf("paxtotal %.8f\n",dstr(total));
295     return(total);
296 }
297
298 int32_t komodo_pending_withdraws(char *opretstr)
299 {
300     struct pax_transaction *pax,*tmp; uint8_t opretbuf[16384]; int32_t ht,len=0; uint64_t total = 0;
301     if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
302         return(0);
303     HASH_ITER(hh,PAX,pax,tmp)
304     {
305         //printf("pax %s marked.%u approved.%u\n",pax->symbol,pax->marked,pax->approved);
306         if ( pax->marked == 0 && strcmp((char *)"KMD",pax->symbol) == 0 && pax->approved == 0 )
307         {
308             // add 'A' opreturn entry
309             if ( len == 0 )
310                 opretbuf[len++] = 'A';
311             len += komodo_rwapproval(1,&opretbuf[len],pax);
312             printf("%s.(marked.%u approved.%d) %p\n",pax->source,pax->marked,pax->approved,pax);
313         }
314     }
315     if ( len > 0 )
316         init_hexbytes_noT(opretstr,opretbuf,len);
317     else opretstr[0] = 0;
318     printf("komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal()));
319     return(len);
320 }
321
322 int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
323 {
324     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 mask;
325     sp = komodo_stateptr(symbol,dest);
326     strcpy(symbol,base);
327     PENDING_KOMODO_TX = 0;
328     if ( tokomodo == 0 )
329     {
330         opcode = 'I';
331         if ( komodo_isrealtime(&ht) == 0 )
332             return(0);
333     } else opcode = 'X';
334     HASH_ITER(hh,PAX,pax,tmp)
335     {
336         //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
337         if ( strcmp(symbol,"KMD") == 0 && (pax->approved == 0 || pax->validated == 0) )
338             continue;
339         if ( pax->marked != 0 || strcmp(pax->symbol,symbol) != 0 )
340             continue;
341         if ( ASSETCHAINS_SYMBOL[0] != 0 )
342             printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
343         txNew->vout.resize(numvouts+1);
344         txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
345         txNew->vout[numvouts].scriptPubKey.resize(25);
346         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
347         *script++ = 0x76;
348         *script++ = 0xa9;
349         *script++ = 20;
350         memcpy(script,pax->rmd160,20), script += 20;
351         *script++ = 0x88;
352         *script++ = 0xac;
353         if ( tokomodo == 0 )
354         {
355             for (i=0; i<32; i++)
356                 data[len++] = ((uint8_t *)&pax->txid)[i];
357             data[len++] = pax->vout & 0xff;
358             data[len++] = (pax->vout >> 8) & 0xff;
359             PENDING_KOMODO_TX += pax->fiatoshis;
360         }
361         else
362         {
363             len += komodo_rwapproval(1,&data[len],pax);
364             PENDING_KOMODO_TX += pax->komodoshis;
365             printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX));
366         }
367         if ( numvouts++ >= 64 )
368             break;
369     }
370     if ( numvouts > 1 )
371     {
372         if ( tokomodo != 0 )
373             strcpy(symbol,(char *)"KMD");
374         for (i=0; symbol[i]!=0; i++)
375             data[len++] = symbol[i];
376         data[len++] = 0;
377         opretlen = komodo_opreturnscript(opret,opcode,data,len);
378         txNew->vout.resize(numvouts+1);
379         txNew->vout[numvouts].nValue = 0;
380         txNew->vout[numvouts].scriptPubKey.resize(opretlen);
381         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
382         memcpy(script,opret,opretlen);
383         printf("MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
384         return(1);
385     }
386     return(0);
387 }
388
389 int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
390 {
391     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]; int64_t values[64],srcvalues[64]; struct pax_transaction *pax;
392     memset(baseids,0xff,sizeof(baseids));
393     memset(values,0,sizeof(values));
394     memset(srcvalues,0,sizeof(srcvalues));
395     memset(rmd160s,0,sizeof(rmd160s));
396     memset(kmdheights,0,sizeof(kmdheights));
397     memset(otherheights,0,sizeof(otherheights));
398     n = block.vtx[0].vout.size();
399     script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
400     if ( n <= 2 || script[0] != 0x6a )
401         return(0);
402     offset += komodo_scriptitemlen(&opretlen,&script[offset]);
403     if ( ASSETCHAINS_SYMBOL[0] == 0 )
404     {
405         //for (i=0; i<opretlen; i++)
406         //    printf("%02x",script[i]);
407         //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
408         opcode = 'X';
409         strcpy(symbol,(char *)"KMD");
410     }
411     else
412     {
413         strcpy(symbol,ASSETCHAINS_SYMBOL);
414         opcode = 'I';
415     }
416     if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
417     {
418         if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
419         {
420             for (i=1; i<n-1; i++)
421             {
422                 if ( (pax= komodo_paxfind(txids[i-1],vouts[i-1])) != 0 )
423                 {
424                     pax->type = opcode;
425                     if ( ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) )
426                     {
427                         if ( pax->marked != 0 && height >= 80820 )
428                             errs++;
429                         else matched++;
430                         if ( opcode == 'X' )
431                             printf("errs.%d i.%d match %.8f == %.8f pax.%p\n",errs,i,dstr(pax != 0 ? pax->komodoshis:-1),dstr(block.vtx[0].vout[i].nValue),pax);
432                     }
433                     else
434                     {
435                         if ( opcode == 'X' )
436                         {
437                             for (j=0; j<32; j++)
438                                 printf("%02x",((uint8_t *)&txids[i-1])[j]);
439                             printf(" cant paxfind X txid\n");
440                         }
441                     }
442                 }
443                 else
444                 {
445                     if  ( opcode == 'X' )
446                     {
447                         hash = block.GetHash();
448                         for (j=0; j<32; j++)
449                             printf("%02x",((uint8_t *)&hash)[j]);
450                         printf(" ht.%d blockhash X couldnt find vout.[%d]\n",height,i);
451                     } else if ( opcode == 'I' )
452                         matched++;
453                 }
454             }
455             if ( matched != num )
456             {
457                 // can easily happen depending on order of loading
458                 if ( height > 100000 && opcode == 'X' )
459                 {
460                     printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
461                     return(-1);
462                 }
463             }
464         }
465         //printf("opretlen.%d num.%d\n",opretlen,num);
466     }
467     return(0);
468 }
469
470 int32_t komodo_paxcmp(uint64_t value,uint64_t checkvalue,uint64_t seed)
471 {
472     int32_t ratio;
473     if ( seed == 0 )
474     {
475         ratio = ((value << 10) / checkvalue);
476         if ( ratio >= 1023 && ratio <= 1025 )
477             return(0);
478         else
479         {
480             printf("value %lld vs checkvalue %lld -> ratio.%d\n",(long long)value,(long long)checkvalue,ratio);
481             return(-1);
482         }
483     }
484     return(value != checkvalue);
485 }
486
487 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
488 {
489     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; struct komodo_state *basesp; double diff;
490     const char *typestr = "unknown";
491     memset(baseids,0xff,sizeof(baseids));
492     memset(values,0,sizeof(values));
493     memset(srcvalues,0,sizeof(srcvalues));
494     memset(rmd160s,0,sizeof(rmd160s));
495     memset(kmdheights,0,sizeof(kmdheights));
496     memset(otherheights,0,sizeof(otherheights));
497     tokomodo = (komodo_is_issuer() == 0);
498     if ( opretbuf[0] == 'D' )
499     {
500         if ( opretlen == 38 ) // any KMD tx
501         {
502             iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
503             memset(base,0,sizeof(base));
504             PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
505             bitcoin_address(coinaddr,addrtype,rmd160,20);
506             checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
507             typestr = "deposit";
508             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);
509             if ( kmdheight <= height )
510             {
511                 for (i=0; i<32; i++)
512                     printf("%02x",((uint8_t *)&txid)[i]);
513                 printf(" <- txid.v%u ",vout);
514                 for (i=0; i<33; i++)
515                     printf("%02x",pubkey33[i]);
516                 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
517                 didstats = 0;
518                 if ( komodo_paxcmp(value,checktoshis,seed) == 0 )
519                 {
520                     if ( (pax= komodo_paxfind(txid,vout)) == 0 )
521                     {
522                         if ( (basesp= komodo_stateptrget(base)) != 0 )
523                         {
524                             basesp->deposited += fiatoshis;
525                             pax->didstats = 1;
526                             printf("########### %p deposited %s += %.8f\n",basesp,base,dstr(fiatoshis));
527                         }
528                         //if ( strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) //tokomodo == 0 &&
529                         {
530                             komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,kmdheight,height,(char *)"KMD",0);
531                         }
532                     }
533                     if ( (pax= komodo_paxfind(txid,vout)) != 0 )
534                     {
535                         pax->type = opretbuf[0];
536                         if ( pax->didstats == 0 )
537                         {
538                             if ( (basesp= komodo_stateptrget(base)) != 0 )
539                             {
540                                 basesp->deposited += fiatoshis;
541                                 pax->didstats = 1;
542                                 printf("########### %p deposited %s += %.8f\n",basesp,base,dstr(fiatoshis));
543                             }
544                         }
545                     }
546                 }
547             }
548         }
549     }
550     else if ( opretbuf[0] == 'W' && opretlen >= 38 )
551     {
552         iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
553         memset(base,0,sizeof(base));
554         PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
555         bitcoin_address(coinaddr,addrtype,rmd160,20);
556         checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
557         typestr = "withdraw";
558         printf("%s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s)\n",ASSETCHAINS_SYMBOL,kmdheight,height,dstr(checktoshis),dstr(komodoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr);
559         didstats = 0;
560         if ( komodo_paxcmp(komodoshis,checktoshis,seed) == 0 )
561         {
562             if ( value != 0 && ((pax= komodo_paxfind(txid,vout)) == 0 || pax->didstats == 0) )
563             {
564                 if ( (basesp= komodo_stateptrget(base)) != 0 )
565                 {
566                     basesp->withdrawn += value;
567                     didstats = 1;
568                     printf("########### %p withdrawn %s += %.8f\n",basesp,base,dstr(value));
569                 }
570                 printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
571             }
572             komodo_gateway_deposit(coinaddr,komodoshis,(char *)"KMD",value,rmd160,txid,vout,kmdheight,height,source,0);
573             if ( (pax= komodo_paxfind(txid,vout)) != 0 )
574             {
575                 if ( didstats != 0 )
576                     pax->didstats = 1;
577                 pax->type = opretbuf[0];
578             }
579         }
580     }
581     else if ( tokomodo != 0 && opretbuf[0] == 'A' )
582     {
583         if ( ASSETCHAINS_SYMBOL[0] != 0 )
584         {
585             for (i=0; i<opretlen; i++)
586                 printf("%02x",opretbuf[i]);
587             printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()));
588         }
589         if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
590         {
591             for (i=0; i<n; i++)
592             {
593                 if ( baseids[i] < 0 )
594                     continue;
595                 didstats = 0;
596                 seed = 0;
597                 checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)srcvalues[i]);
598                 printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",kmdheights[i],CURRENCIES[baseids[i]],(double)srcvalues[i]/COIN,(double)values[i]/COIN,(double)checktoshis/COIN);
599                 for (j=0; j<32; j++)
600                     printf("%02x",((uint8_t *)&txids[i])[j]);
601                 printf(" v%d %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),kmdheights[i],otherheights[i],baseids[i]);
602                 if ( (pax= komodo_paxfind(txids[i],vouts[i])) == 0 )
603                 {
604                     bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
605                     komodo_gateway_deposit(coinaddr,values[i],CURRENCIES[baseids[i]],srcvalues[i],&rmd160s[i*20],txids[i],vouts[i],kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]);
606                     if ( srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
607                     {
608                         basesp->approved += srcvalues[i];
609                         didstats = 1;
610                         printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i]));
611                     }
612                     printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i]));
613                 }
614                 else if ( pax->didstats == 0 && srcvalues[i] != 0 )
615                 {
616                     if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
617                     {
618                         basesp->approved += srcvalues[i];
619                         didstats = 1;
620                         printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i]));
621                     }
622                 }
623                 else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
624                 if ( (pax= komodo_paxfind(txids[i],vouts[i])) != 0 )
625                 {
626                     pax->type = opretbuf[0];
627                     pax->approved = kmdheights[i];
628                     if ( didstats != 0 )
629                         pax->didstats = 1;
630                     printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax);
631                 }
632             }
633         }
634         printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
635     }
636     else if ( opretbuf[0] == 'I' )
637     {
638         if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
639         {
640             if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
641             {
642                 for (i=0; i<n; i++)
643                 {
644                     if ( baseids[i] < 0 )
645                     {
646                         printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]);
647                         continue;
648                     }
649                     bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
650                     printf("ISSUE %s %.8f %.8f\n",CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]));
651                     komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],height,0,CURRENCIES[baseids[i]],0);
652                     if ( komodo_paxmark(height,txids[i],vouts[i],height) == 0 )
653                     {
654                     }
655                     if ( (pax= komodo_paxfind(txids[i],vouts[i])) != 0 )
656                     {
657                         pax->type = opretbuf[0];
658                         strcpy(pax->source,(char *)&opretbuf[opretlen-4]);
659                     }
660                 }
661             } else printf("opreturn none issued?\n");
662         } else printf("opreturn matches KMD? %s\n",(char *)&opretbuf[opretlen-4]);
663     }
664     else if ( opretbuf[0] == 'X' )
665     {
666         printf("got X opreturn height.%d\n",height);
667         if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
668         {
669             for (i=0; i<n; i++)
670             {
671                 if ( baseids[i] < 0 )
672                     continue;
673                 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
674                 printf("X i.%d of %d: %.8f -> %s\n",i,n,dstr(values[i]),coinaddr);
675                 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],height,0,(char *)"KMD",0);
676                 if ( komodo_paxmark(height,txids[i],vouts[i],height) == 0 )
677                     ;
678                 if ( (pax= komodo_paxfind(txids[i],vouts[i])) != 0 )
679                 {
680                     pax->type = opretbuf[0];
681                     if ( baseids[i] >= 0 && srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
682                     {
683                         basesp->redeemed += srcvalues[i];
684                         pax->didstats = 1;
685                     }
686                 }
687             }
688         } else printf("komodo_issued_opreturn returned %d\n",n);
689     }
690     return(typestr);
691 }
692
693 void komodo_passport_iteration()
694 {
695     static long lastpos[34]; static char userpass[33][1024];
696     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;
697     while ( KOMODO_INITDONE == 0 )
698     {
699         fprintf(stderr,"PASSPORT iteration waiting for KOMODO_INITDONE\n");
700         sleep(3);
701     }
702     refsp = komodo_stateptr(symbol,dest);
703     if ( ASSETCHAINS_SYMBOL[0] == 0 )
704         refid = 33;
705     else refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
706     //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
707     for (baseid=0; baseid<=32; baseid++)
708     {
709         sp = 0;
710         isrealtime = 0;
711         base = (char *)CURRENCIES[baseid];
712 if ( strcmp("EUR",base) != 0 && baseid < 32 )
713     continue;
714         if ( baseid+1 != refid )
715         {
716             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
717             komodo_nameset(symbol,dest,base);
718             //port = komodo_port(base,10,&magic) + 1;
719             if ( (fp= fopen(fname,"rb")) != 0 && (sp= komodo_stateptrget(symbol)) != 0 )
720             {
721                 fseek(fp,0,SEEK_END);
722                 if ( ftell(fp) > lastpos[baseid] )
723                 {
724                     //printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
725                     fseek(fp,lastpos[baseid],SEEK_SET);
726                     while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
727                         ;
728                     lastpos[baseid] = ftell(fp);
729                     //printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]);
730                 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
731                 fclose(fp);
732             }
733             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
734             if ( (fp= fopen(fname,"rb")) != 0 )
735             {
736                 if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
737                 {
738                     sp->CURRENT_HEIGHT = buf[0];
739                     if ( buf[0] != 0 && buf[0] == buf[1] && buf[2] > time(NULL)-60 )
740                     {
741                         isrealtime = 1;
742                         RTmask |= (1LL << baseid);
743                         memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
744                     } //else fprintf(stderr,"%s not RT\n",base);
745                 } //else fprintf(stderr,"%s size error RT\n",base);
746                 fclose(fp);
747             } else fprintf(stderr,"%s open error RT\n",base);
748         }
749         else
750         {
751             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
752             if ( (fp= fopen(fname,"wb")) != 0 )
753             {
754                 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
755                 buf[1] = (uint32_t)komodo_longestchain();
756                 if ( buf[0] != 0 && buf[0] == buf[1] )
757                 {
758                     buf[2] = (uint32_t)time(NULL);
759                     RTmask |= (1LL << baseid) | 1;
760                     memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
761                     if ( refid != 0 )
762                         memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
763                 }
764                 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
765                     fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
766                 fclose(fp);
767             } else fprintf(stderr,"%s create error RT\n",base);
768         }
769         if ( sp != 0 && isrealtime == 0 )
770             refsp->RTbufs[0][2] = 0;
771     }
772     refsp->RTmask = RTmask;
773     KOMODO_PASSPORT_INITDONE = 1;
774 }
775
This page took 0.070634 seconds and 4 git commands to generate.