Skip to content

Commit fa7b626

Browse files
author
MarcoFalke
committed
node: Add chainman alias for g_chainman
1 parent cfe22a5 commit fa7b626

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

src/init.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ void Shutdown(NodeContext& node)
244244
}
245245

246246
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
247-
{
247+
if (node.chainman) {
248248
LOCK(cs_main);
249-
for (CChainState* chainstate : g_chainman.GetAll()) {
249+
for (CChainState* chainstate : node.chainman->GetAll()) {
250250
if (chainstate->CanFlushToDisk()) {
251251
chainstate->ForceFlushStateToDisk();
252252
}
@@ -271,9 +271,9 @@ void Shutdown(NodeContext& node)
271271
// up with our current chain to avoid any strange pruning edge cases and make
272272
// next startup faster by avoiding rescan.
273273

274-
{
274+
if (node.chainman) {
275275
LOCK(cs_main);
276-
for (CChainState* chainstate : g_chainman.GetAll()) {
276+
for (CChainState* chainstate : node.chainman->GetAll()) {
277277
if (chainstate->CanFlushToDisk()) {
278278
chainstate->ForceFlushStateToDisk();
279279
chainstate->ResetCoinsViews();
@@ -299,7 +299,8 @@ void Shutdown(NodeContext& node)
299299
globalVerifyHandle.reset();
300300
ECC_Stop();
301301
node.args = nullptr;
302-
if (node.mempool) node.mempool = nullptr;
302+
node.mempool = nullptr;
303+
node.chainman = nullptr;
303304
node.scheduler.reset();
304305

305306
try {
@@ -689,7 +690,7 @@ static void CleanupBlockRevFiles()
689690
}
690691
}
691692

692-
static void ThreadImport(std::vector<fs::path> vImportFiles)
693+
static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
693694
{
694695
const CChainParams& chainparams = Params();
695696
util::ThreadRename("loadblk");
@@ -741,9 +742,9 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)
741742
// scan for better chains in the block chain database, that are not yet connected in the active best chain
742743

743744
// We can't hold cs_main during ActivateBestChain even though we're accessing
744-
// the g_chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
745+
// the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
745746
// the relevant pointers before the ABC call.
746-
for (CChainState* chainstate : WITH_LOCK(::cs_main, return g_chainman.GetAll())) {
747+
for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
747748
BlockValidationState state;
748749
if (!chainstate->ActivateBestChain(state, chainparams, nullptr)) {
749750
LogPrintf("Failed to connect best block (%s)\n", state.ToString());
@@ -1377,6 +1378,9 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
13771378
// which are all started after this, may use it from the node context.
13781379
assert(!node.mempool);
13791380
node.mempool = &::mempool;
1381+
assert(!node.chainman);
1382+
node.chainman = &g_chainman;
1383+
ChainstateManager& chainman = EnsureChainman(node);
13801384

13811385
node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), *node.scheduler, *node.mempool));
13821386
RegisterValidationInterface(node.peer_logic.get());
@@ -1557,7 +1561,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
15571561
const int64_t load_block_index_start_time = GetTimeMillis();
15581562
try {
15591563
LOCK(cs_main);
1560-
g_chainman.InitializeChainstate();
1564+
chainman.InitializeChainstate();
15611565
UnloadBlockIndex();
15621566

15631567
// new CBlockTreeDB tries to delete the existing file, which
@@ -1612,7 +1616,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
16121616

16131617
bool failed_chainstate_init = false;
16141618

1615-
for (CChainState* chainstate : g_chainman.GetAll()) {
1619+
for (CChainState* chainstate : chainman.GetAll()) {
16161620
LogPrintf("Initializing chainstate %s\n", chainstate->ToString());
16171621
chainstate->InitCoinsDB(
16181622
/* cache_size_bytes */ nCoinDBCache,
@@ -1667,7 +1671,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
16671671
bool failed_rewind{false};
16681672
// Can't hold cs_main while calling RewindBlockIndex, so retrieve the relevant
16691673
// chainstates beforehand.
1670-
for (CChainState* chainstate : WITH_LOCK(::cs_main, return g_chainman.GetAll())) {
1674+
for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
16711675
if (!fReset) {
16721676
// Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
16731677
// It both disconnects blocks based on the chainstate, and drops block data in
@@ -1692,7 +1696,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
16921696
try {
16931697
LOCK(cs_main);
16941698

1695-
for (CChainState* chainstate : g_chainman.GetAll()) {
1699+
for (CChainState* chainstate : chainman.GetAll()) {
16961700
if (!is_coinsview_empty(chainstate)) {
16971701
uiInterface.InitMessage(_("Verifying blocks...").translated);
16981702
if (fHavePruned && gArgs.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
@@ -1798,7 +1802,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
17981802
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK);
17991803
if (!fReindex) {
18001804
LOCK(cs_main);
1801-
for (CChainState* chainstate : g_chainman.GetAll()) {
1805+
for (CChainState* chainstate : chainman.GetAll()) {
18021806
uiInterface.InitMessage(_("Pruning blockstore...").translated);
18031807
chainstate->PruneAndFlush();
18041808
}
@@ -1841,7 +1845,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
18411845
vImportFiles.push_back(strFile);
18421846
}
18431847

1844-
threadGroup.create_thread(std::bind(&ThreadImport, vImportFiles));
1848+
threadGroup.create_thread([=, &chainman] { ThreadImport(chainman, vImportFiles); });
18451849

18461850
// Wait for genesis block to be processed
18471851
{

src/node/context.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_NODE_CONTEXT_H
66
#define BITCOIN_NODE_CONTEXT_H
77

8+
#include <cassert>
89
#include <memory>
910
#include <vector>
1011

@@ -13,6 +14,7 @@ class BanMan;
1314
class CConnman;
1415
class CScheduler;
1516
class CTxMemPool;
17+
class ChainstateManager;
1618
class PeerLogicValidation;
1719
namespace interfaces {
1820
class Chain;
@@ -33,6 +35,7 @@ struct NodeContext {
3335
std::unique_ptr<CConnman> connman;
3436
CTxMemPool* mempool{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
3537
std::unique_ptr<PeerLogicValidation> peer_logic;
38+
ChainstateManager* chainman{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
3639
std::unique_ptr<BanMan> banman;
3740
ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
3841
std::unique_ptr<interfaces::Chain> chain;
@@ -46,4 +49,10 @@ struct NodeContext {
4649
~NodeContext();
4750
};
4851

52+
inline ChainstateManager& EnsureChainman(const NodeContext& node)
53+
{
54+
assert(node.chainman);
55+
return *node.chainman;
56+
}
57+
4958
#endif // BITCOIN_NODE_CONTEXT_H

src/test/util/setup_common.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
134134

135135
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
136136

137-
g_chainman.InitializeChainstate();
137+
m_node.chainman = &::g_chainman;
138+
m_node.chainman->InitializeChainstate();
138139
::ChainstateActive().InitCoinsDB(
139140
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
140141
assert(!::ChainstateActive().CanFlushToDisk());
@@ -181,7 +182,8 @@ TestingSetup::~TestingSetup()
181182
m_node.mempool = nullptr;
182183
m_node.scheduler.reset();
183184
UnloadBlockIndex();
184-
g_chainman.Reset();
185+
m_node.chainman->Reset();
186+
m_node.chainman = nullptr;
185187
pblocktree.reset();
186188
}
187189

src/validation.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class CConnman;
4343
class CScriptCheck;
4444
class CBlockPolicyEstimator;
4545
class CTxMemPool;
46+
class ChainstateManager;
4647
class TxValidationState;
4748
struct ChainTxData;
4849

@@ -493,9 +494,6 @@ enum class CoinsCacheSizeState
493494
OK = 0
494495
};
495496

496-
// Defined below, but needed for `friend` usage in CChainState.
497-
class ChainstateManager;
498-
499497
/**
500498
* CChainState stores and provides an API to update our local knowledge of the
501499
* current best chain.

0 commit comments

Comments
 (0)