diff --git a/src/komodo.h b/src/komodo.h index 2161d1a3f..f041e54b7 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -31,17 +31,46 @@ #include #include - #define GENESIS_NBITS 0x1f00ffff #define KOMODO_MINRATIFY 7 +#define KOMODO_MAXBLOCKS 5000000 FILE *Minerfp; -int8_t Minerids[1024 * 1024 * 5]; // 5 million blocks +int8_t Minerids[KOMODO_MAXBLOCKS]; // 5 million blocks +#define KOMODO_ELECTION_GAP ((ASSETCHAINS_SYMBOL[0] == 0) ? 2000 : 100) +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; +}; + +struct nutxo_entry { UT_hash_handle hh; uint256 txhash; uint64_t voutmask; int32_t notaryid,height; }; +struct knotary_entry { UT_hash_handle hh; uint8_t pubkey[33],notaryid; }; +struct knotaries_entry { int32_t height,numnotaries; struct knotary_entry *Notaries; }; +struct notarized_checkpoint { uint256 notarized_hash,notarized_desttxid; int32_t nHeight,notarized_height; }; +pthread_mutex_t komodo_mutex; +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 +{ + uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID; + int32_t CURRENT_HEIGHT,NOTARIZED_HEIGHT; + // gateway state +} KOMODO_STATES[33]; #include "komodo_globals.h" #include "komodo_utils.h" -#include "komodo_events.h" #include "cJSON.c" #include "komodo_bitcoind.h" @@ -49,11 +78,157 @@ int8_t Minerids[1024 * 1024 * 5]; // 5 million blocks #include "komodo_pax.h" #include "komodo_notary.h" #include "komodo_gateway.h" +#include "komodo_events.h" +void komodo_setkmdheight(int32_t kmdheight) +{ + if ( kmdheight > KMDHEIGHT ) + KMDHEIGHT = kmdheight; +} +struct komodo_state *komodo_stateptr(char *symbol,char *dest) +{ + int32_t baseid; struct komodo_state *sp; + if ( ASSETCHAINS_SYMBOL[0] == 0 ) + { + strcpy(symbol,"KMD"); + strcpy(dest,"BTC"); + sp = &KOMODO_STATES[0]; + } + else + { + strcpy(symbol,ASSETCHAINS_SYMBOL); + strcpy(dest,"KMD"); + if ( (baseid= komodo_baseid(ASSETCHAINS_SYMBOL)) >= 0 ) + sp = &KOMODO_STATES[baseid]; + else + { + fprintf(stderr,"komodo_stateupdate.(%s) not supported\n",ASSETCHAINS_SYMBOL); + return(0); + } + } + return(sp); +} + +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; + char symbol[16]; int32_t func,ht,notarized_height,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,voutmask,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,opretlen); + } 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,height,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],fname2[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 @@ -77,95 +252,8 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar 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("unexpected function 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); @@ -178,22 +266,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 ) { @@ -212,7 +292,8 @@ 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 ) { @@ -223,7 +304,7 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar 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 ) { @@ -237,7 +318,8 @@ 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); + //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts); } //#ifdef KOMODO_PAX else if ( pvals != 0 && numpvals > 0 ) @@ -254,7 +336,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); @@ -266,13 +348,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); } @@ -331,12 +414,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]); } @@ -378,6 +461,7 @@ 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); void komodo_connectblock(CBlockIndex *pindex,CBlock& block) { @@ -394,7 +478,7 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block) printf("hwmheight.%d vs pindex->nHeight.%d reorg.%d\n",hwmheight,pindex->nHeight,hwmheight-pindex->nHeight); // reset komodostate } - CURRENT_HEIGHT = chainActive.Tip()->nHeight; + komodo_currentheight_set(chainActive.Tip()->nHeight); if ( komodo_is_issuer() != 0 ) { while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME ) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 8979bae06..c4e5b9822 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -385,7 +385,6 @@ void komodo_disconnect(CBlockIndex *pindex,CBlock& block) //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); } int32_t komodo_is_notarytx(const CTransaction& tx) @@ -468,7 +467,7 @@ 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 ) + if ( depth < 3 && height <= komodo_currentheight() )//chainActive.Tip()->nHeight ) { if ( Minerids[height] >= -1 ) { @@ -510,7 +509,7 @@ int32_t komodo_is_special(int32_t height,uint8_t pubkey33[33]) 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); + //fprintf(stderr,"second -2 for Minerids[%d] current.%d\n",height-i,komodo_currentheight()); return(-2); } } diff --git a/src/komodo_events.h b/src/komodo_events.h index 11cf1dc88..dcd589e13 100644 --- a/src/komodo_events.h +++ b/src/komodo_events.h @@ -51,6 +51,8 @@ struct komodo_event 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); @@ -61,10 +63,12 @@ struct komodo_event *komodo_eventadd(int32_t height,char *symbol,uint8_t type,ui strcpy(ep->symbol,symbol); if ( datalen != 0 ) memcpy(ep->space,data,datalen); + Komodo_events = realloc(Komodo_events,(1 + Komodo_numevents) * sizeof(*Komodo_events)); + Komodo_events[Komodo_numevents++] = ep; return(ep); } -void komodo_eventadd_notarized(char *symbol,int32_t height,char *dest,bits256 blockhash,bits256 desttxid,int32_t notarizedheight) +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)); @@ -73,18 +77,22 @@ void komodo_eventadd_notarized(char *symbol,int32_t height,char *dest,bits256 bl 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(char *symbol,int32_t height,uint8_t num,uint8_t pubkeys[64][33]) +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; 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(char *symbol,int32_t height,uint8_t notaryid,bits256 txid,uint64_t voutmask,uint8_t numvouts) +void komodo_eventadd_utxo(struct komodo_state *sp,char *symbol,int32_t height,uint8_t notaryid,bits256 txid,uint64_t voutmask,uint8_t numvouts) { struct komodo_event_utxo U; memset(&U,0,sizeof(U)); @@ -92,23 +100,29 @@ void komodo_eventadd_utxo(char *symbol,int32_t height,uint8_t notaryid,bits256 t 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(char *symbol,int32_t height,uint32_t *prices,uint8_t num) +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(char *symbol,int32_t height,int32_t kmdheight) +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(char *symbol,int32_t height,uint8_t type,bits256 txid,uint64_t value,uint16_t vout,uint8_t *buf,uint16_t opretlen) +void komodo_eventadd_opreturn(struct komodo_state *sp,char *symbol,int32_t height,bits256 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)); @@ -118,32 +132,62 @@ void komodo_eventadd_opreturn(char *symbol,int32_t height,uint8_t type,bits256 t memcpy(opret,&O,sizeof(O)); memcpy(&opret[sizeof(O)],buf,opretlen); O.oplen = (int32_t)(opretlen + sizeof(O)); - komodo_eventadd(height,symbol,type,opret,O.oplen); + komodo_eventadd(height,symbol,KOMODO_EVENT_OPRETURN,opret,O.oplen); + if ( sp != 0 ) + komodo_opreturn(height,value,buf,opretlen,txid,vout); } -void komodo_eventadd_deposit(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) +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(int32_t height) +{ + while ( Komodo_numevents > 0 ) + { + if ( (ep= Komodo_events[Komodo_numevents-1]) != 0 ) + { + if ( ep->height < height ) + break; + 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(char *symbol,int32_t height,int32_t fiatheight,bits256 fiattxid,uint16_t fiatvout,bits256 kmdtxid,uint16_t kmdvout,uint64_t fiatoshis) +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(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) +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(char *symbol,int32_t height,bits256 kmdtxid,uint16_t kmdvout,int32_t fiatheight,bits256 fiattxid,uint16_t fiatvout,uint64_t komodoshis) +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 // 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..693bf429a 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -23,7 +23,7 @@ int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33) 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 +34,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..8fc4f765a 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -52,13 +52,6 @@ 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) { struct nutxo_entry *np; @@ -124,17 +117,20 @@ int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height) 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; + if ( Pubkeys == 0 ) + Pubkeys = (struct knotaries_entry *)calloc(KOMODO_MAXBLOCKS / KOMODO_ELECTION_GAP,sizeof(*Pubkeys)); memset(&N,0,sizeof(N)); if ( height > 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\n",htind,height,origheight); } else htind = 0; pthread_mutex_lock(&komodo_mutex); for (k=0; k nHeight ) @@ -201,9 +197,26 @@ 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; +} + +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) diff --git a/src/komodo_pax.h b/src/komodo_pax.h index a10c4fc0c..d63ffa0f6 100644 --- a/src/komodo_pax.h +++ b/src/komodo_pax.h @@ -13,8 +13,6 @@ * * ******************************************************************************/ -int32_t NUM_PRICES; uint32_t *PVALS; - #define USD 0 #define MAX_CURRENCIES 32 diff --git a/src/main.cpp b/src/main.cpp index be9d34e40..ba779dc19 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3328,14 +3328,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..661e0c9d0 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -106,7 +106,8 @@ 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; +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); 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()));