Skip to content

Commit bcf73d3

Browse files
committed
refactoring: move LoadChainTip to CChainState method
1 parent f5809d5 commit bcf73d3

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3932,28 +3932,31 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams) EXCLUSIVE_LOCKS_RE
39323932
return true;
39333933
}
39343934

3935-
bool LoadChainTip(const CChainParams& chainparams)
3935+
bool CChainState::LoadChainTip(const CChainParams& chainparams)
39363936
{
39373937
AssertLockHeld(cs_main);
3938-
const CCoinsViewCache& coins_cache = ::ChainstateActive().CoinsTip();
3938+
const CCoinsViewCache& coins_cache = CoinsTip();
39393939
assert(!coins_cache.GetBestBlock().IsNull()); // Never called when the coins view is empty
3940+
const CBlockIndex* tip = m_chain.Tip();
39403941

3941-
if (::ChainActive().Tip() &&
3942-
::ChainActive().Tip()->GetBlockHash() == coins_cache.GetBestBlock()) return true;
3942+
if (tip && tip->GetBlockHash() == coins_cache.GetBestBlock()) {
3943+
return true;
3944+
}
39433945

39443946
// Load pointer to end of best chain
39453947
CBlockIndex* pindex = LookupBlockIndex(coins_cache.GetBestBlock());
39463948
if (!pindex) {
39473949
return false;
39483950
}
3949-
::ChainActive().SetTip(pindex);
3950-
3951-
::ChainstateActive().PruneBlockIndexCandidates();
3951+
m_chain.SetTip(pindex);
3952+
PruneBlockIndexCandidates();
39523953

3954+
tip = m_chain.Tip();
39533955
LogPrintf("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
3954-
::ChainActive().Tip()->GetBlockHash().ToString(), ::ChainActive().Height(),
3955-
FormatISO8601DateTime(::ChainActive().Tip()->GetBlockTime()),
3956-
GuessVerificationProgress(chainparams.TxData(), ::ChainActive().Tip()));
3956+
tip->GetBlockHash().ToString(),
3957+
m_chain.Height(),
3958+
FormatISO8601DateTime(tip->GetBlockTime()),
3959+
GuessVerificationProgress(chainparams.TxData(), tip));
39573960
return true;
39583961
}
39593962

src/validation.h

Lines changed: 3 additions & 2 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 */
@@ -721,6 +719,9 @@ class CChainState {
721719
*/
722720
void CheckBlockIndex(const Consensus::Params& consensusParams);
723721

722+
/** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
723+
bool LoadChainTip(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
724+
724725
private:
725726
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);
726727
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)