18
18
#include < sync.h>
19
19
#include < txmempool.h>
20
20
#include < util/check.h>
21
+ #include < util/ref.h>
21
22
#include < util/strencodings.h>
22
23
#include < validation.h>
23
24
#include < version.h>
@@ -80,13 +81,14 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
80
81
* @param[in] req the HTTP request
81
82
* return pointer to the mempool or nullptr if no mempool found
82
83
*/
83
- static CTxMemPool* GetMemPool (HTTPRequest* req)
84
+ static CTxMemPool* GetMemPool (const util::Ref& context, HTTPRequest* req)
84
85
{
85
- if (!g_rpc_node || !g_rpc_node->mempool ) {
86
+ NodeContext* node = context.Has <NodeContext>() ? &context.Get <NodeContext>() : nullptr ;
87
+ if (!node || !node->mempool ) {
86
88
RESTERR (req, HTTP_NOT_FOUND, " Mempool disabled or instance not found" );
87
89
return nullptr ;
88
90
}
89
- return g_rpc_node ->mempool ;
91
+ return node ->mempool ;
90
92
}
91
93
92
94
static RetFormat ParseDataFormat (std::string& param, const std::string& strReq)
@@ -134,7 +136,8 @@ static bool CheckWarmup(HTTPRequest* req)
134
136
return true ;
135
137
}
136
138
137
- static bool rest_headers (HTTPRequest* req,
139
+ static bool rest_headers (const util::Ref& context,
140
+ HTTPRequest* req,
138
141
const std::string& strURIPart)
139
142
{
140
143
if (!CheckWarmup (req))
@@ -275,20 +278,20 @@ static bool rest_block(HTTPRequest* req,
275
278
}
276
279
}
277
280
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)
279
282
{
280
283
return rest_block (req, strURIPart, true );
281
284
}
282
285
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)
284
287
{
285
288
return rest_block (req, strURIPart, false );
286
289
}
287
290
288
291
// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
289
292
UniValue getblockchaininfo (const JSONRPCRequest& request);
290
293
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)
292
295
{
293
296
if (!CheckWarmup (req))
294
297
return false ;
@@ -297,7 +300,7 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
297
300
298
301
switch (rf) {
299
302
case RetFormat::JSON: {
300
- JSONRPCRequest jsonRequest;
303
+ JSONRPCRequest jsonRequest (context) ;
301
304
jsonRequest.params = UniValue (UniValue::VARR);
302
305
UniValue chainInfoObject = getblockchaininfo (jsonRequest);
303
306
std::string strJSON = chainInfoObject.write () + " \n " ;
@@ -311,11 +314,11 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
311
314
}
312
315
}
313
316
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)
315
318
{
316
319
if (!CheckWarmup (req))
317
320
return false ;
318
- const CTxMemPool* mempool = GetMemPool (req);
321
+ const CTxMemPool* mempool = GetMemPool (context, req);
319
322
if (!mempool) return false ;
320
323
std::string param;
321
324
const RetFormat rf = ParseDataFormat (param, strURIPart);
@@ -335,10 +338,10 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
335
338
}
336
339
}
337
340
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)
339
342
{
340
343
if (!CheckWarmup (req)) return false ;
341
- const CTxMemPool* mempool = GetMemPool (req);
344
+ const CTxMemPool* mempool = GetMemPool (context, req);
342
345
if (!mempool) return false ;
343
346
std::string param;
344
347
const RetFormat rf = ParseDataFormat (param, strURIPart);
@@ -358,7 +361,7 @@ static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPar
358
361
}
359
362
}
360
363
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)
362
365
{
363
366
if (!CheckWarmup (req))
364
367
return false ;
@@ -414,7 +417,7 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
414
417
}
415
418
}
416
419
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)
418
421
{
419
422
if (!CheckWarmup (req))
420
423
return false ;
@@ -523,7 +526,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
523
526
};
524
527
525
528
if (fCheckMemPool ) {
526
- const CTxMemPool* mempool = GetMemPool (req);
529
+ const CTxMemPool* mempool = GetMemPool (context, req);
527
530
if (!mempool) return false ;
528
531
// use db+mempool as cache backend in case user likes to query mempool
529
532
LOCK2 (cs_main, mempool->cs );
@@ -600,7 +603,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
600
603
}
601
604
}
602
605
603
- static bool rest_blockhash_by_height (HTTPRequest* req,
606
+ static bool rest_blockhash_by_height (const util::Ref& context, HTTPRequest* req,
604
607
const std::string& str_uri_part)
605
608
{
606
609
if (!CheckWarmup (req)) return false ;
@@ -648,7 +651,7 @@ static bool rest_blockhash_by_height(HTTPRequest* req,
648
651
649
652
static const struct {
650
653
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);
652
655
} uri_prefixes[] = {
653
656
{" /rest/tx/" , rest_tx},
654
657
{" /rest/block/notxdetails/" , rest_block_notxdetails},
@@ -661,10 +664,12 @@ static const struct {
661
664
{" /rest/blockhashbyheight/" , rest_blockhash_by_height},
662
665
};
663
666
664
- void StartREST ()
667
+ void StartREST (const util::Ref& context )
665
668
{
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
+ }
668
673
}
669
674
670
675
void InterruptREST ()
0 commit comments