Skip to content

Commit 6fca33b

Browse files
committed
refactor: Pass NodeContext to RPC and REST methods through util::Ref
This commit does not change behavior
1 parent 691c817 commit 6fca33b

17 files changed

+150
-86
lines changed

src/bitcoind.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <noui.h>
1717
#include <shutdown.h>
1818
#include <ui_interface.h>
19+
#include <util/ref.h>
1920
#include <util/strencodings.h>
2021
#include <util/system.h>
2122
#include <util/threadnames.h>
@@ -77,6 +78,7 @@ static bool AppInit(int argc, char* argv[])
7778
return true;
7879
}
7980

81+
util::Ref context{node};
8082
try
8183
{
8284
if (!CheckDataDirOption()) {
@@ -145,7 +147,7 @@ static bool AppInit(int argc, char* argv[])
145147
// If locking the data directory failed, exit immediately
146148
return false;
147149
}
148-
fRet = AppInitMain(node);
150+
fRet = AppInitMain(context, node);
149151
}
150152
catch (const std::exception& e) {
151153
PrintExceptionContinue(&e, "AppInit()");

src/httprpc.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
151151
return multiUserAuthorized(strUserPass);
152152
}
153153

154-
static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
154+
static bool HTTPReq_JSONRPC(const util::Ref& context, HTTPRequest* req)
155155
{
156156
// JSONRPC handles only POST
157157
if (req->GetRequestMethod() != HTTPRequest::POST) {
@@ -166,7 +166,7 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
166166
return false;
167167
}
168168

169-
JSONRPCRequest jreq;
169+
JSONRPCRequest jreq(context);
170170
jreq.peerAddr = req->GetPeer().ToString();
171171
if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
172172
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr);
@@ -288,15 +288,16 @@ static bool InitRPCAuthentication()
288288
return true;
289289
}
290290

291-
bool StartHTTPRPC()
291+
bool StartHTTPRPC(const util::Ref& context)
292292
{
293293
LogPrint(BCLog::RPC, "Starting HTTP RPC server\n");
294294
if (!InitRPCAuthentication())
295295
return false;
296296

297-
RegisterHTTPHandler("/", true, HTTPReq_JSONRPC);
297+
auto handle_rpc = [&context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); };
298+
RegisterHTTPHandler("/", true, handle_rpc);
298299
if (g_wallet_init_interface.HasWalletSupport()) {
299-
RegisterHTTPHandler("/wallet/", false, HTTPReq_JSONRPC);
300+
RegisterHTTPHandler("/wallet/", false, handle_rpc);
300301
}
301302
struct event_base* eventBase = EventBase();
302303
assert(eventBase);

src/httprpc.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
#ifndef BITCOIN_HTTPRPC_H
66
#define BITCOIN_HTTPRPC_H
77

8+
namespace util {
9+
class Ref;
10+
} // namespace util
811

912
/** Start HTTP RPC subsystem.
1013
* Precondition; HTTP and RPC has been started.
1114
*/
12-
bool StartHTTPRPC();
15+
bool StartHTTPRPC(const util::Ref& context);
1316
/** Interrupt HTTP RPC subsystem.
1417
*/
1518
void InterruptHTTPRPC();
@@ -21,7 +24,7 @@ void StopHTTPRPC();
2124
/** Start HTTP REST subsystem.
2225
* Precondition; HTTP and RPC has been started.
2326
*/
24-
void StartREST();
27+
void StartREST(const util::Ref& context);
2528
/** Interrupt RPC REST subsystem.
2629
*/
2730
void InterruptREST();

src/init.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -783,16 +783,16 @@ static bool InitSanityCheck()
783783
return true;
784784
}
785785

786-
static bool AppInitServers()
786+
static bool AppInitServers(const util::Ref& context)
787787
{
788788
RPCServer::OnStarted(&OnRPCStarted);
789789
RPCServer::OnStopped(&OnRPCStopped);
790790
if (!InitHTTPServer())
791791
return false;
792792
StartRPC();
793-
if (!StartHTTPRPC())
793+
if (!StartHTTPRPC(context))
794794
return false;
795-
if (gArgs.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST();
795+
if (gArgs.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST(context);
796796
StartHTTPServer();
797797
return true;
798798
}
@@ -1237,7 +1237,7 @@ bool AppInitLockDataDirectory()
12371237
return true;
12381238
}
12391239

1240-
bool AppInitMain(NodeContext& node)
1240+
bool AppInitMain(const util::Ref& context, NodeContext& node)
12411241
{
12421242
const CChainParams& chainparams = Params();
12431243
// ********************************************************* Step 4a: application initialization
@@ -1352,7 +1352,7 @@ bool AppInitMain(NodeContext& node)
13521352
if (gArgs.GetBoolArg("-server", false))
13531353
{
13541354
uiInterface.InitMessage_connect(SetRPCWarmupStatus);
1355-
if (!AppInitServers())
1355+
if (!AppInitServers(context))
13561356
return InitError(_("Unable to start HTTP server. See debug log for details."));
13571357
}
13581358

src/init.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ struct NodeContext;
1414
namespace boost {
1515
class thread_group;
1616
} // namespace boost
17+
namespace util {
18+
class Ref;
19+
} // namespace util
1720

1821
/** Interrupt threads */
1922
void Interrupt(NodeContext& node);
@@ -51,7 +54,7 @@ bool AppInitLockDataDirectory();
5154
* @note This should only be done after daemonization. Call Shutdown() if this function fails.
5255
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
5356
*/
54-
bool AppInitMain(NodeContext& node);
57+
bool AppInitMain(const util::Ref& context, NodeContext& node);
5558

5659
/**
5760
* Register all arguments with the ArgsManager

src/interfaces/node.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <sync.h>
2828
#include <txmempool.h>
2929
#include <ui_interface.h>
30+
#include <util/ref.h>
3031
#include <util/system.h>
3132
#include <util/translation.h>
3233
#include <validation.h>
@@ -80,7 +81,7 @@ class NodeImpl : public Node
8081
bool appInitMain() override
8182
{
8283
m_context.chain = MakeChain(m_context);
83-
return AppInitMain(m_context);
84+
return AppInitMain(m_context_ref, m_context);
8485
}
8586
void appShutdown() override
8687
{
@@ -225,7 +226,7 @@ class NodeImpl : public Node
225226
CFeeRate getDustRelayFee() override { return ::dustRelayFee; }
226227
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
227228
{
228-
JSONRPCRequest req;
229+
JSONRPCRequest req(m_context_ref);
229230
req.params = params;
230231
req.strMethod = command;
231232
req.URI = uri;
@@ -323,6 +324,7 @@ class NodeImpl : public Node
323324
}
324325
NodeContext* context() override { return &m_context; }
325326
NodeContext m_context;
327+
util::Ref m_context_ref{m_context};
326328
};
327329

328330
} // namespace

src/rest.cpp

+25-20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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

9294
static 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
289292
UniValue 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

649652
static 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

670675
void InterruptREST()

0 commit comments

Comments
 (0)