From 8d1a3e6498de6087501969a9d243b0697ca3fe97 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 13 Dec 2021 13:24:17 +0700 Subject: rpc: allow empty JSON object result --- src/rpc/util.cpp | 5 +++++ src/rpc/util.h | 1 + 2 files changed, 6 insertions(+) (limited to 'src') diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 4a5cd0a4be..33b55707eb 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -830,6 +830,10 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const return; } case Type::OBJ_DYN: + case Type::OBJ_EMPTY: { + sections.PushSection({indent + maybe_key + "{}", Description("empty JSON object")}); + return; + } case Type::OBJ: { sections.PushSection({indent + maybe_key + "{", Description("json object")}); for (const auto& i : m_inner) { @@ -879,6 +883,7 @@ bool RPCResult::MatchesType(const UniValue& result) const return UniValue::VARR == result.getType(); } case Type::OBJ_DYN: + case Type::OBJ_EMPTY: case Type::OBJ: { return UniValue::VOBJ == result.getType(); } diff --git a/src/rpc/util.h b/src/rpc/util.h index d43ee33b0f..352a3e4e4e 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -240,6 +240,7 @@ struct RPCResult { STR_AMOUNT, //!< Special string to represent a floating point amount STR_HEX, //!< Special string with only hex chars OBJ_DYN, //!< Special dictionary with keys that are not literals + OBJ_EMPTY, //!< Special type to allow empty OBJ ARR_FIXED, //!< Special array that has a fixed number of entries NUM_TIME, //!< Special numeric to denote unix epoch time ELISION, //!< Special type to denote elision (...) -- cgit v1.2.3 From 0e3d7c5ee16d5a4c061ab9a57285bceb7899b512 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 8 Dec 2021 19:17:29 +0700 Subject: refactor: drop redundant hash argument from FetchBlock --- src/net_processing.cpp | 7 ++++--- src/net_processing.h | 9 ++++----- src/rpc/blockchain.cpp | 7 +++---- 3 files changed, 11 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/net_processing.cpp b/src/net_processing.cpp index e4b8e9b6a9..5f47a9c16b 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -312,7 +312,7 @@ public: /** Implement PeerManager */ void StartScheduledTasks(CScheduler& scheduler) override; void CheckForStaleTipAndEvictPeers() override; - bool FetchBlock(NodeId id, const uint256& hash, const CBlockIndex& index) override; + bool FetchBlock(NodeId id, const CBlockIndex& block_index) override; bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override; bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; } void SendPings() override; @@ -1428,7 +1428,7 @@ 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) +bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index) { if (fImporting || fReindex) return false; @@ -1440,9 +1440,10 @@ bool PeerManagerImpl::FetchBlock(NodeId id, const uint256& hash, const CBlockInd if (!state->fHaveWitness) return false; // Mark block as in-flight unless it already is - if (!BlockRequested(id, index)) return false; + if (!BlockRequested(id, block_index)) return false; // Construct message to request the block + const uint256& hash{block_index.GetBlockHash()}; std::vector invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, hash)}; // Send block request message to the peer diff --git a/src/net_processing.h b/src/net_processing.h index 6c18e8ddfa..2b22d8ce85 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -45,12 +45,11 @@ public: /** * 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 + * @param[in] id The peer id + * @param[in] block_index The blockindex + * @returns Whether a request was successfully made */ - virtual bool FetchBlock(NodeId id, const uint256& hash, const CBlockIndex& pindex) = 0; + virtual bool FetchBlock(NodeId id, const CBlockIndex& block_index) = 0; /** Begin running background tasks, should only be called once */ virtual void StartScheduledTasks(CScheduler& scheduler) = 0; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 22455058bd..c6c88f5057 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -801,9 +801,8 @@ static RPCHelpMan getblockfrompeer() PeerManager& peerman = EnsurePeerman(node); CConnman& connman = EnsureConnman(node); - uint256 hash(ParseHashV(request.params[0], "hash")); - - const NodeId nodeid = static_cast(request.params[1].get_int64()); + const uint256 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;})) { @@ -820,7 +819,7 @@ static RPCHelpMan getblockfrompeer() if (index->nStatus & BLOCK_HAVE_DATA) { result.pushKV("warnings", "Block already downloaded"); - } else if (!peerman.FetchBlock(nodeid, hash, *index)) { + } else if (!peerman.FetchBlock(nodeid, *index)) { throw JSONRPCError(RPC_MISC_ERROR, "Failed to fetch block from peer"); } return result; -- cgit v1.2.3 From 809d66bb65aa78048e27c2a878d6f7becaecfe11 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 8 Dec 2021 19:44:46 +0700 Subject: rpc: clarify getblockfrompeer behavior when called multiple times --- src/net_processing.cpp | 4 +++- src/rpc/blockchain.cpp | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 5f47a9c16b..1319267944 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1439,7 +1439,9 @@ bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index) // Ignore pre-segwit peers if (!state->fHaveWitness) return false; - // Mark block as in-flight unless it already is + // Mark block as in-flight unless it already is (for this peer). + // If a block was already in-flight for a different peer, its BLOCKTXN + // response will be dropped. if (!BlockRequested(id, block_index)) return false; // Construct message to request the block diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index c6c88f5057..93d5719e25 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -781,7 +781,8 @@ static RPCHelpMan getblockfrompeer() "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", + "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)"}, -- cgit v1.2.3 From 60243cac7286e4c4bdda7094bef4cf6d1564b583 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 13 Dec 2021 13:25:15 +0700 Subject: rpc: turn already downloaded into error in getblockfrompeer --- src/rpc/blockchain.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 93d5719e25..27b04ed834 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -787,10 +787,7 @@ static RPCHelpMan getblockfrompeer() {"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"}, - }}, + RPCResult{RPCResult::Type::OBJ_EMPTY, "", /*optional=*/ false, "", {}}, RPCExamples{ HelpExampleCli("getblockfrompeer", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0") + HelpExampleRpc("getblockfrompeer", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0") @@ -816,14 +813,14 @@ static RPCHelpMan getblockfrompeer() 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, *index)) { + throw JSONRPCError(RPC_MISC_ERROR, "Block already downloaded"); + } + + if (!peerman.FetchBlock(nodeid, *index)) { throw JSONRPCError(RPC_MISC_ERROR, "Failed to fetch block from peer"); } - return result; + return UniValue::VOBJ; }, }; } -- cgit v1.2.3 From 34d5399211eeb61e7e7961c301fb2ddea8aa3f6a Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 8 Dec 2021 20:16:36 +0700 Subject: rpc: more detailed errors for getblockfrompeer --- src/net_processing.cpp | 25 +++++++++++-------------- src/net_processing.h | 4 ++-- src/rpc/blockchain.cpp | 12 +++--------- 3 files changed, 16 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 1319267944..b7b1c828d4 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -312,7 +312,7 @@ public: /** Implement PeerManager */ void StartScheduledTasks(CScheduler& scheduler) override; void CheckForStaleTipAndEvictPeers() override; - bool FetchBlock(NodeId id, const CBlockIndex& block_index) override; + std::optional FetchBlock(NodeId id, const CBlockIndex& block_index) override; bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override; bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; } void SendPings() override; @@ -1428,21 +1428,22 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex) (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT); } -bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index) +std::optional PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index) { - if (fImporting || fReindex) return false; + if (fImporting) return "Importing..."; + if (fReindex) return "Reindexing..."; LOCK(cs_main); // Ensure this peer exists and hasn't been disconnected CNodeState* state = State(id); - if (state == nullptr) return false; + if (state == nullptr) return "Peer does not exist"; // Ignore pre-segwit peers - if (!state->fHaveWitness) return false; + if (!state->fHaveWitness) return "Pre-SegWit peer"; // Mark block as in-flight unless it already is (for this peer). // If a block was already in-flight for a different peer, its BLOCKTXN // response will be dropped. - if (!BlockRequested(id, block_index)) return false; + if (!BlockRequested(id, block_index)) return "Already requested from this peer"; // Construct message to request the block const uint256& hash{block_index.GetBlockHash()}; @@ -1455,15 +1456,11 @@ bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index) return true; }); - if (success) { - LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n", - hash.ToString(), id); - } else { - RemoveBlockRequest(hash); - LogPrint(BCLog::NET, "Failed to request block %s from peer=%d\n", + if (!success) return "Node not fully connected"; + + LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n", hash.ToString(), id); - } - return success; + return std::nullopt; } std::unique_ptr PeerManager::make(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman, diff --git a/src/net_processing.h b/src/net_processing.h index 2b22d8ce85..2c8ae05a26 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -47,9 +47,9 @@ public: * * @param[in] id The peer id * @param[in] block_index The blockindex - * @returns Whether a request was successfully made + * @returns std::nullopt if a request was successfully made, otherwise an error message */ - virtual bool FetchBlock(NodeId id, const CBlockIndex& block_index) = 0; + virtual std::optional FetchBlock(NodeId id, const CBlockIndex& block_index) = 0; /** Begin running background tasks, should only be called once */ virtual void StartScheduledTasks(CScheduler& scheduler) = 0; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 27b04ed834..64c852f6a5 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -797,16 +797,10 @@ static RPCHelpMan getblockfrompeer() const NodeContext& node = EnsureAnyNodeContext(request.context); ChainstateManager& chainman = EnsureChainman(node); PeerManager& peerman = EnsurePeerman(node); - CConnman& connman = EnsureConnman(node); - const uint256 hash(ParseHashV(request.params[0], "hash")); + const uint256& 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) { @@ -817,8 +811,8 @@ static RPCHelpMan getblockfrompeer() throw JSONRPCError(RPC_MISC_ERROR, "Block already downloaded"); } - if (!peerman.FetchBlock(nodeid, *index)) { - throw JSONRPCError(RPC_MISC_ERROR, "Failed to fetch block from peer"); + if (const auto err{peerman.FetchBlock(nodeid, *index)}) { + throw JSONRPCError(RPC_MISC_ERROR, err.value()); } return UniValue::VOBJ; }, -- cgit v1.2.3 From 923312fbf6a89efde1739da0b7209694d4f892ba Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 10 Dec 2021 20:12:05 +0700 Subject: rpc: use peer_id, block_hash for FetchBlock --- src/net_processing.cpp | 14 +++++++------- src/net_processing.h | 4 ++-- src/rpc/blockchain.cpp | 12 ++++++------ src/rpc/client.cpp | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/net_processing.cpp b/src/net_processing.cpp index b7b1c828d4..5047ba7f30 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -312,7 +312,7 @@ public: /** Implement PeerManager */ void StartScheduledTasks(CScheduler& scheduler) override; void CheckForStaleTipAndEvictPeers() override; - std::optional FetchBlock(NodeId id, const CBlockIndex& block_index) override; + std::optional FetchBlock(NodeId peer_id, const CBlockIndex& block_index) override; bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override; bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; } void SendPings() override; @@ -1428,14 +1428,14 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex) (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT); } -std::optional PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index) +std::optional PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index) { if (fImporting) return "Importing..."; if (fReindex) return "Reindexing..."; LOCK(cs_main); // Ensure this peer exists and hasn't been disconnected - CNodeState* state = State(id); + CNodeState* state = State(peer_id); if (state == nullptr) return "Peer does not exist"; // Ignore pre-segwit peers if (!state->fHaveWitness) return "Pre-SegWit peer"; @@ -1443,23 +1443,23 @@ std::optional PeerManagerImpl::FetchBlock(NodeId id, const CBlockIn // Mark block as in-flight unless it already is (for this peer). // If a block was already in-flight for a different peer, its BLOCKTXN // response will be dropped. - if (!BlockRequested(id, block_index)) return "Already requested from this peer"; + if (!BlockRequested(peer_id, block_index)) return "Already requested from this peer"; // Construct message to request the block const uint256& hash{block_index.GetBlockHash()}; std::vector invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, hash)}; // Send block request message to the peer - bool success = m_connman.ForNode(id, [this, &invs](CNode* node) { + bool success = m_connman.ForNode(peer_id, [this, &invs](CNode* node) { const CNetMsgMaker msgMaker(node->GetCommonVersion()); this->m_connman.PushMessage(node, msgMaker.Make(NetMsgType::GETDATA, invs)); return true; }); - if (!success) return "Node not fully connected"; + if (!success) return "Peer not fully connected"; LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n", - hash.ToString(), id); + hash.ToString(), peer_id); return std::nullopt; } diff --git a/src/net_processing.h b/src/net_processing.h index 2c8ae05a26..c30ce056fa 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -45,11 +45,11 @@ public: /** * Attempt to manually fetch block from a given peer. We must already have the header. * - * @param[in] id The peer id + * @param[in] peer_id The peer id * @param[in] block_index The blockindex * @returns std::nullopt if a request was successfully made, otherwise an error message */ - virtual std::optional FetchBlock(NodeId id, const CBlockIndex& block_index) = 0; + virtual std::optional FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0; /** Begin running background tasks, should only be called once */ virtual void StartScheduledTasks(CScheduler& scheduler) = 0; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 64c852f6a5..7588f01256 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -784,8 +784,8 @@ static RPCHelpMan getblockfrompeer() "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)"}, + {"block_hash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash to try to fetch"}, + {"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer to fetch it from (see getpeerinfo for peer IDs)"}, }, RPCResult{RPCResult::Type::OBJ_EMPTY, "", /*optional=*/ false, "", {}}, RPCExamples{ @@ -798,10 +798,10 @@ static RPCHelpMan getblockfrompeer() ChainstateManager& chainman = EnsureChainman(node); PeerManager& peerman = EnsurePeerman(node); - const uint256& hash{ParseHashV(request.params[0], "hash")}; - const NodeId nodeid{request.params[1].get_int64()}; + const uint256& block_hash{ParseHashV(request.params[0], "block_hash")}; + const NodeId peer_id{request.params[1].get_int64()}; - const CBlockIndex* const index = WITH_LOCK(cs_main, return chainman.m_blockman.LookupBlockIndex(hash);); + const CBlockIndex* const index = WITH_LOCK(cs_main, return chainman.m_blockman.LookupBlockIndex(block_hash);); if (!index) { throw JSONRPCError(RPC_MISC_ERROR, "Block header missing"); @@ -811,7 +811,7 @@ static RPCHelpMan getblockfrompeer() throw JSONRPCError(RPC_MISC_ERROR, "Block already downloaded"); } - if (const auto err{peerman.FetchBlock(nodeid, *index)}) { + if (const auto err{peerman.FetchBlock(peer_id, *index)}) { throw JSONRPCError(RPC_MISC_ERROR, err.value()); } return UniValue::VOBJ; diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index a171972da3..0166230853 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -60,7 +60,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "getbalance", 1, "minconf" }, { "getbalance", 2, "include_watchonly" }, { "getbalance", 3, "avoid_reuse" }, - { "getblockfrompeer", 1, "nodeid" }, + { "getblockfrompeer", 1, "peer_id" }, { "getblockhash", 0, "height" }, { "waitforblockheight", 0, "height" }, { "waitforblockheight", 1, "timeout" }, -- cgit v1.2.3