Skip to content

Commit fac07f2

Browse files
author
MarcoFalke
committed
node: Add reference to mempool in NodeContext
Currently it is an alias to the global ::mempool and should be used as follows. * Node code (validation and transaction relay) can use either ::mempool or node.mempool, whichever seems a better fit. * RPC code should use the added convenience getter EnsureMempool, which makes sure the mempool exists before use. This prepares the RPC code to a future where the mempool might be disabled at runtime or compile time. * Test code should use m_node.mempool directly, as the mempool is always initialized for tests.
1 parent 2706162 commit fac07f2

File tree

7 files changed

+28
-5
lines changed

7 files changed

+28
-5
lines changed

src/init.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ void Shutdown(NodeContext& node)
284284
GetMainSignals().UnregisterWithMempoolSignals(mempool);
285285
globalVerifyHandle.reset();
286286
ECC_Stop();
287+
if (node.mempool) node.mempool = nullptr;
287288
LogPrintf("%s: done\n", __func__);
288289
}
289290

@@ -1632,6 +1633,11 @@ bool AppInitMain(NodeContext& node)
16321633
return false;
16331634
}
16341635

1636+
// Now that the chain state is loaded, make mempool generally available in the node context. For example the
1637+
// connection manager, wallet, or RPC threads, which are all started after this, may use it from the node context.
1638+
assert(!node.mempool);
1639+
node.mempool = &::mempool;
1640+
16351641
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
16361642
CAutoFile est_filein(fsbridge::fopen(est_path, "rb"), SER_DISK, CLIENT_VERSION);
16371643
// Allowed to fail as this file IS missing on first startup.

src/node/context.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
class BanMan;
1212
class CConnman;
13+
class CTxMemPool;
1314
class PeerLogicValidation;
1415
namespace interfaces {
1516
class Chain;
@@ -22,13 +23,13 @@ class ChainClient;
2223
//! This is used by init, rpc, and test code to pass object references around
2324
//! without needing to declare the same variables and parameters repeatedly, or
2425
//! to use globals. More variables could be added to this struct (particularly
25-
//! references to validation and mempool objects) to eliminate use of globals
26+
//! references to validation objects) to eliminate use of globals
2627
//! and make code more modular and testable. The struct isn't intended to have
2728
//! any member functions. It should just be a collection of references that can
2829
//! be used without pulling in unwanted dependencies or functionality.
29-
struct NodeContext
30-
{
30+
struct NodeContext {
3131
std::unique_ptr<CConnman> connman;
32+
CTxMemPool* mempool{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
3233
std::unique_ptr<PeerLogicValidation> peer_logic;
3334
std::unique_ptr<BanMan> banman;
3435
std::unique_ptr<interfaces::Chain> chain;

src/qt/test/rpcnestedtests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ void RPCNestedTests::rpcNestedTests()
3232
// do some test setup
3333
// could be moved to a more generic place when we add more tests on QT level
3434
tableRPC.appendCommand("rpcNestedTest", &vRPCCommands[0]);
35-
//mempool.setSanityCheck(1.0);
3635

3736
TestingSetup test;
3837

src/rpc/blockchain.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <hash.h>
1616
#include <index/blockfilterindex.h>
1717
#include <node/coinstats.h>
18+
#include <node/context.h>
1819
#include <node/utxo_snapshot.h>
1920
#include <policy/feerate.h>
2021
#include <policy/policy.h>
@@ -53,6 +54,15 @@ static Mutex cs_blockchange;
5354
static std::condition_variable cond_blockchange;
5455
static CUpdatedBlock latestblock;
5556

57+
CTxMemPool& EnsureMemPool()
58+
{
59+
CHECK_NONFATAL(g_rpc_node);
60+
if (!g_rpc_node->mempool) {
61+
throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
62+
}
63+
return *g_rpc_node->mempool;
64+
}
65+
5666
/* Calculate the difficulty for a given block index.
5767
*/
5868
double GetDifficulty(const CBlockIndex* blockindex)

src/rpc/blockchain.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES],
5252
//! direct way to pass in state to RPC methods without globals.
5353
extern NodeContext* g_rpc_node;
5454

55+
CTxMemPool& EnsureMemPool();
56+
5557
#endif

src/rpc/protocol.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ enum RPCErrorCode
6363
RPC_CLIENT_INVALID_IP_OR_SUBNET = -30, //!< Invalid IP/Subnet
6464
RPC_CLIENT_P2P_DISABLED = -31, //!< No valid connection manager instance found
6565

66+
//! Chain errors
67+
RPC_CLIENT_MEMPOOL_DISABLED = -33, //!< No mempool instance found
68+
6669
//! Wallet errors
6770
RPC_WALLET_ERROR = -4, //!< Unspecified problem with wallet (key not found etc.)
6871
RPC_WALLET_INSUFFICIENT_FUNDS = -6, //!< Not enough funds in wallet or account

src/test/util/setup_common.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
107107
threadGroup.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler));
108108
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
109109

110-
mempool.setSanityCheck(1.0);
111110
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
112111
g_chainstate = MakeUnique<CChainState>();
113112
::ChainstateActive().InitCoinsDB(
@@ -131,6 +130,8 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
131130
}
132131
g_parallel_script_checks = true;
133132

133+
m_node.mempool = &::mempool;
134+
m_node.mempool->setSanityCheck(1.0);
134135
m_node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
135136
m_node.connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
136137
}
@@ -144,6 +145,7 @@ TestingSetup::~TestingSetup()
144145
g_rpc_node = nullptr;
145146
m_node.connman.reset();
146147
m_node.banman.reset();
148+
m_node.mempool = nullptr;
147149
UnloadBlockIndex();
148150
g_chainstate.reset();
149151
pblocktree.reset();

0 commit comments

Comments
 (0)