Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/blockchain.cpp
Show All 13 Lines | |||||
#include <consensus/params.h> | #include <consensus/params.h> | ||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
#include <core_io.h> | #include <core_io.h> | ||||
#include <deploymentinfo.h> | #include <deploymentinfo.h> | ||||
#include <deploymentstatus.h> | #include <deploymentstatus.h> | ||||
#include <hash.h> | #include <hash.h> | ||||
#include <index/blockfilterindex.h> | #include <index/blockfilterindex.h> | ||||
#include <index/coinstatsindex.h> | #include <index/coinstatsindex.h> | ||||
#include <net.h> | |||||
#include <net_processing.h> | |||||
#include <node/blockstorage.h> | #include <node/blockstorage.h> | ||||
#include <node/coinstats.h> | #include <node/coinstats.h> | ||||
#include <node/context.h> | #include <node/context.h> | ||||
#include <node/utxo_snapshot.h> | #include <node/utxo_snapshot.h> | ||||
#include <policy/policy.h> | #include <policy/policy.h> | ||||
#include <primitives/transaction.h> | #include <primitives/transaction.h> | ||||
#include <rpc/server.h> | #include <rpc/server.h> | ||||
#include <rpc/server_util.h> | #include <rpc/server_util.h> | ||||
▲ Show 20 Lines • Show All 840 Lines • ▼ Show 20 Lines | return RPCHelpMan{ | ||||
const CTxMemPoolEntry &e = *it; | const CTxMemPoolEntry &e = *it; | ||||
UniValue info(UniValue::VOBJ); | UniValue info(UniValue::VOBJ); | ||||
entryToJSON(mempool, info, e); | entryToJSON(mempool, info, e); | ||||
return info; | return info; | ||||
}, | }, | ||||
}; | }; | ||||
} | } | ||||
static RPCHelpMan getblockfrompeer() { | |||||
return RPCHelpMan{ | |||||
"getblockfrompeer", | |||||
"\nAttempt to fetch block from a given peer.\n" | |||||
"\nWe must have the header for this block, e.g. using submitheader.\n" | |||||
"Subsequent calls for the same block and a new peer will cause the " | |||||
"response from the previous peer to be ignored.\n" | |||||
"\nReturns an empty JSON object if the request was successfully " | |||||
"scheduled.", | |||||
{ | |||||
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, | |||||
"The block hash"}, | |||||
{"nodeid", RPCArg::Type::NUM, RPCArg::Optional::NO, | |||||
"The node ID (see getpeerinfo for node IDs)"}, | |||||
}, | |||||
RPCResult{RPCResult::Type::OBJ, | |||||
"", | |||||
"", | |||||
{{RPCResult::Type::STR, "warnings", /*optional=*/true, | |||||
"any warnings"}}}, | |||||
RPCExamples{HelpExampleCli("getblockfrompeer", | |||||
"\"00000000c937983704a73af28acdec37b049d214a" | |||||
"dbda81d7e2a3dd146f6ed09\" 0") + | |||||
HelpExampleRpc("getblockfrompeer", | |||||
"\"00000000c937983704a73af28acdec37b049d214a" | |||||
"dbda81d7e2a3dd146f6ed09\" 0")}, | |||||
[&](const RPCHelpMan &self, const Config &config, | |||||
const JSONRPCRequest &request) -> UniValue { | |||||
const NodeContext &node = EnsureAnyNodeContext(request.context); | |||||
ChainstateManager &chainman = EnsureChainman(node); | |||||
PeerManager &peerman = EnsurePeerman(node); | |||||
CConnman &connman = EnsureConnman(node); | |||||
const BlockHash hash{ParseHashV(request.params[0], "hash")}; | |||||
const NodeId nodeid{request.params[1].get_int64()}; | |||||
// Check that the peer with nodeid exists | |||||
if (!connman.ForNode(nodeid, [](CNode *node) { return true; })) { | |||||
throw JSONRPCError( | |||||
RPC_MISC_ERROR, | |||||
strprintf("Peer nodeid %d does not exist", nodeid)); | |||||
} | |||||
const CBlockIndex *const index = WITH_LOCK( | |||||
cs_main, return chainman.m_blockman.LookupBlockIndex(hash);); | |||||
if (!index) { | |||||
throw JSONRPCError(RPC_MISC_ERROR, "Block header missing"); | |||||
} | |||||
UniValue result = UniValue::VOBJ; | |||||
if (index->nStatus.hasData()) { | |||||
result.pushKV("warnings", "Block already downloaded"); | |||||
} else if (!peerman.FetchBlock(config, nodeid, *index)) { | |||||
throw JSONRPCError(RPC_MISC_ERROR, | |||||
"Failed to fetch block from peer"); | |||||
} | |||||
return result; | |||||
}, | |||||
}; | |||||
} | |||||
static RPCHelpMan getblockhash() { | static RPCHelpMan getblockhash() { | ||||
return RPCHelpMan{ | return RPCHelpMan{ | ||||
"getblockhash", | "getblockhash", | ||||
"Returns hash of block in best-block-chain at height provided.\n", | "Returns hash of block in best-block-chain at height provided.\n", | ||||
{ | { | ||||
{"height", RPCArg::Type::NUM, RPCArg::Optional::NO, | {"height", RPCArg::Type::NUM, RPCArg::Optional::NO, | ||||
"The height index"}, | "The height index"}, | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 2,418 Lines • ▼ Show 20 Lines | |||||
void RegisterBlockchainRPCCommands(CRPCTable &t) { | void RegisterBlockchainRPCCommands(CRPCTable &t) { | ||||
// clang-format off | // clang-format off | ||||
static const CRPCCommand commands[] = { | static const CRPCCommand commands[] = { | ||||
// category actor (function) | // category actor (function) | ||||
// ------------------ ---------------------- | // ------------------ ---------------------- | ||||
{ "blockchain", getbestblockhash, }, | { "blockchain", getbestblockhash, }, | ||||
{ "blockchain", getblock, }, | { "blockchain", getblock, }, | ||||
{ "blockchain", getblockfrompeer, }, | |||||
{ "blockchain", getblockchaininfo, }, | { "blockchain", getblockchaininfo, }, | ||||
{ "blockchain", getblockcount, }, | { "blockchain", getblockcount, }, | ||||
{ "blockchain", getblockhash, }, | { "blockchain", getblockhash, }, | ||||
{ "blockchain", getblockheader, }, | { "blockchain", getblockheader, }, | ||||
{ "blockchain", getblockstats, }, | { "blockchain", getblockstats, }, | ||||
{ "blockchain", getchaintips, }, | { "blockchain", getchaintips, }, | ||||
{ "blockchain", getchaintxstats, }, | { "blockchain", getchaintxstats, }, | ||||
{ "blockchain", getdifficulty, }, | { "blockchain", getdifficulty, }, | ||||
Show All 32 Lines |