mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -05:00
rest: Use mempool from node context instead of global
This commit is contained in:
parent
fa660d65d7
commit
fa8e650b52
1 changed files with 32 additions and 7 deletions
39
src/rest.cpp
39
src/rest.cpp
|
@ -8,6 +8,7 @@
|
||||||
#include <core_io.h>
|
#include <core_io.h>
|
||||||
#include <httpserver.h>
|
#include <httpserver.h>
|
||||||
#include <index/txindex.h>
|
#include <index/txindex.h>
|
||||||
|
#include <node/context.h>
|
||||||
#include <primitives/block.h>
|
#include <primitives/block.h>
|
||||||
#include <primitives/transaction.h>
|
#include <primitives/transaction.h>
|
||||||
#include <rpc/blockchain.h>
|
#include <rpc/blockchain.h>
|
||||||
|
@ -16,6 +17,7 @@
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
|
#include <util/check.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
#include <version.h>
|
#include <version.h>
|
||||||
|
@ -69,6 +71,24 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the node context mempool.
|
||||||
|
*
|
||||||
|
* Set the HTTP error and return nullptr if node context
|
||||||
|
* mempool is not found.
|
||||||
|
*
|
||||||
|
* @param[in] req the HTTP request
|
||||||
|
* return pointer to the mempool or nullptr if no mempool found
|
||||||
|
*/
|
||||||
|
static CTxMemPool* GetMemPool(HTTPRequest* req)
|
||||||
|
{
|
||||||
|
if (!g_rpc_node || !g_rpc_node->mempool) {
|
||||||
|
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return g_rpc_node->mempool;
|
||||||
|
}
|
||||||
|
|
||||||
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
|
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
|
||||||
{
|
{
|
||||||
const std::string::size_type pos = strReq.rfind('.');
|
const std::string::size_type pos = strReq.rfind('.');
|
||||||
|
@ -295,12 +315,14 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
if (!CheckWarmup(req))
|
if (!CheckWarmup(req))
|
||||||
return false;
|
return false;
|
||||||
|
const CTxMemPool* mempool = GetMemPool(req);
|
||||||
|
if (!mempool) return false;
|
||||||
std::string param;
|
std::string param;
|
||||||
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
||||||
|
|
||||||
switch (rf) {
|
switch (rf) {
|
||||||
case RetFormat::JSON: {
|
case RetFormat::JSON: {
|
||||||
UniValue mempoolInfoObject = MempoolInfoToJSON(::mempool);
|
UniValue mempoolInfoObject = MempoolInfoToJSON(*mempool);
|
||||||
|
|
||||||
std::string strJSON = mempoolInfoObject.write() + "\n";
|
std::string strJSON = mempoolInfoObject.write() + "\n";
|
||||||
req->WriteHeader("Content-Type", "application/json");
|
req->WriteHeader("Content-Type", "application/json");
|
||||||
|
@ -315,14 +337,15 @@ 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(HTTPRequest* req, const std::string& strURIPart)
|
||||||
{
|
{
|
||||||
if (!CheckWarmup(req))
|
if (!CheckWarmup(req)) return false;
|
||||||
return false;
|
const CTxMemPool* mempool = GetMemPool(req);
|
||||||
|
if (!mempool) return false;
|
||||||
std::string param;
|
std::string param;
|
||||||
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
||||||
|
|
||||||
switch (rf) {
|
switch (rf) {
|
||||||
case RetFormat::JSON: {
|
case RetFormat::JSON: {
|
||||||
UniValue mempoolObject = MempoolToJSON(::mempool, true);
|
UniValue mempoolObject = MempoolToJSON(*mempool, true);
|
||||||
|
|
||||||
std::string strJSON = mempoolObject.write() + "\n";
|
std::string strJSON = mempoolObject.write() + "\n";
|
||||||
req->WriteHeader("Content-Type", "application/json");
|
req->WriteHeader("Content-Type", "application/json");
|
||||||
|
@ -500,11 +523,13 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
|
||||||
};
|
};
|
||||||
|
|
||||||
if (fCheckMemPool) {
|
if (fCheckMemPool) {
|
||||||
|
const CTxMemPool* mempool = GetMemPool(req);
|
||||||
|
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);
|
||||||
CCoinsViewCache& viewChain = ::ChainstateActive().CoinsTip();
|
CCoinsViewCache& viewChain = ::ChainstateActive().CoinsTip();
|
||||||
CCoinsViewMemPool viewMempool(&viewChain, mempool);
|
CCoinsViewMemPool viewMempool(&viewChain, *mempool);
|
||||||
process_utxos(viewMempool, mempool);
|
process_utxos(viewMempool, *mempool);
|
||||||
} else {
|
} else {
|
||||||
LOCK(cs_main); // no need to lock mempool!
|
LOCK(cs_main); // no need to lock mempool!
|
||||||
process_utxos(::ChainstateActive().CoinsTip(), CTxMemPool());
|
process_utxos(::ChainstateActive().CoinsTip(), CTxMemPool());
|
||||||
|
|
Loading…
Add table
Reference in a new issue