diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index e7b0b1476..53204a8c4 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -59,7 +59,7 @@ public: #include "uint256.h" #include "arith_uint256.h" -int8_t Minerids[1024 * 1024 * 5]; // 5 million blocks +#include "komodo_structs.h" #include "komodo_globals.h" #include "komodo_utils.h" diff --git a/src/komodo-tx.cpp b/src/komodo-tx.cpp index d5e333093..46004a92e 100644 --- a/src/komodo-tx.cpp +++ b/src/komodo-tx.cpp @@ -25,6 +25,7 @@ using namespace std; #include "uint256.h" #include "arith_uint256.h" +#include "komodo_structs.h" #include "komodo_globals.h" #include "komodo_interest.h" diff --git a/src/komodo.h b/src/komodo.h index f308c893b..ed05c1a46 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -17,49 +17,174 @@ #define H_KOMODO_H // Todo: -// 0. optimize assetchains RT loop +// 0. optimize assetchains // 1. error check fiat redeem amounts // 2. net balance limiter // 3. verify: reorgs -// 4. automate notarization fee payouts -// 5. automated distribution of test REVS snapshot + +// non komodod (non-hardfork) todo: +// a. automate notarization fee payouts +// b. automated distribution of test REVS snapshot #include #include #include #include +#include "uthash.h" +#include "utlist.h" +int32_t gettxout_scriptPubKey(uint8_t *scriptPubkey,int32_t maxsize,uint256 txid,int32_t n); +void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height); +void komodo_connectblock(CBlockIndex *pindex,CBlock& block); -#define GENESIS_NBITS 0x1f00ffff -#define KOMODO_MINRATIFY 7 - -FILE *Minerfp; -int8_t Minerids[1024 * 1024 * 5]; // 5 million blocks - +#include "komodo_structs.h" #include "komodo_globals.h" #include "komodo_utils.h" +void komodo_setkmdheight(int32_t kmdheight) +{ + if ( kmdheight > KMDHEIGHT ) + KMDHEIGHT = kmdheight; +} + #include "cJSON.c" #include "komodo_bitcoind.h" #include "komodo_interest.h" #include "komodo_pax.h" #include "komodo_notary.h" #include "komodo_gateway.h" +#include "komodo_events.h" +void komodo_currentheight_set(int32_t height) +{ + char symbol[16],dest[16]; struct komodo_state *sp; + if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) + sp->CURRENT_HEIGHT = height; +} + +int32_t komodo_currentheight() +{ + char symbol[16],dest[16]; struct komodo_state *sp; + if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) + return(sp->CURRENT_HEIGHT); + else return(0); +} + +int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest) +{ + static int32_t errs; + int32_t func,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33]; + if ( (func= fgetc(fp)) != EOF ) + { + if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 ) + matched = 1; + else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0); + if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) ) + errs++; + //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht); + if ( func == 'P' ) + { + if ( (num= fgetc(fp)) <= 64 ) + { + if ( fread(pubkeys,33,num,fp) != num ) + errs++; + else + { + printf("updated %d pubkeys at ht.%d\n",num,ht); + if ( matched != 0 ) + komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys); + } + } else printf("illegal num.%d\n",num); + } + else if ( func == 'N' ) + { + if ( fread(¬arized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) ) + errs++; + if ( fread(¬arized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) ) + errs++; + if ( fread(¬arized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) ) + errs++; + printf("load NOTARIZED %d %s\n",notarized_height,notarized_hash.ToString().c_str()); + if ( matched != 0 ) + komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height); + } + else if ( func == 'U' ) + { + uint8_t n,nid; uint256 hash; uint64_t mask; + n = fgetc(fp); + nid = fgetc(fp); + //printf("U %d %d\n",n,nid); + if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) ) + errs++; + if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) ) + errs++; + //if ( matched != 0 ) + // komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n); + } + else if ( func == 'K' ) + { + int32_t kheight; + if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) ) + errs++; + if ( matched != 0 ) + komodo_eventadd_kmdheight(sp,symbol,ht,kheight); + } + else if ( func == 'R' ) + { + uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000]; + if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) ) + errs++; + if ( fread(&v,1,sizeof(v),fp) != sizeof(v) ) + errs++; + if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) ) + errs++; + if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) ) + errs++; + if ( olen < sizeof(opret) ) + { + if ( fread(opret,1,olen,fp) != olen ) + errs++; + if ( matched != 0 ) + komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); + } else printf("illegal olen.%u\n",olen); + } + else if ( func == 'D' ) + { + printf("unexpected function D[%d]\n",ht); + } + else if ( func == 'V' ) + { + int32_t numpvals; uint32_t pvals[128]; + numpvals = fgetc(fp); + if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals ) + { + if ( matched != 0 ) + komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals); + //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals); + } else printf("error loading pvals[%d]\n",numpvals); + } + else printf("illegal func.(%d %c)\n",func,func); + return(func); + } else return(-1); +} + 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) { - static FILE *fp; static int32_t errs; char fname[512],fname2[512]; int32_t ht,func; uint8_t num,pubkeys[64][33]; + static FILE *fp; static int32_t errs; + struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33]; + if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) + return; if ( fp == 0 ) { #ifdef WIN32 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate"); - sprintf(fname2,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"minerids"); + //sprintf(fname2,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"minerids"); #else sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate"); - sprintf(fname2,"%s/%s",GetDataDir(false).string().c_str(),(char *)"minerids"); + //sprintf(fname2,"%s/%s",GetDataDir(false).string().c_str(),(char *)"minerids"); #endif - memset(Minerids,0xfe,sizeof(Minerids)); + /*memset(Minerids,0xfe,sizeof(Minerids)); if ( (Minerfp= fopen(fname2,"rb+")) == 0 ) { if ( (Minerfp= fopen(fname2,"wb")) != 0 ) @@ -70,98 +195,11 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar Minerfp = fopen(fname2,"rb+"); } if ( Minerfp != 0 && fread(Minerids,1,sizeof(Minerids),Minerfp) != sizeof(Minerids) ) - printf("read error Minerids\n"); + printf("read error Minerids\n");*/ if ( (fp= fopen(fname,"rb+")) != 0 ) { - while ( (func= fgetc(fp)) != EOF ) - { - if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) ) - errs++; - //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht); - if ( func == 'P' ) - { - if ( (num= fgetc(fp)) < 64 ) - { - if ( fread(pubkeys,33,num,fp) != num ) - errs++; - else - { - printf("updated %d pubkeys at ht.%d\n",num,ht); - komodo_notarysinit(ht,pubkeys,num); - } - } else printf("illegal num.%d\n",num); - //printf("P[%d]\n",num); - } - else if ( func == 'N' ) - { - if ( fread(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) ) - errs++; - if ( fread(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) ) - errs++; - if ( fread(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) ) - errs++; - printf("load NOTARIZED %d %s\n",NOTARIZED_HEIGHT,NOTARIZED_HASH.ToString().c_str()); - komodo_notarized_update(ht,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID); - } - else if ( func == 'U' ) - { - uint8_t n,nid; uint256 hash; uint64_t mask; - n = fgetc(fp); - nid = fgetc(fp); - //printf("U %d %d\n",n,nid); - if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) ) - errs++; - if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) ) - errs++; - komodo_nutxoadd(ht,nid,hash,mask,n); - } - else if ( func == 'K' ) - { - int32_t kheight; - if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) ) - errs++; - if ( kheight > KMDHEIGHT ) - { - KMDHEIGHT = kheight; - } - //printf("ht.%d KMDHEIGHT <- %d\n",ht,kheight); - } - else if ( func == 'R' ) - { - uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000]; - if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) ) - errs++; - if ( fread(&v,1,sizeof(v),fp) != sizeof(v) ) - errs++; - if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) ) - errs++; - if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) ) - errs++; - if ( olen < sizeof(opret) ) - { - if ( fread(opret,1,olen,fp) != olen ) - errs++; - komodo_opreturn(ht,ovalue,opret,olen,txid,v); - } else printf("illegal olen.%u\n",olen); - } - else if ( func == 'D' ) - { - //printf("D[%d]\n",ht); - } -//#ifdef KOMODO_PAX - else if ( func == 'V' ) - { - int32_t numpvals; uint32_t pvals[128]; - numpvals = fgetc(fp); - if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals ) - { - komodo_pvals(ht,pvals,numpvals); - //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals); - } else printf("error loading pvals[%d]\n",numpvals); - } -//#endif - else printf("illegal func.(%d %c)\n",func,func); - } + while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) + ; } else fp = fopen(fname,"wb+"); printf("fname.(%s) fpos.%ld\n",fname,ftell(fp)); KOMODO_INITDONE = (uint32_t)time(NULL); @@ -174,22 +212,14 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar if ( fp != 0 ) // write out funcid, height, other fields, call side effect function { //printf("fpos.%ld ",ftell(fp)); - if ( height < 0 ) - { - height = -height; - //printf("func D[%d] errs.%d\n",height,errs); - fputc('D',fp); - if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) - errs++; - } - else if ( KMDheight > 0 ) + if ( KMDheight > 0 ) { fputc('K',fp); if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) errs++; if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) ) errs++; - //printf("ht.%d K %d\n",height,KMDheight); + komodo_eventadd_kmdheight(sp,symbol,height,KMDheight); } else if ( opretbuf != 0 && opretlen > 0 ) { @@ -208,18 +238,19 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar if ( fwrite(opretbuf,1,olen,fp) != olen ) errs++; //printf("ht.%d R opret[%d]\n",height,olen); - komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout); + //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout); + komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen); } else if ( notarypubs != 0 && numnotaries > 0 ) { - //printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs); + printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs); fputc('P',fp); if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) errs++; fputc(numnotaries,fp); if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries ) errs++; - komodo_notarysinit(height,notarypubs,numnotaries); + komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs); } else if ( voutmask != 0 && numvouts > 0 ) { @@ -233,7 +264,7 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar errs++; if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) errs++; - komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts); + //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts); } //#ifdef KOMODO_PAX else if ( pvals != 0 && numpvals > 0 ) @@ -250,7 +281,7 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar fputc(numpvals,fp); if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals ) errs++; - komodo_pvals(height,pvals,numpvals); + komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals); //printf("ht.%d V numpvals[%d]\n",height,numpvals); } //printf("save pvals height.%d numpvals.%d\n",height,numpvals); @@ -262,13 +293,14 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar fputc('N',fp); if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) errs++; - if ( fwrite(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) ) + if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) ) errs++; - if ( fwrite(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) ) + if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) ) errs++; - if ( fwrite(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) ) + if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) ) errs++; - komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID); + komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT); + //komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID); } fflush(fp); } @@ -276,7 +308,9 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar 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) { - static uint256 zero; int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; + 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]; + if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) + return(-1); if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) { decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR); @@ -327,12 +361,12 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp); len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid); - if ( *notarizedheightp > NOTARIZED_HEIGHT && *notarizedheightp < height ) + if ( *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height ) { 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]); - NOTARIZED_HEIGHT = *notarizedheightp; - NOTARIZED_HASH = kmdtxid; - NOTARIZED_DESTTXID = desttxid; + sp->NOTARIZED_HEIGHT = *notarizedheightp; + sp->NOTARIZED_HASH = kmdtxid; + sp->NOTARIZED_DESTTXID = desttxid; komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0); } 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]); } @@ -340,12 +374,11 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr komodo_paxpricefeed(height,&scriptbuf[len],opretlen); else { - //int32_t k; for (k=0; k= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) { - iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); for (k=0; k<32; k++) if ( scriptbuf[len+k] != 0 ) break; @@ -374,23 +407,42 @@ int32_t komodo_isratify(int32_t isspecial,int32_t numvalid) // if all outputs to notary -> notary utxo // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed // OP_RETURN: 'D' -> deposit, 'W' -> withdraw +void komodo_currentheight_set(int32_t height); +int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n); + +int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20]) +{ + int32_t i; + if ( scriptlen == 25 && memcmp(&scriptPubKey[3],rmd160,20) == 0 ) + return(0); + else if ( scriptlen == 35 ) + { + for (i=0; inHeight); numnotaries = komodo_notaries(pubkeys,pindex->nHeight); + calc_rmd160_sha256(rmd160,pubkeys[0],33); if ( pindex->nHeight > hwmheight ) hwmheight = pindex->nHeight; else { printf("hwmheight.%d vs pindex->nHeight.%d reorg.%d\n",hwmheight,pindex->nHeight,hwmheight-pindex->nHeight); - // reset komodostate + if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) + komodo_event_rewind(sp,symbol,pindex->nHeight); + // komodo_stateupdate(); } - CURRENT_HEIGHT = chainActive.Tip()->nHeight; + komodo_currentheight_set(chainActive.Tip()->nHeight); if ( komodo_is_issuer() != 0 ) { while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME ) @@ -404,8 +456,6 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block) { height = pindex->nHeight; txn_count = block.vtx.size(); - if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) - printf("%s ht.%d connect txn_count.%d\n",ASSETCHAINS_SYMBOL,height,txn_count); for (i=0; i= 0 ) - { - if ( height < sizeof(Minerids)/sizeof(*Minerids) ) - { - if ( (Minerids[height]= nid) >= -1 ) - { - if ( Minerfp != 0 ) - { - fseek(Minerfp,height,SEEK_SET); - fputc(Minerids[height],Minerfp); - fflush(Minerfp); - } - } - } - } if ( 0 && i > 0 ) { for (k=0; k= 0 && notaryid < 64 && voutmask != 0 ) { - komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0); - //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts); + //komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0); } signedmask = 0; numvins = block.vtx[i].vin.size(); for (j=0; j= 0 ) + if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 ) + { + if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 ) + signedmask |= (1LL << k); + } + /*if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 ) signedmask |= (1LL << k); - else if ( 0 && signedmask != 0 ) - 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); + else if ( signedmask != 0 ) + 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);*/ } + if ( signedmask != 0 ) + printf("ht.%d signedmask.%llx numvins.%d numvouts.%d\n",height,(long long)signedmask,numvins,numvouts); if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) ) { printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx); @@ -492,7 +533,7 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block) memset(&txhash,0,sizeof(txhash)); komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0); 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); - } + } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries); } } } diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 8979bae06..8e915a170 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -375,17 +375,13 @@ uint64_t komodo_seed(int32_t height) void komodo_disconnect(CBlockIndex *pindex,CBlock& block) { - //int32_t i; uint256 hash; + char symbol[16],dest[16]; struct komodo_state *sp; komodo_init(pindex->nHeight); - Minerids[pindex->nHeight] = -2; - //hash = block.GetHash(); - //for (i=0; i<32; i++) - // printf("%02x",((uint8_t *)&hash)[i]); - //printf(" <- disconnect block\n"); - //uint256 zero; - //printf("disconnect ht.%d\n",pindex->nHeight); - //memset(&zero,0,sizeof(zero)); - //komodo_stateupdate(-pindex->nHeight,0,0,0,zero,0,0,0,0,0,0,0); + if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) + { + //sp->rewinding = pindex->nHeight; + //fprintf(stderr,"-%d ",pindex->nHeight); + } else printf("komodo_disconnect: ht.%d cant get komodo_state.(%s)\n",pindex->nHeight,ASSETCHAINS_SYMBOL); } int32_t komodo_is_notarytx(const CTransaction& tx) @@ -432,13 +428,34 @@ int32_t komodo_block2height(CBlock *block) void komodo_block2pubkey33(uint8_t *pubkey33,CBlock& block) { + int32_t n; #ifdef KOMODO_ZCASH uint8_t *ptr = (uint8_t *)block.vtx[0].vout[0].scriptPubKey.data(); #else uint8_t *ptr = (uint8_t *)&block.vtx[0].vout[0].scriptPubKey[0]; #endif komodo_init(0); - memcpy(pubkey33,ptr+1,33); + n = block.vtx[0].vout[0].scriptPubKey.size(); + if ( n == 35 ) + memcpy(pubkey33,ptr+1,33); + else memset(pubkey33,0,33); +} + +int32_t komodo_blockload(CBlock& block,CBlockIndex *pindex) +{ + block.SetNull(); + // Open history file to read + CAutoFile filein(OpenBlockFile(pindex->GetBlockPos(),true),SER_DISK,CLIENT_VERSION); + if (filein.IsNull()) + return(-1); + // Read block + try { filein >> block; } + catch (const std::exception& e) + { + fprintf(stderr,"readblockfromdisk err B\n"); + return(-1); + } + return(0); } void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height) @@ -448,14 +465,8 @@ void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height) memset(pubkey33,0,33); if ( pindex != 0 ) { - if ( ReadBlockFromDisk(block,(const CBlockIndex *)pindex -#ifndef KOMODO_ZCASH - ,Params().GetConsensus() -#endif - ) != 0 ) - { + if ( komodo_blockload(block,pindex) == 0 ) komodo_block2pubkey33(pubkey33,block); - } } else { @@ -464,57 +475,41 @@ void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height) } } +void komodo_connectpindex(CBlockIndex *pindex) +{ + CBlock block; + if ( komodo_blockload(block,pindex) == 0 ) + komodo_connectblock(pindex,block); +} + +int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height); + int8_t komodo_minerid(int32_t height) { - static uint32_t depth; - int32_t notaryid; CBlockIndex *pindex; uint8_t pubkey33[33]; - if ( depth < 3 && height <= CURRENT_HEIGHT )//chainActive.Tip()->nHeight ) + int32_t num,i; CBlockIndex *pindex; uint8_t pubkey33[33],pubkeys[64][33]; + if ( (pindex= chainActive[height]) != 0 ) { - if ( Minerids[height] >= -1 ) + komodo_index2pubkey33(pubkey33,pindex,height); + if ( (num= komodo_notaries(pubkeys,height)) > 0 ) { - printf("cached[%d] -> %d\n",height,Minerids[height]); - return(Minerids[height]); - } - if ( (pindex= chainActive[height]) != 0 ) - { - depth++; - komodo_index2pubkey33(pubkey33,pindex,height); - komodo_chosennotary(¬aryid,height,pubkey33); - if ( notaryid >= -1 ) - { - Minerids[height] = notaryid; - if ( Minerfp != 0 ) - { - fseek(Minerfp,height,SEEK_SET); - fputc(Minerids[height],Minerfp); - fflush(Minerfp); - } - } - depth--; - return(notaryid); + for (i=0; i= 34000 && notaryid >= 0 ) { for (i=1; i<64; i++) { - if ( Minerids[height-i] == -2 ) - { - Minerids[height-i] = komodo_minerid(height-i); - if ( Minerids[height - i] == -2 ) - { - //fprintf(stderr,"second -2 for Minerids[%d] current.%d\n",height-i,CURRENT_HEIGHT); - return(-2); - } - } - if ( Minerids[height-i] == notaryid ) + if ( komodo_minerid(height-i) == notaryid ) return(-1); } return(1); @@ -543,7 +538,7 @@ int32_t komodo_checkpoint(int32_t *notarized_heightp,int32_t nHeight,uint256 has return(-1); } } else fprintf(stderr,"unexpected error notary_hash %s ht.%d at ht.%d\n",notarized_hash.ToString().c_str(),notarized_height,notary->nHeight); - } else if ( notarized_height > 0 ) + } else if ( notarized_height > 0 && notarized_height != 73880 ) fprintf(stderr,"couldnt find notary_hash %s ht.%d\n",notarized_hash.ToString().c_str(),notarized_height); return(0); } diff --git a/src/komodo_events.h b/src/komodo_events.h new file mode 100644 index 000000000..017fcb145 --- /dev/null +++ b/src/komodo_events.h @@ -0,0 +1,203 @@ +/****************************************************************************** + * Copyright © 2014-2016 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef H_KOMODOEVENTS_H +#define H_KOMODOEVENTS_H + + +#ifdef WIN32 +#define PACKED +#else +#define PACKED __attribute__((packed)) +#endif + +#define KOMODO_EVENT_RATIFY 'P' +#define KOMODO_EVENT_NOTARIZED 'N' +#define KOMODO_EVENT_UTXO 'U' +#define KOMODO_EVENT_KMDHEIGHT 'K' +#define KOMODO_EVENT_REWIND 'B' +//#define KOMODO_EVENT_DELETE 'D' +#define KOMODO_EVENT_PRICEFEED 'V' +#define KOMODO_EVENT_OPRETURN 'R' +#define KOMODO_OPRETURN_DEPOSIT 'D' +#define KOMODO_OPRETURN_ISSUED 'I' // assetchain +#define KOMODO_OPRETURN_WITHDRAW 'W' // assetchain +#define KOMODO_OPRETURN_REDEEMED 'X' + +struct komodo_event_notarized { uint256 blockhash,desttxid; int32_t notarizedheight; char dest[16]; }; +struct komodo_event_pubkeys { uint8_t num; uint8_t pubkeys[64][33]; }; +struct komodo_event_utxo { uint256 txid; uint64_t voutmask; uint8_t numvouts; }; +struct komodo_event_opreturn { uint256 txid; uint64_t value; uint16_t vout,oplen; uint8_t opret[]; }; +struct komodo_event_pricefeed { uint8_t num; uint32_t prices[35]; }; + +struct komodo_event +{ + struct komodo_event *related; + uint16_t len; + int32_t height; + uint8_t type,reorged; + char symbol[16]; + uint8_t space[]; +} PACKED; + +struct komodo_event **Komodo_events; int32_t Komodo_numevents; + +struct komodo_event *komodo_eventadd(int32_t height,char *symbol,uint8_t type,uint8_t *data,uint16_t datalen) +{ + struct komodo_event *ep; uint16_t len = (uint16_t)(sizeof(*ep) + datalen); + ep = (struct komodo_event *)calloc(1,len); + ep->len = len; + ep->height = height; + ep->type = type; + strcpy(ep->symbol,symbol); + if ( datalen != 0 ) + memcpy(ep->space,data,datalen); + Komodo_events = (struct komodo_event **)realloc(Komodo_events,(1 + Komodo_numevents) * sizeof(*Komodo_events)); + Komodo_events[Komodo_numevents++] = ep; + return(ep); +} + +void komodo_eventadd_notarized(struct komodo_state *sp,char *symbol,int32_t height,char *dest,uint256 notarized_hash,uint256 notarized_desttxid,int32_t notarizedheight) +{ + struct komodo_event_notarized N; + memset(&N,0,sizeof(N)); + N.blockhash = notarized_hash; + N.desttxid = notarized_desttxid; + N.notarizedheight = notarizedheight; + strcpy(N.dest,dest); + komodo_eventadd(height,symbol,KOMODO_EVENT_NOTARIZED,(uint8_t *)&N,sizeof(N)); + if ( sp != 0 ) + komodo_notarized_update(sp,height,notarizedheight,notarized_hash,notarized_desttxid); +} + +void komodo_eventadd_pubkeys(struct komodo_state *sp,char *symbol,int32_t height,uint8_t num,uint8_t pubkeys[64][33]) +{ + struct komodo_event_pubkeys P; + printf("eventadd pubkeys ht.%d\n",height); + memset(&P,0,sizeof(P)); + P.num = num; + memcpy(P.pubkeys,pubkeys,33 * num); + komodo_eventadd(height,symbol,KOMODO_EVENT_RATIFY,(uint8_t *)&P,(int32_t)(sizeof(P.num) + 33 * num)); + if ( sp != 0 ) + komodo_notarysinit(height,pubkeys,num); +} + +/*void komodo_eventadd_utxo(struct komodo_state *sp,char *symbol,int32_t height,uint8_t notaryid,uint256 txid,uint64_t voutmask,uint8_t numvouts) +{ + struct komodo_event_utxo U; + memset(&U,0,sizeof(U)); + U.txid = txid; + U.voutmask = voutmask; + U.numvouts = numvouts; + komodo_eventadd(height,symbol,KOMODO_EVENT_UTXO,(uint8_t *)&U,sizeof(U)); + if ( sp != 0 ) + komodo_nutxoadd(height,notaryid,txid,voutmask,numvouts); +}*/ + +void komodo_eventadd_pricefeed(struct komodo_state *sp,char *symbol,int32_t height,uint32_t *prices,uint8_t num) +{ + struct komodo_event_pricefeed F; + memset(&F,0,sizeof(F)); + F.num = num; + memcpy(F.prices,prices,sizeof(*F.prices) * num); + komodo_eventadd(height,symbol,KOMODO_EVENT_PRICEFEED,(uint8_t *)&F,(int32_t)(sizeof(F.num) + sizeof(*F.prices) * num)); + if ( sp != 0 ) + komodo_pvals(height,prices,num); +} + +void komodo_eventadd_kmdheight(struct komodo_state *sp,char *symbol,int32_t height,int32_t kmdheight) +{ + komodo_eventadd(height,symbol,KOMODO_EVENT_KMDHEIGHT,(uint8_t *)&kmdheight,sizeof(kmdheight)); + if ( sp != 0 ) + komodo_setkmdheight(kmdheight); +} + +void komodo_eventadd_opreturn(struct komodo_state *sp,char *symbol,int32_t height,uint256 txid,uint64_t value,uint16_t vout,uint8_t *buf,uint16_t opretlen) +{ + struct komodo_event_opreturn O; uint8_t opret[10000]; + memset(&O,0,sizeof(O)); + O.txid = txid; + O.value = value; + O.vout = vout; + memcpy(opret,&O,sizeof(O)); + memcpy(&opret[sizeof(O)],buf,opretlen); + O.oplen = (int32_t)(opretlen + sizeof(O)); + komodo_eventadd(height,symbol,KOMODO_EVENT_OPRETURN,opret,O.oplen); + if ( sp != 0 ) + komodo_opreturn(height,value,buf,opretlen,txid,vout); +} + +void komodo_event_undo(struct komodo_event *ep) +{ + switch ( ep->type ) + { + case KOMODO_EVENT_RATIFY: + case KOMODO_EVENT_NOTARIZED: + case KOMODO_EVENT_UTXO: + case KOMODO_EVENT_KMDHEIGHT: + case KOMODO_EVENT_PRICEFEED: + case KOMODO_EVENT_OPRETURN: + break; + } +} + +void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height) +{ + struct komodo_event *ep; + komodo_eventadd(height,symbol,KOMODO_EVENT_REWIND,(uint8_t *)&height,sizeof(height)); + if ( sp != 0 ) + { + while ( Komodo_numevents > 0 ) + { + if ( (ep= Komodo_events[Komodo_numevents-1]) != 0 ) + { + if ( ep->height < height ) + break; + printf("undo event.%c ht.%d for rewind.%d\n",ep->type,ep->height,height); + komodo_event_undo(ep); + Komodo_numevents--; + } + } + } +} + +/*void komodo_eventadd_deposit(int32_t actionflag,char *symbol,int32_t height,uint64_t komodoshis,char *fiat,uint64_t fiatoshis,uint8_t rmd160[20],bits256 kmdtxid,uint16_t kmdvout,uint64_t price) +{ + uint8_t opret[512]; uint16_t opretlen; + komodo_eventadd_opreturn(symbol,height,KOMODO_OPRETURN_DEPOSIT,kmdtxid,komodoshis,kmdvout,opret,opretlen); +} + +void komodo_eventadd_issued(int32_t actionflag,char *symbol,int32_t height,int32_t fiatheight,bits256 fiattxid,uint16_t fiatvout,bits256 kmdtxid,uint16_t kmdvout,uint64_t fiatoshis) +{ + uint8_t opret[512]; uint16_t opretlen; + komodo_eventadd_opreturn(symbol,height,KOMODO_OPRETURN_ISSUED,fiattxid,fiatoshis,fiatvout,opret,opretlen); +} + +void komodo_eventadd_withdraw(int32_t actionflag,char *symbol,int32_t height,uint64_t komodoshis,char *fiat,uint64_t fiatoshis,uint8_t rmd160[20],bits256 fiattxid,int32_t fiatvout,uint64_t price) +{ + uint8_t opret[512]; uint16_t opretlen; + komodo_eventadd_opreturn(symbol,height,KOMODO_OPRETURN_WITHDRAW,fiattxid,fiatoshis,fiatvout,opret,opretlen); +} + +void komodo_eventadd_redeemed(int32_t actionflag,char *symbol,int32_t height,bits256 kmdtxid,uint16_t kmdvout,int32_t fiatheight,bits256 fiattxid,uint16_t fiatvout,uint64_t komodoshis) +{ + uint8_t opret[512]; uint16_t opretlen; + komodo_eventadd_opreturn(symbol,height,KOMODO_OPRETURN_REDEEMED,kmdtxid,komodoshis,kmdvout,opret,opretlen); +}*/ + +// process events +// + +#endif diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6b88f8fae..d72c4b1c0 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -15,16 +15,6 @@ // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse -struct pax_transaction -{ - UT_hash_handle hh; - uint256 txid; - uint64_t komodoshis,fiatoshis; - int32_t marked,height,otherheight; - uint16_t vout; - char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag; -} *PAX; - uint64_t komodo_paxtotal() { struct pax_transaction *pax,*tmp; uint64_t total = 0; diff --git a/src/komodo_globals.h b/src/komodo_globals.h index bf9ac25ce..f5eba4929 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -21,9 +21,25 @@ void komodo_init(int32_t height); void komodo_assetchain_pubkeys(char *jsonstr); int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33); +pthread_mutex_t komodo_mutex; + +//FILE *Minerfp; +//int8_t Minerids[KOMODO_MAXBLOCKS]; // 5 million blocks +#define KOMODO_ELECTION_GAP ((ASSETCHAINS_SYMBOL[0] == 0) ? 2000 : 100) + +int32_t KMDHEIGHT = 43000; + +struct pax_transaction *PAX; +int32_t NUM_PRICES; uint32_t *PVALS; +struct knotaries_entry *Pubkeys; +//struct nutxo_entry *NUTXOS; int32_t Num_nutxos; +struct notarized_checkpoint *NPOINTS; int32_t NUM_NPOINTS; + +struct komodo_state KOMODO_STATES[33]; + int COINBASE_MATURITY = 100; -int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,CURRENT_HEIGHT,ASSETCHAINS_SEED,KOMODO_ON_DEMAND; +int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND; std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES; uint8_t NOTARY_PUBKEY33[33]; @@ -34,9 +50,6 @@ uint32_t ASSETCHAIN_INIT; uint32_t ASSETCHAINS_MAGIC = 2387029918; uint64_t ASSETCHAINS_SUPPLY = 10; -int32_t NOTARIZED_HEIGHT,Num_nutxos,KMDHEIGHT = 43000; -uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID; -pthread_mutex_t komodo_mutex; uint32_t KOMODO_INITDONE,KOMODO_REALTIME; char KMDUSERPASS[1024]; uint16_t BITCOIND_PORT = 7771; uint64_t PENDING_KOMODO_TX; diff --git a/src/komodo_interest.h b/src/komodo_interest.h index b3b1e21f7..bc9df3e3a 100644 --- a/src/komodo_interest.h +++ b/src/komodo_interest.h @@ -58,6 +58,8 @@ uint64_t komodo_moneysupply(int32_t height) uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime) { int32_t minutes; uint64_t numerator,denominator,interest = 0; + if ( ASSETCHAINS_SYMBOL[0] != 0 ) + return(0); if ( komodo_moneysupply(txheight) < MAX_MONEY && nLockTime >= LOCKTIME_THRESHOLD && tiptime != 0 && nLockTime < tiptime && nValue >= 10*COIN ) { if ( (minutes= (tiptime - nLockTime) / 60) >= 60 ) diff --git a/src/komodo_notary.h b/src/komodo_notary.h index 8cbd3bc0d..954c029a5 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -52,14 +52,7 @@ const char *Notaries_genesis[][2] = { "titomane_SH", "035f49d7a308dd9a209e894321f010d21b7793461b0c89d6d9231a3fe5f68d9960" }, }; -#define KOMODO_ELECTION_GAP ((ASSETCHAINS_SYMBOL[0] == 0) ? 2000 : 100) - -struct nutxo_entry { UT_hash_handle hh; uint256 txhash; uint64_t voutmask; int32_t notaryid,height; } *NUTXOS; -struct knotary_entry { UT_hash_handle hh; uint8_t pubkey[33],notaryid; }; -struct knotaries_entry { int32_t height,numnotaries; struct knotary_entry *Notaries; } Pubkeys[10000]; -struct notarized_checkpoint { uint256 notarized_hash,notarized_desttxid; int32_t nHeight,notarized_height; } *NPOINTS; int32_t NUM_NPOINTS; - -void komodo_nutxoadd(int32_t height,int32_t notaryid,uint256 txhash,uint64_t voutmask,int32_t numvouts) +/*void komodo_nutxoadd(int32_t height,int32_t notaryid,uint256 txhash,uint64_t voutmask,int32_t numvouts) { struct nutxo_entry *np; if ( numvouts > 1 && notaryid < 64 ) @@ -86,7 +79,7 @@ int32_t komodo_nutxofind(int32_t height,uint256 txhash,int32_t vout) if ( np != 0 && ((1LL << vout) & np->voutmask) != 0 ) return(np->notaryid); return(-1); -} +}*/ int32_t komodo_ratify_threshold(int32_t height,uint64_t signedmask) { @@ -118,23 +111,26 @@ int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height) } else printf("illegal notaryid.%d vs n.%d\n",kp->notaryid,n); } pthread_mutex_unlock(&komodo_mutex); - if ( mask == ((1LL << n)-1) ) + if ( (n < 64 && mask == ((1LL << n)-1)) || (n == 64 && mask == 0xffffffffffffffffLL) ) return(n); printf("error retrieving notaries ht.%d got mask.%llx for n.%d\n",height,(long long)mask,n); return(-1); } -void komodo_notarysinit(int32_t height,uint8_t pubkeys[64][33],int32_t num) +void komodo_notarysinit(int32_t origheight,uint8_t pubkeys[64][33],int32_t num) { static int32_t hwmheight; - int32_t k,i,htind,nonz; struct knotary_entry *kp; struct knotaries_entry N; + int32_t k,i,htind,nonz,height; struct knotary_entry *kp; struct knotaries_entry N; + if ( Pubkeys == 0 ) + Pubkeys = (struct knotaries_entry *)calloc(KOMODO_MAXBLOCKS / KOMODO_ELECTION_GAP,sizeof(*Pubkeys)); memset(&N,0,sizeof(N)); - if ( height > 0 ) + if ( origheight > 0 ) { - height += KOMODO_ELECTION_GAP/2; + height = (origheight + KOMODO_ELECTION_GAP/2); height /= KOMODO_ELECTION_GAP; height = ((height + 1) * KOMODO_ELECTION_GAP); htind = (height / KOMODO_ELECTION_GAP); + printf("htind.%d activation %d from %d vs %d | hwmheight.%d\n",htind,height,origheight,(((origheight+KOMODO_ELECTION_GAP/2)/KOMODO_ELECTION_GAP)+1)*KOMODO_ELECTION_GAP,hwmheight); } else htind = 0; pthread_mutex_lock(&komodo_mutex); for (k=0; kpubkey,pubkeys[k],33); kp->notaryid = k; HASH_ADD_KEYPTR(hh,N.Notaries,kp->pubkey,33,kp); - if ( height > 0 ) + //if ( height > 0 ) { for (i=0; i<33; i++) printf("%02x",pubkeys[k][i]); - printf(" notarypubs.[%d] ht.%d active at %d\n",k,height,htind*KOMODO_ELECTION_GAP); + printf(" notarypubs.[%d] ht.%d active at %d\n",k,origheight,htind*KOMODO_ELECTION_GAP); } } N.numnotaries = num; - for (i=htind; i hwmheight ) - hwmheight = height; + if ( origheight > hwmheight ) + hwmheight = origheight; } int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33) { // -1 if not notary, 0 if notary, 1 if special notary - struct knotary_entry *kp; int32_t numnotaries,htind,modval = -1; + struct knotary_entry *kp; int32_t numnotaries=0,htind,modval = -1; *notaryidp = -1; - if ( height < 0 || height/KOMODO_ELECTION_GAP >= sizeof(Pubkeys)/sizeof(*Pubkeys) ) + if ( height < 0 || height >= KOMODO_MAXBLOCKS ) + { + printf("komodo_chosennotary ht.%d illegal\n",height); return(-1); + } htind = height / KOMODO_ELECTION_GAP; pthread_mutex_lock(&komodo_mutex); HASH_FIND(hh,Pubkeys[htind].Notaries,pubkey33,33,kp); @@ -185,11 +187,11 @@ int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33) } //int32_t i; for (i=0; i<33; i++) // printf("%02x",pubkey33[i]); - //printf(" ht.%d notary.%d special.%d\n",height,*notaryidp,modval); + //printf(" ht.%d notary.%d special.%d htind.%d num.%d\n",height,*notaryidp,modval,htind,numnotaries); return(modval); } -void komodo_notarized_update(int32_t nHeight,int32_t notarized_height,uint256 notarized_hash,uint256 notarized_desttxid) +void komodo_notarized_update(struct komodo_state *sp,int32_t nHeight,int32_t notarized_height,uint256 notarized_hash,uint256 notarized_desttxid) { struct notarized_checkpoint *np; if ( notarized_height > nHeight ) @@ -201,9 +203,27 @@ void komodo_notarized_update(int32_t nHeight,int32_t notarized_height,uint256 no np = &NPOINTS[NUM_NPOINTS++]; memset(np,0,sizeof(*np)); np->nHeight = nHeight; - np->notarized_height = notarized_height; - np->notarized_hash = notarized_hash; - np->notarized_desttxid = notarized_desttxid; + sp->NOTARIZED_HEIGHT = np->notarized_height = notarized_height; + sp->NOTARIZED_HASH = np->notarized_hash = notarized_hash; + sp->NOTARIZED_DESTTXID = np->notarized_desttxid = notarized_desttxid; +} + +struct komodo_state *komodo_stateptr(char *symbol,char *dest); +int32_t komodo_notarized_height(uint256 *hashp,uint256 *txidp) +{ + char symbol[16],dest[16]; struct komodo_state *sp; + if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) + { + *hashp = sp->NOTARIZED_HASH; + *txidp = sp->NOTARIZED_DESTTXID; + return(sp->NOTARIZED_HEIGHT); + } + else + { + memset(hashp,0,sizeof(*hashp)); + memset(txidp,0,sizeof(*txidp)); + return(0); + } } int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp) @@ -247,8 +267,8 @@ void komodo_init(int32_t height) komodo_notarysinit(0,pubkeys,k); } memset(&zero,0,sizeof(zero)); - for (i=0; i= 0 ) + sp = &KOMODO_STATES[baseid]; + else + { + fprintf(stderr,"komodo_stateupdate.(%s) not supported\n",ASSETCHAINS_SYMBOL); + return(0); + } + } + return(sp); +} diff --git a/src/main.cpp b/src/main.cpp index 578cc25ea..41d21c7b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2324,7 +2324,6 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin //FlushStateToDisk(); komodo_connectblock(pindex,*(CBlock *)&block); - return true; } @@ -3242,8 +3241,6 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc return false; if (pindex == NULL) pindex = AddToBlockIndex(block); - //if (!CheckBlockHeader(pindex!=0?pindex->nHeight:0,pindex, block, state)) - // return false; if (ppindex) *ppindex = pindex; return true; @@ -3328,14 +3325,14 @@ static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned return (nFound >= nRequired); } +void komodo_currentheight_set(int32_t height); bool ProcessNewBlock(int32_t height,CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp) { // Preliminary checks - extern int32_t CURRENT_HEIGHT; bool checked; if ( chainActive.Tip() != 0 ) - CURRENT_HEIGHT = chainActive.Tip()->nHeight; + komodo_currentheight_set(chainActive.Tip()->nHeight); if ( ASSETCHAINS_SYMBOL[0] == 0 ) checked = CheckBlock(height!=0?height:komodo_block2height(pblock),0,*pblock, state); else checked = CheckBlock(0,0,*pblock, state); diff --git a/src/pow.cpp b/src/pow.cpp index e9d092bb2..8d97786e0 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -106,8 +106,9 @@ bool CheckEquihashSolution(const CBlockHeader *pblock, const CChainParams& param int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33); int32_t komodo_is_special(int32_t height,uint8_t pubkey33[33]); -extern int32_t KOMODO_CHOSEN_ONE,CURRENT_HEIGHT; -extern int8_t Minerids[1024 * 1024 * 5]; // 5 million blocks +int32_t komodo_currentheight(); +extern int32_t KOMODO_CHOSEN_ONE; +//extern int8_t Minerids[1024 * 1024 * 5]; // 5 million blocks bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned int nBits, const Consensus::Params& params) { @@ -116,7 +117,7 @@ bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned in bnTarget.SetCompact(nBits, &fNegative, &fOverflow); if ( height == 0 ) - height = CURRENT_HEIGHT + 1; + height = komodo_currentheight() + 1; if ( height > 34000 ) // 0 -> non-special notary { special = komodo_chosennotary(¬aryid,height,pubkey33); @@ -132,7 +133,7 @@ bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned in if ( notaryid >= 0 ) { special2 = komodo_is_special(height,pubkey33); - if ( 0 && special2 == -2 ) + if ( special2 == -2 ) printf("height.%d special2.%d special.%d\n",height,special2,special); if ( special2 == -2 || (height < 70000 && (special != 0 || special2 > 0)) || (height >= 70000 && special2 > 0) ) diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index ae9f41540..007600a91 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -431,6 +431,7 @@ Value notaries(const Array& params, bool fHelp) } } ret.push_back(Pair("notaries", a)); + ret.push_back(Pair("numnotaries", n)); } return ret; } @@ -589,6 +590,29 @@ Value gettxout(const Array& params, bool fHelp) return ret; } +int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n) +{ + int32_t i,m; uint8_t *ptr; + LOCK(cs_main); + CCoins coins; + if ( 1 ) + { + LOCK(mempool.cs); + CCoinsViewMemPool view(pcoinsTip,mempool); + if ( view.GetCoins(txid,coins) == 0 ) + return(-1); + mempool.pruneSpent(txid, coins); // TODO: this should be done by the CCoinsViewMemPool + } else if ( pcoinsTip->GetCoins(txid,coins) == 0 ) + return(-1); + if ( n < 0 || (unsigned int)n >= coins.vout.size() || coins.vout[n].IsNull() ) + return(-1); + ptr = (uint8_t *)coins.vout[n].scriptPubKey.data(); + m = coins.vout[n].scriptPubKey.size(); + for (i=0; i 2) diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index b2f8aa0b2..9cc9d4d12 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -40,11 +40,12 @@ using namespace std; * Or alternatively, create a specific query method for the information. **/ uint64_t komodo_interestsum(); +int32_t komodo_notarized_height(uint256 *hashp,uint256 *txidp); Value getinfo(const Array& params, bool fHelp) { - extern uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID; - extern int32_t NOTARIZED_HEIGHT; + uint256 notarized_hash,notarized_desttxid; + int32_t notarized_height; if (fHelp || params.size() != 0) throw runtime_error( "getinfo\n" @@ -81,13 +82,14 @@ Value getinfo(const Array& params, bool fHelp) proxyType proxy; GetProxy(NET_IPV4, proxy); + notarized_height = komodo_notarized_height(¬arized_hash,¬arized_desttxid); Object obj; obj.push_back(Pair("version", CLIENT_VERSION)); obj.push_back(Pair("protocolversion", PROTOCOL_VERSION)); - obj.push_back(Pair("notarized", NOTARIZED_HEIGHT)); - obj.push_back(Pair("notarizedhash", NOTARIZED_HASH.ToString())); - obj.push_back(Pair("notarizedtxid", NOTARIZED_DESTTXID.ToString())); + obj.push_back(Pair("notarized", notarized_height)); + obj.push_back(Pair("notarizedhash", notarized_hash.ToString())); + obj.push_back(Pair("notarizedtxid", notarized_desttxid.ToString())); #ifdef ENABLE_WALLET if (pwalletMain) { obj.push_back(Pair("walletversion", pwalletMain->GetVersion())); diff --git a/src/tinyformat.h b/src/tinyformat.h index a259077f1..e0b5bde76 100644 --- a/src/tinyformat.h +++ b/src/tinyformat.h @@ -821,7 +821,7 @@ inline void formatImpl(std::ostream& out, const char* fmt, // Print remaining part of format string. fmt = printFormatStringLiteral(out, fmt); - if(*fmt != '\0') + if(*fmt != '\0' && 0 ) // disabled due to complaints TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string"); // Restore stream state diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index d37b2793f..0ef2bb723 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2627,11 +2627,20 @@ bool CWallet::CreateTransaction(const vector& vecSend, // Reserve a new key pair from key pool CPubKey vchPubKey; - bool ret; - ret = reservekey.GetReservedKey(vchPubKey); - assert(ret); // should never fail, as we just unlocked - - scriptChange = GetScriptForDestination(vchPubKey.GetID()); + extern int32_t USE_EXTERNAL_PUBKEY; extern std::string NOTARY_PUBKEY; + if ( USE_EXTERNAL_PUBKEY == 0 ) + { + //fprintf(stderr,"use notary pubkey\n"); + //scriptPubKey = CScript() << ParseHex(NOTARY_PUBKEY) << OP_CHECKSIG; + bool ret; + ret = reservekey.GetReservedKey(vchPubKey); + assert(ret); // should never fail, as we just unlocked + scriptChange = GetScriptForDestination(vchPubKey.GetID()); + } + else + { + scriptChange = CScript() << ParseHex(NOTARY_PUBKEY) << OP_CHECKSIG; + } } CTxOut newTxOut(nChange, scriptChange);