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 ******************************************************************************/
19 // Todo: handle reorg: clear all entries above reorged height
26 void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t kheight,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout);
28 int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp);
29 char *komodo_issuemethod(char *method,char *params,uint16_t port);
31 int32_t ASSETCHAINS_SHORTFLAG,NOTARIZED_HEIGHT,Num_nutxos,KMDHEIGHT = 43000;
32 uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID;
33 pthread_mutex_t komodo_mutex;
34 char KMDUSERPASS[1024]; uint16_t BITCOIND_PORT = 7771;
35 uint64_t KOMODO_DEPOSIT,PENDING_KOMODO_TX;
37 #include "komodo_utils.h"
38 queue_t DepositsQ,PendingsQ;
41 #include "komodo_bitcoind.h"
42 #include "komodo_interest.h"
43 #include "komodo_pax.h"
44 #include "komodo_notary.h"
45 #include "komodo_gateway.h"
48 void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
50 static FILE *fp; static int32_t errs; char fname[512]; int32_t ht,func; uint8_t num,pubkeys[64][33];
52 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
54 sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
58 if ( (fp= fopen(fname,"rb+")) != 0 )
60 while ( (func= fgetc(fp)) != EOF )
62 if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
64 //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht);
67 if ( (num= fgetc(fp)) < 64 )
69 if ( fread(pubkeys,33,num,fp) != num )
73 printf("updated %d pubkeys at ht.%d\n",num,ht);
74 komodo_notarysinit(ht,pubkeys,num);
76 } else printf("illegal num.%d\n",num);
77 //printf("P[%d]\n",num);
79 else if ( func == 'N' )
81 if ( fread(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) )
83 if ( fread(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) )
85 if ( fread(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) )
87 printf("load NOTARIZED %d %s\n",NOTARIZED_HEIGHT,NOTARIZED_HASH.ToString().c_str());
88 komodo_notarized_update(ht,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
90 else if ( func == 'U' )
92 uint8_t n,nid; uint256 hash; uint64_t mask;
95 //printf("U %d %d\n",n,nid);
96 if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
98 if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
100 komodo_nutxoadd(ht,nid,hash,mask,n);
102 else if ( func == 'K' )
105 if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
107 if ( kheight > KMDHEIGHT )
111 //printf("ht.%d KMDHEIGHT <- %d\n",ht,kheight);
113 else if ( func == 'R' )
115 uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000];
116 if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
118 if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
120 if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
122 if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
124 if ( olen < sizeof(opret) )
126 if ( fread(opret,1,olen,fp) != olen )
128 komodo_opreturn(ht,ovalue,opret,olen,txid,v);
129 } else printf("illegal olen.%u\n",olen);
131 else if ( func == 'D' )
133 //printf("D[%d]\n",ht);
136 else if ( func == 'V' )
138 int32_t numpvals; uint32_t pvals[128];
139 numpvals = fgetc(fp);
140 if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
142 komodo_pvals(ht,pvals,numpvals);
143 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
144 } else printf("error loading pvals[%d]\n",numpvals);
147 else printf("illegal func.(%d %c)\n",func,func);
149 } else fp = fopen(fname,"wb+");
150 printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
154 printf("early return: stateupdate height.%d\n",height);
157 if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
159 //printf("fpos.%ld ",ftell(fp));
163 //printf("func D[%d] errs.%d\n",height,errs);
165 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
168 else if ( KMDheight > 0 )
171 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
173 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
175 //printf("ht.%d K %d\n",height,KMDheight);
177 else if ( opretbuf != 0 && opretlen > 0 )
179 uint16_t olen = opretlen;
181 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
183 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
185 if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
187 if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
189 if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
191 if ( fwrite(opretbuf,1,olen,fp) != olen )
193 //printf("ht.%d R opret[%d]\n",height,olen);
194 komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
196 else if ( notarypubs != 0 && numnotaries > 0 )
198 //printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
200 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
202 fputc(numnotaries,fp);
203 if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
205 komodo_notarysinit(height,notarypubs,numnotaries);
207 else if ( voutmask != 0 && numvouts > 0 )
209 //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
211 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
215 if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
217 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
219 komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
222 else if ( pvals != 0 && numpvals > 0 )
231 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
234 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
236 komodo_pvals(height,pvals,numpvals);
237 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
239 //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
242 else if ( height != 0 )
244 //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
246 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
248 if ( fwrite(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) )
250 if ( fwrite(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) )
252 if ( fwrite(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) )
254 komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
260 int32_t komodo_voutupdate(int32_t notaryid,uint8_t *scriptbuf,int32_t scriptlen,int32_t height,uint256 txhash,int32_t i,int32_t j,uint64_t *voutmaskp,int32_t *specialtxp,int32_t *notarizedheightp,uint64_t value)
262 static uint256 zero; int32_t opretlen,nid,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33];
263 if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
265 decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
266 /*for (k=0; k<33; k++)
267 printf("%02x",crypto777[k]);
268 printf(" crypto777 ");
269 for (k=0; k<scriptlen; k++)
270 printf("%02x",scriptbuf[k]);
271 printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
272 if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
277 else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
279 //printf("found notary.k%d\n",k);
285 *voutmaskp |= (1LL << j);
287 else if ( notaryid != nid )
289 printf("mismatch notaryid.%d k.%d\n",notaryid,nid);
293 else *voutmaskp |= (1LL << j);
297 if ( scriptbuf[len++] == 0x6a )
299 if ( (opretlen= scriptbuf[len++]) == 0x4c )
300 opretlen = scriptbuf[len++];
301 else if ( opretlen == 0x4d )
303 opretlen = scriptbuf[len++];
304 opretlen = (opretlen << 8) + scriptbuf[len++];
306 //for (k=0; k<scriptlen; k++)
307 // printf("%02x",scriptbuf[k]);
308 //printf(" <- script ht.%d i.%d j.%d value %.8f\n",height,i,j,dstr(value));
309 if ( j == 1 && opretlen >= 32*2+4 && strcmp(KOMODO_SOURCE,(char *)&scriptbuf[len+32*2+4]) == 0 )
311 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
312 len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
313 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
314 if ( *notarizedheightp > NOTARIZED_HEIGHT && *notarizedheightp < height )
316 printf("ht.%d NOTARIZED.%d KMD.%s BTCTXID.%s (%s)\n",height,*notarizedheightp,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
317 NOTARIZED_HEIGHT = *notarizedheightp;
318 NOTARIZED_HASH = kmdtxid;
319 NOTARIZED_DESTTXID = desttxid;
320 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0);
321 } else printf("reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",height,*notarizedheightp,KOMODO_SOURCE,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
323 else if ( i == 0 && j == 1 && opretlen == 149 )
324 komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
325 else komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
330 int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
332 if ( isspecial != 0 && numvalid > 13 )
337 // Special tx have vout[0] -> CRYPTO777
338 // with more than 13 pay2pubkey outputs -> ratify
339 // if all outputs to notary -> notary utxo
340 // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
341 // OP_RETURN: 'D' -> deposit, 'W' -> withdraw
343 void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
345 uint64_t signedmask,voutmask;
346 uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash;
347 int32_t i,j,k,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
350 komodo_gateway_issuer();
352 komodo_gateway_redeemer();
356 height = pindex->nHeight;
357 txn_count = block.vtx.size();
358 for (i=0; i<txn_count; i++)
360 txhash = block.vtx[i].GetHash();
361 numvouts = block.vtx[i].vout.size();
363 voutmask = specialtx = notarizedheight = 0;
364 for (j=0; j<numvouts; j++)
366 len = block.vtx[i].vout[j].scriptPubKey.size();
367 if ( len <= sizeof(scriptbuf) )
370 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
372 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
374 // signedmask is needed here!
375 notaryid = komodo_voutupdate(notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,¬arizedheight,(uint64_t)block.vtx[i].vout[j].nValue);
378 for (k=0; k<len; k++)
379 printf("%02x",scriptbuf[k]);
380 printf(" <- notaryid.%d ht.%d i.%d j.%d numvouts.%d numvins.%d voutmask.%llx txid.(%s)\n",notaryid,height,i,j,numvouts,numvins,(long long)voutmask,txhash.ToString().c_str());
384 if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 )
386 komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0);
387 //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
390 numvins = block.vtx[i].vin.size();
391 for (j=0; j<numvins; j++)
393 if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 )
394 signedmask |= (1LL << k);
395 else if ( 0 && signedmask != 0 )
396 printf("signedmask.%llx but ht.%d i.%d j.%d not found (%s %d)\n",(long long)signedmask,height,i,j,block.vtx[i].vin[j].prevout.hash.ToString().c_str(),block.vtx[i].vin[j].prevout.n);
398 if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
400 printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
401 if ( specialtx != 0 && numvouts > 2 && komodo_ratify_threshold(height,signedmask) > 0 )
404 memset(pubkeys,0,sizeof(pubkeys));
405 for (j=1; j<numvouts; j++)
407 len = block.vtx[i].vout[j].scriptPubKey.size();
408 if ( len <= sizeof(scriptbuf) )
411 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
413 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
415 if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
417 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
419 printf("%02x",scriptbuf[k+1]);
420 printf(" <- new notary.[%d]\n",j-1);
424 if ( komodo_isratify(1,numvalid) > 13 )
426 memset(&txhash,0,sizeof(txhash));
427 komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0);
429 printf("new notaries.%d newheight.%d from height.%d\n",numvouts-1,KOMODO_PUBKEYS_HEIGHT(height),height);
433 } else printf("komodo_connectblock: unexpected null pindex\n");