1 /******************************************************************************
2 * Copyright © 2014-2017 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 ******************************************************************************/
18 #include "komodo_defs.h"
27 #define KOMODO_ASSETCHAINS_WAITNOTARIZE
28 #define KOMODO_PAXMAX (10000 * COIN)
37 int32_t gettxout_scriptPubKey(uint8_t *scriptPubkey,int32_t maxsize,uint256 txid,int32_t n);
38 void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height);
39 void komodo_connectblock(CBlockIndex *pindex,CBlock& block);
41 #include "komodo_structs.h"
42 #include "komodo_globals.h"
43 #include "komodo_utils.h"
44 #include "komodo_curve25519.h"
46 #include "komodo_cJSON.c"
47 #include "komodo_bitcoind.h"
48 #include "komodo_interest.h"
49 #include "komodo_pax.h"
50 #include "komodo_notary.h"
52 int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest);
53 #include "komodo_kv.h"
54 #include "komodo_jumblr.h"
55 #include "komodo_gateway.h"
56 #include "komodo_events.h"
58 void komodo_currentheight_set(int32_t height)
60 char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp;
61 if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
62 sp->CURRENT_HEIGHT = height;
65 int32_t komodo_currentheight()
67 char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp;
68 if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
69 return(sp->CURRENT_HEIGHT);
73 int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest)
76 int32_t func,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33];
77 if ( (func= fgetc(fp)) != EOF )
79 if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
81 else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
82 if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
84 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && func != 'T' )
85 printf("[%s] matched.%d fpos.%ld func.(%d %c) ht.%d\n",ASSETCHAINS_SYMBOL,matched,ftell(fp),func,func,ht);
88 if ( (num= fgetc(fp)) <= 64 )
90 if ( fread(pubkeys,33,num,fp) != num )
94 //printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
95 if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
96 komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
98 } else printf("illegal num.%d\n",num);
100 else if ( func == 'N' )
102 if ( fread(¬arized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) )
104 if ( fread(¬arized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) )
106 if ( fread(¬arized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) )
108 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && sp != 0 )
109 printf("%s load[%s.%d -> %s] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,dest,notarized_height,notarized_hash.ToString().c_str());
110 //if ( matched != 0 ) global independent states -> inside *sp
111 komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
113 else if ( func == 'U' ) // deprecated
115 uint8_t n,nid; uint256 hash; uint64_t mask;
118 //printf("U %d %d\n",n,nid);
119 if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
121 if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
123 //if ( matched != 0 )
124 // komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n);
126 else if ( func == 'K' )
129 if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
131 //if ( matched != 0 ) global independent states -> inside *sp
132 //printf("%s.%d load[%s] ht.%d\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight);
133 komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
135 else if ( func == 'T' )
137 int32_t kheight,ktimestamp;
138 if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
140 if ( fread(&ktimestamp,1,sizeof(ktimestamp),fp) != sizeof(ktimestamp) )
142 //if ( matched != 0 ) global independent states -> inside *sp
143 //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
144 komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
146 else if ( func == 'R' )
148 uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384];
149 if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
151 if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
153 if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
155 if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
157 if ( olen < sizeof(opret) )
159 if ( fread(opret,1,olen,fp) != olen )
161 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && matched != 0 )
163 int32_t i; for (i=0; i<olen; i++)
164 printf("%02x",opret[i]);
165 printf(" %s.%d load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,ht,symbol,opret[0],olen,(double)ovalue/COIN);
167 komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); // global shared state -> global PAX
171 for (i=0; i<olen; i++)
173 //printf("illegal olen.%u\n",olen);
176 else if ( func == 'D' )
178 printf("unexpected function D[%d]\n",ht);
180 else if ( func == 'V' )
182 int32_t numpvals; uint32_t pvals[128];
183 numpvals = fgetc(fp);
184 if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
186 //if ( matched != 0 ) global shared state -> global PVALS
187 //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
188 komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
189 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
190 } else printf("error loading pvals[%d]\n",numpvals);
192 else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func);
197 int32_t memread(void *dest,int32_t size,uint8_t *filedata,long *fposp,long datalen)
199 if ( *fposp+size <= datalen )
201 memcpy(dest,&filedata[*fposp],size);
208 int32_t komodo_parsestatefiledata(struct komodo_state *sp,uint8_t *filedata,long *fposp,long datalen,char *symbol,char *dest)
211 int32_t func= -1,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33]; long fpos = *fposp;
212 if ( fpos < datalen )
214 func = filedata[fpos++];
215 if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
217 else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
218 if ( memread(&ht,sizeof(ht),filedata,&fpos,datalen) != sizeof(ht) )
222 if ( (num= filedata[fpos++]) <= 64 )
224 if ( memread(pubkeys,33*num,filedata,&fpos,datalen) != 33*num )
228 //printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
229 if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
230 komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
232 } else printf("illegal num.%d\n",num);
234 else if ( func == 'N' )
236 if ( memread(¬arized_height,sizeof(notarized_height),filedata,&fpos,datalen) != sizeof(notarized_height) )
238 if ( memread(¬arized_hash,sizeof(notarized_hash),filedata,&fpos,datalen) != sizeof(notarized_hash) )
240 if ( memread(¬arized_desttxid,sizeof(notarized_desttxid),filedata,&fpos,datalen) != sizeof(notarized_desttxid) )
242 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && sp != 0 )
243 printf("%s load[%s.%d -> %s] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,dest,notarized_height,notarized_hash.ToString().c_str());
244 //if ( matched != 0 ) global independent states -> inside *sp
245 komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
247 else if ( func == 'U' ) // deprecated
249 uint8_t n,nid; uint256 hash; uint64_t mask;
250 n = filedata[fpos++];
251 nid = filedata[fpos++];
252 //printf("U %d %d\n",n,nid);
253 if ( memread(&mask,sizeof(mask),filedata,&fpos,datalen) != sizeof(mask) )
255 if ( memread(&hash,sizeof(hash),filedata,&fpos,datalen) != sizeof(hash) )
258 else if ( func == 'K' )
261 if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) )
263 komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
265 else if ( func == 'T' )
267 int32_t kheight,ktimestamp;
268 if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) )
270 if ( memread(&ktimestamp,sizeof(ktimestamp),filedata,&fpos,datalen) != sizeof(ktimestamp) )
272 //if ( matched != 0 ) global independent states -> inside *sp
273 //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
274 komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
276 else if ( func == 'R' )
278 uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384];
279 if ( memread(&txid,sizeof(txid),filedata,&fpos,datalen) != sizeof(txid) )
281 if ( memread(&v,sizeof(v),filedata,&fpos,datalen) != sizeof(v) )
283 if ( memread(&ovalue,sizeof(ovalue),filedata,&fpos,datalen) != sizeof(ovalue) )
285 if ( memread(&olen,sizeof(olen),filedata,&fpos,datalen) != sizeof(olen) )
287 if ( olen < sizeof(opret) )
289 if ( memread(opret,olen,filedata,&fpos,datalen) != olen )
291 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && matched != 0 )
293 int32_t i; for (i=0; i<olen; i++)
294 printf("%02x",opret[i]);
295 printf(" %s.%d load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,ht,symbol,opret[0],olen,(double)ovalue/COIN);
297 komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); // global shared state -> global PAX
301 for (i=0; i<olen; i++)
303 //printf("illegal olen.%u\n",olen);
306 else if ( func == 'D' )
308 printf("unexpected function D[%d]\n",ht);
310 else if ( func == 'V' )
312 int32_t numpvals; uint32_t pvals[128];
313 numpvals = filedata[fpos++];
314 if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && memread(pvals,(int32_t)(sizeof(uint32_t)*numpvals),filedata,&fpos,datalen) == numpvals*sizeof(uint32_t) )
316 //if ( matched != 0 ) global shared state -> global PVALS
317 //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
318 komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
319 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
320 } else printf("error loading pvals[%d]\n",numpvals);
322 else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func);
329 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,uint32_t KMDtimestamp,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
331 static FILE *fp; static int32_t errs,didinit;
332 struct komodo_state *sp; char fname[512],symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; int32_t retval,ht,func; uint8_t num,pubkeys[64][33];
335 portable_mutex_init(&KOMODO_KV_mutex);
338 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
340 KOMODO_INITDONE = (uint32_t)time(NULL);
341 printf("[%s] no komodo_stateptr\n",ASSETCHAINS_SYMBOL);
344 //printf("[%s] (%s) -> (%s)\n",ASSETCHAINS_SYMBOL,symbol,dest);
347 komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"komodostate");
348 if ( (fp= fopen(fname,"rb+")) != 0 )
350 if ( (retval= komodo_faststateinit(sp,fname,symbol,dest)) > 0 )
351 fseek(fp,0,SEEK_END);
354 fprintf(stderr,"komodo_faststateinit retval.%d\n",retval);
355 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
358 } else fp = fopen(fname,"wb+");
359 KOMODO_INITDONE = (uint32_t)time(NULL);
363 //printf("early return: stateupdate height.%d\n",height);
366 if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
368 //printf("fpos.%ld ",ftell(fp));
369 if ( KMDheight != 0 )
371 if ( KMDtimestamp != 0 )
374 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
376 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
378 if ( fwrite(&KMDtimestamp,1,sizeof(KMDtimestamp),fp) != sizeof(KMDtimestamp) )
384 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
386 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
389 komodo_eventadd_kmdheight(sp,symbol,height,KMDheight,KMDtimestamp);
391 else if ( opretbuf != 0 && opretlen > 0 )
393 uint16_t olen = opretlen;
395 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
397 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
399 if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
401 if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
403 if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
405 if ( fwrite(opretbuf,1,olen,fp) != olen )
407 //printf("ht.%d R opret[%d] sp.%p\n",height,olen,sp);
408 //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
409 komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen);
411 else if ( notarypubs != 0 && numnotaries > 0 )
413 printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
415 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
417 fputc(numnotaries,fp);
418 if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
420 komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs);
422 else if ( voutmask != 0 && numvouts > 0 )
424 //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
426 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
430 if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
432 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
434 //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts);
436 else if ( pvals != 0 && numpvals > 0 )
445 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
448 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
450 komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals);
451 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
453 //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
455 else if ( height != 0 )
457 //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
461 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
463 if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) )
465 if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) )
467 if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) )
469 komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT);
476 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,int32_t notarized,uint64_t signedmask)
478 static uint256 zero; static FILE *signedfp;
479 int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN];
480 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
482 if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
484 if ( i == 0 && j == 0 && memcmp(NOTARY_PUBKEY33,scriptbuf+1,33) == 0 && NOTARY_PUBKEY33[0] != 0 )
486 printf("%s KOMODO_LASTMINED.%d -> %d\n",ASSETCHAINS_SYMBOL,KOMODO_LASTMINED,height);
487 prevKOMODO_LASTMINED = KOMODO_LASTMINED;
488 KOMODO_LASTMINED = height;
490 decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
491 /*for (k=0; k<33; k++)
492 printf("%02x",crypto777[k]);
493 printf(" crypto777 ");
494 for (k=0; k<scriptlen; k++)
495 printf("%02x",scriptbuf[k]);
496 printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
497 if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
500 //printf(">>>>>>>> ");
502 else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
504 //printf("found notary.k%d\n",k);
510 *voutmaskp |= (1LL << j);
512 else if ( notaryid != nid )
514 //for (i=0; i<33; i++)
515 // printf("%02x",scriptbuf[i+1]);
516 //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid);
520 else *voutmaskp |= (1LL << j);
524 if ( scriptbuf[len++] == 0x6a )
526 if ( (opretlen= scriptbuf[len++]) == 0x4c )
527 opretlen = scriptbuf[len++];
528 else if ( opretlen == 0x4d )
530 opretlen = scriptbuf[len++];
531 opretlen += (scriptbuf[len++] << 8);
533 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
534 printf("[%s] notarized.%d notarizedht.%d sp.Nht %d sp.ht %d opretlen.%d (%c %c %c)\n",ASSETCHAINS_SYMBOL,notarized,*notarizedheightp,sp->NOTARIZED_HEIGHT,sp->CURRENT_HEIGHT,opretlen,scriptbuf[len+32*2+4],scriptbuf[len+32*2+4+1],scriptbuf[len+32*2+4+2]);
535 if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
537 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
538 len += iguana_rwnum(0,&scriptbuf[len],sizeof(*notarizedheightp),(uint8_t *)notarizedheightp);
539 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
540 if ( notarized != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height && (height < sp->CURRENT_HEIGHT-1000 || komodo_verifynotarization(ASSETCHAINS_SYMBOL[0]==0?(char *)"KMD":ASSETCHAINS_SYMBOL,(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "BTC" : "KMD"),height,*notarizedheightp,kmdtxid,desttxid) == 0) )
542 sp->NOTARIZED_HEIGHT = *notarizedheightp;
543 sp->NOTARIZED_HASH = kmdtxid;
544 sp->NOTARIZED_DESTTXID = desttxid;
545 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0);
547 if ( ASSETCHAINS_SYMBOL[0] != 0 )
548 printf("[%s] ht.%d NOTARIZED.%d %s.%s %sTXID.%s lens.(%d %d)\n",ASSETCHAINS_SYMBOL,height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),opretlen,len);
552 komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"signedmasks");
553 if ( (signedfp= fopen(fname,"rb+")) == 0 )
554 signedfp = fopen(fname,"wb");
555 else fseek(signedfp,0,SEEK_END);
559 fwrite(&height,1,sizeof(height),signedfp);
560 fwrite(&signedmask,1,sizeof(signedmask),signedfp);
563 if ( ASSETCHAINS_SYMBOL[0] == 0 )
565 if ( opretlen > len && scriptbuf[len] == 'A' )
567 //for (i=0; i<opretlen-len; i++)
568 // printf("%02x",scriptbuf[len+i]);
569 //printf(" Found extradata.[%d] %d - %d\n",opretlen-len,opretlen,len);
570 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen-len+4+3+(scriptbuf[1] == 0x4d),j);
573 } else if ( height >= KOMODO_MAINNET_START )
574 printf("notarized.%d %llx reject ht.%d NOTARIZED.%d prev.%d %s.%s DESTTXID.%s (%s)\n",notarized,(long long)signedmask,height,*notarizedheightp,sp->NOTARIZED_HEIGHT,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
576 else if ( i == 0 && j == 1 && opretlen == 149 )
578 if ( notaryid >= 0 && notaryid < 64 )
579 komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
583 //int32_t k; for (k=0; k<scriptlen; k++)
584 // printf("%02x",scriptbuf[k]);
585 //printf(" <- script ht.%d i.%d j.%d value %.8f %s\n",height,i,j,dstr(value),ASSETCHAINS_SYMBOL);
586 if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
589 if ( scriptbuf[len+k] != 0 )
593 *isratificationp = 1;
594 printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]);
598 if ( *isratificationp == 0 && (signedmask != 0 || (scriptbuf[len] != 'X' && scriptbuf[len] != 'A')) ) // && scriptbuf[len] != 'I')
599 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
605 /*int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
607 if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY )
612 // Special tx have vout[0] -> CRYPTO777
613 // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify
614 // if all outputs to notary -> notary utxo
615 // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
616 // OP_RETURN: 'D' -> deposit, 'W' -> withdraw
618 int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n);
620 int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20])
623 if ( scriptlen == 25 && memcmp(&scriptPubKey[3],rmd160,20) == 0 )
625 else if ( scriptlen == 35 )
627 for (i=0; i<numnotaries; i++)
628 if ( memcmp(&scriptPubKey[1],pubkeys[i],33) == 0 )
634 void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
636 static int32_t hwmheight;
637 uint64_t signedmask,voutmask; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp;
638 uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,zero,btctxid,txhash;
639 int32_t i,j,k,numnotaries,notarized,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
640 memset(&zero,0,sizeof(zero));
641 komodo_init(pindex->nHeight);
642 KOMODO_INITDONE = (uint32_t)time(NULL);
643 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
645 fprintf(stderr,"unexpected null komodostateptr.[%s]\n",ASSETCHAINS_SYMBOL);
648 //fprintf(stderr,"%s connect.%d\n",ASSETCHAINS_SYMBOL,pindex->nHeight);
649 numnotaries = komodo_notaries(pubkeys,pindex->nHeight);
650 calc_rmd160_sha256(rmd160,pubkeys[0],33);
651 if ( pindex->nHeight > hwmheight )
652 hwmheight = pindex->nHeight;
655 if ( pindex->nHeight != hwmheight )
656 printf("%s hwmheight.%d vs pindex->nHeight.%d t.%u reorg.%d\n",ASSETCHAINS_SYMBOL,hwmheight,pindex->nHeight,(uint32_t)pindex->nTime,hwmheight-pindex->nHeight);
657 komodo_event_rewind(sp,symbol,pindex->nHeight);
658 komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,pindex->nTime,0,0,0,0);
660 komodo_currentheight_set(chainActive.Tip()->nHeight);
663 height = pindex->nHeight;
664 txn_count = block.vtx.size();
665 for (i=0; i<txn_count; i++)
667 txhash = block.vtx[i].GetHash();
668 numvouts = block.vtx[i].vout.size();
670 voutmask = specialtx = notarizedheight = isratification = notarized = 0;
671 signedmask = (height < 91400) ? 1 : 0;
672 numvins = block.vtx[i].vin.size();
673 for (j=0; j<numvins; j++)
675 if ( i == 0 && j == 0 )
677 if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 )
679 if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 )
680 signedmask |= (1LL << k);
681 else if ( 0 && numvins >= 17 )
684 for (k=0; k<scriptlen; k++)
685 printf("%02x",scriptPubKey[k]);
686 printf(" scriptPubKey doesnt match any notary vini.%d of %d\n",j,numvins);
688 } else printf("cant get scriptPubKey for ht.%d txi.%d vin.%d\n",height,i,j);
690 numvalid = bitweight(signedmask);
691 if ( (((height < 90000 || (signedmask & 1) != 0) && numvalid >= KOMODO_MINRATIFY) ||
692 (numvalid >= KOMODO_MINRATIFY && ASSETCHAINS_SYMBOL[0] != 0) ||
693 numvalid > (numnotaries/5)) )
695 if ( height > 500000 || ASSETCHAINS_SYMBOL[0] != 0 )
696 printf("[%s] ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d <<<<<<<<<<< notarized\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts);
699 if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 )
700 printf("(tx.%d: ",i);
701 for (j=0; j<numvouts; j++)
703 /*if ( i == 0 && j == 0 )
705 uint8_t *script = (uint8_t *)block.vtx[0].vout[numvouts-1].scriptPubKey.data();
706 if ( numvouts <= 2 || script[0] != 0x6a )
708 if ( numvouts == 2 && block.vtx[0].vout[1].nValue != 0 )
710 fprintf(stderr,"ht.%d numvouts.%d value %.8f\n",height,numvouts,dstr(block.vtx[0].vout[1].nValue));
711 if ( height >= 235300 && block.vtx[0].vout[1].nValue >= 100000*COIN )
712 block.vtx[0].vout[1].nValue = 0;
717 if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 )
718 printf("%.8f ",dstr(block.vtx[i].vout[j].nValue));
719 len = block.vtx[i].vout[j].scriptPubKey.size();
720 if ( len >= sizeof(uint32_t) && len <= sizeof(scriptbuf) )
723 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
725 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
727 notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,¬arizedheight,(uint64_t)block.vtx[i].vout[j].nValue,notarized,signedmask);
730 for (k=0; k<len; k++)
731 printf("%02x",scriptbuf[k]);
732 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());
736 if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 )
738 if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 )
739 printf("[%s] ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d notarized.%d special.%d isratification.%d\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts,notarized,specialtx,isratification);
740 if ( notarized != 0 && (notarizedheight != 0 || specialtx != 0) )
742 if ( isratification != 0 )
744 printf("%s NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",ASSETCHAINS_SYMBOL,(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
745 printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries);
747 if ( specialtx != 0 && isratification != 0 && numvouts > 2 )
750 memset(pubkeys,0,sizeof(pubkeys));
751 for (j=1; j<numvouts-1; j++)
753 len = block.vtx[i].vout[j].scriptPubKey.size();
754 if ( len >= sizeof(uint32_t) && len <= sizeof(scriptbuf) )
757 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
759 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
761 if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
763 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
765 printf("%02x",scriptbuf[k+1]);
766 printf(" <- new notary.[%d]\n",j-1);
770 if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries/3) )
772 memset(&txhash,0,sizeof(txhash));
773 komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0,0);
774 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);
775 } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries);
779 if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 )
780 printf("%s ht.%d\n",ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL,height);
781 if ( pindex->nHeight == hwmheight )
782 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,(uint32_t)pindex->nTime,0,0,0,0);
783 } else fprintf(stderr,"komodo_connectblock: unexpected null pindex\n");
784 //KOMODO_INITDONE = (uint32_t)time(NULL);
785 //fprintf(stderr,"%s end connect.%d\n",ASSETCHAINS_SYMBOL,pindex->nHeight);