Skip to content

Commit fa8e650

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

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/rest.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <core_io.h>
99
#include <httpserver.h>
1010
#include <index/txindex.h>
11+
#include <node/context.h>
1112
#include <primitives/block.h>
1213
#include <primitives/transaction.h>
1314
#include <rpc/blockchain.h>
@@ -16,6 +17,7 @@
1617
#include <streams.h>
1718
#include <sync.h>
1819
#include <txmempool.h>
20+
#include <util/check.h>
1921
#include <util/strencodings.h>
2022
#include <validation.h>
2123
#include <version.h>
@@ -69,6 +71,24 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
6971
return false;
7072
}
7173

74+
/**
75+
* Get the node context mempool.
76+
*
77+
* Set the HTTP error and return nullptr if node context
78+
* mempool is not found.
79+
*
80+
* @param[in] req the HTTP request
81+
* return pointer to the mempool or nullptr if no mempool found
82+
*/
83+
static CTxMemPool* GetMemPool(HTTPRequest* req)
84+
{
85+
if (!g_rpc_node || !g_rpc_node->mempool) {
86+
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
87+
return nullptr;
88+
}
89+
return g_rpc_node->mempool;
90+
}
91+
7292
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
7393
{
7494
const std::string::size_type pos = strReq.rfind('.');
@@ -295,12 +315,14 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
295315
{
296316
if (!CheckWarmup(req))
297317
return false;
318+
const CTxMemPool* mempool = GetMemPool(req);
319+
if (!mempool) return false;
298320
std::string param;
299321
const RetFormat rf = ParseDataFormat(param, strURIPart);
300322

301323
switch (rf) {
302324
case RetFormat::JSON: {
303-
UniValue mempoolInfoObject = MempoolInfoToJSON(::mempool);
325+
UniValue mempoolInfoObject = MempoolInfoToJSON(*mempool);
304326

305327
std::string strJSON = mempoolInfoObject.write() + "\n";
306328
req->WriteHeader("Content-Type", "application/json");
@@ -315,14 +337,15 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
315337

316338
static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPart)
317339
{
318-
if (!CheckWarmup(req))
319-
return false;
340+
if (!CheckWarmup(req)) return false;
341+
const CTxMemPool* mempool = GetMemPool(req);
342+
if (!mempool) return false;
320343
std::string param;
321344
const RetFormat rf = ParseDataFormat(param, strURIPart);
322345

323346
switch (rf) {
324347
case RetFormat::JSON: {
325-
UniValue mempoolObject = MempoolToJSON(::mempool, true);
348+
UniValue mempoolObject = MempoolToJSON(*mempool, true);
326349

327350
std::string strJSON = mempoolObject.write() + "\n";
328351
req->WriteHeader("Content-Type", "application/json");
@@ -500,11 +523,13 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
500523
};
501524

502525
if (fCheckMemPool) {
526+
const CTxMemPool* mempool = GetMemPool(req);
527+
if (!mempool) return false;
503528
// use db+mempool as cache backend in case user likes to query mempool
504-
LOCK2(cs_main, mempool.cs);
529+
LOCK2(cs_main, mempool->cs);
505530
CCoinsViewCache& viewChain = ::ChainstateActive().CoinsTip();
506-
CCoinsViewMemPool viewMempool(&viewChain, mempool);
507-
process_utxos(viewMempool, mempool);
531+
CCoinsViewMemPool viewMempool(&viewChain, *mempool);
532+
process_utxos(viewMempool, *mempool);
508533
} else {
509534
LOCK(cs_main); // no need to lock mempool!
510535
process_utxos(::ChainstateActive().CoinsTip(), CTxMemPool());

0 commit comments

Comments
 (0)