From d0de61b764fc7e9c670b69d8210705da296dd245 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Wed, 4 Nov 2020 19:25:46 -0500 Subject: [PATCH] miner: Pass in chainstate to BlockAssembler::CreateNewBlock --- src/miner.cpp | 11 +++++++++-- src/miner.h | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 076d43c951cd3..e0e5ad41feebc 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -100,6 +100,11 @@ Optional BlockAssembler::m_last_block_num_txs{nullopt}; Optional BlockAssembler::m_last_block_weight{nullopt}; std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn) +{ + return CreateNewBlock(::ChainstateActive(), scriptPubKeyIn); +} + +std::unique_ptr BlockAssembler::CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn) { int64_t nTimeStart = GetTimeMicros(); @@ -117,7 +122,8 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end LOCK2(cs_main, m_mempool.cs); - CBlockIndex* pindexPrev = ::ChainActive().Tip(); + assert(std::addressof(*::ChainActive().Tip()) == std::addressof(*chainstate.m_chain.Tip())); + CBlockIndex* pindexPrev = chainstate.m_chain.Tip(); assert(pindexPrev != nullptr); nHeight = pindexPrev->nHeight + 1; @@ -176,7 +182,8 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]); BlockValidationState state; - if (!TestBlockValidity(state, chainparams, ::ChainstateActive(), *pblock, pindexPrev, false, false)) { + assert(std::addressof(::ChainstateActive()) == std::addressof(chainstate)); + if (!TestBlockValidity(state, chainparams, chainstate, *pblock, pindexPrev, false, false)) { throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString())); } int64_t nTime2 = GetTimeMicros(); diff --git a/src/miner.h b/src/miner.h index 9a2b7063f4dbe..95f267e491a2e 100644 --- a/src/miner.h +++ b/src/miner.h @@ -159,6 +159,7 @@ class BlockAssembler /** Construct a new block template with coinbase to scriptPubKeyIn */ std::unique_ptr CreateNewBlock(const CScript& scriptPubKeyIn); + std::unique_ptr CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn); static Optional m_last_block_num_txs; static Optional m_last_block_weight;