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 ******************************************************************************/
20 // 0. optimize assetchains
21 // 1. error check fiat redeem amounts
22 // 2. net balance limiter
25 // non komodod (non-hardfork) todo:
26 // a. automate notarization fee payouts
27 // b. automated distribution of test REVS snapshot
36 #define GENESIS_NBITS 0x1f00ffff
37 #define KOMODO_MINRATIFY 7
38 #define KOMODO_MAXBLOCKS 5000000
40 #include "komodo_structs.h"
45 uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID;
46 int32_t CURRENT_HEIGHT,NOTARIZED_HEIGHT;
50 #include "komodo_globals.h"
51 #include "komodo_utils.h"
52 struct komodo_state *komodo_stateptr(char *symbol,char *dest)
54 int32_t baseid; struct komodo_state *sp;
55 if ( ASSETCHAINS_SYMBOL[0] == 0 )
59 sp = &KOMODO_STATES[0];
63 strcpy(symbol,ASSETCHAINS_SYMBOL);
65 if ( (baseid= komodo_baseid(ASSETCHAINS_SYMBOL)) >= 0 )
66 sp = &KOMODO_STATES[baseid];
69 fprintf(stderr,"komodo_stateupdate.(%s) not supported\n",ASSETCHAINS_SYMBOL);
76 void komodo_setkmdheight(int32_t kmdheight)
78 if ( kmdheight > KMDHEIGHT )
79 KMDHEIGHT = kmdheight;
83 #include "komodo_bitcoind.h"
84 #include "komodo_interest.h"
85 #include "komodo_pax.h"
86 #include "komodo_notary.h"
87 #include "komodo_gateway.h"
88 #include "komodo_events.h"
91 void komodo_currentheight_set(int32_t height)
93 char symbol[16],dest[16]; struct komodo_state *sp;
94 if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
95 sp->CURRENT_HEIGHT = height;
98 int32_t komodo_currentheight()
100 char symbol[16],dest[16]; struct komodo_state *sp;
101 if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
102 return(sp->CURRENT_HEIGHT);
106 int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest)
109 int32_t func,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33];
110 if ( (func= fgetc(fp)) != EOF )
112 if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
114 else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
115 if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
117 //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht);
120 if ( (num= fgetc(fp)) < 64 )
122 if ( fread(pubkeys,33,num,fp) != num )
126 printf("updated %d pubkeys at ht.%d\n",num,ht);
128 komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
130 } else printf("illegal num.%d\n",num);
132 else if ( func == 'N' )
134 if ( fread(¬arized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) )
136 if ( fread(¬arized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) )
138 if ( fread(¬arized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) )
140 printf("load NOTARIZED %d %s\n",notarized_height,notarized_hash.ToString().c_str());
142 komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
144 else if ( func == 'U' )
146 uint8_t n,nid; uint256 hash; uint64_t mask;
149 //printf("U %d %d\n",n,nid);
150 if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
152 if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
155 komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n);
157 else if ( func == 'K' )
160 if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
163 komodo_eventadd_kmdheight(sp,symbol,ht,kheight);
165 else if ( func == 'R' )
167 uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000];
168 if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
170 if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
172 if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
174 if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
176 if ( olen < sizeof(opret) )
178 if ( fread(opret,1,olen,fp) != olen )
181 komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen);
182 } else printf("illegal olen.%u\n",olen);
184 else if ( func == 'D' )
186 printf("unexpected function D[%d]\n",ht);
188 else if ( func == 'V' )
190 int32_t numpvals; uint32_t pvals[128];
191 numpvals = fgetc(fp);
192 if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
195 komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
196 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
197 } else printf("error loading pvals[%d]\n",numpvals);
199 else printf("illegal func.(%d %c)\n",func,func);
204 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)
206 static FILE *fp; static int32_t errs;
207 struct komodo_state *sp; char fname[512],fname2[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33];
208 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
213 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
214 sprintf(fname2,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"minerids");
216 sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
217 sprintf(fname2,"%s/%s",GetDataDir(false).string().c_str(),(char *)"minerids");
219 memset(Minerids,0xfe,sizeof(Minerids));
220 if ( (Minerfp= fopen(fname2,"rb+")) == 0 )
222 if ( (Minerfp= fopen(fname2,"wb")) != 0 )
224 fwrite(Minerids,1,sizeof(Minerids),Minerfp);
227 Minerfp = fopen(fname2,"rb+");
229 if ( Minerfp != 0 && fread(Minerids,1,sizeof(Minerids),Minerfp) != sizeof(Minerids) )
230 printf("read error Minerids\n");
231 if ( (fp= fopen(fname,"rb+")) != 0 )
233 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
235 } else fp = fopen(fname,"wb+");
236 printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
237 KOMODO_INITDONE = (uint32_t)time(NULL);
241 //printf("early return: stateupdate height.%d\n",height);
244 if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
246 //printf("fpos.%ld ",ftell(fp));
250 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
252 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
254 komodo_eventadd_kmdheight(sp,symbol,height,KMDheight);
256 else if ( opretbuf != 0 && opretlen > 0 )
258 uint16_t olen = opretlen;
260 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
262 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
264 if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
266 if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
268 if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
270 if ( fwrite(opretbuf,1,olen,fp) != olen )
272 //printf("ht.%d R opret[%d]\n",height,olen);
273 //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
274 komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen);
276 else if ( notarypubs != 0 && numnotaries > 0 )
278 //printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
280 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
282 fputc(numnotaries,fp);
283 if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
285 komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs);
287 else if ( voutmask != 0 && numvouts > 0 )
289 //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
291 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
295 if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
297 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
299 komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts);
300 //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
303 else if ( pvals != 0 && numpvals > 0 )
312 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
315 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
317 komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals);
318 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
320 //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
323 else if ( height != 0 )
325 //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
327 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
329 if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) )
331 if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) )
333 if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) )
335 komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT);
336 //komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
342 int32_t komodo_voutupdate(int32_t *isratificationp,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)
344 static uint256 zero; int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[16],dest[16];
345 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
347 if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
349 decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
350 /*for (k=0; k<33; k++)
351 printf("%02x",crypto777[k]);
352 printf(" crypto777 ");
353 for (k=0; k<scriptlen; k++)
354 printf("%02x",scriptbuf[k]);
355 printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
356 if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
359 //printf(">>>>>>>> ");
361 else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
363 //printf("found notary.k%d\n",k);
369 *voutmaskp |= (1LL << j);
371 else if ( notaryid != nid )
373 //for (i=0; i<33; i++)
374 // printf("%02x",scriptbuf[i+1]);
375 //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid);
379 else *voutmaskp |= (1LL << j);
383 if ( scriptbuf[len++] == 0x6a )
385 if ( (opretlen= scriptbuf[len++]) == 0x4c )
386 opretlen = scriptbuf[len++];
387 else if ( opretlen == 0x4d )
389 opretlen = scriptbuf[len++];
390 opretlen = (opretlen << 8) + scriptbuf[len++];
392 if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
394 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
395 len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
396 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
397 if ( *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height )
399 printf("ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s)\n",height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
400 sp->NOTARIZED_HEIGHT = *notarizedheightp;
401 sp->NOTARIZED_HASH = kmdtxid;
402 sp->NOTARIZED_DESTTXID = desttxid;
403 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0);
404 } else printf("reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
406 else if ( i == 0 && j == 1 && opretlen == 149 )
407 komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
410 //int32_t k; for (k=0; k<scriptlen; k++)
411 // printf("%02x",scriptbuf[k]);
412 //printf(" <- script ht.%d i.%d j.%d value %.8f\n",height,i,j,dstr(value));
413 if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
415 iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
417 if ( scriptbuf[len+k] != 0 )
421 *isratificationp = 1;
422 printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]);
425 if ( *isratificationp == 0 )
426 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
432 int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
434 if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY )
439 // Special tx have vout[0] -> CRYPTO777
440 // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify
441 // if all outputs to notary -> notary utxo
442 // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
443 // OP_RETURN: 'D' -> deposit, 'W' -> withdraw
444 void komodo_currentheight_set(int32_t height);
446 void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
448 static int32_t hwmheight;
449 uint64_t signedmask,voutmask;
450 uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash;
451 int32_t i,j,k,numnotaries,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
452 komodo_init(pindex->nHeight);
453 numnotaries = komodo_notaries(pubkeys,pindex->nHeight);
454 if ( pindex->nHeight > hwmheight )
455 hwmheight = pindex->nHeight;
458 printf("hwmheight.%d vs pindex->nHeight.%d reorg.%d\n",hwmheight,pindex->nHeight,hwmheight-pindex->nHeight);
461 komodo_currentheight_set(chainActive.Tip()->nHeight);
462 if ( komodo_is_issuer() != 0 )
464 while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME )
466 fprintf(stderr,"komodo_connect.(%s) waiting for realtime RT.%u now.%u\n",ASSETCHAINS_SYMBOL,KOMODO_REALTIME,(uint32_t)time(NULL));
470 KOMODO_REALTIME = KOMODO_INITDONE = (uint32_t)time(NULL);
473 height = pindex->nHeight;
474 txn_count = block.vtx.size();
475 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
476 printf("%s ht.%d connect txn_count.%d\n",ASSETCHAINS_SYMBOL,height,txn_count);
477 for (i=0; i<txn_count; i++)
479 txhash = block.vtx[i].GetHash();
480 numvouts = block.vtx[i].vout.size();
482 voutmask = specialtx = notarizedheight = isratification = 0;
483 for (j=0; j<numvouts; j++)
485 len = block.vtx[i].vout[j].scriptPubKey.size();
486 if ( len <= sizeof(scriptbuf) )
489 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
491 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
493 notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,¬arizedheight,(uint64_t)block.vtx[i].vout[j].nValue);
494 if ( i == 0 && j == 0 && komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
496 if ( height < sizeof(Minerids)/sizeof(*Minerids) )
498 if ( (Minerids[height]= nid) >= -1 )
502 fseek(Minerfp,height,SEEK_SET);
503 fputc(Minerids[height],Minerfp);
511 for (k=0; k<len; k++)
512 printf("%02x",scriptbuf[k]);
513 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());
517 if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 )
519 komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0);
520 //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
523 numvins = block.vtx[i].vin.size();
524 for (j=0; j<numvins; j++)
526 if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 )
527 signedmask |= (1LL << k);
528 else if ( 0 && signedmask != 0 )
529 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);
531 if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
533 printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
534 printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries);
535 if ( specialtx != 0 && isratification != 0 && numvouts > 2 )
538 memset(pubkeys,0,sizeof(pubkeys));
539 for (j=1; j<numvouts-1; j++)
541 len = block.vtx[i].vout[j].scriptPubKey.size();
542 if ( len <= sizeof(scriptbuf) )
545 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
547 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
549 if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
551 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
553 printf("%02x",scriptbuf[k+1]);
554 printf(" <- new notary.[%d]\n",j-1);
558 if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) )
560 memset(&txhash,0,sizeof(txhash));
561 komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0);
562 printf("RATIFIED! >>>>>>>>>> new notaries.%d newheight.%d from height.%d\n",numvalid,(((height+KOMODO_ELECTION_GAP/2)/KOMODO_ELECTION_GAP)+1)*KOMODO_ELECTION_GAP,height);
567 } else printf("komodo_connectblock: unexpected null pindex\n");
568 KOMODO_INITDONE = (uint32_t)time(NULL);