mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-26 15:36:19 +01:00
refactor: Pass NodeContext to RPC and REST methods through util::Ref
This commit does not change behavior
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
#include <txdb.h>
|
||||
#include <txmempool.h>
|
||||
#include <undo.h>
|
||||
#include <util/ref.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/system.h>
|
||||
#include <validation.h>
|
||||
@@ -53,13 +54,21 @@ static Mutex cs_blockchange;
|
||||
static std::condition_variable cond_blockchange;
|
||||
static CUpdatedBlock latestblock;
|
||||
|
||||
CTxMemPool& EnsureMemPool()
|
||||
NodeContext& EnsureNodeContext(const util::Ref& context)
|
||||
{
|
||||
CHECK_NONFATAL(g_rpc_node);
|
||||
if (!g_rpc_node->mempool) {
|
||||
if (!context.Has<NodeContext>()) {
|
||||
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");
|
||||
}
|
||||
return *g_rpc_node->mempool;
|
||||
return *node.mempool;
|
||||
}
|
||||
|
||||
/* Calculate the difficulty for a given block index.
|
||||
@@ -519,7 +528,7 @@ static UniValue getrawmempool(const JSONRPCRequest& request)
|
||||
if (!request.params[0].isNull())
|
||||
fVerbose = request.params[0].get_bool();
|
||||
|
||||
return MempoolToJSON(EnsureMemPool(), fVerbose);
|
||||
return MempoolToJSON(EnsureMemPool(request.context), fVerbose);
|
||||
}
|
||||
|
||||
static UniValue getmempoolancestors(const JSONRPCRequest& request)
|
||||
@@ -549,7 +558,7 @@ static UniValue getmempoolancestors(const JSONRPCRequest& request)
|
||||
|
||||
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
||||
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||
LOCK(mempool.cs);
|
||||
|
||||
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");
|
||||
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||
LOCK(mempool.cs);
|
||||
|
||||
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");
|
||||
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||
LOCK(mempool.cs);
|
||||
|
||||
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
||||
@@ -1045,7 +1054,7 @@ UniValue gettxout(const JSONRPCRequest& request)
|
||||
CCoinsViewCache* coins_view = &::ChainstateActive().CoinsTip();
|
||||
|
||||
if (fMempool) {
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||
LOCK(mempool.cs);
|
||||
CCoinsViewMemPool view(coins_view, mempool);
|
||||
if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
|
||||
@@ -1415,7 +1424,7 @@ static UniValue getmempoolinfo(const JSONRPCRequest& request)
|
||||
},
|
||||
}.Check(request);
|
||||
|
||||
return MempoolInfoToJSON(EnsureMemPool());
|
||||
return MempoolInfoToJSON(EnsureMemPool(request.context));
|
||||
}
|
||||
|
||||
static UniValue preciousblock(const JSONRPCRequest& request)
|
||||
@@ -1934,7 +1943,7 @@ static UniValue savemempool(const JSONRPCRequest& request)
|
||||
},
|
||||
}.Check(request);
|
||||
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
const CTxMemPool& mempool = EnsureMemPool(request.context);
|
||||
|
||||
if (!mempool.IsLoaded()) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
|
||||
|
||||
Reference in New Issue
Block a user