]> 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 struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout)
19 {
20     struct pax_transaction *pax;
21     pthread_mutex_lock(&komodo_mutex);
22     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
23     pthread_mutex_unlock(&komodo_mutex);
24     return(pax);
25 }
26
27 struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,int32_t mark)
28 {
29     struct pax_transaction *pax;
30     pthread_mutex_lock(&komodo_mutex);
31     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
32     if ( pax == 0 )
33     {
34         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
35         pax->txid = txid;
36         pax->vout = vout;
37         HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
38         //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
39     }
40     if ( pax != 0 )
41     {
42         pax->marked = mark;
43         //int32_t i; for (i=0; i<32; i++)
44         //    printf("%02x",((uint8_t *)&txid)[i]);
45         //printf(" paxmark.ht %d vout%d\n",mark,vout);
46     }
47     pthread_mutex_unlock(&komodo_mutex);
48     return(pax);
49 }
50
51 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
52 {
53     struct pax_transaction *pax; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s;
54     sp = komodo_stateptr(str,dest);
55     pthread_mutex_lock(&komodo_mutex);
56     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
57     if ( pax == 0 )
58     {
59         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
60         pax->txid = txid;
61         pax->vout = vout;
62         HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
63         addflag = 1;
64         if ( ASSETCHAINS_SYMBOL[0] == 0 )
65         {
66             int32_t i; for (i=0; i<32; i++)
67                 printf("%02x",((uint8_t *)&txid)[i]);
68             printf(" v.%d [%s] kht.%d ht.%d create pax.%p\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax);
69         }
70     }
71     pthread_mutex_unlock(&komodo_mutex);
72     if ( coinaddr != 0 )
73     {
74         strcpy(pax->coinaddr,coinaddr);
75         if ( value != 0 )
76             pax->komodoshis = value;
77         if ( symbol != 0 )
78             strcpy(pax->symbol,symbol);
79         if ( source != 0 )
80             strcpy(pax->source,source);
81         if ( fiatoshis != 0 )
82             pax->fiatoshis = fiatoshis;
83         if ( rmd160 != 0 )
84             memcpy(pax->rmd160,rmd160,20);
85         if ( height != 0 )
86             pax->height = height;
87         if ( otherheight != 0 )
88             pax->otherheight = otherheight;
89         if ( pax->marked == 0 )
90         {
91             if ( addflag != 0 )
92             {
93                 if ( (pax->approved= approved) != 0 )
94                     s = (char *)"APPROVED";
95                 else s = (char *)(ASSETCHAINS_SYMBOL[0]==0?"WITHDRAW":"DEPOSIT");
96                 printf("[%s] addflag.%d ADD %s %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,s,symbol,dstr(ASSETCHAINS_SYMBOL[0]==0?pax->komodoshis:pax->fiatoshis),pax->coinaddr,pax->height,pax->otherheight,dstr(komodo_paxtotal()));
97             }
98         }
99         //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);
100     }
101     else
102     {
103         pax->marked = height;
104         printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
105     }
106 }
107
108 int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax)
109 {
110     int32_t i,len = 0;
111     if ( rwflag == 1 )
112     {
113         for (i=0; i<32; i++)
114             opretbuf[len++] = ((uint8_t *)&pax->txid)[i];
115         opretbuf[len++] = pax->vout & 0xff;
116         opretbuf[len++] = (pax->vout >> 8) & 0xff;
117     }
118     else
119     {
120         for (i=0; i<32; i++)
121             ((uint8_t *)&pax->txid)[31-i] = opretbuf[len++];
122         for (i=0; i<32; i++)
123             printf("%02x",((uint8_t *)&pax->txid)[i]);
124         pax->vout = opretbuf[len++];
125         pax->vout += ((uint32_t)opretbuf[len++] << 8);
126         printf(" txid v.%d\n",pax->vout);
127     }
128     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis);
129     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
130     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height);
131     len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight);
132     if ( rwflag != 0 )
133     {
134         memcpy(&opretbuf[len],pax->rmd160,20), len += 20;
135         for (i=0; i<4; i++)
136             opretbuf[len++] = pax->source[i];
137     }
138     else
139     {
140         memcpy(pax->rmd160,&opretbuf[len],20), len += 20;
141         for (i=0; i<4; i++)
142             pax->source[i] = opretbuf[len++];
143     }
144     return(len);
145 }
146
147 int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
148 {
149     struct pax_transaction p; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t rmd160[20]; uint64_t fiatoshis; char symbol[16];
150     for (i=0; i<4; i++)
151         base[i] = opretbuf[opretlen-4+i];
152     if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
153     {
154         opretbuf++, opretlen--;
155         incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
156         for (n=0; n<opretlen/incr; n++)
157         {
158             //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
159             if ( iskomodo != 0 )
160             {
161                 memset(&p,0,sizeof(p));
162                 len += komodo_rwapproval(0,&opretbuf[len],&p);
163                 if ( values != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 )
164                 {
165                     values[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.komodoshis : p.fiatoshis;
166                     kmdheights[n] = p.height;
167                     otherheights[n] = p.otherheight;
168                     memcpy(&rmd160s[n * 20],p.rmd160,20);
169                     baseids[n] = komodo_baseid(p.source);
170                     {
171                         char coinaddr[64];
172                         bitcoin_address(coinaddr,60,&rmd160s[n * 20],20);
173                         printf(">>>>>>> approved A: (%s) fiat %.8f kmdheight.%d other.%d -> %s %.8f\n",baseids[n]>=0?CURRENCIES[baseids[n]]:"???",dstr(p.komodoshis),kmdheights[n],otherheights[n],coinaddr,dstr(values[n]));
174                     }
175                 }
176             }
177             else
178             {
179                 for (j=0; j<32; j++)
180                 {
181                     ((uint8_t *)&txids[n])[j] = opretbuf[len++];
182                     //printf("%02x",((uint8_t *)&txids[n])[j]);
183                 }
184                 vouts[n] = opretbuf[len++];
185                 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
186             }
187         }
188     }
189     return(n);
190 }
191
192 uint64_t komodo_paxtotal()
193 {
194     struct pax_transaction *pax,*tmp; char symbol[16],dest[16]; int32_t ht; uint64_t total = 0;
195     if ( komodo_isrealtime(&ht) == 0 )
196         return(0);
197     komodo_stateptr(symbol,dest);
198     HASH_ITER(hh,PAX,pax,tmp)
199     {
200         //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
201         if ( strcmp(symbol,pax->symbol) == 0 )
202         {
203             if ( pax->marked == 0 )
204             {
205                 if ( komodo_is_issuer() != 0 )
206                     total += pax->fiatoshis;
207                 else if ( pax->approved != 0 )
208                     total += pax->komodoshis;
209             }
210         }
211     }
212     //printf("paxtotal %.8f\n",dstr(total));
213     return(total);
214 }
215
216 int32_t komodo_pending_withdraws(char *opretstr)
217 {
218     struct pax_transaction *pax,*tmp; uint8_t opretbuf[16384]; int32_t ht,len=0; uint64_t total = 0;
219     if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
220         return(0);
221     HASH_ITER(hh,PAX,pax,tmp)
222     {
223         //printf("pax %s marked.%u approved.%u\n",pax->symbol,pax->marked,pax->approved);
224         if ( pax->marked == 0 && strcmp((char *)"KMD",pax->symbol) == 0 && pax->approved == 0 )
225         {
226             // add 'A' opreturn entry
227             if ( len == 0 )
228                 opretbuf[len++] = 'A';
229             len += komodo_rwapproval(1,&opretbuf[len],pax);
230             printf("%s.(marked.%u approved.%d) %p\n",pax->source,pax->marked,pax->approved,pax);
231         }
232     }
233     if ( len > 0 )
234         init_hexbytes_noT(opretstr,opretbuf,len);
235     else opretstr[0] = 0;
236     return(len);
237 }
238
239 int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
240 {
241     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;
242     sp = komodo_stateptr(symbol,dest);
243     strcpy(symbol,base);
244     PENDING_KOMODO_TX = 0;
245     if ( tokomodo == 0 )
246     {
247         opcode = 'I';
248         if ( komodo_isrealtime(&ht) == 0 )
249             return(0);
250     }
251     else opcode = 'X';
252     HASH_ITER(hh,PAX,pax,tmp)
253     {
254         //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
255         if ( pax->marked != 0 || strcmp(pax->symbol,symbol) != 0 || (strcmp(symbol,"KMD") == 0 && pax->approved == 0) )
256             continue;
257         //if ( ASSETCHAINS_SYMBOL[0] != 0 )
258             printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
259         txNew->vout.resize(numvouts+1);
260         txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
261         txNew->vout[numvouts].scriptPubKey.resize(25);
262         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
263         *script++ = 0x76;
264         *script++ = 0xa9;
265         *script++ = 20;
266         memcpy(script,pax->rmd160,20), script += 20;
267         *script++ = 0x88;
268         *script++ = 0xac;
269         for (i=0; i<32; i++)
270         {
271             //printf("%02x",((uint8_t *)&pax->txid)[i]);
272             data[len++] = ((uint8_t *)&pax->txid)[i];
273         }
274         data[len++] = pax->vout & 0xff;
275         data[len++] = (pax->vout >> 8) & 0xff;
276         if ( tokomodo == 0 )
277             PENDING_KOMODO_TX += pax->fiatoshis;
278         else
279         {
280             //[{"prev_hash":"5d5c9a49489b558de9e84f991f996dedaae6b9d0f157f82b2fec64662476d5cf","prev_vout":2,"EUR":0.10000000,"fiat":"EUR","kmdheight":57930,"height":153,"KMD":0.78329000,"address":"RDhEGYScNQYetCyG75Kf8Fg61UWPdwc1C5","rmd160":"306c507eea639e7220b3069ed9f49f3bc97eaca1"}]
281             len += iguana_rwnum(1,&data[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
282             len += iguana_rwnum(1,&data[len],sizeof(pax->height),&pax->height);
283             len += iguana_rwnum(1,&data[len],sizeof(pax->otherheight),&pax->otherheight);
284             for (i=0; pax->symbol[i]!=0&&i<3; i++) // must be 3 letter currency
285                 data[len++] = pax->symbol[i];
286             data[len++] = 0;
287             PENDING_KOMODO_TX += pax->komodoshis;
288             printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX));
289         }
290         if ( numvouts++ >= 64 )
291             break;
292     }
293     if ( numvouts > 1 )
294     {
295         if ( tokomodo != 0 )
296             strcpy(symbol,(char *)"KMD");
297         for (i=0; symbol[i]!=0; i++)
298             data[len++] = symbol[i];
299         data[len++] = 0;
300         opretlen = komodo_opreturnscript(opret,opcode,data,len);
301         txNew->vout.resize(numvouts+1);
302         txNew->vout[numvouts].nValue = 0;
303         txNew->vout[numvouts].scriptPubKey.resize(opretlen);
304         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
305         memcpy(script,opret,opretlen);
306         printf("MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
307         return(1);
308     }
309     return(0);
310 }
311
312 int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
313 {
314     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]; struct pax_transaction *pax;
315     memset(baseids,0xff,sizeof(baseids));
316     memset(values,0,sizeof(values));
317     memset(rmd160s,0,sizeof(rmd160s));
318     memset(kmdheights,0,sizeof(kmdheights));
319     memset(otherheights,0,sizeof(otherheights));
320     n = block.vtx[0].vout.size();
321     script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
322     if ( n <= 2 || script[0] != 0x6a )
323         return(0);
324     offset += komodo_scriptitemlen(&opretlen,&script[offset]);
325     if ( ASSETCHAINS_SYMBOL[0] == 0 )
326     {
327         //for (i=0; i<opretlen; i++)
328         //    printf("%02x",script[i]);
329         //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
330         opcode = 'X';
331         strcpy(symbol,(char *)"KMD");
332     }
333     else
334     {
335         strcpy(symbol,ASSETCHAINS_SYMBOL);
336         opcode = 'I';
337     }
338     if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
339     {
340         if ( (num= komodo_issued_opreturn(base,txids,vouts,values,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
341         {
342             for (i=1; i<n-1; i++)
343             {
344                 if ( (pax= komodo_paxfind(txids[i-1],vouts[i-1])) != 0 )
345                 {
346                     if ( ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) )
347                     {
348                         if ( pax->marked != 0 )
349                             errs++;
350                         else matched++;
351                         if ( 0 && opcode == 'X' )
352                             printf("errs.%d i.%d match %.8f == %.8f\n",errs,i,dstr(pax != 0 ? pax->fiatoshis:-1),dstr(block.vtx[0].vout[i].nValue));
353                         komodo_paxmark(height,txids[i-1],vouts[i-1],height);
354                     }
355                     else
356                     {
357                         hash = block.GetHash();
358                         if ( opcode == 'X' )
359                         {
360                             for (j=0; j<32; j++)
361                                 printf("%02x",((uint8_t *)&hash)[j]);
362                             printf(" ht.%d blockhash X couldnt find vout.[%d]\n",height,i);
363                             // validate amount! via fiat chain
364                         }
365                     }
366                 }
367                 else
368                 {
369                     if  ( opcode == 'X' )
370                     {
371                         matched++;
372                         for (j=0; j<32; j++)
373                             printf("%02x",((uint8_t *)&txids[i-1])[j]);
374                         printf(" cant paxfind X txid\n");
375                         // validate amount! via fiat chain
376                     } else if ( opcode == 'I' )
377                         matched++;
378                 }
379             }
380             if ( matched != num )
381             {
382                 // can easily happen depending on order of loading
383                 if ( height > 60000 )
384                     printf("WARNING: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
385             }
386         }
387         //printf("opretlen.%d num.%d\n",opretlen,num);
388     }
389     return(0);
390 }
391
392 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
393 {
394     uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t 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]; struct pax_transaction *pax;
395     const char *typestr = "unknown";
396     memset(baseids,0xff,sizeof(baseids));
397     memset(values,0,sizeof(values));
398     memset(rmd160s,0,sizeof(rmd160s));
399     memset(kmdheights,0,sizeof(kmdheights));
400     memset(otherheights,0,sizeof(otherheights));
401     tokomodo = (komodo_is_issuer() == 0);
402     if ( opretbuf[0] == 'D' )
403     {
404         if ( opretlen == 38 ) // any KMD tx
405         {
406             iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
407             memset(base,0,sizeof(base));
408             PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
409             bitcoin_address(coinaddr,addrtype,rmd160,20);
410             checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
411             typestr = "deposit";
412             printf("%s kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",ASSETCHAINS_SYMBOL,kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
413             if ( kmdheight <= height )
414             {
415                 if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
416                 {
417                     for (i=0; i<32; i++)
418                         printf("%02x",((uint8_t *)&txid)[i]);
419                     printf(" <- txid.v%u ",vout);
420                     for (i=0; i<33; i++)
421                         printf("%02x",pubkey33[i]);
422                     printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
423                     if ( value >= checktoshis-(checktoshis >> 10) )
424                     {
425                         if ( komodo_paxfind(txid,vout) == 0 )
426                         {
427                             komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,kmdheight,height,(char *)"KMD",0);
428                         } else printf("duplicate deposit\n");
429                     }
430                 }
431             }
432         }
433     }
434     else if ( opretbuf[0] == 'W' && opretlen >= 38 )
435     {
436         iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
437         memset(base,0,sizeof(base));
438         PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
439         bitcoin_address(coinaddr,addrtype,rmd160,20);
440         checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
441         typestr = "withdraw";
442         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);
443         if ( checktoshis <= komodoshis+(komodoshis >> 10) )
444         {
445             if ( (pax= komodo_paxfind(txid,vout)) == 0 )
446             {
447                 printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
448             }
449             komodo_gateway_deposit(coinaddr,komodoshis,(char *)"KMD",value,rmd160,txid,vout,kmdheight,height,source,0);
450         }
451     }
452     else if ( tokomodo != 0 && opretbuf[0] == 'A' )
453     {
454         //if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
455         {
456             for (i=0; i<opretlen; i++)
457                 printf("%02x",opretbuf[i]);
458             printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()));
459         }
460         printf("extra 'A' opret[%d]\n",opretlen);
461         if ( (n= komodo_issued_opreturn(base,txids,vouts,values,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
462         {
463             for (i=0; i<n; i++)
464             {
465                 for (j=0; j<32; j++)
466                     printf("%02x",((uint8_t *)&txids[i])[j]);
467                 printf(" v%d %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),kmdheights[i],otherheights[i],baseids[i]);
468                 if ( (pax= komodo_paxfind(txids[i],vouts[i])) == 0 && baseids[i] >= 0 )
469                 {
470                     bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
471                     komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]);
472                     printf(" i.%d (%s) <- %.8f\n",i,coinaddr,dstr(values[i]));
473                 } else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
474                 if ( (pax= komodo_paxfind(txids[i],vouts[i])) != 0 )
475                 {
476                     pax->approved = kmdheights[i];
477                     printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax);
478                 }
479             }
480         }
481         printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
482     }
483     else if ( tokomodo == 0 && opretbuf[0] == 'I' )
484     {
485         if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
486         {
487             if ( (n= komodo_issued_opreturn(base,txids,vouts,values,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
488             {
489                 for (i=0; i<n; i++)
490                 {
491                     bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
492                     if ( komodo_paxmark(height,txids[i],vouts[i],height) == 0 && baseids[i] >= 0 )
493                         komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],height,0,CURRENCIES[baseids[i]],0);
494                 }
495             }
496         }
497     }
498     return(typestr);
499 }
500
501 void komodo_passport_iteration()
502 {
503     static long lastpos[34]; static char userpass[33][1024];
504     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;
505     while ( KOMODO_INITDONE == 0 )
506     {
507         fprintf(stderr,"PASSPORT iteration waiting for KOMODO_INITDONE\n");
508         sleep(3);
509     }
510     refsp = komodo_stateptr(symbol,dest);
511     if ( ASSETCHAINS_SYMBOL[0] == 0 )
512         refid = 33;
513     else refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
514     //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
515     for (baseid=0; baseid<=32; baseid++)
516     {
517         sp = 0;
518         isrealtime = 0;
519         base = (char *)CURRENCIES[baseid];
520         if ( baseid+1 != refid )
521         {
522             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
523             komodo_nameset(symbol,dest,base);
524             //port = komodo_port(base,10,&magic) + 1;
525             if ( (fp= fopen(fname,"rb")) != 0 && (sp= komodo_stateptrget(symbol)) != 0 )
526             {
527                 fseek(fp,0,SEEK_END);
528                 if ( ftell(fp) > lastpos[baseid] )
529                 {
530                     //printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
531                     fseek(fp,lastpos[baseid],SEEK_SET);
532                     while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
533                         ;
534                     lastpos[baseid] = ftell(fp);
535                     //printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]);
536                 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
537                 fclose(fp);
538             }
539             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
540             if ( (fp= fopen(fname,"rb")) != 0 )
541             {
542                 if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
543                 {
544                     sp->CURRENT_HEIGHT = buf[0];
545                     if ( buf[0] != 0 && buf[0] == buf[1] && buf[2] > time(NULL)-60 )
546                     {
547                         isrealtime = 1;
548                         RTmask |= (1LL << baseid);
549                         memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
550                     } //else fprintf(stderr,"%s not RT\n",base);
551                 } //else fprintf(stderr,"%s size error RT\n",base);
552                 fclose(fp);
553             } else fprintf(stderr,"%s open error RT\n",base);
554         }
555         else
556         {
557             komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
558             if ( (fp= fopen(fname,"wb")) != 0 )
559             {
560                 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
561                 buf[1] = (uint32_t)komodo_longestchain();
562                 if ( buf[0] != 0 && buf[0] == buf[1] )
563                 {
564                     buf[2] = (uint32_t)time(NULL);
565                     RTmask |= (1LL << baseid) | 1;
566                     memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
567                     if ( refid != 0 )
568                         memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
569                 }
570                 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
571                     fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
572                 fclose(fp);
573             } else fprintf(stderr,"%s create error RT\n",base);
574         }
575         if ( sp != 0 && isrealtime == 0 )
576             refsp->RTbufs[0][2] = 0;
577     }
578     refsp->RTmask = RTmask;
579     KOMODO_PASSPORT_INITDONE = 1;
580 }
581
This page took 0.060357 seconds and 4 git commands to generate.