1818#include < sync.h>
1919#include < txmempool.h>
2020#include < util/check.h>
21+ #include < util/ref.h>
2122#include < util/strencodings.h>
2223#include < validation.h>
2324#include < version.h>
@@ -80,13 +81,14 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
8081 * @param[in] req the HTTP request
8182 * return pointer to the mempool or nullptr if no mempool found
8283 */
83- static CTxMemPool* GetMemPool (HTTPRequest* req)
84+ static CTxMemPool* GetMemPool (const util::Ref& context, HTTPRequest* req)
8485{
85- if (!g_rpc_node || !g_rpc_node->mempool ) {
86+ NodeContext* node = context.Has <NodeContext>() ? &context.Get <NodeContext>() : nullptr ;
87+ if (!node || !node->mempool ) {
8688 RESTERR (req, HTTP_NOT_FOUND, " Mempool disabled or instance not found" );
8789 return nullptr ;
8890 }
89- return g_rpc_node ->mempool ;
91+ return node ->mempool ;
9092}
9193
9294static RetFormat ParseDataFormat (std::string& param, const std::string& strReq)
@@ -134,7 +136,8 @@ static bool CheckWarmup(HTTPRequest* req)
134136 return true ;
135137}
136138
137- static bool rest_headers (HTTPRequest* req,
139+ static bool rest_headers (const util::Ref& context,
140+ HTTPRequest* req,
138141 const std::string& strURIPart)
139142{
140143 if (!CheckWarmup (req))
@@ -275,20 +278,20 @@ static bool rest_block(HTTPRequest* req,
275278 }
276279}
277280
278- static bool rest_block_extended (HTTPRequest* req, const std::string& strURIPart)
281+ static bool rest_block_extended (const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
279282{
280283 return rest_block (req, strURIPart, true );
281284}
282285
283- static bool rest_block_notxdetails (HTTPRequest* req, const std::string& strURIPart)
286+ static bool rest_block_notxdetails (const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
284287{
285288 return rest_block (req, strURIPart, false );
286289}
287290
288291// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
289292UniValue getblockchaininfo (const JSONRPCRequest& request);
290293
291- static bool rest_chaininfo (HTTPRequest* req, const std::string& strURIPart)
294+ static bool rest_chaininfo (const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
292295{
293296 if (!CheckWarmup (req))
294297 return false ;
@@ -297,7 +300,7 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
297300
298301 switch (rf) {
299302 case RetFormat::JSON: {
300- JSONRPCRequest jsonRequest;
303+ JSONRPCRequest jsonRequest (context) ;
301304 jsonRequest.params = UniValue (UniValue::VARR);
302305 UniValue chainInfoObject = getblockchaininfo (jsonRequest);
303306 std::string strJSON = chainInfoObject.write () + " \n " ;
@@ -311,11 +314,11 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
311314 }
312315}
313316
314- static bool rest_mempool_info (HTTPRequest* req, const std::string& strURIPart)
317+ static bool rest_mempool_info (const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
315318{
316319 if (!CheckWarmup (req))
317320 return false ;
318- const CTxMemPool* mempool = GetMemPool (req);
321+ const CTxMemPool* mempool = GetMemPool (context, req);
319322 if (!mempool) return false ;
320323 std::string param;
321324 const RetFormat rf = ParseDataFormat (param, strURIPart);
@@ -335,10 +338,10 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
335338 }
336339}
337340
338- static bool rest_mempool_contents (HTTPRequest* req, const std::string& strURIPart)
341+ static bool rest_mempool_contents (const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
339342{
340343 if (!CheckWarmup (req)) return false ;
341- const CTxMemPool* mempool = GetMemPool (req);
344+ const CTxMemPool* mempool = GetMemPool (context, req);
342345 if (!mempool) return false ;
343346 std::string param;
344347 const RetFormat rf = ParseDataFormat (param, strURIPart);
@@ -358,7 +361,7 @@ static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPar
358361 }
359362}
360363
361- static bool rest_tx (HTTPRequest* req, const std::string& strURIPart)
364+ static bool rest_tx (const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
362365{
363366 if (!CheckWarmup (req))
364367 return false ;
@@ -414,7 +417,7 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
414417 }
415418}
416419
417- static bool rest_getutxos (HTTPRequest* req, const std::string& strURIPart)
420+ static bool rest_getutxos (const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
418421{
419422 if (!CheckWarmup (req))
420423 return false ;
@@ -523,7 +526,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
523526 };
524527
525528 if (fCheckMemPool ) {
526- const CTxMemPool* mempool = GetMemPool (req);
529+ const CTxMemPool* mempool = GetMemPool (context, req);
527530 if (!mempool) return false ;
528531 // use db+mempool as cache backend in case user likes to query mempool
529532 LOCK2 (cs_main, mempool->cs );
@@ -600,7 +603,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
600603 }
601604}
602605
603- static bool rest_blockhash_by_height (HTTPRequest* req,
606+ static bool rest_blockhash_by_height (const util::Ref& context, HTTPRequest* req,
604607 const std::string& str_uri_part)
605608{
606609 if (!CheckWarmup (req)) return false ;
@@ -648,7 +651,7 @@ static bool rest_blockhash_by_height(HTTPRequest* req,
648651
649652static const struct {
650653 const char * prefix;
651- bool (*handler)(HTTPRequest* req, const std::string& strReq);
654+ bool (*handler)(const util::Ref& context, HTTPRequest* req, const std::string& strReq);
652655} uri_prefixes[] = {
653656 {" /rest/tx/" , rest_tx},
654657 {" /rest/block/notxdetails/" , rest_block_notxdetails},
@@ -661,10 +664,12 @@ static const struct {
661664 {" /rest/blockhashbyheight/" , rest_blockhash_by_height},
662665};
663666
664- void StartREST ()
667+ void StartREST (const util::Ref& context )
665668{
666- for (unsigned int i = 0 ; i < ARRAYLEN (uri_prefixes); i++)
667- RegisterHTTPHandler (uri_prefixes[i].prefix , false , uri_prefixes[i].handler );
669+ for (const auto & up : uri_prefixes) {
670+ auto handler = [&context, up](HTTPRequest* req, const std::string& prefix) { return up.handler (context, req, prefix); };
671+ RegisterHTTPHandler (up.prefix , false , handler);
672+ }
668673}
669674
670675void InterruptREST ()
0 commit comments