mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-10 09:06:15 +01:00
refactor: Pass NodeContext to RPC and REST methods through util::Ref
This commit does not change behavior
This commit is contained in:
parent
691c817b34
commit
6fca33b2ed
17 changed files with 150 additions and 86 deletions
|
@ -16,6 +16,7 @@
|
||||||
#include <noui.h>
|
#include <noui.h>
|
||||||
#include <shutdown.h>
|
#include <shutdown.h>
|
||||||
#include <ui_interface.h>
|
#include <ui_interface.h>
|
||||||
|
#include <util/ref.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
#include <util/threadnames.h>
|
#include <util/threadnames.h>
|
||||||
|
@ -77,6 +78,7 @@ static bool AppInit(int argc, char* argv[])
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
util::Ref context{node};
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!CheckDataDirOption()) {
|
if (!CheckDataDirOption()) {
|
||||||
|
@ -145,7 +147,7 @@ static bool AppInit(int argc, char* argv[])
|
||||||
// If locking the data directory failed, exit immediately
|
// If locking the data directory failed, exit immediately
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
fRet = AppInitMain(node);
|
fRet = AppInitMain(context, node);
|
||||||
}
|
}
|
||||||
catch (const std::exception& e) {
|
catch (const std::exception& e) {
|
||||||
PrintExceptionContinue(&e, "AppInit()");
|
PrintExceptionContinue(&e, "AppInit()");
|
||||||
|
|
|
@ -151,7 +151,7 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
|
||||||
return multiUserAuthorized(strUserPass);
|
return multiUserAuthorized(strUserPass);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
|
static bool HTTPReq_JSONRPC(const util::Ref& context, HTTPRequest* req)
|
||||||
{
|
{
|
||||||
// JSONRPC handles only POST
|
// JSONRPC handles only POST
|
||||||
if (req->GetRequestMethod() != HTTPRequest::POST) {
|
if (req->GetRequestMethod() != HTTPRequest::POST) {
|
||||||
|
@ -166,7 +166,7 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONRPCRequest jreq;
|
JSONRPCRequest jreq(context);
|
||||||
jreq.peerAddr = req->GetPeer().ToString();
|
jreq.peerAddr = req->GetPeer().ToString();
|
||||||
if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
|
if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
|
||||||
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr);
|
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr);
|
||||||
|
@ -288,15 +288,16 @@ static bool InitRPCAuthentication()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StartHTTPRPC()
|
bool StartHTTPRPC(const util::Ref& context)
|
||||||
{
|
{
|
||||||
LogPrint(BCLog::RPC, "Starting HTTP RPC server\n");
|
LogPrint(BCLog::RPC, "Starting HTTP RPC server\n");
|
||||||
if (!InitRPCAuthentication())
|
if (!InitRPCAuthentication())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
RegisterHTTPHandler("/", true, HTTPReq_JSONRPC);
|
auto handle_rpc = [&context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); };
|
||||||
|
RegisterHTTPHandler("/", true, handle_rpc);
|
||||||
if (g_wallet_init_interface.HasWalletSupport()) {
|
if (g_wallet_init_interface.HasWalletSupport()) {
|
||||||
RegisterHTTPHandler("/wallet/", false, HTTPReq_JSONRPC);
|
RegisterHTTPHandler("/wallet/", false, handle_rpc);
|
||||||
}
|
}
|
||||||
struct event_base* eventBase = EventBase();
|
struct event_base* eventBase = EventBase();
|
||||||
assert(eventBase);
|
assert(eventBase);
|
||||||
|
|
|
@ -5,11 +5,14 @@
|
||||||
#ifndef BITCOIN_HTTPRPC_H
|
#ifndef BITCOIN_HTTPRPC_H
|
||||||
#define BITCOIN_HTTPRPC_H
|
#define BITCOIN_HTTPRPC_H
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
class Ref;
|
||||||
|
} // namespace util
|
||||||
|
|
||||||
/** Start HTTP RPC subsystem.
|
/** Start HTTP RPC subsystem.
|
||||||
* Precondition; HTTP and RPC has been started.
|
* Precondition; HTTP and RPC has been started.
|
||||||
*/
|
*/
|
||||||
bool StartHTTPRPC();
|
bool StartHTTPRPC(const util::Ref& context);
|
||||||
/** Interrupt HTTP RPC subsystem.
|
/** Interrupt HTTP RPC subsystem.
|
||||||
*/
|
*/
|
||||||
void InterruptHTTPRPC();
|
void InterruptHTTPRPC();
|
||||||
|
@ -21,7 +24,7 @@ void StopHTTPRPC();
|
||||||
/** Start HTTP REST subsystem.
|
/** Start HTTP REST subsystem.
|
||||||
* Precondition; HTTP and RPC has been started.
|
* Precondition; HTTP and RPC has been started.
|
||||||
*/
|
*/
|
||||||
void StartREST();
|
void StartREST(const util::Ref& context);
|
||||||
/** Interrupt RPC REST subsystem.
|
/** Interrupt RPC REST subsystem.
|
||||||
*/
|
*/
|
||||||
void InterruptREST();
|
void InterruptREST();
|
||||||
|
|
10
src/init.cpp
10
src/init.cpp
|
@ -783,16 +783,16 @@ static bool InitSanityCheck()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool AppInitServers()
|
static bool AppInitServers(const util::Ref& context)
|
||||||
{
|
{
|
||||||
RPCServer::OnStarted(&OnRPCStarted);
|
RPCServer::OnStarted(&OnRPCStarted);
|
||||||
RPCServer::OnStopped(&OnRPCStopped);
|
RPCServer::OnStopped(&OnRPCStopped);
|
||||||
if (!InitHTTPServer())
|
if (!InitHTTPServer())
|
||||||
return false;
|
return false;
|
||||||
StartRPC();
|
StartRPC();
|
||||||
if (!StartHTTPRPC())
|
if (!StartHTTPRPC(context))
|
||||||
return false;
|
return false;
|
||||||
if (gArgs.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST();
|
if (gArgs.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST(context);
|
||||||
StartHTTPServer();
|
StartHTTPServer();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1237,7 +1237,7 @@ bool AppInitLockDataDirectory()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AppInitMain(NodeContext& node)
|
bool AppInitMain(const util::Ref& context, NodeContext& node)
|
||||||
{
|
{
|
||||||
const CChainParams& chainparams = Params();
|
const CChainParams& chainparams = Params();
|
||||||
// ********************************************************* Step 4a: application initialization
|
// ********************************************************* Step 4a: application initialization
|
||||||
|
@ -1352,7 +1352,7 @@ bool AppInitMain(NodeContext& node)
|
||||||
if (gArgs.GetBoolArg("-server", false))
|
if (gArgs.GetBoolArg("-server", false))
|
||||||
{
|
{
|
||||||
uiInterface.InitMessage_connect(SetRPCWarmupStatus);
|
uiInterface.InitMessage_connect(SetRPCWarmupStatus);
|
||||||
if (!AppInitServers())
|
if (!AppInitServers(context))
|
||||||
return InitError(_("Unable to start HTTP server. See debug log for details."));
|
return InitError(_("Unable to start HTTP server. See debug log for details."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,9 @@ struct NodeContext;
|
||||||
namespace boost {
|
namespace boost {
|
||||||
class thread_group;
|
class thread_group;
|
||||||
} // namespace boost
|
} // namespace boost
|
||||||
|
namespace util {
|
||||||
|
class Ref;
|
||||||
|
} // namespace util
|
||||||
|
|
||||||
/** Interrupt threads */
|
/** Interrupt threads */
|
||||||
void Interrupt(NodeContext& node);
|
void Interrupt(NodeContext& node);
|
||||||
|
@ -51,7 +54,7 @@ bool AppInitLockDataDirectory();
|
||||||
* @note This should only be done after daemonization. Call Shutdown() if this function fails.
|
* @note This should only be done after daemonization. Call Shutdown() if this function fails.
|
||||||
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
|
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
|
||||||
*/
|
*/
|
||||||
bool AppInitMain(NodeContext& node);
|
bool AppInitMain(const util::Ref& context, NodeContext& node);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register all arguments with the ArgsManager
|
* Register all arguments with the ArgsManager
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <ui_interface.h>
|
#include <ui_interface.h>
|
||||||
|
#include <util/ref.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
@ -80,7 +81,7 @@ public:
|
||||||
bool appInitMain() override
|
bool appInitMain() override
|
||||||
{
|
{
|
||||||
m_context.chain = MakeChain(m_context);
|
m_context.chain = MakeChain(m_context);
|
||||||
return AppInitMain(m_context);
|
return AppInitMain(m_context_ref, m_context);
|
||||||
}
|
}
|
||||||
void appShutdown() override
|
void appShutdown() override
|
||||||
{
|
{
|
||||||
|
@ -225,7 +226,7 @@ public:
|
||||||
CFeeRate getDustRelayFee() override { return ::dustRelayFee; }
|
CFeeRate getDustRelayFee() override { return ::dustRelayFee; }
|
||||||
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
|
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
|
||||||
{
|
{
|
||||||
JSONRPCRequest req;
|
JSONRPCRequest req(m_context_ref);
|
||||||
req.params = params;
|
req.params = params;
|
||||||
req.strMethod = command;
|
req.strMethod = command;
|
||||||
req.URI = uri;
|
req.URI = uri;
|
||||||
|
@ -323,6 +324,7 @@ public:
|
||||||
}
|
}
|
||||||
NodeContext* context() override { return &m_context; }
|
NodeContext* context() override { return &m_context; }
|
||||||
NodeContext m_context;
|
NodeContext m_context;
|
||||||
|
util::Ref m_context_ref{m_context};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
45
src/rest.cpp
45
src/rest.cpp
|
@ -18,6 +18,7 @@
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <util/check.h>
|
#include <util/check.h>
|
||||||
|
#include <util/ref.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
#include <version.h>
|
#include <version.h>
|
||||||
|
@ -80,13 +81,14 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
|
||||||
* @param[in] req the HTTP request
|
* @param[in] req the HTTP request
|
||||||
* return pointer to the mempool or nullptr if no mempool found
|
* return pointer to the mempool or nullptr if no mempool found
|
||||||
*/
|
*/
|
||||||
static CTxMemPool* GetMemPool(HTTPRequest* req)
|
static CTxMemPool* GetMemPool(const util::Ref& context, HTTPRequest* req)
|
||||||
{
|
{
|
||||||
if (!g_rpc_node || !g_rpc_node->mempool) {
|
NodeContext* node = context.Has<NodeContext>() ? &context.Get<NodeContext>() : nullptr;
|
||||||
|
if (!node || !node->mempool) {
|
||||||
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
|
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return g_rpc_node->mempool;
|
return node->mempool;
|
||||||
}
|
}
|
||||||
|
|
||||||
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
|
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
|
||||||
|
@ -134,7 +136,8 @@ static bool CheckWarmup(HTTPRequest* req)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rest_headers(HTTPRequest* req,
|
static bool rest_headers(const util::Ref& context,
|
||||||
|
HTTPRequest* req,
|
||||||
const std::string& strURIPart)
|
const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
if (!CheckWarmup(req))
|
if (!CheckWarmup(req))
|
||||||
|
@ -275,12 +278,12 @@ static bool rest_block(HTTPRequest* req,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rest_block_extended(HTTPRequest* req, const std::string& strURIPart)
|
static bool rest_block_extended(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
return rest_block(req, strURIPart, true);
|
return rest_block(req, strURIPart, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rest_block_notxdetails(HTTPRequest* req, const std::string& strURIPart)
|
static bool rest_block_notxdetails(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
return rest_block(req, strURIPart, false);
|
return rest_block(req, strURIPart, false);
|
||||||
}
|
}
|
||||||
|
@ -288,7 +291,7 @@ static bool rest_block_notxdetails(HTTPRequest* req, const std::string& strURIPa
|
||||||
// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
|
// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
|
||||||
UniValue getblockchaininfo(const JSONRPCRequest& request);
|
UniValue getblockchaininfo(const JSONRPCRequest& request);
|
||||||
|
|
||||||
static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
|
static bool rest_chaininfo(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
if (!CheckWarmup(req))
|
if (!CheckWarmup(req))
|
||||||
return false;
|
return false;
|
||||||
|
@ -297,7 +300,7 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
|
||||||
|
|
||||||
switch (rf) {
|
switch (rf) {
|
||||||
case RetFormat::JSON: {
|
case RetFormat::JSON: {
|
||||||
JSONRPCRequest jsonRequest;
|
JSONRPCRequest jsonRequest(context);
|
||||||
jsonRequest.params = UniValue(UniValue::VARR);
|
jsonRequest.params = UniValue(UniValue::VARR);
|
||||||
UniValue chainInfoObject = getblockchaininfo(jsonRequest);
|
UniValue chainInfoObject = getblockchaininfo(jsonRequest);
|
||||||
std::string strJSON = chainInfoObject.write() + "\n";
|
std::string strJSON = chainInfoObject.write() + "\n";
|
||||||
|
@ -311,11 +314,11 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
|
static bool rest_mempool_info(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
if (!CheckWarmup(req))
|
if (!CheckWarmup(req))
|
||||||
return false;
|
return false;
|
||||||
const CTxMemPool* mempool = GetMemPool(req);
|
const CTxMemPool* mempool = GetMemPool(context, req);
|
||||||
if (!mempool) return false;
|
if (!mempool) return false;
|
||||||
std::string param;
|
std::string param;
|
||||||
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
||||||
|
@ -335,10 +338,10 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPart)
|
static bool rest_mempool_contents(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
if (!CheckWarmup(req)) return false;
|
if (!CheckWarmup(req)) return false;
|
||||||
const CTxMemPool* mempool = GetMemPool(req);
|
const CTxMemPool* mempool = GetMemPool(context, req);
|
||||||
if (!mempool) return false;
|
if (!mempool) return false;
|
||||||
std::string param;
|
std::string param;
|
||||||
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
||||||
|
@ -358,7 +361,7 @@ static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPar
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
|
static bool rest_tx(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
if (!CheckWarmup(req))
|
if (!CheckWarmup(req))
|
||||||
return false;
|
return false;
|
||||||
|
@ -414,7 +417,7 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
|
static bool rest_getutxos(const util::Ref& context, HTTPRequest* req, const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
if (!CheckWarmup(req))
|
if (!CheckWarmup(req))
|
||||||
return false;
|
return false;
|
||||||
|
@ -523,7 +526,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
|
||||||
};
|
};
|
||||||
|
|
||||||
if (fCheckMemPool) {
|
if (fCheckMemPool) {
|
||||||
const CTxMemPool* mempool = GetMemPool(req);
|
const CTxMemPool* mempool = GetMemPool(context, req);
|
||||||
if (!mempool) return false;
|
if (!mempool) return false;
|
||||||
// use db+mempool as cache backend in case user likes to query mempool
|
// use db+mempool as cache backend in case user likes to query mempool
|
||||||
LOCK2(cs_main, mempool->cs);
|
LOCK2(cs_main, mempool->cs);
|
||||||
|
@ -600,7 +603,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rest_blockhash_by_height(HTTPRequest* req,
|
static bool rest_blockhash_by_height(const util::Ref& context, HTTPRequest* req,
|
||||||
const std::string& str_uri_part)
|
const std::string& str_uri_part)
|
||||||
{
|
{
|
||||||
if (!CheckWarmup(req)) return false;
|
if (!CheckWarmup(req)) return false;
|
||||||
|
@ -648,7 +651,7 @@ static bool rest_blockhash_by_height(HTTPRequest* req,
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
const char* prefix;
|
const char* prefix;
|
||||||
bool (*handler)(HTTPRequest* req, const std::string& strReq);
|
bool (*handler)(const util::Ref& context, HTTPRequest* req, const std::string& strReq);
|
||||||
} uri_prefixes[] = {
|
} uri_prefixes[] = {
|
||||||
{"/rest/tx/", rest_tx},
|
{"/rest/tx/", rest_tx},
|
||||||
{"/rest/block/notxdetails/", rest_block_notxdetails},
|
{"/rest/block/notxdetails/", rest_block_notxdetails},
|
||||||
|
@ -661,10 +664,12 @@ static const struct {
|
||||||
{"/rest/blockhashbyheight/", rest_blockhash_by_height},
|
{"/rest/blockhashbyheight/", rest_blockhash_by_height},
|
||||||
};
|
};
|
||||||
|
|
||||||
void StartREST()
|
void StartREST(const util::Ref& context)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++)
|
for (const auto& up : uri_prefixes) {
|
||||||
RegisterHTTPHandler(uri_prefixes[i].prefix, false, uri_prefixes[i].handler);
|
auto handler = [&context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); };
|
||||||
|
RegisterHTTPHandler(up.prefix, false, handler);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InterruptREST()
|
void InterruptREST()
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <txdb.h>
|
#include <txdb.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <undo.h>
|
#include <undo.h>
|
||||||
|
#include <util/ref.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
@ -53,13 +54,21 @@ static Mutex cs_blockchange;
|
||||||
static std::condition_variable cond_blockchange;
|
static std::condition_variable cond_blockchange;
|
||||||
static CUpdatedBlock latestblock;
|
static CUpdatedBlock latestblock;
|
||||||
|
|
||||||
CTxMemPool& EnsureMemPool()
|
NodeContext& EnsureNodeContext(const util::Ref& context)
|
||||||
{
|
{
|
||||||
CHECK_NONFATAL(g_rpc_node);
|
if (!context.Has<NodeContext>()) {
|
||||||
if (!g_rpc_node->mempool) {
|
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
|
||||||
|
}
|
||||||
|
return context.Get<NodeContext>();
|
||||||
|
}
|
||||||
|
|
||||||
|
CTxMemPool& EnsureMemPool(const util::Ref& context)
|
||||||
|
{
|
||||||
|
NodeContext& node = EnsureNodeContext(context);
|
||||||
|
if (!node.mempool) {
|
||||||
throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
|
throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
|
||||||
}
|
}
|
||||||
return *g_rpc_node->mempool;
|
return *node.mempool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Calculate the difficulty for a given block index.
|
/* Calculate the difficulty for a given block index.
|
||||||
|
@ -519,7 +528,7 @@ static UniValue getrawmempool(const JSONRPCRequest& request)
|
||||||
if (!request.params[0].isNull())
|
if (!request.params[0].isNull())
|
||||||
fVerbose = request.params[0].get_bool();
|
fVerbose = request.params[0].get_bool();
|
||||||
|
|
||||||
return MempoolToJSON(EnsureMemPool(), fVerbose);
|
return MempoolToJSON(EnsureMemPool(request.context), fVerbose);
|
||||||
}
|
}
|
||||||
|
|
||||||
static UniValue getmempoolancestors(const JSONRPCRequest& request)
|
static UniValue getmempoolancestors(const JSONRPCRequest& request)
|
||||||
|
@ -549,7 +558,7 @@ static UniValue getmempoolancestors(const JSONRPCRequest& request)
|
||||||
|
|
||||||
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
||||||
|
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
LOCK(mempool.cs);
|
LOCK(mempool.cs);
|
||||||
|
|
||||||
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
||||||
|
@ -612,7 +621,7 @@ static UniValue getmempooldescendants(const JSONRPCRequest& request)
|
||||||
|
|
||||||
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
||||||
|
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
LOCK(mempool.cs);
|
LOCK(mempool.cs);
|
||||||
|
|
||||||
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
||||||
|
@ -662,7 +671,7 @@ static UniValue getmempoolentry(const JSONRPCRequest& request)
|
||||||
|
|
||||||
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
||||||
|
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
LOCK(mempool.cs);
|
LOCK(mempool.cs);
|
||||||
|
|
||||||
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
||||||
|
@ -1045,7 +1054,7 @@ UniValue gettxout(const JSONRPCRequest& request)
|
||||||
CCoinsViewCache* coins_view = &::ChainstateActive().CoinsTip();
|
CCoinsViewCache* coins_view = &::ChainstateActive().CoinsTip();
|
||||||
|
|
||||||
if (fMempool) {
|
if (fMempool) {
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
LOCK(mempool.cs);
|
LOCK(mempool.cs);
|
||||||
CCoinsViewMemPool view(coins_view, mempool);
|
CCoinsViewMemPool view(coins_view, mempool);
|
||||||
if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
|
if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
|
||||||
|
@ -1415,7 +1424,7 @@ static UniValue getmempoolinfo(const JSONRPCRequest& request)
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
return MempoolInfoToJSON(EnsureMemPool());
|
return MempoolInfoToJSON(EnsureMemPool(request.context));
|
||||||
}
|
}
|
||||||
|
|
||||||
static UniValue preciousblock(const JSONRPCRequest& request)
|
static UniValue preciousblock(const JSONRPCRequest& request)
|
||||||
|
@ -1934,7 +1943,7 @@ static UniValue savemempool(const JSONRPCRequest& request)
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
|
|
||||||
if (!mempool.IsLoaded()) {
|
if (!mempool.IsLoaded()) {
|
||||||
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
|
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
|
||||||
|
|
|
@ -18,6 +18,9 @@ class CBlockIndex;
|
||||||
class CTxMemPool;
|
class CTxMemPool;
|
||||||
class UniValue;
|
class UniValue;
|
||||||
struct NodeContext;
|
struct NodeContext;
|
||||||
|
namespace util {
|
||||||
|
class Ref;
|
||||||
|
} // namespace util
|
||||||
|
|
||||||
static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5;
|
static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5;
|
||||||
|
|
||||||
|
@ -52,6 +55,7 @@ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES],
|
||||||
//! direct way to pass in state to RPC methods without globals.
|
//! direct way to pass in state to RPC methods without globals.
|
||||||
extern NodeContext* g_rpc_node;
|
extern NodeContext* g_rpc_node;
|
||||||
|
|
||||||
CTxMemPool& EnsureMemPool();
|
NodeContext& EnsureNodeContext(const util::Ref& context);
|
||||||
|
CTxMemPool& EnsureMemPool(const util::Ref& context);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -227,7 +227,7 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
|
||||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
|
|
||||||
return generateBlocks(mempool, coinbase_script, num_blocks, max_tries);
|
return generateBlocks(mempool, coinbase_script, num_blocks, max_tries);
|
||||||
}
|
}
|
||||||
|
@ -265,7 +265,7 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
|
||||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: Invalid address");
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: Invalid address");
|
||||||
}
|
}
|
||||||
|
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
|
|
||||||
CScript coinbase_script = GetScriptForDestination(destination);
|
CScript coinbase_script = GetScriptForDestination(destination);
|
||||||
|
|
||||||
|
@ -311,7 +311,7 @@ static UniValue generateblock(const JSONRPCRequest& request)
|
||||||
coinbase_script = GetScriptForDestination(destination);
|
coinbase_script = GetScriptForDestination(destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
|
|
||||||
std::vector<CTransactionRef> txs;
|
std::vector<CTransactionRef> txs;
|
||||||
const auto raw_txs_or_txids = request.params[1].get_array();
|
const auto raw_txs_or_txids = request.params[1].get_array();
|
||||||
|
@ -403,7 +403,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
|
|
||||||
UniValue obj(UniValue::VOBJ);
|
UniValue obj(UniValue::VOBJ);
|
||||||
obj.pushKV("blocks", (int)::ChainActive().Height());
|
obj.pushKV("blocks", (int)::ChainActive().Height());
|
||||||
|
@ -449,7 +449,7 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)
|
||||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.");
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.");
|
||||||
}
|
}
|
||||||
|
|
||||||
EnsureMemPool().PrioritiseTransaction(hash, nAmount);
|
EnsureMemPool(request.context).PrioritiseTransaction(hash, nAmount);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -635,7 +635,8 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
|
||||||
if (strMode != "template")
|
if (strMode != "template")
|
||||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
|
||||||
|
|
||||||
if(!g_rpc_node->connman)
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if(!node.connman)
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
|
|
||||||
if (g_rpc_node->connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0)
|
if (g_rpc_node->connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0)
|
||||||
|
@ -645,7 +646,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
|
||||||
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is in initial sync and waiting for blocks...");
|
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is in initial sync and waiting for blocks...");
|
||||||
|
|
||||||
static unsigned int nTransactionsUpdatedLast;
|
static unsigned int nTransactionsUpdatedLast;
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
|
|
||||||
if (!lpval.isNull())
|
if (!lpval.isNull())
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include <script/descriptor.h>
|
#include <script/descriptor.h>
|
||||||
#include <util/check.h>
|
#include <util/check.h>
|
||||||
#include <util/message.h> // For MessageSign(), MessageVerify()
|
#include <util/message.h> // For MessageSign(), MessageVerify()
|
||||||
|
#include <util/ref.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
|
|
||||||
|
@ -366,8 +367,8 @@ static UniValue setmocktime(const JSONRPCRequest& request)
|
||||||
RPCTypeCheck(request.params, {UniValue::VNUM});
|
RPCTypeCheck(request.params, {UniValue::VNUM});
|
||||||
int64_t time = request.params[0].get_int64();
|
int64_t time = request.params[0].get_int64();
|
||||||
SetMockTime(time);
|
SetMockTime(time);
|
||||||
if (g_rpc_node) {
|
if (request.context.Has<NodeContext>()) {
|
||||||
for (const auto& chain_client : g_rpc_node->chain_clients) {
|
for (const auto& chain_client : request.context.Get<NodeContext>().chain_clients) {
|
||||||
chain_client->setMockTime(time);
|
chain_client->setMockTime(time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -398,9 +399,10 @@ static UniValue mockscheduler(const JSONRPCRequest& request)
|
||||||
}
|
}
|
||||||
|
|
||||||
// protect against null pointer dereference
|
// protect against null pointer dereference
|
||||||
CHECK_NONFATAL(g_rpc_node);
|
CHECK_NONFATAL(request.context.Has<NodeContext>());
|
||||||
CHECK_NONFATAL(g_rpc_node->scheduler);
|
NodeContext& node = request.context.Get<NodeContext>();
|
||||||
g_rpc_node->scheduler->MockForward(std::chrono::seconds(delta_seconds));
|
CHECK_NONFATAL(node.scheduler);
|
||||||
|
node.scheduler->MockForward(std::chrono::seconds(delta_seconds));
|
||||||
|
|
||||||
return NullUniValue;
|
return NullUniValue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,8 @@ static UniValue getconnectioncount(const JSONRPCRequest& request)
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
if(!g_rpc_node->connman)
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if(!node.connman)
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
|
|
||||||
return (int)g_rpc_node->connman->GetNodeCount(CConnman::CONNECTIONS_ALL);
|
return (int)g_rpc_node->connman->GetNodeCount(CConnman::CONNECTIONS_ALL);
|
||||||
|
@ -62,7 +63,8 @@ static UniValue ping(const JSONRPCRequest& request)
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
if(!g_rpc_node->connman)
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if(!node.connman)
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
|
|
||||||
// Request that each node send a ping during next message processing pass
|
// Request that each node send a ping during next message processing pass
|
||||||
|
@ -139,7 +141,8 @@ static UniValue getpeerinfo(const JSONRPCRequest& request)
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
if(!g_rpc_node->connman)
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if(!node.connman)
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
|
|
||||||
std::vector<CNodeStats> vstats;
|
std::vector<CNodeStats> vstats;
|
||||||
|
@ -248,7 +251,8 @@ static UniValue addnode(const JSONRPCRequest& request)
|
||||||
},
|
},
|
||||||
}.ToString());
|
}.ToString());
|
||||||
|
|
||||||
if(!g_rpc_node->connman)
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if(!node.connman)
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
|
|
||||||
std::string strNode = request.params[0].get_str();
|
std::string strNode = request.params[0].get_str();
|
||||||
|
@ -293,7 +297,8 @@ static UniValue disconnectnode(const JSONRPCRequest& request)
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
if(!g_rpc_node->connman)
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if(!node.connman)
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
|
|
||||||
bool success;
|
bool success;
|
||||||
|
@ -350,7 +355,8 @@ static UniValue getaddednodeinfo(const JSONRPCRequest& request)
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
if(!g_rpc_node->connman)
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if(!node.connman)
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
|
|
||||||
std::vector<AddedNodeInfo> vInfo = g_rpc_node->connman->GetAddedNodeInfo();
|
std::vector<AddedNodeInfo> vInfo = g_rpc_node->connman->GetAddedNodeInfo();
|
||||||
|
@ -417,7 +423,8 @@ static UniValue getnettotals(const JSONRPCRequest& request)
|
||||||
+ HelpExampleRpc("getnettotals", "")
|
+ HelpExampleRpc("getnettotals", "")
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
if(!g_rpc_node->connman)
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if(!node.connman)
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
|
|
||||||
UniValue obj(UniValue::VOBJ);
|
UniValue obj(UniValue::VOBJ);
|
||||||
|
@ -513,7 +520,8 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request)
|
||||||
obj.pushKV("version", CLIENT_VERSION);
|
obj.pushKV("version", CLIENT_VERSION);
|
||||||
obj.pushKV("subversion", strSubVersion);
|
obj.pushKV("subversion", strSubVersion);
|
||||||
obj.pushKV("protocolversion",PROTOCOL_VERSION);
|
obj.pushKV("protocolversion",PROTOCOL_VERSION);
|
||||||
if (g_rpc_node->connman) {
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if (node.connman) {
|
||||||
ServiceFlags services = g_rpc_node->connman->GetLocalServices();
|
ServiceFlags services = g_rpc_node->connman->GetLocalServices();
|
||||||
obj.pushKV("localservices", strprintf("%016x", services));
|
obj.pushKV("localservices", strprintf("%016x", services));
|
||||||
obj.pushKV("localservicesnames", GetServicesNames(services));
|
obj.pushKV("localservicesnames", GetServicesNames(services));
|
||||||
|
@ -567,7 +575,8 @@ static UniValue setban(const JSONRPCRequest& request)
|
||||||
if (request.fHelp || !help.IsValidNumArgs(request.params.size()) || (strCommand != "add" && strCommand != "remove")) {
|
if (request.fHelp || !help.IsValidNumArgs(request.params.size()) || (strCommand != "add" && strCommand != "remove")) {
|
||||||
throw std::runtime_error(help.ToString());
|
throw std::runtime_error(help.ToString());
|
||||||
}
|
}
|
||||||
if (!g_rpc_node->banman) {
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if (!node.banman) {
|
||||||
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -645,7 +654,8 @@ static UniValue listbanned(const JSONRPCRequest& request)
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
if(!g_rpc_node->banman) {
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if(!node.banman) {
|
||||||
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -679,7 +689,8 @@ static UniValue clearbanned(const JSONRPCRequest& request)
|
||||||
+ HelpExampleRpc("clearbanned", "")
|
+ HelpExampleRpc("clearbanned", "")
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
if (!g_rpc_node->banman) {
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if (!node.banman) {
|
||||||
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -699,7 +710,8 @@ static UniValue setnetworkactive(const JSONRPCRequest& request)
|
||||||
RPCExamples{""},
|
RPCExamples{""},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
|
|
||||||
if (!g_rpc_node->connman) {
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if (!node.connman) {
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -732,7 +744,8 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request)
|
||||||
+ HelpExampleRpc("getnodeaddresses", "8")
|
+ HelpExampleRpc("getnodeaddresses", "8")
|
||||||
},
|
},
|
||||||
}.Check(request);
|
}.Check(request);
|
||||||
if (!g_rpc_node->connman) {
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
if (!node.connman) {
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -653,7 +653,7 @@ static UniValue combinerawtransaction(const JSONRPCRequest& request)
|
||||||
CCoinsView viewDummy;
|
CCoinsView viewDummy;
|
||||||
CCoinsViewCache view(&viewDummy);
|
CCoinsViewCache view(&viewDummy);
|
||||||
{
|
{
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
LOCK(mempool.cs);
|
LOCK(mempool.cs);
|
||||||
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
|
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
|
||||||
|
@ -778,7 +778,8 @@ static UniValue signrawtransactionwithkey(const JSONRPCRequest& request)
|
||||||
for (const CTxIn& txin : mtx.vin) {
|
for (const CTxIn& txin : mtx.vin) {
|
||||||
coins[txin.prevout]; // Create empty map entry keyed by prevout.
|
coins[txin.prevout]; // Create empty map entry keyed by prevout.
|
||||||
}
|
}
|
||||||
FindCoins(*g_rpc_node, coins);
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
FindCoins(node, coins);
|
||||||
|
|
||||||
// Parse the prevtxs array
|
// Parse the prevtxs array
|
||||||
ParsePrevouts(request.params[2], &keystore, coins);
|
ParsePrevouts(request.params[2], &keystore, coins);
|
||||||
|
@ -837,7 +838,8 @@ static UniValue sendrawtransaction(const JSONRPCRequest& request)
|
||||||
|
|
||||||
std::string err_string;
|
std::string err_string;
|
||||||
AssertLockNotHeld(cs_main);
|
AssertLockNotHeld(cs_main);
|
||||||
const TransactionError err = BroadcastTransaction(*g_rpc_node, tx, err_string, max_raw_tx_fee, /*relay*/ true, /*wait_callback*/ true);
|
NodeContext& node = EnsureNodeContext(request.context);
|
||||||
|
const TransactionError err = BroadcastTransaction(node, tx, err_string, max_raw_tx_fee, /*relay*/ true, /*wait_callback*/ true);
|
||||||
if (TransactionError::OK != err) {
|
if (TransactionError::OK != err) {
|
||||||
throw JSONRPCTransactionError(err, err_string);
|
throw JSONRPCTransactionError(err, err_string);
|
||||||
}
|
}
|
||||||
|
@ -904,7 +906,7 @@ static UniValue testmempoolaccept(const JSONRPCRequest& request)
|
||||||
DEFAULT_MAX_RAW_TX_FEE_RATE :
|
DEFAULT_MAX_RAW_TX_FEE_RATE :
|
||||||
CFeeRate(AmountFromValue(request.params[1]));
|
CFeeRate(AmountFromValue(request.params[1]));
|
||||||
|
|
||||||
CTxMemPool& mempool = EnsureMemPool();
|
CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
int64_t virtual_size = GetVirtualTransactionSize(*tx);
|
int64_t virtual_size = GetVirtualTransactionSize(*tx);
|
||||||
CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
|
CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
|
||||||
|
|
||||||
|
@ -1555,7 +1557,7 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
|
||||||
CCoinsView viewDummy;
|
CCoinsView viewDummy;
|
||||||
CCoinsViewCache view(&viewDummy);
|
CCoinsViewCache view(&viewDummy);
|
||||||
{
|
{
|
||||||
const CTxMemPool& mempool = EnsureMemPool();
|
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||||
LOCK2(cs_main, mempool.cs);
|
LOCK2(cs_main, mempool.cs);
|
||||||
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
|
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
|
||||||
CCoinsViewMemPool viewMempool(&viewChain, mempool);
|
CCoinsViewMemPool viewMempool(&viewChain, mempool);
|
||||||
|
|
|
@ -10,6 +10,10 @@
|
||||||
|
|
||||||
#include <univalue.h>
|
#include <univalue.h>
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
class Ref;
|
||||||
|
} // namespace util
|
||||||
|
|
||||||
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
|
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
|
||||||
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id);
|
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id);
|
||||||
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id);
|
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id);
|
||||||
|
@ -34,8 +38,9 @@ public:
|
||||||
std::string URI;
|
std::string URI;
|
||||||
std::string authUser;
|
std::string authUser;
|
||||||
std::string peerAddr;
|
std::string peerAddr;
|
||||||
|
const util::Ref& context;
|
||||||
|
|
||||||
JSONRPCRequest() : id(NullUniValue), params(NullUniValue), fHelp(false) {}
|
JSONRPCRequest(const util::Ref& context) : id(NullUniValue), params(NullUniValue), fHelp(false), context(context) {}
|
||||||
void parse(const UniValue& valRequest);
|
void parse(const UniValue& valRequest);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <interfaces/chain.h>
|
#include <interfaces/chain.h>
|
||||||
#include <node/context.h>
|
#include <node/context.h>
|
||||||
#include <test/util/setup_common.h>
|
#include <test/util/setup_common.h>
|
||||||
|
#include <util/ref.h>
|
||||||
#include <util/time.h>
|
#include <util/time.h>
|
||||||
|
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
|
@ -19,13 +20,20 @@
|
||||||
|
|
||||||
#include <rpc/blockchain.h>
|
#include <rpc/blockchain.h>
|
||||||
|
|
||||||
UniValue CallRPC(std::string args)
|
class RPCTestingSetup : public TestingSetup
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
UniValue CallRPC(std::string args);
|
||||||
|
};
|
||||||
|
|
||||||
|
UniValue RPCTestingSetup::CallRPC(std::string args)
|
||||||
{
|
{
|
||||||
std::vector<std::string> vArgs;
|
std::vector<std::string> vArgs;
|
||||||
boost::split(vArgs, args, boost::is_any_of(" \t"));
|
boost::split(vArgs, args, boost::is_any_of(" \t"));
|
||||||
std::string strMethod = vArgs[0];
|
std::string strMethod = vArgs[0];
|
||||||
vArgs.erase(vArgs.begin());
|
vArgs.erase(vArgs.begin());
|
||||||
JSONRPCRequest request;
|
util::Ref context{m_node};
|
||||||
|
JSONRPCRequest request(context);
|
||||||
request.strMethod = strMethod;
|
request.strMethod = strMethod;
|
||||||
request.params = RPCConvertValues(strMethod, vArgs);
|
request.params = RPCConvertValues(strMethod, vArgs);
|
||||||
request.fHelp = false;
|
request.fHelp = false;
|
||||||
|
@ -40,7 +48,7 @@ UniValue CallRPC(std::string args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_SUITE(rpc_tests, TestingSetup)
|
BOOST_FIXTURE_TEST_SUITE(rpc_tests, RPCTestingSetup)
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(rpc_rawparams)
|
BOOST_AUTO_TEST_CASE(rpc_rawparams)
|
||||||
{
|
{
|
||||||
|
|
|
@ -131,7 +131,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
|
||||||
// We have to run a scheduler thread to prevent ActivateBestChain
|
// We have to run a scheduler thread to prevent ActivateBestChain
|
||||||
// from blocking due to queue overrun.
|
// from blocking due to queue overrun.
|
||||||
threadGroup.create_thread([&]{ m_node.scheduler->serviceQueue(); });
|
threadGroup.create_thread([&]{ m_node.scheduler->serviceQueue(); });
|
||||||
GetMainSignals().RegisterBackgroundSignalScheduler(*g_rpc_node->scheduler);
|
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
|
||||||
|
|
||||||
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
|
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include <rpc/server.h>
|
#include <rpc/server.h>
|
||||||
#include <test/util/logging.h>
|
#include <test/util/logging.h>
|
||||||
#include <test/util/setup_common.h>
|
#include <test/util/setup_common.h>
|
||||||
|
#include <util/ref.h>
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
#include <wallet/coincontrol.h>
|
#include <wallet/coincontrol.h>
|
||||||
|
@ -208,7 +209,8 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
|
||||||
key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1);
|
key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1);
|
||||||
key.pushKV("internal", UniValue(true));
|
key.pushKV("internal", UniValue(true));
|
||||||
keys.push_back(key);
|
keys.push_back(key);
|
||||||
JSONRPCRequest request;
|
util::Ref context;
|
||||||
|
JSONRPCRequest request(context);
|
||||||
request.params.setArray();
|
request.params.setArray();
|
||||||
request.params.push_back(keys);
|
request.params.push_back(keys);
|
||||||
|
|
||||||
|
@ -262,7 +264,8 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
|
||||||
AddWallet(wallet);
|
AddWallet(wallet);
|
||||||
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
||||||
}
|
}
|
||||||
JSONRPCRequest request;
|
util::Ref context;
|
||||||
|
JSONRPCRequest request(context);
|
||||||
request.params.setArray();
|
request.params.setArray();
|
||||||
request.params.push_back(backup_file);
|
request.params.push_back(backup_file);
|
||||||
|
|
||||||
|
@ -277,7 +280,8 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
|
||||||
LOCK(wallet->cs_wallet);
|
LOCK(wallet->cs_wallet);
|
||||||
wallet->SetupLegacyScriptPubKeyMan();
|
wallet->SetupLegacyScriptPubKeyMan();
|
||||||
|
|
||||||
JSONRPCRequest request;
|
util::Ref context;
|
||||||
|
JSONRPCRequest request(context);
|
||||||
request.params.setArray();
|
request.params.setArray();
|
||||||
request.params.push_back(backup_file);
|
request.params.push_back(backup_file);
|
||||||
AddWallet(wallet);
|
AddWallet(wallet);
|
||||||
|
|
Loading…
Add table
Reference in a new issue