node: Add chainman alias for g_chainman

This commit is contained in:
MarcoFalke
2020-04-18 08:05:52 -04:00
parent cfe22a5f9e
commit fa7b626d7a
4 changed files with 32 additions and 19 deletions

View File

@@ -244,9 +244,9 @@ void Shutdown(NodeContext& node)
} }
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing // FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
{ if (node.chainman) {
LOCK(cs_main); LOCK(cs_main);
for (CChainState* chainstate : g_chainman.GetAll()) { for (CChainState* chainstate : node.chainman->GetAll()) {
if (chainstate->CanFlushToDisk()) { if (chainstate->CanFlushToDisk()) {
chainstate->ForceFlushStateToDisk(); chainstate->ForceFlushStateToDisk();
} }
@@ -271,9 +271,9 @@ void Shutdown(NodeContext& node)
// up with our current chain to avoid any strange pruning edge cases and make // up with our current chain to avoid any strange pruning edge cases and make
// next startup faster by avoiding rescan. // next startup faster by avoiding rescan.
{ if (node.chainman) {
LOCK(cs_main); LOCK(cs_main);
for (CChainState* chainstate : g_chainman.GetAll()) { for (CChainState* chainstate : node.chainman->GetAll()) {
if (chainstate->CanFlushToDisk()) { if (chainstate->CanFlushToDisk()) {
chainstate->ForceFlushStateToDisk(); chainstate->ForceFlushStateToDisk();
chainstate->ResetCoinsViews(); chainstate->ResetCoinsViews();
@@ -299,7 +299,8 @@ void Shutdown(NodeContext& node)
globalVerifyHandle.reset(); globalVerifyHandle.reset();
ECC_Stop(); ECC_Stop();
node.args = nullptr; node.args = nullptr;
if (node.mempool) node.mempool = nullptr; node.mempool = nullptr;
node.chainman = nullptr;
node.scheduler.reset(); node.scheduler.reset();
try { try {
@@ -689,7 +690,7 @@ static void CleanupBlockRevFiles()
} }
} }
static void ThreadImport(std::vector<fs::path> vImportFiles) static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
{ {
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();
util::ThreadRename("loadblk"); util::ThreadRename("loadblk");
@@ -741,9 +742,9 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)
// scan for better chains in the block chain database, that are not yet connected in the active best chain // scan for better chains in the block chain database, that are not yet connected in the active best chain
// We can't hold cs_main during ActivateBestChain even though we're accessing // We can't hold cs_main during ActivateBestChain even though we're accessing
// the g_chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve // the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
// the relevant pointers before the ABC call. // the relevant pointers before the ABC call.
for (CChainState* chainstate : WITH_LOCK(::cs_main, return g_chainman.GetAll())) { for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
BlockValidationState state; BlockValidationState state;
if (!chainstate->ActivateBestChain(state, chainparams, nullptr)) { if (!chainstate->ActivateBestChain(state, chainparams, nullptr)) {
LogPrintf("Failed to connect best block (%s)\n", state.ToString()); LogPrintf("Failed to connect best block (%s)\n", state.ToString());
@@ -1377,6 +1378,9 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
// which are all started after this, may use it from the node context. // which are all started after this, may use it from the node context.
assert(!node.mempool); assert(!node.mempool);
node.mempool = &::mempool; node.mempool = &::mempool;
assert(!node.chainman);
node.chainman = &g_chainman;
ChainstateManager& chainman = EnsureChainman(node);
node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), *node.scheduler, *node.mempool)); node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), *node.scheduler, *node.mempool));
RegisterValidationInterface(node.peer_logic.get()); RegisterValidationInterface(node.peer_logic.get());
@@ -1557,7 +1561,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
const int64_t load_block_index_start_time = GetTimeMillis(); const int64_t load_block_index_start_time = GetTimeMillis();
try { try {
LOCK(cs_main); LOCK(cs_main);
g_chainman.InitializeChainstate(); chainman.InitializeChainstate();
UnloadBlockIndex(); UnloadBlockIndex();
// new CBlockTreeDB tries to delete the existing file, which // new CBlockTreeDB tries to delete the existing file, which
@@ -1612,7 +1616,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
bool failed_chainstate_init = false; bool failed_chainstate_init = false;
for (CChainState* chainstate : g_chainman.GetAll()) { for (CChainState* chainstate : chainman.GetAll()) {
LogPrintf("Initializing chainstate %s\n", chainstate->ToString()); LogPrintf("Initializing chainstate %s\n", chainstate->ToString());
chainstate->InitCoinsDB( chainstate->InitCoinsDB(
/* cache_size_bytes */ nCoinDBCache, /* cache_size_bytes */ nCoinDBCache,
@@ -1667,7 +1671,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
bool failed_rewind{false}; bool failed_rewind{false};
// Can't hold cs_main while calling RewindBlockIndex, so retrieve the relevant // Can't hold cs_main while calling RewindBlockIndex, so retrieve the relevant
// chainstates beforehand. // chainstates beforehand.
for (CChainState* chainstate : WITH_LOCK(::cs_main, return g_chainman.GetAll())) { for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
if (!fReset) { if (!fReset) {
// Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate. // Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
// It both disconnects blocks based on the chainstate, and drops block data in // 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)
try { try {
LOCK(cs_main); LOCK(cs_main);
for (CChainState* chainstate : g_chainman.GetAll()) { for (CChainState* chainstate : chainman.GetAll()) {
if (!is_coinsview_empty(chainstate)) { if (!is_coinsview_empty(chainstate)) {
uiInterface.InitMessage(_("Verifying blocks...").translated); uiInterface.InitMessage(_("Verifying blocks...").translated);
if (fHavePruned && gArgs.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) { if (fHavePruned && gArgs.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
@@ -1798,7 +1802,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK); nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK);
if (!fReindex) { if (!fReindex) {
LOCK(cs_main); LOCK(cs_main);
for (CChainState* chainstate : g_chainman.GetAll()) { for (CChainState* chainstate : chainman.GetAll()) {
uiInterface.InitMessage(_("Pruning blockstore...").translated); uiInterface.InitMessage(_("Pruning blockstore...").translated);
chainstate->PruneAndFlush(); chainstate->PruneAndFlush();
} }
@@ -1841,7 +1845,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
vImportFiles.push_back(strFile); vImportFiles.push_back(strFile);
} }
threadGroup.create_thread(std::bind(&ThreadImport, vImportFiles)); threadGroup.create_thread([=, &chainman] { ThreadImport(chainman, vImportFiles); });
// Wait for genesis block to be processed // Wait for genesis block to be processed
{ {

View File

@@ -5,6 +5,7 @@
#ifndef BITCOIN_NODE_CONTEXT_H #ifndef BITCOIN_NODE_CONTEXT_H
#define BITCOIN_NODE_CONTEXT_H #define BITCOIN_NODE_CONTEXT_H
#include <cassert>
#include <memory> #include <memory>
#include <vector> #include <vector>
@@ -13,6 +14,7 @@ class BanMan;
class CConnman; class CConnman;
class CScheduler; class CScheduler;
class CTxMemPool; class CTxMemPool;
class ChainstateManager;
class PeerLogicValidation; class PeerLogicValidation;
namespace interfaces { namespace interfaces {
class Chain; class Chain;
@@ -33,6 +35,7 @@ struct NodeContext {
std::unique_ptr<CConnman> connman; std::unique_ptr<CConnman> connman;
CTxMemPool* mempool{nullptr}; // Currently a raw pointer because the memory is not managed by this struct CTxMemPool* mempool{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
std::unique_ptr<PeerLogicValidation> peer_logic; std::unique_ptr<PeerLogicValidation> peer_logic;
ChainstateManager* chainman{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
std::unique_ptr<BanMan> banman; std::unique_ptr<BanMan> banman;
ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
std::unique_ptr<interfaces::Chain> chain; std::unique_ptr<interfaces::Chain> chain;
@@ -46,4 +49,10 @@ struct NodeContext {
~NodeContext(); ~NodeContext();
}; };
inline ChainstateManager& EnsureChainman(const NodeContext& node)
{
assert(node.chainman);
return *node.chainman;
}
#endif // BITCOIN_NODE_CONTEXT_H #endif // BITCOIN_NODE_CONTEXT_H

View File

@@ -134,7 +134,8 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
pblocktree.reset(new CBlockTreeDB(1 << 20, true)); pblocktree.reset(new CBlockTreeDB(1 << 20, true));
g_chainman.InitializeChainstate(); m_node.chainman = &::g_chainman;
m_node.chainman->InitializeChainstate();
::ChainstateActive().InitCoinsDB( ::ChainstateActive().InitCoinsDB(
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false); /* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
assert(!::ChainstateActive().CanFlushToDisk()); assert(!::ChainstateActive().CanFlushToDisk());
@@ -181,7 +182,8 @@ TestingSetup::~TestingSetup()
m_node.mempool = nullptr; m_node.mempool = nullptr;
m_node.scheduler.reset(); m_node.scheduler.reset();
UnloadBlockIndex(); UnloadBlockIndex();
g_chainman.Reset(); m_node.chainman->Reset();
m_node.chainman = nullptr;
pblocktree.reset(); pblocktree.reset();
} }

View File

@@ -43,6 +43,7 @@ class CConnman;
class CScriptCheck; class CScriptCheck;
class CBlockPolicyEstimator; class CBlockPolicyEstimator;
class CTxMemPool; class CTxMemPool;
class ChainstateManager;
class TxValidationState; class TxValidationState;
struct ChainTxData; struct ChainTxData;
@@ -493,9 +494,6 @@ enum class CoinsCacheSizeState
OK = 0 OK = 0
}; };
// Defined below, but needed for `friend` usage in CChainState.
class ChainstateManager;
/** /**
* CChainState stores and provides an API to update our local knowledge of the * CChainState stores and provides an API to update our local knowledge of the
* current best chain. * current best chain.