Skip to content

Commit 7d4bc60

Browse files
author
MarcoFalke
committed
Merge #16743: refactor: move LoadChainTip/RelayBlocks under CChainState
3cf3673 refactoring: move ReplayBlocks under CChainState (James O'Beirne) bcf73d3 refactoring: move LoadChainTip to CChainState method (James O'Beirne) f5809d5 doc: fix CChainState::ActivateBestChain doc (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: #15606 Issue: #15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal --- Move more chainstate-related functionality to methods on CChainState. Nothing too interesting here, but needed to work with multiple chainstates. And brief to review. :) Also fixes doc on ActivateBestChain. ACKs for top commit: MarcoFalke: ACK 3cf3673 ryanofsky: Can confirm. utACK 3cf3673. Removes wrapper functions and removes more ::ChainActive() and ::ChainstateActive() calls than it adds, so seems good. Tree-SHA512: 4bf8a1dd454ca9d61c85f6736910fa7354c57acc0002e3a8e5ce494035d8280e4c20e066f03478eeff7d44195e7912c282a486526da9be53854b478b961affaa
2 parents 9bf5768 + 3cf3673 commit 7d4bc60

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ bool AppInitMain(InitInterfaces& interfaces)
15451545
}
15461546

15471547
// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
1548-
if (!ReplayBlocks(chainparams, &::ChainstateActive().CoinsDB())) {
1548+
if (!::ChainstateActive().ReplayBlocks(chainparams)) {
15491549
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.").translated;
15501550
break;
15511551
}
@@ -1557,8 +1557,8 @@ bool AppInitMain(InitInterfaces& interfaces)
15571557
is_coinsview_empty = fReset || fReindexChainState ||
15581558
::ChainstateActive().CoinsTip().GetBestBlock().IsNull();
15591559
if (!is_coinsview_empty) {
1560-
// LoadChainTip sets ::ChainActive() based on CoinsTip()'s best block
1561-
if (!LoadChainTip(chainparams)) {
1560+
// LoadChainTip initializes the chain based on CoinsTip()'s best block
1561+
if (!::ChainstateActive().LoadChainTip(chainparams)) {
15621562
strLoadError = _("Error initializing block database").translated;
15631563
break;
15641564
}

src/validation.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4087,28 +4087,31 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams) EXCLUSIVE_LOCKS_RE
40874087
return true;
40884088
}
40894089

4090-
bool LoadChainTip(const CChainParams& chainparams)
4090+
bool CChainState::LoadChainTip(const CChainParams& chainparams)
40914091
{
40924092
AssertLockHeld(cs_main);
4093-
const CCoinsViewCache& coins_cache = ::ChainstateActive().CoinsTip();
4093+
const CCoinsViewCache& coins_cache = CoinsTip();
40944094
assert(!coins_cache.GetBestBlock().IsNull()); // Never called when the coins view is empty
4095+
const CBlockIndex* tip = m_chain.Tip();
40954096

4096-
if (::ChainActive().Tip() &&
4097-
::ChainActive().Tip()->GetBlockHash() == coins_cache.GetBestBlock()) return true;
4097+
if (tip && tip->GetBlockHash() == coins_cache.GetBestBlock()) {
4098+
return true;
4099+
}
40984100

40994101
// Load pointer to end of best chain
41004102
CBlockIndex* pindex = LookupBlockIndex(coins_cache.GetBestBlock());
41014103
if (!pindex) {
41024104
return false;
41034105
}
4104-
::ChainActive().SetTip(pindex);
4105-
4106-
::ChainstateActive().PruneBlockIndexCandidates();
4106+
m_chain.SetTip(pindex);
4107+
PruneBlockIndexCandidates();
41074108

4109+
tip = m_chain.Tip();
41084110
LogPrintf("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
4109-
::ChainActive().Tip()->GetBlockHash().ToString(), ::ChainActive().Height(),
4110-
FormatISO8601DateTime(::ChainActive().Tip()->GetBlockTime()),
4111-
GuessVerificationProgress(chainparams.TxData(), ::ChainActive().Tip()));
4111+
tip->GetBlockHash().ToString(),
4112+
m_chain.Height(),
4113+
FormatISO8601DateTime(tip->GetBlockTime()),
4114+
GuessVerificationProgress(chainparams.TxData(), tip));
41124115
return true;
41134116
}
41144117

@@ -4243,13 +4246,14 @@ bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& i
42434246
return true;
42444247
}
42454248

4246-
bool CChainState::ReplayBlocks(const CChainParams& params, CCoinsView* view)
4249+
bool CChainState::ReplayBlocks(const CChainParams& params)
42474250
{
42484251
LOCK(cs_main);
42494252

4250-
CCoinsViewCache cache(view);
4253+
CCoinsView& db = this->CoinsDB();
4254+
CCoinsViewCache cache(&db);
42514255

4252-
std::vector<uint256> hashHeads = view->GetHeadBlocks();
4256+
std::vector<uint256> hashHeads = db.GetHeadBlocks();
42534257
if (hashHeads.empty()) return true; // We're already in a consistent state.
42544258
if (hashHeads.size() != 2) return error("ReplayBlocks(): unknown inconsistent state");
42554259

@@ -4309,10 +4313,6 @@ bool CChainState::ReplayBlocks(const CChainParams& params, CCoinsView* view)
43094313
return true;
43104314
}
43114315

4312-
bool ReplayBlocks(const CChainParams& params, CCoinsView* view) {
4313-
return ::ChainstateActive().ReplayBlocks(params, view);
4314-
}
4315-
43164316
//! Helper for CChainState::RewindBlockIndex
43174317
void CChainState::EraseBlockData(CBlockIndex* index)
43184318
{

src/validation.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@ bool LoadGenesisBlock(const CChainParams& chainparams);
240240
/** Load the block tree and coins database from disk,
241241
* initializing state if we're running with -reindex. */
242242
bool LoadBlockIndex(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
243-
/** Update the chain tip based on database information. */
244-
bool LoadChainTip(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
245243
/** Unload database information */
246244
void UnloadBlockIndex();
247245
/** Run an instance of the script checking thread */
@@ -400,9 +398,6 @@ class CVerifyDB {
400398
bool VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
401399
};
402400

403-
/** Replay blocks that aren't fully applied to the database. */
404-
bool ReplayBlocks(const CChainParams& params, CCoinsView* view);
405-
406401
CBlockIndex* LookupBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
407402

408403
/** Find the last common block between the parameter chain and a locator. */
@@ -678,9 +673,13 @@ class CChainState {
678673
* we avoid holding cs_main for an extended period of time; the length of this
679674
* call may be quite long during reindexing or a substantial reorg.
680675
*
676+
* May not be called with cs_main held. May not be called in a
677+
* validationinterface callback.
678+
*
681679
* @returns true unless a system error occurred
682680
*/
683-
bool ActivateBestChain(CValidationState& state,
681+
bool ActivateBestChain(
682+
CValidationState& state,
684683
const CChainParams& chainparams,
685684
std::shared_ptr<const CBlock> pblock) LOCKS_EXCLUDED(cs_main);
686685

@@ -699,7 +698,8 @@ class CChainState {
699698
bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindex) LOCKS_EXCLUDED(cs_main);
700699
void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
701700

702-
bool ReplayBlocks(const CChainParams& params, CCoinsView* view);
701+
/** Replay blocks that aren't fully applied to the database. */
702+
bool ReplayBlocks(const CChainParams& params);
703703
bool RewindBlockIndex(const CChainParams& params) LOCKS_EXCLUDED(cs_main);
704704
bool LoadGenesisBlock(const CChainParams& chainparams);
705705

@@ -717,6 +717,9 @@ class CChainState {
717717
*/
718718
void CheckBlockIndex(const Consensus::Params& consensusParams);
719719

720+
/** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
721+
bool LoadChainTip(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
722+
720723
private:
721724
bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs);
722725
bool ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs);

0 commit comments

Comments
 (0)