diff --git a/src/miner.h b/src/miner.h --- a/src/miner.h +++ b/src/miner.h @@ -226,9 +226,8 @@ }; /** Modify the extranonce in a block */ -void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, - uint64_t nExcessiveBlockSize, - unsigned int &nExtraNonce); +uint32_t IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, + uint64_t nExcessiveBlockSize); int64_t UpdateTime(CBlockHeader *pblock, const CChainParams &chainParams, const CBlockIndex *pindexPrev); #endif // BITCOIN_MINER_H diff --git a/src/miner.cpp b/src/miner.cpp --- a/src/miner.cpp +++ b/src/miner.cpp @@ -27,6 +27,7 @@ #include #include +#include #include int64_t UpdateTime(CBlockHeader *pblock, const CChainParams &chainParams, @@ -543,22 +544,17 @@ return vec; } -void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, - uint64_t nExcessiveBlockSize, - unsigned int &nExtraNonce) { +uint32_t IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, + uint64_t nExcessiveBlockSize) { // Update nExtraNonce - static uint256 hashPrevBlock; - if (hashPrevBlock != pblock->hashPrevBlock) { - nExtraNonce = 0; - hashPrevBlock = pblock->hashPrevBlock; - } + static std::atomic_uint32_t nExtraNonce; + auto extraNonceReturn{nExtraNonce++}; - ++nExtraNonce; // Height first in coinbase required for block.version=2 unsigned int nHeight = pindexPrev->nHeight + 1; CMutableTransaction txCoinbase(*pblock->vtx[0]); txCoinbase.vin[0].scriptSig = - (CScript() << nHeight << CScriptNum(nExtraNonce) + (CScript() << nHeight << CScriptNum(extraNonceReturn) << getExcessiveBlockSizeSig(nExcessiveBlockSize)); // Make sure the coinbase is big enough. @@ -573,4 +569,5 @@ pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase)); pblock->hashMerkleRoot = BlockMerkleRoot(*pblock); + return extraNonceReturn; } diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -120,14 +120,13 @@ static bool GenerateBlock(const Config &config, ChainstateManager &chainman, CBlock &block, uint64_t &max_tries, - unsigned int &extra_nonce, BlockHash &block_hash) { + BlockHash &block_hash) { block_hash.SetNull(); const uint64_t nExcessiveBlockSize = config.GetMaxBlockSize(); { LOCK(cs_main); - IncrementExtraNonce(&block, ::ChainActive().Tip(), nExcessiveBlockSize, - extra_nonce); + IncrementExtraNonce(&block, ::ChainActive().Tip(), nExcessiveBlockSize); } const Consensus::Params ¶ms = config.GetChainParams().GetConsensus(); @@ -172,7 +171,6 @@ nHeightEnd = nHeight + nGenerate; } - unsigned int nExtraNonce = 0; UniValue blockHashes(UniValue::VARR); while (nHeight < nHeightEnd && !ShutdownRequested()) { std::unique_ptr pblocktemplate( @@ -185,8 +183,7 @@ CBlock *pblock = &pblocktemplate->block; BlockHash block_hash; - if (!GenerateBlock(config, chainman, *pblock, nMaxTries, nExtraNonce, - block_hash)) { + if (!GenerateBlock(config, chainman, *pblock, nMaxTries, block_hash)) { break; } @@ -453,10 +450,9 @@ BlockHash block_hash; uint64_t max_tries{1000000}; - unsigned int extra_nonce{0}; if (!GenerateBlock(config, EnsureChainman(request.context), block, - max_tries, extra_nonce, block_hash) || + max_tries, block_hash) || block_hash.IsNull()) { throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block."); } diff --git a/src/test/blockfilter_index_tests.cpp b/src/test/blockfilter_index_tests.cpp --- a/src/test/blockfilter_index_tests.cpp +++ b/src/test/blockfilter_index_tests.cpp @@ -80,8 +80,7 @@ block.vtx.push_back(MakeTransactionRef(tx)); } // IncrementExtraNonce creates a valid coinbase and merkleRoot - unsigned int extraNonce = 0; - IncrementExtraNonce(&block, prev, config.GetMaxBlockSize(), extraNonce); + IncrementExtraNonce(&block, prev, config.GetMaxBlockSize()); while (!CheckProofOfWork(block.GetHash(), block.nBits, config.GetChainParams().GetConsensus())) { diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -237,9 +237,8 @@ CBlock *pblock = &pblocktemplate->block; // IncrementExtraNonce creates a valid coinbase and merkleRoot - unsigned int extraNonce = 0; - IncrementExtraNonce(pblock, ::ChainActive().Tip(), config.GetMaxBlockSize(), - extraNonce); + auto extraNonce = IncrementExtraNonce(pblock, ::ChainActive().Tip(), + config.GetMaxBlockSize()); unsigned int nHeight = ::ChainActive().Tip()->nHeight + 1; std::vector vec(cbmsg.begin(), cbmsg.end()); BOOST_CHECK(pblock->vtx[0]->vin[0].scriptSig == diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -275,9 +275,8 @@ // IncrementExtraNonce creates a valid coinbase and merkleRoot { LOCK(cs_main); - unsigned int extraNonce = 0; IncrementExtraNonce(&block, ::ChainActive().Tip(), - config.GetMaxBlockSize(), extraNonce); + config.GetMaxBlockSize()); } const Consensus::Params ¶ms = config.GetChainParams().GetConsensus(); diff --git a/test/functional/rpc_dumptxoutset.py b/test/functional/rpc_dumptxoutset.py --- a/test/functional/rpc_dumptxoutset.py +++ b/test/functional/rpc_dumptxoutset.py @@ -35,14 +35,14 @@ # Blockhash should be deterministic based on mocked time. assert_equal( out['base_hash'], - '65d0aec2439aae14373c153f596fb90a87b643d9bff3e65f250aa8f055e6816b') + '0ad635f9fdebb88f4c1d8d3e3348b9e0bc065a09f7c0f4a22aa82efcff52c282') with open(str(expected_path), 'rb') as f: digest = hashlib.sha256(f.read()).hexdigest() # UTXO snapshot hash should be deterministic based on mocked time. assert_equal( digest, - '05957e146e38153d84e9294999cc24f0dcdb9902c4834b32c79ae8e8985babea') + '95eb6944beb0fe3e93e63e50836ee2c9e2d4e755c4eba16314232af005c319a3') # Specifying a path to an existing file will fail. assert_raises_rpc_error(