From 43aa4a7d90aef5615e586852cfbf539f21e577e5 Mon Sep 17 00:00:00 2001 From: Duke Date: Mon, 25 Dec 2023 14:05:05 -0500 Subject: [PATCH 1/6] Create a local variable instead of modifying the global ASSETCHAINS_SCRIPTPUB --- src/hush_bitcoind.h | 37 ++++++++++++++++++++++--------------- src/miner.cpp | 24 ++++++++++++++---------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/hush_bitcoind.h b/src/hush_bitcoind.h index 19c0ad9b3..58428d75c 100644 --- a/src/hush_bitcoind.h +++ b/src/hush_bitcoind.h @@ -1619,7 +1619,10 @@ int64_t hush_checkcommission(CBlock *pblock,int32_t height) if(fDebug) fprintf(stderr,"%s at height=%d\n",__func__,height); int64_t checktoshis=0; uint8_t *script,scripthex[8192]; int32_t scriptlen,matched = 0; static bool didinit = false; - ASSETCHAINS_SCRIPTPUB = devtax_scriptpub_for_height(height); + + // Create a local variable instead of modifying the global ASSETCHAINS_SCRIPTPUB + auto assetchains_scriptpub = devtax_scriptpub_for_height(height); + if ( ASSETCHAINS_COMMISSION != 0 || ASSETCHAINS_FOUNDERS_REWARD != 0 ) { checktoshis = the_commission(pblock,height); @@ -1639,19 +1642,22 @@ int64_t hush_checkcommission(CBlock *pblock,int32_t height) fprintf(stderr,"%02x",script[i]); fprintf(stderr," vout[1] %.8f vs %.8f\n",(double)checktoshis/COIN,(double)pblock->vtx[0].vout[1].nValue/COIN); } - if ( ASSETCHAINS_SCRIPTPUB.size() > 1 ) + if ( assetchains_scriptpub.size() > 1 ) { static bool didinit = false; if ( !didinit && height > HUSH_EARLYTXID_HEIGHT && HUSH_EARLYTXID != zeroid && hush_appendACscriptpub() ) { - fprintf(stderr, "appended CC_op_return to ASSETCHAINS_SCRIPTPUB.%s\n", ASSETCHAINS_SCRIPTPUB.c_str()); + fprintf(stderr, "appended CC_op_return to assetchains_scriptpub.%s\n", assetchains_scriptpub.c_str()); didinit = true; } - if ( ASSETCHAINS_SCRIPTPUB.size()/2 == scriptlen && scriptlen < sizeof(scripthex) ) + if ( assetchains_scriptpub.size()/2 == scriptlen && scriptlen < sizeof(scripthex) ) { - decode_hex(scripthex,scriptlen,(char *)ASSETCHAINS_SCRIPTPUB.c_str()); - if ( memcmp(scripthex,script,scriptlen) == 0 ) + decode_hex(scripthex,scriptlen,(char *)assetchains_scriptpub.c_str()); + if ( memcmp(scripthex,script,scriptlen) == 0 ) { matched = scriptlen; + } else { + fprintf(stderr, "%s: assetchains_scriptpub != scripthex scriptlen=%d\n", __func__, scriptlen); + } } } else if ( scriptlen == 35 && script[0] == 33 && script[34] == OP_CHECKSIG && memcmp(script+1,ASSETCHAINS_OVERRIDE_PUBKEY33,33) == 0 ) @@ -1660,19 +1666,19 @@ int64_t hush_checkcommission(CBlock *pblock,int32_t height) matched = 25; if ( matched == 0 ) { - if ( ASSETCHAINS_SCRIPTPUB.size() > 1 ) + if ( assetchains_scriptpub.size() > 1 ) { int32_t i; - fprintf(stderr,"%s: ASSETCHAINS_SCRIPTPUB=", __func__); - for (i=0; i 1 && ASSETCHAINS_SCRIPTPUB[ASSETCHAINS_SCRIPTPUB.back()] != 49 && ASSETCHAINS_SCRIPTPUB[ASSETCHAINS_SCRIPTPUB.back()-1] != 51 ) { + // Create a local variable instead of modifying the global assetchains_scriptpub + auto assetchains_scriptpub = devtax_scriptpub_for_height(height); + if ( assetchains_scriptpub.size() > 1 && assetchains_scriptpub[assetchains_scriptpub.back()] != 49 && assetchains_scriptpub[assetchains_scriptpub.back()-1] != 51 ) { int32_t scriptlen; uint8_t scripthex[10000]; script = (uint8_t *)&pblock->vtx[0].vout[0].scriptPubKey[0]; scriptlen = (int32_t)pblock->vtx[0].vout[0].scriptPubKey.size(); - if ( ASSETCHAINS_SCRIPTPUB.size()/2 == scriptlen && scriptlen < sizeof(scripthex) ) { - decode_hex(scripthex,scriptlen,(char *)ASSETCHAINS_SCRIPTPUB.c_str()); + if ( assetchains_scriptpub.size()/2 == scriptlen && scriptlen < sizeof(scripthex) ) { + decode_hex(scripthex,scriptlen,(char *)assetchains_scriptpub.c_str()); if ( memcmp(scripthex,script,scriptlen) != 0 ) return(-1); } else return(-1); diff --git a/src/miner.cpp b/src/miner.cpp index 94033bc32..45ec30174 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -660,27 +660,29 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 pblock->vtx[0] = txNew; - ASSETCHAINS_SCRIPTPUB = devtax_scriptpub_for_height(nHeight); - if ( nHeight > 1 && SMART_CHAIN_SYMBOL[0] != 0 && (ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 || ASSETCHAINS_SCRIPTPUB.size() > 1) && (ASSETCHAINS_COMMISSION != 0 || ASSETCHAINS_FOUNDERS_REWARD != 0) && (commission= the_commission((CBlock*)&pblocktemplate->block,(int32_t)nHeight)) != 0 ) + // Create a local variable instead of modifying the global ASSETCHAINS_SCRIPTPUB + auto assetchains_scriptpub = devtax_scriptpub_for_height(nHeight); + + if ( nHeight > 1 && SMART_CHAIN_SYMBOL[0] != 0 && (ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 || assetchains_scriptpub.size() > 1) && (ASSETCHAINS_COMMISSION != 0 || ASSETCHAINS_FOUNDERS_REWARD != 0) && (commission= the_commission((CBlock*)&pblocktemplate->block,(int32_t)nHeight)) != 0 ) { int32_t i; uint8_t *ptr; txNew.vout.resize(2); txNew.vout[1].nValue = commission; - if ( ASSETCHAINS_SCRIPTPUB.size() > 1 ) + if ( assetchains_scriptpub.size() > 1 ) { static bool didinit = false; if ( !didinit && nHeight > HUSH_EARLYTXID_HEIGHT && HUSH_EARLYTXID != zeroid && hush_appendACscriptpub() ) { - fprintf(stderr, "appended ccopreturn to ASSETCHAINS_SCRIPTPUB.%s\n", ASSETCHAINS_SCRIPTPUB.c_str()); + fprintf(stderr, "appended ccopreturn to assetchains_scriptpub.%s\n", assetchains_scriptpub.c_str()); didinit = true; } //fprintf(stderr,"mine to -ac_script\n"); //txNew.vout[1].scriptPubKey = CScript() << ParseHex(); - int32_t len = strlen(ASSETCHAINS_SCRIPTPUB.c_str()); + int32_t len = strlen(assetchains_scriptpub.c_str()); len >>= 1; txNew.vout[1].scriptPubKey.resize(len); ptr = (uint8_t *)&txNew.vout[1].scriptPubKey[0]; - decode_hex(ptr,len,(char *)ASSETCHAINS_SCRIPTPUB.c_str()); + decode_hex(ptr,len,(char *)assetchains_scriptpub.c_str()); } else { txNew.vout[1].scriptPubKey.resize(35); ptr = (uint8_t *)&txNew.vout[1].scriptPubKey[0]; @@ -850,8 +852,10 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, int32_t nHeight, { CPubKey pubkey; CScript scriptPubKey; uint8_t *script,*ptr; int32_t i,len; // fprintf(stderr,"%s: with nHeight=%d\n", __func__, nHeight); - ASSETCHAINS_SCRIPTPUB = devtax_scriptpub_for_height(nHeight); - if ( nHeight == 1 && ASSETCHAINS_COMMISSION != 0 && ASSETCHAINS_SCRIPTPUB[ASSETCHAINS_SCRIPTPUB.back()] != 49 && ASSETCHAINS_SCRIPTPUB[ASSETCHAINS_SCRIPTPUB.back()-1] != 51 ) + + // Create a local variable instead of modifying the global assetchains_scriptpub + auto assetchains_scriptpub = devtax_scriptpub_for_height(nHeight); + if ( nHeight == 1 && ASSETCHAINS_COMMISSION != 0 && assetchains_scriptpub[assetchains_scriptpub.back()] != 49 && assetchains_scriptpub[assetchains_scriptpub.back()-1] != 51 ) { if ( ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 ) { @@ -859,11 +863,11 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, int32_t nHeight, scriptPubKey = CScript() << ParseHex(HexStr(pubkey)) << OP_CHECKSIG; // fprintf(stderr,"%s: with pubkey=%s\n", __func__, HexStr(pubkey).c_str() ); } else { - len = strlen(ASSETCHAINS_SCRIPTPUB.c_str()); + len = strlen(assetchains_scriptpub.c_str()); len >>= 1; scriptPubKey.resize(len); ptr = (uint8_t *)&scriptPubKey[0]; - decode_hex(ptr,len,(char *)ASSETCHAINS_SCRIPTPUB.c_str()); + decode_hex(ptr,len,(char *)assetchains_scriptpub.c_str()); } } else if ( USE_EXTERNAL_PUBKEY != 0 ) { //fprintf(stderr,"use notary pubkey\n"); From 77ab75c01d6a16c6c9489f0c403599465f4ad1ff Mon Sep 17 00:00:00 2001 From: Duke Date: Sun, 24 Dec 2023 12:14:37 -0800 Subject: [PATCH 2/6] Document hush_block2pubkey33() --- src/hush_bitcoind.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/hush_bitcoind.h b/src/hush_bitcoind.h index 58428d75c..e44c1eb6d 100644 --- a/src/hush_bitcoind.h +++ b/src/hush_bitcoind.h @@ -724,6 +724,7 @@ int32_t hush_block2height(CBlock *block) return(height); } +// return true if the first output of the first tx in a block is valid int32_t hush_block2pubkey33(uint8_t *pubkey33,CBlock *block) { int32_t n; From 0f5e291f9f0136614fe2756b79dbc90c2c6cc3f1 Mon Sep 17 00:00:00 2001 From: Duke Date: Sun, 24 Dec 2023 15:18:32 -0800 Subject: [PATCH 3/6] Remove unused pubkeys variable --- src/hush_bitcoind.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hush_bitcoind.h b/src/hush_bitcoind.h index e44c1eb6d..02b6b09b6 100644 --- a/src/hush_bitcoind.h +++ b/src/hush_bitcoind.h @@ -1699,7 +1699,8 @@ bool HUSH_TEST_ASSETCHAIN_SKIP_POW = 0; int32_t hush_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) { - uint256 hash,merkleroot; arith_uint256 bnTarget,bhash; bool fNegative,fOverflow; uint8_t *script,pubkey33[33],pubkeys[64][33]; int32_t i,scriptlen,possible,PoSperc,is_PoSblock=0,n,failed = 0,notaryid = -1; int64_t checktoshis,value; CBlockIndex *pprev; + uint256 hash,merkleroot; arith_uint256 bnTarget,bhash; bool fNegative,fOverflow; uint8_t *script,pubkey33[33]; + int32_t i,scriptlen,possible,PoSperc,is_PoSblock=0,n,failed = 0,notaryid = -1; int64_t checktoshis,value; CBlockIndex *pprev; if ( HUSH_TEST_ASSETCHAIN_SKIP_POW == 0 && Params().NetworkIDString() == "regtest" ) HUSH_TEST_ASSETCHAIN_SKIP_POW = 1; if ( !CheckEquihashSolution(pblock, Params()) ) From d8840beb3bd3aa07e641255c21c686bb62be7275 Mon Sep 17 00:00:00 2001 From: Duke Date: Sun, 24 Dec 2023 15:12:50 -0800 Subject: [PATCH 4/6] Remove unused code --- src/hush_bitcoind.h | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/hush_bitcoind.h b/src/hush_bitcoind.h index 02b6b09b6..75548da26 100644 --- a/src/hush_bitcoind.h +++ b/src/hush_bitcoind.h @@ -1726,18 +1726,7 @@ int32_t hush_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) if ( (SMART_CHAIN_SYMBOL[0] != 0) && bhash > bnTarget ) { failed = 1; - if ( height > 0 && SMART_CHAIN_SYMBOL[0] == 0 ) // for the fast case - { - if ( (n= hush_notaries(pubkeys,height,pblock->nTime)) > 0 ) - { - for (i=0; i Date: Thu, 14 Dec 2023 19:33:37 -0800 Subject: [PATCH 5/6] Remove unused variables in BitcoinMiner/RandomXMiner which saves about 3.5KB of memory per mining thread --- src/miner.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 45ec30174..5b0544200 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1270,7 +1270,6 @@ void static RandomXMiner() LogPrintf("Running HushRandomXMiner with %u transactions in block (%u bytes)\n",pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION)); // Search - uint8_t pubkeys[66][33]; arith_uint256 bnMaxPoSdiff; uint32_t blocktimes[66]; int mids[256],nonzpkeys,i,j,externalflag; uint32_t savebits; int64_t nStart = GetTime(); pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus()); @@ -1617,7 +1616,7 @@ void static BitcoinMiner() LogPrintf("Running HushMiner.%s with %u transactions in block (%u bytes)\n",solver.c_str(),pblock->vtx.size(),::GetSerializeSize(*pblock,SER_NETWORK,PROTOCOL_VERSION)); // Search - uint8_t pubkeys[66][33]; arith_uint256 bnMaxPoSdiff; uint32_t blocktimes[66]; int mids[256],nonzpkeys,i,j,externalflag; uint32_t savebits; int64_t nStart = GetTime(); + uint32_t savebits; int64_t nStart = GetTime(); pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus()); savebits = pblock->nBits; HASHTarget = arith_uint256().SetCompact(savebits); From 497ace0922d1f7b51b99398e265ff11673dcc810 Mon Sep 17 00:00:00 2001 From: Duke Date: Sun, 24 Dec 2023 12:01:20 -0800 Subject: [PATCH 6/6] Add debug output to stderr when decode_hex() is called on invalid hex input --- src/hush_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hush_utils.h b/src/hush_utils.h index d03c1e055..060464c1e 100644 --- a/src/hush_utils.h +++ b/src/hush_utils.h @@ -858,7 +858,7 @@ int32_t unhex(char c) int32_t hex; if ( (hex= _unhex(c)) < 0 ) { - //printf("unhex: illegal hexchar.(%c)\n",c); + fprintf(stderr,"unhex: illegal hexchar.(%c)\n",c); } return(hex); }