8
8
#include < core_io.h>
9
9
#include < httpserver.h>
10
10
#include < index/txindex.h>
11
+ #include < node/context.h>
11
12
#include < primitives/block.h>
12
13
#include < primitives/transaction.h>
13
14
#include < rpc/blockchain.h>
16
17
#include < streams.h>
17
18
#include < sync.h>
18
19
#include < txmempool.h>
20
+ #include < util/check.h>
19
21
#include < util/strencodings.h>
20
22
#include < validation.h>
21
23
#include < version.h>
@@ -69,6 +71,24 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
69
71
return false ;
70
72
}
71
73
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
+
72
92
static RetFormat ParseDataFormat (std::string& param, const std::string& strReq)
73
93
{
74
94
const std::string::size_type pos = strReq.rfind (' .' );
@@ -295,12 +315,14 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
295
315
{
296
316
if (!CheckWarmup (req))
297
317
return false ;
318
+ const CTxMemPool* mempool = GetMemPool (req);
319
+ if (!mempool) return false ;
298
320
std::string param;
299
321
const RetFormat rf = ParseDataFormat (param, strURIPart);
300
322
301
323
switch (rf) {
302
324
case RetFormat::JSON: {
303
- UniValue mempoolInfoObject = MempoolInfoToJSON (:: mempool);
325
+ UniValue mempoolInfoObject = MempoolInfoToJSON (* mempool);
304
326
305
327
std::string strJSON = mempoolInfoObject.write () + " \n " ;
306
328
req->WriteHeader (" Content-Type" , " application/json" );
@@ -315,14 +337,15 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
315
337
316
338
static bool rest_mempool_contents (HTTPRequest* req, const std::string& strURIPart)
317
339
{
318
- if (!CheckWarmup (req))
319
- return false ;
340
+ if (!CheckWarmup (req)) return false ;
341
+ const CTxMemPool* mempool = GetMemPool (req);
342
+ if (!mempool) return false ;
320
343
std::string param;
321
344
const RetFormat rf = ParseDataFormat (param, strURIPart);
322
345
323
346
switch (rf) {
324
347
case RetFormat::JSON: {
325
- UniValue mempoolObject = MempoolToJSON (:: mempool, true );
348
+ UniValue mempoolObject = MempoolToJSON (* mempool, true );
326
349
327
350
std::string strJSON = mempoolObject.write () + " \n " ;
328
351
req->WriteHeader (" Content-Type" , " application/json" );
@@ -500,11 +523,13 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
500
523
};
501
524
502
525
if (fCheckMemPool ) {
526
+ const CTxMemPool* mempool = GetMemPool (req);
527
+ if (!mempool) return false ;
503
528
// 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 );
505
530
CCoinsViewCache& viewChain = ::ChainstateActive ().CoinsTip ();
506
- CCoinsViewMemPool viewMempool (&viewChain, mempool);
507
- process_utxos (viewMempool, mempool);
531
+ CCoinsViewMemPool viewMempool (&viewChain, * mempool);
532
+ process_utxos (viewMempool, * mempool);
508
533
} else {
509
534
LOCK (cs_main); // no need to lock mempool!
510
535
process_utxos (::ChainstateActive ().CoinsTip (), CTxMemPool ());
0 commit comments