diff options
Diffstat (limited to 'src/node/interfaces.cpp')
-rw-r--r-- | src/node/interfaces.cpp | 84 |
1 files changed, 54 insertions, 30 deletions
diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 0f4094e14b..8befbf5e30 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -4,7 +4,6 @@ #include <addrdb.h> #include <banman.h> -#include <boost/signals2/signal.hpp> #include <chain.h> #include <chainparams.h> #include <init.h> @@ -17,6 +16,7 @@ #include <net_processing.h> #include <netaddress.h> #include <netbase.h> +#include <node/blockstorage.h> #include <node/coin.h> #include <node/context.h> #include <node/transaction.h> @@ -53,6 +53,8 @@ #include <optional> #include <utility> +#include <boost/signals2/signal.hpp> + using interfaces::BlockTip; using interfaces::Chain; using interfaces::FoundBlock; @@ -65,6 +67,8 @@ namespace node { namespace { class NodeImpl : public Node { +private: + ChainstateManager& chainman() { return *Assert(m_context->chainman); } public: explicit NodeImpl(NodeContext* context) { setContext(context); } void initLogging() override { InitLogging(*Assert(m_context->args)); } @@ -78,7 +82,7 @@ public: } bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override { - return AppInitMain(m_context_ref, *m_context, tip_info); + return AppInitMain(*m_context, tip_info); } void appShutdown() override { @@ -183,21 +187,28 @@ public: int getNumBlocks() override { LOCK(::cs_main); - assert(std::addressof(::ChainActive()) == std::addressof(m_context->chainman->ActiveChain())); - return m_context->chainman->ActiveChain().Height(); + assert(std::addressof(::ChainActive()) == std::addressof(chainman().ActiveChain())); + return chainman().ActiveChain().Height(); } uint256 getBestBlockHash() override { - assert(std::addressof(::ChainActive()) == std::addressof(m_context->chainman->ActiveChain())); - const CBlockIndex* tip = WITH_LOCK(::cs_main, return m_context->chainman->ActiveChain().Tip()); + const CBlockIndex* tip; + { + // TODO: Temporary scope to check correctness of refactored code. + // Should be removed manually after merge of + // https://github.com/bitcoin/bitcoin/pull/20158 + LOCK(cs_main); + assert(std::addressof(::ChainActive()) == std::addressof(chainman().ActiveChain())); + tip = chainman().ActiveChain().Tip(); + } return tip ? tip->GetBlockHash() : Params().GenesisBlock().GetHash(); } int64_t getLastBlockTime() override { LOCK(::cs_main); - assert(std::addressof(::ChainActive()) == std::addressof(m_context->chainman->ActiveChain())); - if (m_context->chainman->ActiveChain().Tip()) { - return m_context->chainman->ActiveChain().Tip()->GetBlockTime(); + assert(std::addressof(::ChainActive()) == std::addressof(chainman().ActiveChain())); + if (chainman().ActiveChain().Tip()) { + return chainman().ActiveChain().Tip()->GetBlockTime(); } return Params().GenesisBlock().GetBlockTime(); // Genesis block's time of current network } @@ -206,14 +217,22 @@ public: const CBlockIndex* tip; { LOCK(::cs_main); - assert(std::addressof(::ChainActive()) == std::addressof(m_context->chainman->ActiveChain())); - tip = m_context->chainman->ActiveChain().Tip(); + assert(std::addressof(::ChainActive()) == std::addressof(chainman().ActiveChain())); + tip = chainman().ActiveChain().Tip(); } return GuessVerificationProgress(Params().TxData(), tip); } bool isInitialBlockDownload() override { - assert(std::addressof(::ChainstateActive()) == std::addressof(m_context->chainman->ActiveChainstate())); - return m_context->chainman->ActiveChainstate().IsInitialBlockDownload(); + const CChainState* active_chainstate; + { + // TODO: Temporary scope to check correctness of refactored code. + // Should be removed manually after merge of + // https://github.com/bitcoin/bitcoin/pull/20158 + LOCK(::cs_main); + active_chainstate = &m_context->chainman->ActiveChainstate(); + assert(std::addressof(::ChainstateActive()) == std::addressof(*active_chainstate)); + } + return active_chainstate->IsInitialBlockDownload(); } bool getReindex() override { return ::fReindex; } bool getImporting() override { return ::fImporting; } @@ -227,7 +246,8 @@ public: CFeeRate getDustRelayFee() override { return ::dustRelayFee; } UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override { - JSONRPCRequest req(m_context_ref); + JSONRPCRequest req; + req.context = m_context; req.params = params; req.strMethod = command; req.URI = uri; @@ -239,8 +259,8 @@ public: bool getUnspentOutput(const COutPoint& output, Coin& coin) override { LOCK(::cs_main); - assert(std::addressof(::ChainstateActive()) == std::addressof(m_context->chainman->ActiveChainstate())); - return m_context->chainman->ActiveChainstate().CoinsTip().GetCoin(output, coin); + assert(std::addressof(::ChainstateActive()) == std::addressof(chainman().ActiveChainstate())); + return chainman().ActiveChainstate().CoinsTip().GetCoin(output, coin); } WalletClient& walletClient() override { @@ -297,14 +317,8 @@ public: void setContext(NodeContext* context) override { m_context = context; - if (context) { - m_context_ref = context; - } else { - m_context_ref.reset(); - } } NodeContext* m_context{nullptr}; - std::any m_context_ref; }; bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active) @@ -414,6 +428,8 @@ public: class ChainImpl : public Chain { +private: + ChainstateManager& chainman() { return *Assert(m_node.chainman); } public: explicit ChainImpl(NodeContext& node) : m_node(node) {} std::optional<int> getHeight() override @@ -450,8 +466,8 @@ public: bool checkFinalTx(const CTransaction& tx) override { LOCK(cs_main); - assert(std::addressof(::ChainActive()) == std::addressof(m_node.chainman->ActiveChain())); - return CheckFinalTx(m_node.chainman->ActiveChain().Tip(), tx); + assert(std::addressof(::ChainActive()) == std::addressof(chainman().ActiveChain())); + return CheckFinalTx(chainman().ActiveChain().Tip(), tx); } std::optional<int> findLocatorFork(const CBlockLocator& locator) override { @@ -516,8 +532,8 @@ public: double guessVerificationProgress(const uint256& block_hash) override { LOCK(cs_main); - assert(std::addressof(g_chainman) == std::addressof(*m_node.chainman)); - return GuessVerificationProgress(Params().TxData(), m_node.chainman->m_blockman.LookupBlockIndex(block_hash)); + assert(std::addressof(g_chainman.m_blockman) == std::addressof(chainman().m_blockman)); + return GuessVerificationProgress(Params().TxData(), chainman().m_blockman.LookupBlockIndex(block_hash)); } bool hasBlocks(const uint256& block_hash, int min_height, std::optional<int> max_height) override { @@ -529,8 +545,8 @@ public: // used to limit the range, and passing min_height that's too low or // max_height that's too high will not crash or change the result. LOCK(::cs_main); - assert(std::addressof(g_chainman) == std::addressof(*m_node.chainman)); - if (CBlockIndex* block = m_node.chainman->m_blockman.LookupBlockIndex(block_hash)) { + assert(std::addressof(g_chainman.m_blockman) == std::addressof(chainman().m_blockman)); + if (CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) { if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height); for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) { // Check pprev to not segfault if min_height is too low @@ -621,8 +637,16 @@ public: } bool isReadyToBroadcast() override { return !::fImporting && !::fReindex && !isInitialBlockDownload(); } bool isInitialBlockDownload() override { - assert(std::addressof(::ChainstateActive()) == std::addressof(m_node.chainman->ActiveChainstate())); - return m_node.chainman->ActiveChainstate().IsInitialBlockDownload(); + const CChainState* active_chainstate; + { + // TODO: Temporary scope to check correctness of refactored code. + // Should be removed manually after merge of + // https://github.com/bitcoin/bitcoin/pull/20158 + LOCK(::cs_main); + active_chainstate = &chainman().ActiveChainstate(); + assert(std::addressof(::ChainstateActive()) == std::addressof(*active_chainstate)); + } + return active_chainstate->IsInitialBlockDownload(); } bool shutdownRequested() override { return ShutdownRequested(); } int64_t getAdjustedTime() override { return GetAdjustedTime(); } |