Skip to content

Commit 471da5f

Browse files
committed
Move block-arrival information / preciousblock counters to ChainstateManager
Block arrival information (and the preciousblock RPC, a related concept) are both chainstate-agnostic, so these are moved to ChainstateManager. This should just be a refactor, without any observable behavior changes.
1 parent 1cfc887 commit 471da5f

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

src/node/chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
221221

222222
// A reload of the block index is required to recompute setBlockIndexCandidates
223223
// for the fully validated chainstate.
224-
chainman.ActiveChainstate().UnloadBlockIndex();
224+
chainman.ActiveChainstate().ClearBlockIndexCandidates();
225225

226226
auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
227227
if (init_status != ChainstateLoadStatus::SUCCESS) {

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,13 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
433433
CBlockIndex* assumed_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
434434

435435
auto reload_all_block_indexes = [&]() {
436+
// For completeness, we also reset the block sequence counters to
437+
// ensure that no state which affects the ranking of tip-candidates is
438+
// retained (even though this isn't strictly necessary).
439+
WITH_LOCK(::cs_main, return chainman.ResetBlockSequenceCounters());
436440
for (Chainstate* cs : chainman.GetAll()) {
437441
LOCK(::cs_main);
438-
cs->UnloadBlockIndex();
442+
cs->ClearBlockIndexCandidates();
439443
BOOST_CHECK(cs->setBlockIndexCandidates.empty());
440444
}
441445

src/validation.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,17 +3213,17 @@ bool Chainstate::PreciousBlock(BlockValidationState& state, CBlockIndex* pindex)
32133213
// Nothing to do, this block is not at the tip.
32143214
return true;
32153215
}
3216-
if (m_chain.Tip()->nChainWork > nLastPreciousChainwork) {
3216+
if (m_chain.Tip()->nChainWork > m_chainman.nLastPreciousChainwork) {
32173217
// The chain has been extended since the last call, reset the counter.
3218-
nBlockReverseSequenceId = -1;
3218+
m_chainman.nBlockReverseSequenceId = -1;
32193219
}
3220-
nLastPreciousChainwork = m_chain.Tip()->nChainWork;
3220+
m_chainman.nLastPreciousChainwork = m_chain.Tip()->nChainWork;
32213221
setBlockIndexCandidates.erase(pindex);
3222-
pindex->nSequenceId = nBlockReverseSequenceId;
3223-
if (nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
3222+
pindex->nSequenceId = m_chainman.nBlockReverseSequenceId;
3223+
if (m_chainman.nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
32243224
// We can't keep reducing the counter if somebody really wants to
32253225
// call preciousblock 2**31-1 times on the same set of tips...
3226-
nBlockReverseSequenceId--;
3226+
m_chainman.nBlockReverseSequenceId--;
32273227
}
32283228
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->HaveTxsDownloaded()) {
32293229
setBlockIndexCandidates.insert(pindex);
@@ -3442,7 +3442,7 @@ void Chainstate::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pin
34423442
CBlockIndex *pindex = queue.front();
34433443
queue.pop_front();
34443444
pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
3445-
pindex->nSequenceId = nBlockSequenceId++;
3445+
pindex->nSequenceId = m_chainman.nBlockSequenceId++;
34463446
if (m_chain.Tip() == nullptr || !setBlockIndexCandidates.value_comp()(pindex, m_chain.Tip())) {
34473447
setBlockIndexCandidates.insert(pindex);
34483448
}
@@ -4379,10 +4379,9 @@ bool Chainstate::NeedsRedownload() const
43794379
return false;
43804380
}
43814381

4382-
void Chainstate::UnloadBlockIndex()
4382+
void Chainstate::ClearBlockIndexCandidates()
43834383
{
43844384
AssertLockHeld(::cs_main);
4385-
nBlockSequenceId = 1;
43864385
setBlockIndexCandidates.clear();
43874386
}
43884387

src/validation.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,6 @@ enum class CoinsCacheSizeState
465465
class Chainstate
466466
{
467467
protected:
468-
/**
469-
* Every received block is assigned a unique and increasing identifier, so we
470-
* know which one to give priority in case of a fork.
471-
*/
472-
/** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
473-
int32_t nBlockSequenceId GUARDED_BY(::cs_main) = 1;
474-
/** Decreasing counter (used by subsequent preciousblock calls). */
475-
int32_t nBlockReverseSequenceId = -1;
476-
/** chainwork for the last block that preciousblock has been applied to. */
477-
arith_uint256 nLastPreciousChainwork = 0;
478-
479468
/**
480469
* The ChainState Mutex
481470
* A lock that must be held when modifying this ChainState - held in ActivateBestChain() and
@@ -740,7 +729,7 @@ class Chainstate
740729

741730
void PruneBlockIndexCandidates();
742731

743-
void UnloadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
732+
void ClearBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
744733

745734
/** Check whether we are doing an initial block download (synchronizing from disk or network) */
746735
bool IsInitialBlockDownload() const;
@@ -990,6 +979,27 @@ class ChainstateManager
990979
//! chainstate to avoid duplicating block metadata.
991980
node::BlockManager m_blockman;
992981

982+
/**
983+
* Every received block is assigned a unique and increasing identifier, so we
984+
* know which one to give priority in case of a fork.
985+
*/
986+
/** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
987+
int32_t nBlockSequenceId GUARDED_BY(::cs_main) = 1;
988+
/** Decreasing counter (used by subsequent preciousblock calls). */
989+
int32_t nBlockReverseSequenceId = -1;
990+
/** chainwork for the last block that preciousblock has been applied to. */
991+
arith_uint256 nLastPreciousChainwork = 0;
992+
993+
// Reset the memory-only sequence counters we use to track block arrival
994+
// (used by tests to reset state)
995+
void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
996+
{
997+
AssertLockHeld(::cs_main);
998+
nBlockSequenceId = 1;
999+
nBlockReverseSequenceId = -1;
1000+
}
1001+
1002+
9931003
/**
9941004
* In order to efficiently track invalidity of headers, we keep the set of
9951005
* blocks which we tried to connect and found to be invalid here (ie which

0 commit comments

Comments
 (0)