diff --git a/src/komodo.h b/src/komodo.h index 5b45126a5..9fe331708 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -19,10 +19,6 @@ // Todo: // verify: reorgs -// non komodod (non-hardfork) todo: -// a. automate notarization fee payouts -// b. automated distribution of test REVS snapshot - #define KOMODO_ASSETCHAINS_WAITNOTARIZE #define KOMODO_PAXMAX (10000 * COIN) @@ -380,7 +376,7 @@ 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],sizeof(*notarizedheightp),(uint8_t *)notarizedheightp); len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid); - if ( notarized != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height ) + 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) ) { sp->NOTARIZED_HEIGHT = *notarizedheightp; sp->NOTARIZED_HASH = kmdtxid; @@ -413,7 +409,7 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen-len+4+3+(scriptbuf[1] == 0x4d),j); } } - } else printf("notarized.%d %llx reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",notarized,(long long)signedmask,height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]); + } else 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]); } else if ( i == 0 && j == 1 && opretlen == 149 ) { diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index dbb887963..5af538d9b 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -346,6 +346,82 @@ char *komodo_issuemethod(char *userpass,char *method,char *params,uint16_t port) return(retstr2); } +int32_t komodo_verifynotarizedscript(int32_t height,uint8_t *script,int32_t len,uint256 NOTARIZED_HASH) +{ + int32_t i; uint256 hash; char params[256]; + for (i=0; i<32; i++) + ((uint8_t *)&hash)[i] = script[2+i]; + if ( hash == NOTARIZED_HASH ) + return(0); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&NOTARIZED_HASH)[i]); + printf(" notarized, "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&hash)[i]); + printf(" opreturn from [%s] ht.%d\n",ASSETCHAINS_SYMBOL,height); + return(-1); +} + +int32_t komodo_verifynotarization(char *symbol,char *dest,int32_t height,int32_t NOTARIZED_HEIGHT,uint256 NOTARIZED_HASH,uint256 NOTARIZED_DESTTXID) +{ + char params[256],*jsonstr,*hexstr; uint8_t script[8192]; int32_t n,len,retval = -1; cJSON *json,*txjson,*vouts,*vout,*skey; + /*params[0] = '['; + params[1] = '"'; + for (i=0; i<32; i++) + sprintf(¶ms[i*2 + 2],"%02x",((uint8_t *)&NOTARIZED_DESTTXID)[31-i]); + strcat(params,"\", 1]");*/ + sprintf(params,"[\"%s\", 1]",NOTARIZED_DESTTXID.ToString().c_str()); + if ( strcmp(symbol,ASSETCHAINS_SYMBOL[0]==0?(char *)"KMD":ASSETCHAINS_SYMBOL) != 0 ) + return(0); + //printf("[%s] src.%s dest.%s params.[%s] ht.%d notarized.%d\n",ASSETCHAINS_SYMBOL,symbol,dest,params,height,NOTARIZED_HEIGHT); + if ( strcmp(dest,"KMD") == 0 ) + { + if ( KMDUSERPASS[0] != 0 ) + jsonstr = komodo_issuemethod(KMDUSERPASS,(char *)"getrawtransaction",params,7771); + //else jsonstr = _dex_getrawtransaction(); + else return(0); // need universal way to issue DEX* API, since notaries mine most blocks, this ok + } + else if ( strcmp(dest,"BTC") == 0 ) + { + if ( BTCUSERPASS[0] != 0 ) + { + //printf("BTCUSERPASS.(%s)\n",BTCUSERPASS); + jsonstr = komodo_issuemethod(BTCUSERPASS,(char *)"getrawtransaction",params,8332); + } + //else jsonstr = _dex_getrawtransaction(); + else return(0); + } + else + { + printf("[%s] verifynotarization error unexpected dest.(%s)\n",ASSETCHAINS_SYMBOL,dest); + return(-1); + } + if ( jsonstr != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (txjson= jobj(json,(char *)"result")) != 0 && (vouts= jarray(&n,txjson,(char *)"vout")) > 0 ) + { + vout = jitem(vouts,n-1); + //printf("vout.(%s)\n",jprint(vout,0)); + if ( (skey= jobj(vout,(char *)"scriptPubKey")) != 0 ) + { + if ( (hexstr= jstr(skey,(char *)"hex")) != 0 ) + { + //printf("HEX.(%s)\n",hexstr); + len = strlen(hexstr) >> 1; + decode_hex(script,len,hexstr); + retval = komodo_verifynotarizedscript(height,script,len,NOTARIZED_HASH); + } + } + } + free_json(txjson); + } + free(jsonstr); + } + return(retval); +} + uint256 komodo_getblockhash(int32_t height) { uint256 hash; char params[128],*hexstr,*jsonstr; cJSON *result; int32_t i; uint8_t revbuf[32]; diff --git a/src/komodo_events.h b/src/komodo_events.h index cb7861107..d8aafb333 100644 --- a/src/komodo_events.h +++ b/src/komodo_events.h @@ -39,14 +39,19 @@ struct komodo_event *komodo_eventadd(struct komodo_state *sp,int32_t height,char 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(sp,height,symbol,KOMODO_EVENT_NOTARIZED,(uint8_t *)&N,sizeof(N)); - if ( sp != 0 ) - komodo_notarized_update(sp,height,notarizedheight,notarized_hash,notarized_desttxid); + if ( komodo_verifynotarization(symbol,dest,height,notarizedheight,notarized_hash,notarized_desttxid) != 0 ) + printf("%s error validating notarization\n",ASSETCHAINS_SYMBOL); + else + { + memset(&N,0,sizeof(N)); + N.blockhash = notarized_hash; + N.desttxid = notarized_desttxid; + N.notarizedheight = notarizedheight; + strcpy(N.dest,dest); + komodo_eventadd(sp,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]) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index e00357ee7..007b16b0b 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -763,7 +763,7 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 } } } - else if ( kmdheight > 91800 ) + else if ( kmdheight > 91800 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) printf("pax %s deposit %.8f rejected kmdheight.%d %.8f KMD\n",base,dstr(fiatoshis),kmdheight,dstr(value)); } } @@ -819,11 +819,11 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 } else if ( opretbuf[0] == 'W' )//&& opretlen >= 38 ) { - if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 ) + if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 && strcmp("KMD",(char *)&opretbuf[opretlen-4]) != 0 ) { for (i=0; iredeemed += value; pax->didstats = 1; - //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) + if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) printf("ht.%d %.8f ########### %p redeemed %s += %.8f %.8f kht.%d ht.%d\n",height,dstr(value),basesp,CURRENCIES[baseids[i]],dstr(value),dstr(srcvalues[i]),kmdheights[i],otherheights[i]); } } diff --git a/src/komodo_globals.h b/src/komodo_globals.h index 1e985da6b..08c80ff79 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -47,5 +47,5 @@ uint32_t ASSETCHAINS_MAGIC = 2387029918; uint64_t ASSETCHAINS_SUPPLY = 10; uint32_t KOMODO_INITDONE; -char KMDUSERPASS[1024]; uint16_t BITCOIND_PORT = 7771; +char KMDUSERPASS[4096],BTCUSERPASS[4096]; uint16_t BITCOIND_PORT = 7771; uint64_t PENDING_KOMODO_TX; diff --git a/src/komodo_notary.h b/src/komodo_notary.h index b6de38df8..fc77d1222 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -13,6 +13,8 @@ * * ******************************************************************************/ +#define KOMODO_MAINNET_START 178999 + const char *Notaries_genesis[][2] = { { "jl777_testA", "03b7621b44118017a16043f19b30cc8a4cfe068ac4e42417bae16ba460c80f3828" }, @@ -52,6 +54,12 @@ const char *Notaries_genesis[][2] = { "titomane_SH", "035f49d7a308dd9a209e894321f010d21b7793461b0c89d6d9231a3fe5f68d9960" }, }; +const char *Notaries_elected[][2] = // update with all elected notaries +{ + { "jl777_testA", "03b7621b44118017a16043f19b30cc8a4cfe068ac4e42417bae16ba460c80f3828" }, + { "jl777_testB", "02ebfc784a4ba768aad88d44d1045d240d47b26e248cafaf1c5169a42d7a61d344" }, +}; + int32_t komodo_ratify_threshold(int32_t height,uint64_t signedmask) { int32_t htind,numnotaries,i,wt = 0; @@ -226,6 +234,8 @@ int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *n void komodo_init(int32_t height) { static int didinit; uint256 zero; int32_t i,k,n; uint8_t pubkeys[64][33]; + if ( 0 && height != 0 ) + printf("komodo_init ht.%d didinit.%d\n",height,didinit); if ( didinit == 0 ) { pthread_mutex_init(&komodo_mutex,NULL); @@ -246,6 +256,18 @@ void komodo_init(int32_t height) // Minerids[i] = -2; didinit = 1; } + else if ( height == KOMODO_MAINNET_START ) + { + n = (int32_t)(sizeof(Notaries_elected)/sizeof(*Notaries_elected)); + for (k=0; k userpass.(%s)\n",fname,KMDUSERPASS); - } else printf("couldnt open.(%s)\n",fname); + strcpy(fname,GetDataDir().string().c_str()); +#ifdef WIN32 + while ( fname[strlen(fname)-1] != '\\' ) + fname[strlen(fname)-1] = 0; + if ( iter == 0 ) + strcat(fname,".komodo\\komodo.conf"); + else strcat(fname,".bitcoin\\bitcoin.conf"); +#else + while ( fname[strlen(fname)-1] != '/' ) + fname[strlen(fname)-1] = 0; +#ifdef __APPLE__ + if ( iter == 0 ) + strcat(fname,"Komodo/Komodo.conf"); + else strcat(fname,"Bitcoin/Bitcoin.conf"); +#else + if ( iter == 0 ) + strcat(fname,".komodo/komodo.conf"); + else strcat(fname,".bitcoin/bitcoin.conf"); +#endif +#endif + if ( (fp= fopen(fname,"rb")) != 0 ) + { + komodo_userpass(username,password,fp); + sprintf(iter == 0 ? KMDUSERPASS : BTCUSERPASS,"%s:%s",username,password); + fclose(fp); + //printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS); + } else printf("couldnt open.(%s)\n",fname); + } } //fprintf(stderr,"%s chain params initialized\n",ASSETCHAINS_SYMBOL); }