1 /******************************************************************************
2 * Copyright © 2014-2016 The SuperNET Developers. *
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. *
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 *
12 * Removal or modification of this copyright notice is prohibited. *
14 ******************************************************************************/
16 // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
18 struct pax_transaction
22 uint64_t komodoshis,fiatoshis;
23 int32_t marked,height;
25 char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag;
28 uint64_t komodo_paxtotal()
30 struct pax_transaction *pax,*tmp; uint64_t total = 0;
31 /*pthread_mutex_lock(&komodo_mutex);
35 pax = (struct pax_transaction *)PAX->hh.next;
36 while ( pax != 0 && pax != tmp && n++ < 1000000 )
38 printf("PAX.[%p %p] pax.%p marked.%d fiat %.8f KMD %.8f\n",PAX->hh.next,PAX->hh.prev,pax,pax->marked,dstr(pax->fiatoshis),dstr(pax->komodoshis));
39 if ( pax->marked == 0 )
41 if ( komodo_is_issuer() != 0 )
42 total += pax->fiatoshis;
43 else total += pax->komodoshis;
46 pax = (struct pax_transaction *)pax->hh.next;
49 pthread_mutex_unlock(&komodo_mutex);
51 printf("komodo_paxtotal n.%d iterations?\n",n);*/
52 HASH_ITER(hh,PAX,pax,tmp)
54 if ( pax->marked == 0 )
56 if ( komodo_is_issuer() != 0 )
57 total += pax->fiatoshis;
58 else total += pax->komodoshis;
64 struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout)
66 struct pax_transaction *pax;
67 pthread_mutex_lock(&komodo_mutex);
68 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
70 memcpy(space,pax,sizeof(*pax));
71 pthread_mutex_unlock(&komodo_mutex);
75 struct pax_transaction *komodo_paxmark(int32_t height,struct pax_transaction *space,uint256 txid,uint16_t vout,int32_t mark)
77 struct pax_transaction *pax;
78 pthread_mutex_lock(&komodo_mutex);
79 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
82 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
85 HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
86 //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
91 //int32_t i; for (i=0; i<32; i++)
92 // printf("%02x",((uint8_t *)&txid)[i]);
93 //printf(" paxmark.ht %d vout%d\n",mark,vout);
94 memcpy(space,pax,sizeof(*pax));
96 pthread_mutex_unlock(&komodo_mutex);
100 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
102 struct pax_transaction *pax; int32_t addflag = 0;
103 pthread_mutex_lock(&komodo_mutex);
104 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
107 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
110 HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
111 //printf("[%s] ht.%d create pax.%p\n",ASSETCHAINS_SYMBOL,height,pax);
113 pthread_mutex_unlock(&komodo_mutex);
116 strcpy(pax->coinaddr,coinaddr);
117 pax->komodoshis = value;
118 pax->shortflag = shortflag;
119 strcpy(pax->symbol,symbol);
120 pax->fiatoshis = fiatoshis;
121 memcpy(pax->rmd160,rmd160,20);
122 pax->height = height;
123 if ( pax->marked == 0 )
124 printf("[%s] %p ADD DEPOSIT %s %.8f -> %s TO PAX ht.%d total %.8f\n",ASSETCHAINS_SYMBOL,pax,symbol,dstr(fiatoshis),coinaddr,height,dstr(komodo_paxtotal()));
125 else printf("%p MARKED.%d DEPOSIT %s %.8f -> %s TO PAX ht.%d\n",pax,pax->marked,symbol,dstr(fiatoshis),coinaddr,height);
129 pax->marked = height;
130 printf("pax.%p MARK DEPOSIT ht.%d\n",pax,height);
134 int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uint16_t *vouts,uint8_t *opretbuf,int32_t opretlen)
137 if ( opretbuf[opretlen-5] == '-' )
139 else *shortflagp = 0;
141 base[i] = opretbuf[opretlen-4+i];
142 if ( (strcmp(base,"KMD") == 0 && ASSETCHAINS_SYMBOL[0] == 0) || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) // shortflag
144 //printf("BASE.(%s) vs (%s)\n",base,ASSETCHAINS_SYMBOL);
145 opretbuf++, opretlen--;
146 for (n=len=0; n<opretlen/34; n++)
150 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
151 //printf("%02x",((uint8_t *)&txids[n])[j]);
153 vouts[n] = opretbuf[len++];
154 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
155 //printf(" issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
161 void komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *symbol)
163 struct pax_transaction *pax,*tmp; uint8_t *script,opcode,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1;
164 PENDING_KOMODO_TX = 0;
165 if ( strcmp(symbol,"KMD") == 0 )
168 HASH_ITER(hh,PAX,pax,tmp)
170 if ( pax->marked != 0 )
172 if ( ASSETCHAINS_SYMBOL[0] != 0 )
173 printf("pax.%p marked.%d %.8f -> %.8f\n",pax,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
174 txNew->vout.resize(numvouts+1);
175 txNew->vout[numvouts].nValue = pax->fiatoshis;
176 txNew->vout[numvouts].scriptPubKey.resize(25);
177 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
181 memcpy(script,pax->rmd160,20), script += 20;
186 printf("%02x",((uint8_t *)&pax->txid)[i]);
187 data[len++] = ((uint8_t *)&pax->txid)[i];
189 data[len++] = pax->vout & 0xff;
190 data[len++] = (pax->vout >> 8) & 0xff;
191 if ( strcmp(symbol,"KMD") != 0 )
192 PENDING_KOMODO_TX += pax->fiatoshis;
193 else PENDING_KOMODO_TX += pax->komodoshis;
194 printf(" vout.%u DEPOSIT %.8f <- paxdeposit.%s pending %.8f\n",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX));
195 if ( numvouts++ >= 64 )
200 if ( shortflag != 0 )
202 for (i=0; symbol[i]!=0; i++)
203 data[len++] = symbol[i];
205 opretlen = komodo_opreturnscript(opret,opcode,data,len);
206 txNew->vout.resize(numvouts+1);
207 txNew->vout[numvouts].nValue = 0;
208 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
209 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
210 memcpy(script,opret,opretlen);
211 printf("total numvouts.%d %.8f opretlen.%d\n",numvouts,dstr(PENDING_KOMODO_TX),opretlen);
215 int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
217 int32_t i,j,n,num,opretlen,offset=1; uint256 hash,txids[64]; uint8_t shortflag; char symbol[16],base[16]; uint16_t vouts[64]; uint8_t *script,opcode; struct pax_transaction *pax,space;
218 n = block.vtx[0].vout.size();
219 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
220 if ( n <= 2 || script[0] != 0x6a )
222 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
223 //printf("checkdeposit n.%d [%02x] [%c] %d vs %d\n",n,script[0],script[offset],script[offset],'I');
224 if ( ASSETCHAINS_SYMBOL[0] == 0 )
227 strcpy(symbol,"KMD");
231 strcpy(symbol,ASSETCHAINS_SYMBOL);
234 if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
236 if ( (num= komodo_issued_opreturn(&shortflag,base,txids,vouts,&script[offset],opretlen)) > 0 )
238 for (i=1; i<n-1; i++)
240 if ( (pax= komodo_paxfind(&space,txids[i-1],vouts[i-1])) != 0 && ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) )
242 //printf("i.%d match %.8f == %.8f\n",i,dstr(pax != 0 ? pax->fiatoshis:-1),dstr(block.vtx[0].vout[i].nValue));
243 komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height);
247 hash = block.GetHash();
248 //for (j=0; j<32; j++)
249 // printf("%02x",((uint8_t *)&hash)[j]);
250 //printf(" ht.%d blockhash couldnt find vout.[%d]\n",height,i);
251 komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height);
255 //printf("opretlen.%d num.%d\n",opretlen,num);
260 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout)
262 uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo,kmdheight; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; double diff; uint64_t seed; int64_t fiatoshis,checktoshis; const char *typestr = "unknown";
263 tokomodo = (komodo_is_issuer() == 0);
264 if ( opretbuf[0] == 'D' )
266 if ( opretlen == 38 ) // any KMD tx
268 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
269 memset(base,0,sizeof(base));
270 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
272 fiatoshis = -fiatoshis;
273 bitcoin_address(coinaddr,addrtype,rmd160,20);
274 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
276 printf("kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
277 diff = ((double)value / checktoshis) - 1.;
280 if ( kmdheight <= height )
282 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
284 for (i=0; i<opretlen; i++)
285 printf("%02x",opretbuf[i]);
286 printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo);
288 //paxprice seed.0 sum 0.07766840 densum 1000.00000000 basevol 0.01000000 height.59532
289 if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 && shortflag == ASSETCHAINS_SHORTFLAG )
291 if ( shortflag == 0 )
294 printf("%02x",((uint8_t *)&txid)[i]);
295 printf(" <- txid.v%u ",vout);
297 printf("%02x",pubkey33[i]);
298 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
299 if ( value >= checktoshis || (seed == 0 && diff < .01) )
301 if ( komodo_paxfind(&space,txid,vout) == 0 )
302 komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,kmdheight);
307 for (i=0; i<opretlen; i++)
308 printf("%02x",opretbuf[i]);
309 printf(" opret[%c] tokomodo.%d value %.8f vs check %.8f\n",opretbuf[0],tokomodo,dstr(value),dstr(checktoshis));
310 if ( value <= checktoshis || (seed == 0 && diff < .01) )
319 else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
321 if ( tokomodo == 0 && opretbuf[0] == 'I' ) // assetchain coinbase
323 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
325 for (i=0; i<opretlen; i++)
326 printf("%02x",opretbuf[i]);
327 printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo);
329 if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG )
334 printf("%02x",((uint8_t *)&txids[i])[j]);
335 printf(" issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen);
336 if ( komodo_paxmark(height,&space,txids[i],vouts[i],height) == 0 )
337 komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height);
341 else if ( tokomodo != 0 && opretbuf[0] == 'X' )
343 // verify and update limits
349 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)
351 int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr;
353 memcpy(&utxid,&txid,sizeof(utxid));
354 if ( script[offset++] == 0x6a )
356 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
357 if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 )
358 typestr = "notarized";
359 else if ( txi == 0 && vout == 1 && opretlen == 149 )
361 typestr = "pricefeed";
362 komodo_paxpricefeed(height,&script[offset],opretlen);
363 //printf("height.%d pricefeed len.%d\n",height,opretlen);
365 else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout);
367 else if ( numvouts >= KOMODO_MINRATIFY )
371 int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
373 char *retstr,params[256],*hexstr; uint8_t script[10000]; cJSON *oldpub,*newpub,*json,*result,*vouts,*item,*sobj; int32_t vout,n,len,isspecial,retval = -1; uint64_t value; bits256 txid;
374 sprintf(params,"[\"%s\", 1]",txidstr);
375 if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 )
377 if ( (json= cJSON_Parse(retstr)) != 0 )
379 if ( (result= jobj(json,(char *)"result")) != 0 )
381 oldpub = jobj(result,(char *)"vpub_old");
382 newpub = jobj(result,(char *)"vpub_new");
384 if ( oldpub == 0 && newpub == 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
387 txid = jbits256(result,(char *)"txid");
388 for (vout=0; vout<n; vout++)
390 item = jitem(vouts,vout);
391 value = SATOSHIDEN * jdouble(item,(char *)"value");
392 if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
394 if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
396 len = (int32_t)strlen(hexstr) >> 1;
397 if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) )
399 else if ( len <= sizeof(script) )
401 decode_hex(script,len,hexstr);
402 komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len);
408 } else printf("error getting txids.(%s) %p\n",retstr,result);
416 int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
418 char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2;
419 sprintf(params,"[%d]",height);
420 if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 )
422 if ( (result= cJSON_Parse(retstr)) != 0 )
424 if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
426 sprintf(params,"[\"%s\"]",txidstr);
427 if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
429 //printf("getblock.(%s)\n",retstr2);
430 if ( (json= cJSON_Parse(retstr2)) != 0 )
432 if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
435 if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
439 else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n);
440 } else printf("cant get result.%p or tx.%p\n",result,tx);
442 } else printf("cant parse2.(%s)\n",retstr2);
444 } else printf("error getblock %s\n",params);
445 } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
447 } else printf("couldnt parse.(%s)\n",retstr);
449 } else printf("error from getblockhash %d\n",height);
453 void komodo_gateway_iteration(char *symbol)
455 char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
456 if ( KMDHEIGHT <= 0 )
459 if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
461 if ( (infoobj= cJSON_Parse(retstr)) != 0 )
463 if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
465 //printf("gateway KMDHEIGHT.%d kmdheight.%d\n",KMDHEIGHT,kmdheight);
466 for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
468 if ( (KMDHEIGHT % 10) == 0 )
470 if ( (KMDHEIGHT % 100) == 0 )
471 fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT);
472 memset(&zero,0,sizeof(zero));
473 komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0);
475 if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
477 printf("error KMDHEIGHT %d\n",KMDHEIGHT);
482 if ( KMDHEIGHT >= kmdheight )
483 KOMODO_REALTIME = (uint32_t)time(NULL);
491 printf("error from %s\n",symbol);
496 void komodo_iteration(char *symbol)
498 char *retstr,*base,*coinaddr,*txidstr,cmd[512]; uint64_t value,fiatoshis; cJSON *array,*item; int32_t i,n,vout,shortflag,height; uint256 txid; uint8_t rmd160[20],addrtype;
499 if ( ASSETCHAINS_SYMBOL[0] == 0 )
501 //[{"prev_hash":"5d5c9a49489b558de9e84f991f996dedaae6b9d0f157f82b2fec64662476d5cf","prev_vout":2,"EUR":0.78329000,"fiat":"EUR","height":57930,"KMD":0.10000000,"address":"RDhEGYScNQYetCyG75Kf8Fg61UWPdwc1C5"}]
502 sprintf(cmd,"{\"agent\":\"dpow\",\"method\":\"pending\",\"fiat\":\"%s\"}",symbol);
503 if ( (retstr= issue_curl(cmd)) != 0 )
505 if ( (array= cJSON_Parse(retstr)) != 0 )
507 if ( (n= cJSON_GetArraySize(array)) > 0 )
511 item = jitem(array,i);
512 coinaddr = jstr(item,(char *)"address");
513 value = jdouble(item,(char *)"KMD") * COIN;
514 shortflag = juint(item,(char *)"short");
515 vout = jint(item,(char *)"prev_vout");
516 height = jint(item,(char *)"height");
517 base = jstr(item,(char *)"fiat");
518 txidstr = jstr(item,(char *)"prev_hash");
519 if ( coinaddr != 0 && base != 0 && value > 0 && height > 0 )
521 fiatoshis = jdouble(item,base) * COIN;
522 decode_hex((uint8_t *)&txid,sizeof(txid),txidstr);
523 bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
524 komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height);
529 printf("retstr.(%s)\n",retstr);