Skip to content

Commit 9c300cc

Browse files
committed
validation: Pass in chainstate to TestBlockValidity
[META] This commit should be followed up by removing the comments and assertions meant only to show that the change is correct.
1 parent 0e17c83 commit 9c300cc

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

src/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
176176
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
177177

178178
BlockValidationState state;
179-
if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
179+
if (!TestBlockValidity(state, chainparams, ::ChainstateActive(), *pblock, pindexPrev, false, false)) {
180180
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
181181
}
182182
int64_t nTime2 = GetTimeMicros();

src/rpc/mining.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static RPCHelpMan generateblock()
375375
LOCK(cs_main);
376376

377377
BlockValidationState state;
378-
if (!TestBlockValidity(state, chainparams, block, g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
378+
if (!TestBlockValidity(state, chainparams, ::ChainstateActive(), block, g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
379379
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
380380
}
381381
}
@@ -632,7 +632,7 @@ static RPCHelpMan getblocktemplate()
632632
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
633633
return "inconclusive-not-best-prevblk";
634634
BlockValidationState state;
635-
TestBlockValidity(state, Params(), block, pindexPrev, false, true);
635+
TestBlockValidity(state, Params(), ::ChainstateActive(), block, pindexPrev, false, true);
636636
return BIP22ValidationResult(state);
637637
}
638638

src/validation.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,25 +3809,27 @@ bool ChainstateManager::ProcessNewBlock(const CChainParams& chainparams, const s
38093809
return true;
38103810
}
38113811

3812-
bool TestBlockValidity(BlockValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
3812+
bool TestBlockValidity(BlockValidationState& state, const CChainParams& chainparams, CChainState& chainstate, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
38133813
{
38143814
AssertLockHeld(cs_main);
3815-
assert(pindexPrev && pindexPrev == ::ChainActive().Tip());
3816-
CCoinsViewCache viewNew(&::ChainstateActive().CoinsTip());
3815+
assert(std::addressof(::ChainstateActive()) == std::addressof(chainstate));
3816+
assert(pindexPrev && pindexPrev == chainstate.m_chain.Tip());
3817+
CCoinsViewCache viewNew(&chainstate.CoinsTip());
38173818
uint256 block_hash(block.GetHash());
38183819
CBlockIndex indexDummy(block);
38193820
indexDummy.pprev = pindexPrev;
38203821
indexDummy.nHeight = pindexPrev->nHeight + 1;
38213822
indexDummy.phashBlock = &block_hash;
38223823

38233824
// NOTE: CheckBlockHeader is called by CheckBlock
3824-
if (!ContextualCheckBlockHeader(block, state, g_chainman.m_blockman, chainparams, pindexPrev, GetAdjustedTime()))
3825+
assert(std::addressof(g_chainman.m_blockman) == std::addressof(chainstate.m_blockman));
3826+
if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman, chainparams, pindexPrev, GetAdjustedTime()))
38253827
return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, state.ToString());
38263828
if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
38273829
return error("%s: Consensus::CheckBlock: %s", __func__, state.ToString());
38283830
if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev))
38293831
return error("%s: Consensus::ContextualCheckBlock: %s", __func__, state.ToString());
3830-
if (!::ChainstateActive().ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
3832+
if (!chainstate.ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
38313833
return false;
38323834
assert(state.IsValid());
38333835

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
292292
bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
293293

294294
/** Check a block is completely valid from start to finish (only works on top of our current best block) */
295-
bool TestBlockValidity(BlockValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
295+
bool TestBlockValidity(BlockValidationState& state, const CChainParams& chainparams, CChainState& chainstate, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
296296

297297
/** Check whether witness commitments are required for a block, and whether to enforce NULLDUMMY (BIP 147) rules.
298298
* Note that transaction witness validation rules are always enforced when P2SH is enforced. */

0 commit comments

Comments
 (0)