diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -207,7 +207,7 @@ bool new_block; auto sc = std::make_shared(block.GetHash()); RegisterSharedValidationInterface(sc); - bool accepted = chainman.ProcessNewBlock(config, blockptr, + bool accepted = chainman.ProcessNewBlock(blockptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block); diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -3929,8 +3929,8 @@ // Now process all the headers. BlockValidationState state; - if (!m_chainman.ProcessNewBlockHeaders( - config, headers, /*min_pow_checked=*/true, state, &pindexLast)) { + if (!m_chainman.ProcessNewBlockHeaders(headers, /*min_pow_checked=*/true, + state, &pindexLast)) { if (state.IsInvalid()) { MaybePunishNodeForBlock(pfrom.GetId(), state, via_compact_block, "invalid header received"); @@ -4324,7 +4324,7 @@ bool force_processing, bool min_pow_checked) { bool new_block{false}; - m_chainman.ProcessNewBlock(config, block, force_processing, min_pow_checked, + m_chainman.ProcessNewBlock(block, force_processing, min_pow_checked, &new_block); if (new_block) { node.m_last_block_time = GetTime(); @@ -5461,7 +5461,7 @@ const CBlockIndex *pindex = nullptr; BlockValidationState state; - if (!m_chainman.ProcessNewBlockHeaders(config, {cmpctblock.header}, + if (!m_chainman.ProcessNewBlockHeaders({cmpctblock.header}, /*min_pow_checked=*/true, state, &pindex)) { if (state.IsInvalid()) { diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -133,13 +133,12 @@ }; } -static bool GenerateBlock(const Config &config, ChainstateManager &chainman, - CBlock &block, uint64_t &max_tries, - BlockHash &block_hash) { +static bool GenerateBlock(ChainstateManager &chainman, CBlock &block, + uint64_t &max_tries, BlockHash &block_hash) { block_hash.SetNull(); block.hashMerkleRoot = BlockMerkleRoot(block); - const Consensus::Params ¶ms = config.GetChainParams().GetConsensus(); + const Consensus::Params ¶ms = chainman.GetConsensus(); while (max_tries > 0 && block.nNonce < std::numeric_limits::max() && @@ -157,7 +156,7 @@ std::shared_ptr shared_pblock = std::make_shared(block); - if (!chainman.ProcessNewBlock(config, shared_pblock, + if (!chainman.ProcessNewBlock(shared_pblock, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) { throw JSONRPCError(RPC_INTERNAL_ERROR, @@ -168,15 +167,15 @@ return true; } -static UniValue generateBlocks(const Config &config, - ChainstateManager &chainman, +static UniValue generateBlocks(ChainstateManager &chainman, const CTxMemPool &mempool, const CScript &coinbase_script, int nGenerate, uint64_t nMaxTries) { UniValue blockHashes(UniValue::VARR); while (nGenerate > 0 && !ShutdownRequested()) { std::unique_ptr pblocktemplate( - BlockAssembler{config, chainman.ActiveChainstate(), &mempool} + BlockAssembler{chainman.GetConfig(), chainman.ActiveChainstate(), + &mempool} .CreateNewBlock(coinbase_script)); if (!pblocktemplate.get()) { @@ -186,7 +185,7 @@ CBlock *pblock = &pblocktemplate->block; BlockHash block_hash; - if (!GenerateBlock(config, chainman, *pblock, nMaxTries, block_hash)) { + if (!GenerateBlock(chainman, *pblock, nMaxTries, block_hash)) { break; } @@ -278,7 +277,7 @@ const CTxMemPool &mempool = EnsureMemPool(node); ChainstateManager &chainman = EnsureChainman(node); - return generateBlocks(config, chainman, mempool, coinbase_script, + return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries); }, }; @@ -343,7 +342,7 @@ CScript coinbase_script = GetScriptForDestination(destination); - return generateBlocks(config, chainman, mempool, coinbase_script, + return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries); }, }; @@ -476,8 +475,7 @@ BlockHash block_hash; uint64_t max_tries{DEFAULT_MAX_TRIES}; - if (!GenerateBlock(config, chainman, block, max_tries, - block_hash) || + if (!GenerateBlock(chainman, block, max_tries, block_hash) || block_hash.IsNull()) { throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block."); } @@ -1193,7 +1191,7 @@ auto sc = std::make_shared(block.GetHash()); RegisterSharedValidationInterface(sc); - bool accepted = chainman.ProcessNewBlock(config, blockptr, + bool accepted = chainman.ProcessNewBlock(blockptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block); @@ -1246,7 +1244,7 @@ } BlockValidationState state; - chainman.ProcessNewBlockHeaders(config, {h}, + chainman.ProcessNewBlockHeaders({h}, /*min_pow_checked=*/true, state); if (state.IsValid()) { return NullUniValue; 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 @@ -108,8 +108,7 @@ BlockValidationState state; if (!Assert(m_node.chainman) - ->ProcessNewBlockHeaders(GetConfig(), {header}, true, state, - &pindex)) { + ->ProcessNewBlockHeaders({header}, true, state, &pindex)) { return false; } } @@ -197,9 +196,8 @@ uint256 chainA_last_header = last_header; for (size_t i = 0; i < 2; i++) { const auto &block = chainA[i]; - BOOST_REQUIRE( - Assert(m_node.chainman) - ->ProcessNewBlock(GetConfig(), block, true, true, nullptr)); + BOOST_REQUIRE(Assert(m_node.chainman) + ->ProcessNewBlock(block, true, true, nullptr)); } for (size_t i = 0; i < 2; i++) { const auto &block = chainA[i]; @@ -218,9 +216,8 @@ uint256 chainB_last_header = last_header; for (size_t i = 0; i < 3; i++) { const auto &block = chainB[i]; - BOOST_REQUIRE( - Assert(m_node.chainman) - ->ProcessNewBlock(GetConfig(), block, true, true, nullptr)); + BOOST_REQUIRE(Assert(m_node.chainman) + ->ProcessNewBlock(block, true, true, nullptr)); } for (size_t i = 0; i < 3; i++) { const auto &block = chainB[i]; @@ -253,9 +250,8 @@ // Reorg back to chain A. for (size_t i = 2; i < 4; i++) { const auto &block = chainA[i]; - BOOST_REQUIRE( - Assert(m_node.chainman) - ->ProcessNewBlock(GetConfig(), block, true, true, nullptr)); + BOOST_REQUIRE(Assert(m_node.chainman) + ->ProcessNewBlock(block, true, true, nullptr)); } // Check that chain A and B blocks can be retrieved. diff --git a/src/test/checkpoints_tests.cpp b/src/test/checkpoints_tests.cpp --- a/src/test/checkpoints_tests.cpp +++ b/src/test/checkpoints_tests.cpp @@ -103,9 +103,9 @@ { BlockValidationState state; - BOOST_CHECK(Assert(m_node.chainman) - ->ProcessNewBlockHeaders(config, {headerG}, true, state, - &pindex)); + BOOST_CHECK( + Assert(m_node.chainman) + ->ProcessNewBlockHeaders({headerG}, true, state, &pindex)); pindex = nullptr; } @@ -169,9 +169,9 @@ // Headers A and AA should be accepted { BlockValidationState state; - BOOST_CHECK(Assert(m_node.chainman) - ->ProcessNewBlockHeaders(config, {headerA}, true, state, - &pindex)); + BOOST_CHECK( + Assert(m_node.chainman) + ->ProcessNewBlockHeaders({headerA}, true, state, &pindex)); BOOST_CHECK(state.IsValid()); BOOST_CHECK(pindex != nullptr); pindex = nullptr; @@ -179,9 +179,9 @@ { BlockValidationState state; - BOOST_CHECK(Assert(m_node.chainman) - ->ProcessNewBlockHeaders(config, {headerAA}, true, - state, &pindex)); + BOOST_CHECK( + Assert(m_node.chainman) + ->ProcessNewBlockHeaders({headerAA}, true, state, &pindex)); BOOST_CHECK(state.IsValid()); BOOST_CHECK(pindex != nullptr); pindex = nullptr; @@ -190,9 +190,9 @@ // Header B should be rejected { BlockValidationState state; - BOOST_CHECK(!Assert(m_node.chainman) - ->ProcessNewBlockHeaders(config, {headerB}, true, - state, &pindex)); + BOOST_CHECK( + !Assert(m_node.chainman) + ->ProcessNewBlockHeaders({headerB}, true, state, &pindex)); BOOST_CHECK(state.IsInvalid()); BOOST_CHECK(state.GetRejectReason() == "bad-fork-prior-to-checkpoint"); BOOST_CHECK(pindex == nullptr); @@ -208,9 +208,9 @@ // Header AB should be rejected { BlockValidationState state; - BOOST_CHECK(!Assert(m_node.chainman) - ->ProcessNewBlockHeaders(config, {headerAB}, true, - state, &pindex)); + BOOST_CHECK( + !Assert(m_node.chainman) + ->ProcessNewBlockHeaders({headerAB}, true, state, &pindex)); BOOST_CHECK(state.IsInvalid()); BOOST_CHECK(state.GetRejectReason() == "checkpoint mismatch"); BOOST_CHECK(pindex == nullptr); 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 @@ -726,9 +726,8 @@ } std::shared_ptr shared_pblock = std::make_shared(*pblock); - BOOST_CHECK( - Assert(m_node.chainman) - ->ProcessNewBlock(config, shared_pblock, true, true, nullptr)); + BOOST_CHECK(Assert(m_node.chainman) + ->ProcessNewBlock(shared_pblock, true, true, nullptr)); pblock->hashPrevBlock = pblock->GetHash(); } diff --git a/src/test/util/mining.cpp b/src/test/util/mining.cpp --- a/src/test/util/mining.cpp +++ b/src/test/util/mining.cpp @@ -40,8 +40,8 @@ assert(block->nNonce); } - bool processed{Assert(node.chainman) - ->ProcessNewBlock(config, block, true, true, nullptr)}; + bool processed{ + Assert(node.chainman)->ProcessNewBlock(block, true, true, nullptr)}; assert(processed); return CTxIn{block->vtx[0]->GetId(), 0}; 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 @@ -390,7 +390,7 @@ std::shared_ptr shared_pblock = std::make_shared(block); Assert(m_node.chainman) - ->ProcessNewBlock(GetConfig(), shared_pblock, true, true, nullptr); + ->ProcessNewBlock(shared_pblock, true, true, nullptr); return block; } diff --git a/src/test/validation_block_tests.cpp b/src/test/validation_block_tests.cpp --- a/src/test/validation_block_tests.cpp +++ b/src/test/validation_block_tests.cpp @@ -188,13 +188,12 @@ [](std::shared_ptr b) { return b->GetBlockHeader(); }); // Process all the headers so we understand the toplogy of the chain - BOOST_CHECK(Assert(m_node.chainman) - ->ProcessNewBlockHeaders(config, headers, true, state)); + BOOST_CHECK( + Assert(m_node.chainman)->ProcessNewBlockHeaders(headers, true, state)); // Connect the genesis block and drain any outstanding events BOOST_CHECK(Assert(m_node.chainman) ->ProcessNewBlock( - config, std::make_shared(chainParams.GenesisBlock()), true, true, &ignored)); SyncWithValidationInterfaceQueue(); @@ -220,16 +219,16 @@ for (int j = 0; j < 1000; j++) { auto block = blocks[insecure.randrange(blocks.size() - 1)]; Assert(m_node.chainman) - ->ProcessNewBlock(config, block, true, true, &tlignored); + ->ProcessNewBlock(block, true, true, &tlignored); } // to make sure that eventually we process the full chain - do it // here for (auto block : blocks) { if (block->vtx.size() == 1) { - bool processed = Assert(m_node.chainman) - ->ProcessNewBlock(config, block, true, - true, &tlignored); + bool processed = + Assert(m_node.chainman) + ->ProcessNewBlock(block, true, true, &tlignored); assert(processed); } } @@ -260,8 +259,8 @@ // Connect the genesis block bool newBlock; BOOST_CHECK(chainman.ProcessNewBlock( - config, std::make_shared(chainParams.GenesisBlock()), true, - true, &newBlock)); + std::make_shared(chainParams.GenesisBlock()), true, true, + &newBlock)); // Generate an invalid block with a valid header const std::shared_ptr pblock = @@ -270,8 +269,8 @@ // Process the valid header const CBlockIndex *pindexBadBlock; BlockValidationState state; - BOOST_CHECK(chainman.ProcessNewBlockHeaders( - config, {pblock->GetBlockHeader()}, true, state, &pindexBadBlock)); + BOOST_CHECK(chainman.ProcessNewBlockHeaders({pblock->GetBlockHeader()}, + true, state, &pindexBadBlock)); // In order to force the invalid block to be finalized, we set the chain // tip manually. This does not happen under normal conditions. Rewind @@ -285,8 +284,7 @@ activeChainstate.m_chain.SetTip(pindex->pprev); // Process the block. It should be found invalid and finalization reverted. - bool processed = - chainman.ProcessNewBlock(config, pblock, true, true, &newBlock); + bool processed = chainman.ProcessNewBlock(pblock, true, true, &newBlock); assert(processed); BOOST_CHECK(newBlock); { @@ -322,7 +320,7 @@ bool ignored; auto ProcessBlock = [&](std::shared_ptr block) -> bool { return Assert(m_node.chainman) - ->ProcessNewBlock(config, block, /*force_processing=*/true, + ->ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&ignored); }; diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -1211,7 +1211,7 @@ * block index (permanent memory storage), indicating that the header is * known to be part of a sufficiently high-work chain (anti-dos check). */ - bool AcceptBlockHeader(const Config &config, const CBlockHeader &block, + bool AcceptBlockHeader(const CBlockHeader &block, BlockValidationState &state, CBlockIndex **ppindex, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main); @@ -1395,7 +1395,6 @@ * * May not be called in a validationinterface callback. * - * @param[in] config The global config. * @param[in] block The block we want to process. * @param[in] force_processing Process this block even if unrequested; * used for non-network block sources. @@ -1408,8 +1407,7 @@ * was first received via this call. * @returns If the block was processed, independently of block validity */ - bool ProcessNewBlock(const Config &config, - const std::shared_ptr &block, + bool ProcessNewBlock(const std::shared_ptr &block, bool force_processing, bool min_pow_checked, bool *new_block) LOCKS_EXCLUDED(cs_main); @@ -1418,7 +1416,6 @@ * * May not be called in a validationinterface callback. * - * @param[in] config The config. * @param[in] block The block headers themselves. * @param[in] min_pow_checked True if proof-of-work anti-DoS checks have * been done by caller for headers chain @@ -1429,8 +1426,7 @@ * headers. * @return True if block headers were accepted as valid. */ - bool ProcessNewBlockHeaders(const Config &config, - const std::vector &block, + bool ProcessNewBlockHeaders(const std::vector &block, bool min_pow_checked, BlockValidationState &state, const CBlockIndex **ppindex = nullptr) diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4080,12 +4080,12 @@ * * Returns true if the block is successfully added to the block index. */ -bool ChainstateManager::AcceptBlockHeader(const Config &config, - const CBlockHeader &block, +bool ChainstateManager::AcceptBlockHeader(const CBlockHeader &block, BlockValidationState &state, CBlockIndex **ppindex, bool min_pow_checked) { AssertLockHeld(cs_main); + const Config &config = this->GetConfig(); const CChainParams &chainparams = config.GetChainParams(); // Check for duplicate @@ -4211,17 +4211,16 @@ // Exposed wrapper for AcceptBlockHeader bool ChainstateManager::ProcessNewBlockHeaders( - const Config &config, const std::vector &headers, - bool min_pow_checked, BlockValidationState &state, - const CBlockIndex **ppindex) { + const std::vector &headers, bool min_pow_checked, + BlockValidationState &state, const CBlockIndex **ppindex) { AssertLockNotHeld(cs_main); { LOCK(cs_main); for (const CBlockHeader &header : headers) { // Use a temp pindex instead of ppindex to avoid a const_cast CBlockIndex *pindex = nullptr; - bool accepted = AcceptBlockHeader(config, header, state, &pindex, - min_pow_checked); + bool accepted = + AcceptBlockHeader(header, state, &pindex, min_pow_checked); ActiveChainstate().CheckBlockIndex(); if (!accepted) { @@ -4240,7 +4239,7 @@ const CBlockIndex &last_accepted{**ppindex}; const int64_t blocks_left{ (GetTime() - last_accepted.GetBlockTime()) / - config.GetChainParams().GetConsensus().nPowTargetSpacing}; + this->GetConsensus().nPowTargetSpacing}; const double progress{100.0 * last_accepted.nHeight / (last_accepted.nHeight + blocks_left)}; LogPrintf("Synchronizing blockheaders, height: %d (~%.2f%%)\n", @@ -4313,8 +4312,8 @@ CBlockIndex *pindex = nullptr; - bool accepted_header{m_chainman.AcceptBlockHeader( - config, block, state, &pindex, min_pow_checked)}; + bool accepted_header{ + m_chainman.AcceptBlockHeader(block, state, &pindex, min_pow_checked)}; CheckBlockIndex(); if (!accepted_header) { @@ -4460,8 +4459,8 @@ } bool ChainstateManager::ProcessNewBlock( - const Config &config, const std::shared_ptr &block, - bool force_processing, bool min_pow_checked, bool *new_block) { + const std::shared_ptr &block, bool force_processing, + bool min_pow_checked, bool *new_block) { AssertLockNotHeld(cs_main); { @@ -4485,14 +4484,13 @@ // https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-February/016697.html. // Because CheckBlock() is not very expensive, the anti-DoS benefits of // caching failure (of a definitely-invalid block) are not substantial. - bool ret = - CheckBlock(*block, state, config.GetChainParams().GetConsensus(), - BlockValidationOptions(config)); + bool ret = CheckBlock(*block, state, this->GetConsensus(), + BlockValidationOptions(this->GetConfig())); if (ret) { // Store to disk - ret = ActiveChainstate().AcceptBlock(config, block, state, - force_processing, nullptr, - new_block, min_pow_checked); + ret = ActiveChainstate().AcceptBlock( + this->GetConfig(), block, state, force_processing, nullptr, + new_block, min_pow_checked); } if (!ret) {