Skip to content

Commit fa660d6

Browse files
author
MarcoFalke
committed
node: Use mempool from node context instead of global
1 parent facbaf0 commit fa660d6

File tree

8 files changed

+16
-14
lines changed

8 files changed

+16
-14
lines changed

src/interfaces/chain.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class ChainImpl : public Chain
263263
}
264264
return true;
265265
}
266-
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(coins); }
266+
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); }
267267
double guessVerificationProgress(const uint256& block_hash) override
268268
{
269269
LOCK(cs_main);

src/interfaces/node.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ class NodeImpl : public Node
167167
}
168168
int64_t getTotalBytesRecv() override { return m_context.connman ? m_context.connman->GetTotalBytesRecv() : 0; }
169169
int64_t getTotalBytesSent() override { return m_context.connman ? m_context.connman->GetTotalBytesSent() : 0; }
170-
size_t getMempoolSize() override { return ::mempool.size(); }
171-
size_t getMempoolDynamicUsage() override { return ::mempool.DynamicMemoryUsage(); }
170+
size_t getMempoolSize() override { return m_context.mempool ? m_context.mempool->size() : 0; }
171+
size_t getMempoolDynamicUsage() override { return m_context.mempool ? m_context.mempool->DynamicMemoryUsage() : 0; }
172172
bool getHeaderTip(int& height, int64_t& block_time) override
173173
{
174174
LOCK(::cs_main);

src/node/coin.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
#include <node/coin.h>
66

7+
#include <node/context.h>
78
#include <txmempool.h>
89
#include <validation.h>
910

10-
void FindCoins(std::map<COutPoint, Coin>& coins)
11+
void FindCoins(const NodeContext& node, std::map<COutPoint, Coin>& coins)
1112
{
12-
LOCK2(cs_main, ::mempool.cs);
13+
assert(node.mempool);
14+
LOCK2(cs_main, node.mempool->cs);
1315
CCoinsViewCache& chain_view = ::ChainstateActive().CoinsTip();
14-
CCoinsViewMemPool mempool_view(&chain_view, ::mempool);
16+
CCoinsViewMemPool mempool_view(&chain_view, *node.mempool);
1517
for (auto& coin : coins) {
1618
if (!mempool_view.GetCoin(coin.first, coin.second)) {
1719
// Either the coin is not in the CCoinsViewCache or is spent. Clear it.

src/node/coin.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99

1010
class COutPoint;
1111
class Coin;
12+
struct NodeContext;
1213

1314
/**
1415
* Look up unspent output information. Returns coins in the mempool and in the
1516
* current chain UTXO set. Iterates through all the keys in the map and
1617
* populates the values.
1718
*
19+
* @param[in] node The node context to use for lookup
1820
* @param[in,out] coins map to fill
1921
*/
20-
void FindCoins(std::map<COutPoint, Coin>& coins);
22+
void FindCoins(const NodeContext& node, std::map<COutPoint, Coin>& coins);
2123

2224
#endif // BITCOIN_NODE_COIN_H

src/node/transaction.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
2020
// node.connman is assigned both before chain clients and before RPC server is accepting calls,
2121
// and reset after chain clients and RPC sever are stopped. node.connman should never be null here.
2222
assert(node.connman);
23+
assert(node.mempool);
2324
std::promise<void> promise;
2425
uint256 hashTx = tx->GetHash();
2526
bool callback_set = false;
@@ -35,10 +36,10 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
3536
// So if the output does exist, then this transaction exists in the chain.
3637
if (!existingCoin.IsSpent()) return TransactionError::ALREADY_IN_CHAIN;
3738
}
38-
if (!mempool.exists(hashTx)) {
39+
if (!node.mempool->exists(hashTx)) {
3940
// Transaction is not already in the mempool. Submit it.
4041
TxValidationState state;
41-
if (!AcceptToMemoryPool(mempool, state, std::move(tx),
42+
if (!AcceptToMemoryPool(*node.mempool, state, std::move(tx),
4243
nullptr /* plTxnReplaced */, false /* bypass_limits */, max_tx_fee)) {
4344
err_string = FormatStateMessage(state);
4445
if (state.IsInvalid()) {

src/qt/test/wallettests.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ void TestGUI(interfaces::Node& node)
134134
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
135135
}
136136
node.context()->connman = std::move(test.m_node.connman);
137+
node.context()->mempool = std::move(test.m_node.mempool);
137138
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), WalletLocation(), WalletDatabase::CreateMock());
138139
bool firstRun;
139140
wallet->LoadWallet(firstRun);

src/rpc/rawtransaction.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ static UniValue signrawtransactionwithkey(const JSONRPCRequest& request)
759759
for (const CTxIn& txin : mtx.vin) {
760760
coins[txin.prevout]; // Create empty map entry keyed by prevout.
761761
}
762-
FindCoins(coins);
762+
FindCoins(*g_rpc_node, coins);
763763

764764
// Parse the prevtxs array
765765
ParsePrevouts(request.params[2], &keystore, coins);

src/test/rpc_tests.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,10 @@ BOOST_AUTO_TEST_CASE(rpc_rawsign)
112112
std::string notsigned = r.get_str();
113113
std::string privkey1 = "\"KzsXybp9jX64P5ekX1KUxRQ79Jht9uzW7LorgwE65i5rWACL6LQe\"";
114114
std::string privkey2 = "\"Kyhdf5LuKTRx4ge69ybABsiUAWjVRK4XGxAKk2FQLp2HjGMy87Z4\"";
115-
NodeContext node;
116-
node.chain = interfaces::MakeChain(node);
117-
g_rpc_node = &node;
118115
r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" [] "+prevout);
119116
BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == false);
120117
r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" ["+privkey1+","+privkey2+"] "+prevout);
121118
BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == true);
122-
g_rpc_node = nullptr;
123119
}
124120

125121
BOOST_AUTO_TEST_CASE(rpc_createraw_op_return)

0 commit comments

Comments
 (0)