Skip to content

Commit 34d5399

Browse files
committed
rpc: more detailed errors for getblockfrompeer
1 parent 60243ca commit 34d5399

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

src/net_processing.cpp

+11-14
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class PeerManagerImpl final : public PeerManager
312312
/** Implement PeerManager */
313313
void StartScheduledTasks(CScheduler& scheduler) override;
314314
void CheckForStaleTipAndEvictPeers() override;
315-
bool FetchBlock(NodeId id, const CBlockIndex& block_index) override;
315+
std::optional<std::string> FetchBlock(NodeId id, const CBlockIndex& block_index) override;
316316
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override;
317317
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
318318
void SendPings() override;
@@ -1428,21 +1428,22 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
14281428
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
14291429
}
14301430

1431-
bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index)
1431+
std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index)
14321432
{
1433-
if (fImporting || fReindex) return false;
1433+
if (fImporting) return "Importing...";
1434+
if (fReindex) return "Reindexing...";
14341435

14351436
LOCK(cs_main);
14361437
// Ensure this peer exists and hasn't been disconnected
14371438
CNodeState* state = State(id);
1438-
if (state == nullptr) return false;
1439+
if (state == nullptr) return "Peer does not exist";
14391440
// Ignore pre-segwit peers
1440-
if (!state->fHaveWitness) return false;
1441+
if (!state->fHaveWitness) return "Pre-SegWit peer";
14411442

14421443
// Mark block as in-flight unless it already is (for this peer).
14431444
// If a block was already in-flight for a different peer, its BLOCKTXN
14441445
// response will be dropped.
1445-
if (!BlockRequested(id, block_index)) return false;
1446+
if (!BlockRequested(id, block_index)) return "Already requested from this peer";
14461447

14471448
// Construct message to request the block
14481449
const uint256& hash{block_index.GetBlockHash()};
@@ -1455,15 +1456,11 @@ bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index)
14551456
return true;
14561457
});
14571458

1458-
if (success) {
1459-
LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
1460-
hash.ToString(), id);
1461-
} else {
1462-
RemoveBlockRequest(hash);
1463-
LogPrint(BCLog::NET, "Failed to request block %s from peer=%d\n",
1459+
if (!success) return "Node not fully connected";
1460+
1461+
LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
14641462
hash.ToString(), id);
1465-
}
1466-
return success;
1463+
return std::nullopt;
14671464
}
14681465

14691466
std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman,

src/net_processing.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
4747
*
4848
* @param[in] id The peer id
4949
* @param[in] block_index The blockindex
50-
* @returns Whether a request was successfully made
50+
* @returns std::nullopt if a request was successfully made, otherwise an error message
5151
*/
52-
virtual bool FetchBlock(NodeId id, const CBlockIndex& block_index) = 0;
52+
virtual std::optional<std::string> FetchBlock(NodeId id, const CBlockIndex& block_index) = 0;
5353

5454
/** Begin running background tasks, should only be called once */
5555
virtual void StartScheduledTasks(CScheduler& scheduler) = 0;

src/rpc/blockchain.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -797,16 +797,10 @@ static RPCHelpMan getblockfrompeer()
797797
const NodeContext& node = EnsureAnyNodeContext(request.context);
798798
ChainstateManager& chainman = EnsureChainman(node);
799799
PeerManager& peerman = EnsurePeerman(node);
800-
CConnman& connman = EnsureConnman(node);
801800

802-
const uint256 hash(ParseHashV(request.params[0], "hash"));
801+
const uint256& hash{ParseHashV(request.params[0], "hash")};
803802
const NodeId nodeid{request.params[1].get_int64()};
804803

805-
// Check that the peer with nodeid exists
806-
if (!connman.ForNode(nodeid, [](CNode* node) {return true;})) {
807-
throw JSONRPCError(RPC_MISC_ERROR, strprintf("Peer nodeid %d does not exist", nodeid));
808-
}
809-
810804
const CBlockIndex* const index = WITH_LOCK(cs_main, return chainman.m_blockman.LookupBlockIndex(hash););
811805

812806
if (!index) {
@@ -817,8 +811,8 @@ static RPCHelpMan getblockfrompeer()
817811
throw JSONRPCError(RPC_MISC_ERROR, "Block already downloaded");
818812
}
819813

820-
if (!peerman.FetchBlock(nodeid, *index)) {
821-
throw JSONRPCError(RPC_MISC_ERROR, "Failed to fetch block from peer");
814+
if (const auto err{peerman.FetchBlock(nodeid, *index)}) {
815+
throw JSONRPCError(RPC_MISC_ERROR, err.value());
822816
}
823817
return UniValue::VOBJ;
824818
},

test/functional/rpc_getblockfrompeer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def run_test(self):
5656
assert_raises_rpc_error(-1, "Block header missing", self.nodes[0].getblockfrompeer, "00" * 32, 0)
5757

5858
self.log.info("Non-existent peer generates error")
59-
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)
59+
assert_raises_rpc_error(-1, "Peer does not exist", self.nodes[0].getblockfrompeer, short_tip, peer_0_peer_1_id + 1)
6060

6161
self.log.info("Successful fetch")
6262
result = self.nodes[0].getblockfrompeer(short_tip, peer_0_peer_1_id)

0 commit comments

Comments
 (0)