]> Git Repo - VerusCoin.git/blob - src/komodo_gateway.h
48b0832037f209269b9257a7a90e0b3d5835bf7d
[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
19 {
20     UT_hash_handle hh;
21     uint256 txid;
22     uint64_t komodoshis,fiatoshis;
23     int32_t marked,height;
24     uint16_t vout;
25     char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag;
26 } *PAX;
27
28 void komodo_gateway_deposit(char *coinaddr,uint64_t value,int32_t shortflag,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height) // assetchain context
29 {
30     struct pax_transaction *pax;
31     pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
32     if ( coinaddr != 0 )
33     {
34         strcpy(pax->coinaddr,coinaddr);
35         pax->komodoshis = value;
36         pax->shortflag = shortflag;
37         strcpy(pax->symbol,symbol);
38         pax->fiatoshis = fiatoshis;
39         memcpy(pax->rmd160,rmd160,20);
40         pax->height = height;
41         printf("ADD DEPOSIT %s %.8f -> %s TO PAX ht.%d\n",symbol,dstr(fiatoshis),coinaddr,height);
42     }
43     else
44     {
45         pax->marked = height;
46         printf("MARK DEPOSIT ht.%d\n",height);
47     }
48     pax->txid = txid;
49     pax->vout = vout;
50     pthread_mutex_lock(&komodo_mutex);
51     HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
52     pthread_mutex_unlock(&komodo_mutex);
53     KOMODO_DEPOSIT += fiatoshis;
54 }
55
56 struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout)
57 {
58     struct pax_transaction *pax;
59     pthread_mutex_lock(&komodo_mutex);
60     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
61     if ( pax != 0 )
62         memcpy(space,pax,sizeof(*pax));
63     pthread_mutex_unlock(&komodo_mutex);
64     return(pax);
65 }
66
67 struct pax_transaction *komodo_paxmark(struct pax_transaction *space,uint256 txid,uint16_t vout,int32_t mark)
68 {
69     struct pax_transaction *pax;
70     pthread_mutex_lock(&komodo_mutex);
71     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
72     if ( pax != 0 )
73     {
74         pax->marked = mark;
75         memcpy(space,pax,sizeof(*pax));
76     }
77     pthread_mutex_unlock(&komodo_mutex);
78     return(pax);
79 }
80
81 int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uint16_t *vouts,uint8_t *opretbuf,int32_t opretlen)
82 {
83     int32_t i,n=0,j,len;
84     if ( opretbuf[opretlen-5] == '-' )
85         *shortflagp = 1;
86     else *shortflagp = 0;
87     for (i=0; i<4; i++)
88         base[i] = opretbuf[opretlen-4+i];
89     if ( strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) // shortflag
90     {
91         //printf("BASE.(%s) vs (%s)\n",base,ASSETCHAINS_SYMBOL);
92         opretbuf++, opretlen--;
93         for (n=len=0; n<opretlen/34; n++)
94         {
95             for (j=0; j<32; j++)
96             {
97                 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
98                 //printf("%02x",((uint8_t *)&txids[n])[j]);
99             }
100             vouts[n] = opretbuf[len++];
101             vouts[n] = (opretbuf[len++] << 8) | vouts[n];
102             //printf(" issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
103         }
104     }
105     return(n);
106 }
107
108 void komodo_gateway_deposits(CMutableTransaction *txNew)
109 {
110     struct pax_transaction *pax,*tmp; uint8_t *script,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1;
111     PENDING_KOMODO_TX = 0;
112     HASH_ITER(hh,PAX,pax,tmp)
113     {
114         if ( pax->marked != 0 )
115             continue;
116         txNew->vout.resize(numvouts+1);
117         txNew->vout[numvouts].nValue = pax->fiatoshis;
118         txNew->vout[numvouts].scriptPubKey.resize(25);
119         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
120         *script++ = 0x76;
121         *script++ = 0xa9;
122         *script++ = 20;
123         memcpy(script,pax->rmd160,20), script += 20;
124         *script++ = 0x88;
125         *script++ = 0xac;
126         for (i=0; i<32; i++)
127         {
128             printf("%02x",((uint8_t *)&pax->txid)[i]);
129             data[len++] = ((uint8_t *)&pax->txid)[i];
130         }
131         data[len++] = pax->vout & 0xff;
132         data[len++] = (pax->vout >> 8) & 0xff;
133         printf(" vout.%u DEPOSIT %.8f <- paxdeposit.%s\n",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,ASSETCHAINS_SYMBOL);
134         PENDING_KOMODO_TX += pax->fiatoshis;
135         if ( numvouts++ >= 64 )
136             break;
137     }
138     if ( numvouts > 1 )
139     {
140         if ( ASSETCHAINS_SHORTFLAG != 0 )
141             data[len++] = '-';
142         for (i=0; ASSETCHAINS_SYMBOL[i]!=0; i++)
143             data[len++] = ASSETCHAINS_SYMBOL[i];
144         data[len++] = 0;
145         opretlen = komodo_opreturnscript(opret,'I',data,len);
146         txNew->vout.resize(numvouts+1);
147         txNew->vout[numvouts].nValue = 0;
148         txNew->vout[numvouts].scriptPubKey.resize(opretlen);
149         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
150         memcpy(script,opret,opretlen);
151         printf("total numvouts.%d %.8f opretlen.%d\n",numvouts,dstr(PENDING_KOMODO_TX),opretlen);
152     } else KOMODO_DEPOSIT = 0;
153 }
154
155 int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
156 {
157     int32_t i,j,n,num,opretlen,offset=1; uint256 hash,txids[64]; uint8_t shortflag; char base[16]; uint16_t vouts[64]; uint8_t *script; struct pax_transaction *pax,space;
158     n = block.vtx[0].vout.size();
159     script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
160     if ( n <= 2 || script[0] != 0x6a )
161         return(0);
162     offset += komodo_scriptitemlen(&opretlen,&script[offset]);
163     //printf("checkdeposit n.%d [%02x] [%c] %d vs %d\n",n,script[0],script[offset],script[offset],'I');
164     if ( script[offset] == 'I' && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
165     {
166         if ( (num= komodo_issued_opreturn(&shortflag,base,txids,vouts,&script[offset],opretlen)) > 0 )
167         {
168             for (i=1; i<n-1; i++)
169             {
170                 if ( (pax= komodo_paxfind(&space,txids[i-1],vouts[i-1])) == 0 || pax->fiatoshis != block.vtx[0].vout[i].nValue )
171                     break;
172             }
173             if ( i != n-1 )
174             {
175                 hash = block.GetHash();
176                 for (j=0; j<32; j++)
177                     printf("%02x",((uint8_t *)&hash)[j]);
178                 printf(" ht.%d blockhash couldnt find vout.[%d]\n",height,i);
179                 //return(-1);
180             }
181         }
182         //printf("opretlen.%d num.%d\n",opretlen,num);
183     }
184     return(0);
185 }
186
187 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout)
188 {
189     uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo=0; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; int64_t fiatoshis,checktoshis; const char *typestr = "unknown";
190     tokomodo = (komodo_is_issuer() == 0);
191     if ( ASSETCHAINS_SYMBOL[0] != 0 )
192     {
193         for (i=0; i<opretlen; i++)
194             printf("%02x",opretbuf[i]);
195         printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo);
196     }
197     if ( opretbuf[0] == ((tokomodo == 0) ? 'D' : 'W') )
198     {
199         if ( opretlen == 34 )
200         {
201             memset(base,0,sizeof(base));
202             PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
203             if ( fiatoshis < 0 )
204                 fiatoshis = -fiatoshis;
205             bitcoin_address(coinaddr,addrtype,rmd160,20);
206             checktoshis = PAX_fiatdest(tokomodo,destaddr,pubkey33,coinaddr,height,base,fiatoshis);
207             typestr = "deposit";
208             if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
209             {
210                 for (i=0; i<32; i++)
211                     printf("%02x",((uint8_t *)&txid)[i]);
212                 printf(" <- txid.v%u ",vout);
213                 for (i=0; i<33; i++)
214                     printf("%02x",pubkey33[i]);
215                 printf(" checkpubkey check %.8f v %.8f dest.(%s) height.%d\n",dstr(checktoshis),dstr(value),destaddr,height);
216                 if ( value >= checktoshis && shortflag == ASSETCHAINS_SHORTFLAG )
217                 {
218                     komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height);
219                 }
220             }
221             else
222             {
223                 if ( value <= checktoshis )
224                 {
225                     
226                 }
227             }
228         }
229     }
230     else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
231     {
232         if ( tokomodo == 0 && opretbuf[0] == 'I' )
233         {
234             if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG )
235             {
236                 for (i=0; i<n; i++)
237                 {
238                     for (j=0; j<32; j++)
239                         printf("%02x",((uint8_t *)&txids[i])[j]);
240                     printf(" issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen);
241                     if ( komodo_paxmark(&space,txids[i],vouts[i],height) == 0 )
242                     {
243                         printf("%s queue remove deposit\n",ASSETCHAINS_SYMBOL);
244                         komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height);
245                     }
246                 }
247             }
248         }
249     }
250     return(typestr);
251 }
252
253 void komodo_gateway_voutupdate(char *symbol,int32_t isspecial,int32_t height,int32_t txi,bits256 txid,int32_t vout,int32_t numvouts,uint64_t value,uint8_t *script,int32_t len)
254 {
255     int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr;
256     typestr = "unknown";
257     memcpy(&utxid,&txid,sizeof(utxid));
258     if ( 0 )//txi != 0 || vout != 0 )
259     {
260         for (i=0; i<len; i++)
261             printf("%02x",script[i]);
262         printf(" <- %s VOUTUPDATE.%d txi.%d vout.%d %.8f scriptlen.%d OP_RETURN.%d (%s) len.%d\n",symbol,height,txi,vout,dstr(value),len,script[0] == 0x6a,typestr,opretlen);
263     }
264     if ( script[offset++] == 0x6a )
265     {
266         offset += komodo_scriptitemlen(&opretlen,&script[offset]);
267         if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 )
268             typestr = "notarized";
269         else if ( txi == 0 && vout == 1 && opretlen == 149 )
270         {
271             typestr = "pricefeed";
272             komodo_paxpricefeed(height,&script[offset],opretlen);
273             //printf("height.%d pricefeed len.%d\n",height,opretlen);
274         }
275         else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout);
276     }
277     else if ( numvouts > 13 )
278         typestr = "ratify";
279 }
280
281 int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
282 {
283     char *retstr,params[256],*hexstr; uint8_t script[10000]; cJSON *json,*result,*vouts,*item,*sobj; int32_t vout,n,len,isspecial,retval = -1; uint64_t value; bits256 txid;
284     sprintf(params,"[\"%s\", 1]",txidstr);
285     if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 )
286     {
287         if ( (json= cJSON_Parse(retstr)) != 0 )
288         {
289             if ( (result= jobj(json,(char *)"result")) != 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
290             {
291                 retval = 0;
292                 isspecial = 0;
293                 txid = jbits256(result,(char *)"txid");
294                 for (vout=0; vout<n; vout++)
295                 {
296                     item = jitem(vouts,vout);
297                     value = SATOSHIDEN * jdouble(item,(char *)"value");
298                     if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
299                     {
300                         if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
301                         {
302                             len = (int32_t)strlen(hexstr) >> 1;
303                             if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) )
304                                 isspecial = 1;
305                             else if ( len <= sizeof(script) )
306                             {
307                                 decode_hex(script,len,hexstr);
308                                 komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len);
309                             }
310                         }
311                     }
312                 }
313             } else printf("error getting txids.(%s)\n",retstr);
314             free_json(json);
315         }
316         free(retstr);
317     }
318     return(retval);
319 }
320
321 int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
322 {
323     char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2;
324     sprintf(params,"[%d]",height);
325     if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 )
326     {
327         if ( (result= cJSON_Parse(retstr)) != 0 )
328         {
329             if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
330             {
331                 sprintf(params,"[\"%s\"]",txidstr);
332                 if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
333                 {
334                     //printf("getblock.(%s)\n",retstr2);
335                     if ( (json= cJSON_Parse(retstr2)) != 0 )
336                     {
337                         if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
338                         {
339                             for (i=0; i<n; i++)
340                                 if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
341                                     break;
342                             if ( i == n )
343                                 retval = 0;
344                             else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n);
345                         } else printf("cant get result.%p or tx.%p\n",result,tx);
346                         free_json(json);
347                     } else printf("cant parse2.(%s)\n",retstr2);
348                     free(retstr2);
349                 } else printf("error getblock %s\n",params);
350             } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
351             free_json(result);
352         } else printf("couldnt parse.(%s)\n",retstr);
353         free(retstr);
354     } else printf("error from getblockhash %d\n",height);
355     return(retval);
356 }
357
358 void komodo_gateway_iteration(char *symbol)
359 {
360     char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
361     if ( KMDHEIGHT <= 0 )
362         KMDHEIGHT = 1;
363     KOMODO_REALTIME = 0;
364     if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
365     {
366         if ( (infoobj= cJSON_Parse(retstr)) != 0 )
367         {
368             if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
369             {
370                 for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
371                 {
372                     if ( (KMDHEIGHT % 100) == 0 )
373                     {
374                         fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT);
375                         memset(&zero,0,sizeof(zero));
376                         komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0);
377                     }
378                     if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
379                     {
380                         printf("error KMDHEIGHT %d\n",KMDHEIGHT);
381                         break;
382                     }
383                     usleep(10000);
384                 }
385                 if ( KMDHEIGHT >= kmdheight )
386                     KOMODO_REALTIME = (uint32_t)time(NULL);
387             }
388             free_json(infoobj);
389         }
390         free(retstr);
391     }
392     else
393     {
394         printf("error from %s\n",symbol);
395         sleep(30);
396     }
397 }
This page took 0.039295 seconds and 2 git commands to generate.