Skip to content

Commit

Permalink
miner: Pass in chainstate to BlockAssembler::CreateNewBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
dongcarl committed Mar 8, 2021
1 parent a04aac4 commit d0de61b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ Optional<int64_t> BlockAssembler::m_last_block_num_txs{nullopt};
Optional<int64_t> BlockAssembler::m_last_block_weight{nullopt};

std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
{
return CreateNewBlock(::ChainstateActive(), scriptPubKeyIn);
}

std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn)
{
int64_t nTimeStart = GetTimeMicros();

Expand All @@ -117,7 +122,8 @@ std::unique_ptr<CBlockTemplate> 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;

Expand Down Expand Up @@ -176,7 +182,8 @@ std::unique_ptr<CBlockTemplate> 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();
Expand Down
1 change: 1 addition & 0 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class BlockAssembler

/** Construct a new block template with coinbase to scriptPubKeyIn */
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
std::unique_ptr<CBlockTemplate> CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn);

static Optional<int64_t> m_last_block_num_txs;
static Optional<int64_t> m_last_block_weight;
Expand Down

0 comments on commit d0de61b

Please sign in to comment.