mirror of
https://github.com/dashpay/dash.git
synced 2024-12-24 19:42:46 +01:00
merge bitcoin#20295: getblockfrompeer
This commit is contained in:
parent
63ac87f011
commit
c294457b52
@ -287,12 +287,12 @@ BITCOIN_CORE_H = \
|
||||
rpc/blockchain.h \
|
||||
rpc/client.h \
|
||||
rpc/mining.h \
|
||||
rpc/net.h \
|
||||
rpc/protocol.h \
|
||||
rpc/rawtransaction_util.h \
|
||||
rpc/register.h \
|
||||
rpc/request.h \
|
||||
rpc/server.h \
|
||||
rpc/server_util.h \
|
||||
rpc/util.h \
|
||||
saltedhasher.h \
|
||||
scheduler.h \
|
||||
@ -510,6 +510,7 @@ libbitcoin_server_a_SOURCES = \
|
||||
rpc/quorums.cpp \
|
||||
rpc/rawtransaction.cpp \
|
||||
rpc/server.cpp \
|
||||
rpc/server_util.cpp \
|
||||
script/sigcache.cpp \
|
||||
shutdown.cpp \
|
||||
spork.cpp \
|
||||
|
@ -398,6 +398,7 @@ public:
|
||||
|
||||
/** Implement PeerManager */
|
||||
void CheckForStaleTipAndEvictPeers() override;
|
||||
bool FetchBlock(NodeId id, const uint256& hash, const CBlockIndex& index) override;
|
||||
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
|
||||
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
|
||||
void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);;
|
||||
@ -1862,6 +1863,39 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
|
||||
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
|
||||
}
|
||||
|
||||
bool PeerManagerImpl::FetchBlock(NodeId id, const uint256& hash, const CBlockIndex& index)
|
||||
{
|
||||
if (fImporting || fReindex) return false;
|
||||
|
||||
LOCK(cs_main);
|
||||
// Ensure this peer exists and hasn't been disconnected
|
||||
CNodeState* state = State(id);
|
||||
if (state == nullptr) return false;
|
||||
|
||||
// Mark block as in-flight unless it already is
|
||||
if (!MarkBlockAsInFlight(id, index.GetBlockHash(), &index)) return false;
|
||||
|
||||
// Construct message to request the block
|
||||
std::vector<CInv> invs{CInv(MSG_BLOCK, hash)};
|
||||
|
||||
// Send block request message to the peer
|
||||
bool success = m_connman.ForNode(id, [this, &invs](CNode* node) {
|
||||
const CNetMsgMaker msgMaker(node->GetCommonVersion());
|
||||
this->m_connman.PushMessage(node, msgMaker.Make(NetMsgType::GETDATA, invs));
|
||||
return true;
|
||||
});
|
||||
|
||||
if (success) {
|
||||
LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
|
||||
hash.ToString(), id);
|
||||
} else {
|
||||
MarkBlockAsReceived(hash);
|
||||
LogPrint(BCLog::NET, "Failed to request block %s from peer=%d\n",
|
||||
hash.ToString(), id);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman, BanMan* banman,
|
||||
CScheduler &scheduler, ChainstateManager& chainman, CTxMemPool& pool,
|
||||
CMasternodeMetaMan& mn_metaman, CMasternodeSync& mn_sync,
|
||||
|
@ -66,6 +66,16 @@ public:
|
||||
const std::unique_ptr<LLMQContext>& llmq_ctx, bool ignore_incoming_txs);
|
||||
virtual ~PeerManager() { }
|
||||
|
||||
/**
|
||||
* Attempt to manually fetch block from a given peer. We must already have the header.
|
||||
*
|
||||
* @param[in] id The peer id
|
||||
* @param[in] hash The block hash
|
||||
* @param[in] pindex The blockindex
|
||||
* @returns Whether a request was successfully made
|
||||
*/
|
||||
virtual bool FetchBlock(NodeId id, const uint256& hash, const CBlockIndex& pindex) = 0;
|
||||
|
||||
/** Get statistics from node state */
|
||||
virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/protocol.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <streams.h>
|
||||
#include <sync.h>
|
||||
#include <txmempool.h>
|
||||
|
@ -22,6 +22,8 @@
|
||||
#include <llmq/context.h>
|
||||
#include <node/blockstorage.h>
|
||||
#include <node/coinstats.h>
|
||||
#include <net.h>
|
||||
#include <net_processing.h>
|
||||
#include <node/context.h>
|
||||
#include <node/utxo_snapshot.h>
|
||||
#include <policy/feerate.h>
|
||||
@ -29,6 +31,7 @@
|
||||
#include <policy/policy.h>
|
||||
#include <primitives/transaction.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <script/descriptor.h>
|
||||
#include <streams.h>
|
||||
@ -73,67 +76,6 @@ static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange);
|
||||
|
||||
extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempool, CChainState& active_chainstate, llmq::CChainLocksHandler& clhandler, llmq::CInstantSendManager& isman, UniValue& entry);
|
||||
|
||||
NodeContext& EnsureAnyNodeContext(const CoreContext& context)
|
||||
{
|
||||
auto* const node_context = GetContext<NodeContext>(context);
|
||||
if (!node_context) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
|
||||
}
|
||||
return *node_context;
|
||||
}
|
||||
|
||||
CTxMemPool& EnsureMemPool(const NodeContext& node)
|
||||
{
|
||||
if (!node.mempool) {
|
||||
throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
|
||||
}
|
||||
return *node.mempool;
|
||||
}
|
||||
|
||||
CTxMemPool& EnsureAnyMemPool(const CoreContext& context)
|
||||
{
|
||||
return EnsureMemPool(EnsureAnyNodeContext(context));
|
||||
}
|
||||
|
||||
ChainstateManager& EnsureChainman(const NodeContext& node)
|
||||
{
|
||||
if (!node.chainman) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
|
||||
}
|
||||
return *node.chainman;
|
||||
}
|
||||
|
||||
ChainstateManager& EnsureAnyChainman(const CoreContext& context)
|
||||
{
|
||||
return EnsureChainman(EnsureAnyNodeContext(context));
|
||||
}
|
||||
|
||||
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
|
||||
{
|
||||
if (!node.fee_estimator) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
|
||||
}
|
||||
return *node.fee_estimator;
|
||||
}
|
||||
|
||||
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const CoreContext& context)
|
||||
{
|
||||
return EnsureFeeEstimator(EnsureAnyNodeContext(context));
|
||||
}
|
||||
|
||||
LLMQContext& EnsureLLMQContext(const NodeContext& node)
|
||||
{
|
||||
if (!node.llmq_ctx) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node LLMQ context not found");
|
||||
}
|
||||
return *node.llmq_ctx;
|
||||
}
|
||||
|
||||
LLMQContext& EnsureAnyLLMQContext(const CoreContext& context)
|
||||
{
|
||||
return EnsureLLMQContext(EnsureAnyNodeContext(context));
|
||||
}
|
||||
|
||||
/* Calculate the difficulty for a given block index.
|
||||
*/
|
||||
double GetDifficulty(const CBlockIndex* blockindex)
|
||||
@ -825,6 +767,58 @@ static RPCHelpMan getmempoolentry()
|
||||
};
|
||||
}
|
||||
|
||||
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"
|
||||
"\nReturns {} if a block-request was successfully scheduled\n",
|
||||
{
|
||||
{"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", "any warnings"}
|
||||
}},
|
||||
RPCExamples{
|
||||
HelpExampleCli("getblockfrompeer", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0")
|
||||
+ HelpExampleRpc("getblockfrompeer", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0")
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
const NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
PeerManager& peerman = EnsurePeerman(node);
|
||||
CConnman& connman = EnsureConnman(node);
|
||||
|
||||
uint256 hash(ParseHashV(request.params[0], "hash"));
|
||||
|
||||
const NodeId nodeid = static_cast<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 & BLOCK_HAVE_DATA) {
|
||||
result.pushKV("warnings", "Block already downloaded");
|
||||
} else if (!peerman.FetchBlock(nodeid, hash, *index)) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Failed to fetch block from peer");
|
||||
}
|
||||
return result;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
static RPCHelpMan getblockhashes()
|
||||
{
|
||||
return RPCHelpMan{"getblockhashes",
|
||||
@ -3058,6 +3052,7 @@ static const CRPCCommand commands[] =
|
||||
{ "blockchain", "getbestchainlock", &getbestchainlock, {} },
|
||||
{ "blockchain", "getblockcount", &getblockcount, {} },
|
||||
{ "blockchain", "getblock", &getblock, {"blockhash","verbosity|verbose"} },
|
||||
{ "blockchain", "getblockfrompeer", &getblockfrompeer, {"blockhash", "nodeid"}},
|
||||
{ "blockchain", "getblockhashes", &getblockhashes, {"high","low"} },
|
||||
{ "blockchain", "getblockhash", &getblockhash, {"height"} },
|
||||
{ "blockchain", "getblockheader", &getblockheader, {"blockhash","verbose"} },
|
||||
|
@ -6,7 +6,6 @@
|
||||
#define BITCOIN_RPC_BLOCKCHAIN_H
|
||||
|
||||
#include <amount.h>
|
||||
#include <context.h>
|
||||
#include <core_io.h>
|
||||
#include <streams.h>
|
||||
#include <sync.h>
|
||||
@ -18,12 +17,9 @@ extern RecursiveMutex cs_main;
|
||||
|
||||
class CBlock;
|
||||
class CBlockIndex;
|
||||
class CBlockPolicyEstimator;
|
||||
class CChainState;
|
||||
class CTxMemPool;
|
||||
class ChainstateManager;
|
||||
class UniValue;
|
||||
struct LLMQContext;
|
||||
struct NodeContext;
|
||||
namespace llmq {
|
||||
class CChainLocksHandler;
|
||||
@ -61,16 +57,6 @@ void CalculatePercentilesBySize(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], s
|
||||
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
|
||||
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex = true, const CTxUndo* txundo = nullptr, const CSpentIndexTxInfo* ptxSpentInfo = nullptr);
|
||||
|
||||
NodeContext& EnsureAnyNodeContext(const CoreContext& context);
|
||||
CTxMemPool& EnsureMemPool(const NodeContext& node);
|
||||
CTxMemPool& EnsureAnyMemPool(const CoreContext& context);
|
||||
ChainstateManager& EnsureChainman(const NodeContext& node);
|
||||
ChainstateManager& EnsureAnyChainman(const CoreContext& context);
|
||||
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node);
|
||||
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const CoreContext& context);
|
||||
LLMQContext& EnsureLLMQContext(const NodeContext& node);
|
||||
LLMQContext& EnsureAnyLLMQContext(const CoreContext& context);
|
||||
|
||||
/**
|
||||
* Helper to create UTXO snapshots given a chainstate and a file handle.
|
||||
* @return a UniValue map containing metadata about the snapshot.
|
||||
|
@ -66,6 +66,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
|
||||
{ "getbalance", 2, "addlocked" },
|
||||
{ "getbalance", 3, "include_watchonly" },
|
||||
{ "getbalance", 4, "avoid_reuse" },
|
||||
{ "getblockfrompeer", 1, "nodeid" },
|
||||
{ "getchaintips", 0, "count" },
|
||||
{ "getchaintips", 1, "branchlen" },
|
||||
{ "getblockhash", 0, "height" },
|
||||
|
@ -7,8 +7,8 @@
|
||||
#include <coinjoin/context.h>
|
||||
#include <coinjoin/server.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/net.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <node/context.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <util/moneystr.h>
|
||||
#include <util/translation.h>
|
||||
|
@ -16,8 +16,8 @@
|
||||
#include <node/context.h>
|
||||
#include <net.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/net.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <governance/common.h>
|
||||
#include <util/strencodings.h>
|
||||
|
@ -15,8 +15,8 @@
|
||||
#include <net.h>
|
||||
#include <netbase.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/net.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <univalue.h>
|
||||
#include <util/strencodings.h>
|
||||
|
@ -26,8 +26,8 @@
|
||||
#include <pow.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/mining.h>
|
||||
#include <rpc/net.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <script/descriptor.h>
|
||||
#include <script/script.h>
|
||||
|
@ -19,8 +19,8 @@
|
||||
#include <net.h>
|
||||
#include <node/context.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/net.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <scheduler.h>
|
||||
#include <script/descriptor.h>
|
||||
|
@ -9,7 +9,6 @@
|
||||
#include <chainparams.h>
|
||||
#include <clientversion.h>
|
||||
#include <core_io.h>
|
||||
#include <net.h>
|
||||
#include <net_permissions.h>
|
||||
#include <net_processing.h>
|
||||
#include <net_types.h> // For banmap_t
|
||||
@ -18,12 +17,12 @@
|
||||
#include <policy/settings.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/protocol.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <sync.h>
|
||||
#include <timedata.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/string.h>
|
||||
#include <util/system.h>
|
||||
#include <util/translation.h>
|
||||
#include <validation.h>
|
||||
#include <version.h>
|
||||
@ -40,22 +39,6 @@ const std::vector<std::string> CONNECTION_TYPE_DOC{
|
||||
"feeler (short-lived automatic connection for testing addresses)"
|
||||
};
|
||||
|
||||
CConnman& EnsureConnman(const NodeContext& node)
|
||||
{
|
||||
if (!node.connman) {
|
||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||
}
|
||||
return *node.connman;
|
||||
}
|
||||
|
||||
PeerManager& EnsurePeerman(const NodeContext& node)
|
||||
{
|
||||
if (!node.peerman) {
|
||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||
}
|
||||
return *node.peerman;
|
||||
}
|
||||
|
||||
static RPCHelpMan getconnectioncount()
|
||||
{
|
||||
return RPCHelpMan{"getconnectioncount",
|
||||
|
@ -1,15 +0,0 @@
|
||||
// Copyright (c) 2021 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_RPC_NET_H
|
||||
#define BITCOIN_RPC_NET_H
|
||||
|
||||
class CConnman;
|
||||
class PeerManager;
|
||||
struct NodeContext;
|
||||
|
||||
CConnman& EnsureConnman(const NodeContext& node);
|
||||
PeerManager& EnsurePeerman(const NodeContext& node);
|
||||
|
||||
#endif // BITCOIN_RPC_NET_H
|
@ -7,8 +7,8 @@
|
||||
#include <index/txindex.h>
|
||||
#include <node/context.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/net.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <validation.h>
|
||||
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/rawtransaction_util.h>
|
||||
#include <rpc/server.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <script/script.h>
|
||||
#include <script/sign.h>
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <chainparams.h>
|
||||
#include <node/context.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <rpc/server_util.h>
|
||||
#include <rpc/util.h>
|
||||
#include <shutdown.h>
|
||||
#include <sync.h>
|
||||
|
93
src/rpc/server_util.cpp
Normal file
93
src/rpc/server_util.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
// Copyright (c) 2021 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <rpc/server_util.h>
|
||||
|
||||
#include <net_processing.h>
|
||||
#include <node/context.h>
|
||||
#include <policy/fees.h>
|
||||
#include <rpc/protocol.h>
|
||||
#include <rpc/request.h>
|
||||
#include <txmempool.h>
|
||||
#include <util/system.h>
|
||||
#include <validation.h>
|
||||
|
||||
#include <any>
|
||||
|
||||
NodeContext& EnsureAnyNodeContext(const CoreContext& context)
|
||||
{
|
||||
auto* const node_context = GetContext<NodeContext>(context);
|
||||
if (!node_context) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
|
||||
}
|
||||
return *node_context;
|
||||
}
|
||||
|
||||
CTxMemPool& EnsureMemPool(const NodeContext& node)
|
||||
{
|
||||
if (!node.mempool) {
|
||||
throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
|
||||
}
|
||||
return *node.mempool;
|
||||
}
|
||||
|
||||
CTxMemPool& EnsureAnyMemPool(const CoreContext& context)
|
||||
{
|
||||
return EnsureMemPool(EnsureAnyNodeContext(context));
|
||||
}
|
||||
|
||||
ChainstateManager& EnsureChainman(const NodeContext& node)
|
||||
{
|
||||
if (!node.chainman) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
|
||||
}
|
||||
return *node.chainman;
|
||||
}
|
||||
|
||||
ChainstateManager& EnsureAnyChainman(const CoreContext& context)
|
||||
{
|
||||
return EnsureChainman(EnsureAnyNodeContext(context));
|
||||
}
|
||||
|
||||
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
|
||||
{
|
||||
if (!node.fee_estimator) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
|
||||
}
|
||||
return *node.fee_estimator;
|
||||
}
|
||||
|
||||
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const CoreContext& context)
|
||||
{
|
||||
return EnsureFeeEstimator(EnsureAnyNodeContext(context));
|
||||
}
|
||||
|
||||
LLMQContext& EnsureLLMQContext(const NodeContext& node)
|
||||
{
|
||||
if (!node.llmq_ctx) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node LLMQ context not found");
|
||||
}
|
||||
return *node.llmq_ctx;
|
||||
}
|
||||
|
||||
LLMQContext& EnsureAnyLLMQContext(const CoreContext& context)
|
||||
{
|
||||
return EnsureLLMQContext(EnsureAnyNodeContext(context));
|
||||
}
|
||||
|
||||
CConnman& EnsureConnman(const NodeContext& node)
|
||||
{
|
||||
if (!node.connman) {
|
||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||
}
|
||||
return *node.connman;
|
||||
}
|
||||
|
||||
PeerManager& EnsurePeerman(const NodeContext& node)
|
||||
{
|
||||
if (!node.peerman) {
|
||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
|
||||
}
|
||||
return *node.peerman;
|
||||
}
|
30
src/rpc/server_util.h
Normal file
30
src/rpc/server_util.h
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright (c) 2021 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_RPC_SERVER_UTIL_H
|
||||
#define BITCOIN_RPC_SERVER_UTIL_H
|
||||
|
||||
#include <context.h>
|
||||
|
||||
class CBlockPolicyEstimator;
|
||||
class CConnman;
|
||||
class ChainstateManager;
|
||||
class CTxMemPool;
|
||||
class PeerManager;
|
||||
struct NodeContext;
|
||||
struct LLMQContext;
|
||||
|
||||
NodeContext& EnsureAnyNodeContext(const CoreContext& context);
|
||||
CTxMemPool& EnsureMemPool(const NodeContext& node);
|
||||
CTxMemPool& EnsureAnyMemPool(const CoreContext& context);
|
||||
ChainstateManager& EnsureChainman(const NodeContext& node);
|
||||
ChainstateManager& EnsureAnyChainman(const CoreContext& context);
|
||||
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node);
|
||||
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const CoreContext& context);
|
||||
LLMQContext& EnsureLLMQContext(const NodeContext& node);
|
||||
LLMQContext& EnsureAnyLLMQContext(const CoreContext& context);
|
||||
CConnman& EnsureConnman(const NodeContext& node);
|
||||
PeerManager& EnsurePeerman(const NodeContext& node);
|
||||
|
||||
#endif // BITCOIN_RPC_SERVER_UTIL_H
|
@ -4170,6 +4170,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
|
||||
// This requires some new chain data structure to efficiently look up if a
|
||||
// block is in a chain leading to a candidate for best tip, despite not
|
||||
// being such a candidate itself.
|
||||
// Note that this would break the getblockfrompeer RPC
|
||||
|
||||
// TODO: deal better with return value and error conditions for duplicate
|
||||
// and unrequested blocks.
|
||||
|
76
test/functional/rpc_getblockfrompeer.py
Executable file
76
test/functional/rpc_getblockfrompeer.py
Executable file
@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2020 The Bitcoin Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
"""Test the getblockfrompeer RPC."""
|
||||
|
||||
from test_framework.authproxy import JSONRPCException
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.util import (
|
||||
assert_equal,
|
||||
assert_raises_rpc_error,
|
||||
)
|
||||
|
||||
class GetBlockFromPeerTest(BitcoinTestFramework):
|
||||
def set_test_params(self):
|
||||
self.num_nodes = 2
|
||||
|
||||
def setup_network(self):
|
||||
self.setup_nodes()
|
||||
|
||||
def check_for_block(self, hash):
|
||||
try:
|
||||
self.nodes[0].getblock(hash)
|
||||
return True
|
||||
except JSONRPCException:
|
||||
return False
|
||||
|
||||
def run_test(self):
|
||||
self.log.info("Mine 4 blocks on Node 0")
|
||||
self.nodes[0].generate(4)
|
||||
assert_equal(self.nodes[0].getblockcount(), 204)
|
||||
|
||||
self.log.info("Mine competing 3 blocks on Node 1")
|
||||
self.nodes[1].generate(3)
|
||||
assert_equal(self.nodes[1].getblockcount(), 203)
|
||||
short_tip = self.nodes[1].getbestblockhash()
|
||||
|
||||
self.log.info("Connect nodes to sync headers")
|
||||
self.connect_nodes(0, 1)
|
||||
self.sync_blocks()
|
||||
|
||||
self.log.info("Node 0 should only have the header for node 1's block 3")
|
||||
for x in self.nodes[0].getchaintips():
|
||||
if x['hash'] == short_tip:
|
||||
assert_equal(x['status'], "headers-only")
|
||||
break
|
||||
else:
|
||||
raise AssertionError("short tip not synced")
|
||||
assert_raises_rpc_error(-1, "Block not found on disk", self.nodes[0].getblock, short_tip)
|
||||
|
||||
self.log.info("Fetch block from node 1")
|
||||
peers = self.nodes[0].getpeerinfo()
|
||||
assert_equal(len(peers), 1)
|
||||
peer_0_peer_1_id = peers[0]["id"]
|
||||
|
||||
self.log.info("Arguments must be sensible")
|
||||
assert_raises_rpc_error(-8, "hash must be of length 64 (not 4, for '1234')", self.nodes[0].getblockfrompeer, "1234", 0)
|
||||
|
||||
self.log.info("We must already have the header")
|
||||
assert_raises_rpc_error(-1, "Block header missing", self.nodes[0].getblockfrompeer, "00" * 32, 0)
|
||||
|
||||
self.log.info("Non-existent peer generates error")
|
||||
assert_raises_rpc_error(-1, f"Peer nodeid {peer_0_peer_1_id + 1} does not exist", self.nodes[0].getblockfrompeer, short_tip, peer_0_peer_1_id + 1)
|
||||
|
||||
self.log.info("Successful fetch")
|
||||
result = self.nodes[0].getblockfrompeer(short_tip, peer_0_peer_1_id)
|
||||
self.wait_until(lambda: self.check_for_block(short_tip), timeout=1)
|
||||
assert(not "warnings" in result)
|
||||
|
||||
self.log.info("Don't fetch blocks we already have")
|
||||
result = self.nodes[0].getblockfrompeer(short_tip, peer_0_peer_1_id)
|
||||
assert("warnings" in result)
|
||||
assert_equal(result["warnings"], "Block already downloaded")
|
||||
|
||||
if __name__ == '__main__':
|
||||
GetBlockFromPeerTest().main()
|
@ -234,6 +234,7 @@ BASE_SCRIPTS = [
|
||||
'wallet_txn_clone.py --mineblock',
|
||||
'feature_notifications.py',
|
||||
'rpc_getblockfilter.py',
|
||||
'rpc_getblockfrompeer.py',
|
||||
'rpc_invalidateblock.py',
|
||||
'feature_txindex.py',
|
||||
'feature_utxo_set_hash.py',
|
||||
|
Loading…
Reference in New Issue
Block a user