Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/blockchain.cpp
Show All 23 Lines | |||||
#include <primitives/transaction.h> | #include <primitives/transaction.h> | ||||
#include <rpc/server.h> | #include <rpc/server.h> | ||||
#include <rpc/util.h> | #include <rpc/util.h> | ||||
#include <script/descriptor.h> | #include <script/descriptor.h> | ||||
#include <streams.h> | #include <streams.h> | ||||
#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 <util/validation.h> | #include <util/validation.h> | ||||
#include <validation.h> | #include <validation.h> | ||||
#include <validationinterface.h> | #include <validationinterface.h> | ||||
#include <versionbitsinfo.h> // For VersionBitsDeploymentInfo | #include <versionbitsinfo.h> // For VersionBitsDeploymentInfo | ||||
#include <warnings.h> | #include <warnings.h> | ||||
#include <boost/thread/thread.hpp> // boost::thread::interrupt | #include <boost/thread/thread.hpp> // boost::thread::interrupt | ||||
#include <condition_variable> | #include <condition_variable> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <memory> | #include <memory> | ||||
#include <mutex> | #include <mutex> | ||||
struct CUpdatedBlock { | struct CUpdatedBlock { | ||||
uint256 hash; | uint256 hash; | ||||
int height; | int height; | ||||
}; | }; | ||||
static Mutex cs_blockchange; | static Mutex cs_blockchange; | ||||
static std::condition_variable cond_blockchange; | static std::condition_variable cond_blockchange; | ||||
static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange); | static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange); | ||||
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, | throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, | ||||
"Mempool disabled or instance not found"); | "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. | ||||
*/ | */ | ||||
double GetDifficulty(const CBlockIndex *blockindex) { | double GetDifficulty(const CBlockIndex *blockindex) { | ||||
CHECK_NONFATAL(blockindex); | CHECK_NONFATAL(blockindex); | ||||
▲ Show 20 Lines • Show All 519 Lines • ▼ Show 20 Lines | static UniValue getrawmempool(const Config &config, | ||||
} | } | ||||
.Check(request); | .Check(request); | ||||
bool fVerbose = false; | bool fVerbose = false; | ||||
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 Config &config, | static UniValue getmempoolancestors(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
RPCHelpMan{ | RPCHelpMan{ | ||||
"getmempoolancestors", | "getmempoolancestors", | ||||
"If txid is in the mempool, returns all in-mempool ancestors.\n", | "If txid is in the mempool, returns all in-mempool ancestors.\n", | ||||
{ | { | ||||
Show All 23 Lines | static UniValue getmempoolancestors(const Config &config, | ||||
bool fVerbose = false; | bool fVerbose = false; | ||||
if (!request.params[1].isNull()) { | if (!request.params[1].isNull()) { | ||||
fVerbose = request.params[1].get_bool(); | fVerbose = request.params[1].get_bool(); | ||||
} | } | ||||
TxId txid(ParseHashV(request.params[0], "parameter 1")); | TxId txid(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(txid); | CTxMemPool::txiter it = mempool.mapTx.find(txid); | ||||
if (it == mempool.mapTx.end()) { | if (it == mempool.mapTx.end()) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Transaction not in mempool"); | "Transaction not in mempool"); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | static UniValue getmempooldescendants(const Config &config, | ||||
bool fVerbose = false; | bool fVerbose = false; | ||||
if (!request.params[1].isNull()) { | if (!request.params[1].isNull()) { | ||||
fVerbose = request.params[1].get_bool(); | fVerbose = request.params[1].get_bool(); | ||||
} | } | ||||
TxId txid(ParseHashV(request.params[0], "parameter 1")); | TxId txid(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(txid); | CTxMemPool::txiter it = mempool.mapTx.find(txid); | ||||
if (it == mempool.mapTx.end()) { | if (it == mempool.mapTx.end()) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Transaction not in mempool"); | "Transaction not in mempool"); | ||||
} | } | ||||
Show All 35 Lines | RPCHelpMan{ | ||||
EntryDescriptionString() + "}\n"}, | EntryDescriptionString() + "}\n"}, | ||||
RPCExamples{HelpExampleCli("getmempoolentry", "\"mytxid\"") + | RPCExamples{HelpExampleCli("getmempoolentry", "\"mytxid\"") + | ||||
HelpExampleRpc("getmempoolentry", "\"mytxid\"")}, | HelpExampleRpc("getmempoolentry", "\"mytxid\"")}, | ||||
} | } | ||||
.Check(request); | .Check(request); | ||||
TxId txid(ParseHashV(request.params[0], "parameter 1")); | TxId txid(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(txid); | CTxMemPool::txiter it = mempool.mapTx.find(txid); | ||||
if (it == mempool.mapTx.end()) { | if (it == mempool.mapTx.end()) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"Transaction not in mempool"); | "Transaction not in mempool"); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 443 Lines • ▼ Show 20 Lines | UniValue gettxout(const Config &config, const JSONRPCRequest &request) { | ||||
if (!request.params[2].isNull()) { | if (!request.params[2].isNull()) { | ||||
fMempool = request.params[2].get_bool(); | fMempool = request.params[2].get_bool(); | ||||
} | } | ||||
Coin coin; | Coin coin; | ||||
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)) { | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
} else { | } else { | ||||
if (!coins_view->GetCoin(out, coin)) { | if (!coins_view->GetCoin(out, coin)) { | ||||
return NullUniValue; | return NullUniValue; | ||||
▲ Show 20 Lines • Show All 421 Lines • ▼ Show 20 Lines | RPCHelpMan{ | ||||
" \"minrelaytxfee\": xxxxx (numeric) Current minimum relay " | " \"minrelaytxfee\": xxxxx (numeric) Current minimum relay " | ||||
"fee for transactions\n" | "fee for transactions\n" | ||||
"}\n"}, | "}\n"}, | ||||
RPCExamples{HelpExampleCli("getmempoolinfo", "") + | RPCExamples{HelpExampleCli("getmempoolinfo", "") + | ||||
HelpExampleRpc("getmempoolinfo", "")}, | HelpExampleRpc("getmempoolinfo", "")}, | ||||
} | } | ||||
.Check(request); | .Check(request); | ||||
return MempoolInfoToJSON(EnsureMemPool()); | return MempoolInfoToJSON(EnsureMemPool(request.context)); | ||||
} | } | ||||
static UniValue preciousblock(const Config &config, | static UniValue preciousblock(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
RPCHelpMan{ | RPCHelpMan{ | ||||
"preciousblock", | "preciousblock", | ||||
"Treats a block as if it were received before others with the same " | "Treats a block as if it were received before others with the same " | ||||
"work.\n" | "work.\n" | ||||
▲ Show 20 Lines • Show All 649 Lines • ▼ Show 20 Lines | RPCHelpMan{ | ||||
"fully loaded.\n", | "fully loaded.\n", | ||||
{}, | {}, | ||||
RPCResults{}, | RPCResults{}, | ||||
RPCExamples{HelpExampleCli("savemempool", "") + | RPCExamples{HelpExampleCli("savemempool", "") + | ||||
HelpExampleRpc("savemempool", "")}, | HelpExampleRpc("savemempool", "")}, | ||||
} | } | ||||
.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"); | ||||
} | } | ||||
if (!DumpMempool(mempool)) { | if (!DumpMempool(mempool)) { | ||||
throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk"); | throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk"); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 497 Lines • Show Last 20 Lines |