Skip to content

Commit 923312f

Browse files
committed
rpc: use peer_id, block_hash for FetchBlock
1 parent 34d5399 commit 923312f

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

src/net_processing.cpp

+7-7
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-
std::optional<std::string> FetchBlock(NodeId id, const CBlockIndex& block_index) override;
315+
std::optional<std::string> FetchBlock(NodeId peer_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,38 +1428,38 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
14281428
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
14291429
}
14301430

1431-
std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& block_index)
1431+
std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index)
14321432
{
14331433
if (fImporting) return "Importing...";
14341434
if (fReindex) return "Reindexing...";
14351435

14361436
LOCK(cs_main);
14371437
// Ensure this peer exists and hasn't been disconnected
1438-
CNodeState* state = State(id);
1438+
CNodeState* state = State(peer_id);
14391439
if (state == nullptr) return "Peer does not exist";
14401440
// Ignore pre-segwit peers
14411441
if (!state->fHaveWitness) return "Pre-SegWit peer";
14421442

14431443
// Mark block as in-flight unless it already is (for this peer).
14441444
// If a block was already in-flight for a different peer, its BLOCKTXN
14451445
// response will be dropped.
1446-
if (!BlockRequested(id, block_index)) return "Already requested from this peer";
1446+
if (!BlockRequested(peer_id, block_index)) return "Already requested from this peer";
14471447

14481448
// Construct message to request the block
14491449
const uint256& hash{block_index.GetBlockHash()};
14501450
std::vector<CInv> invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, hash)};
14511451

14521452
// Send block request message to the peer
1453-
bool success = m_connman.ForNode(id, [this, &invs](CNode* node) {
1453+
bool success = m_connman.ForNode(peer_id, [this, &invs](CNode* node) {
14541454
const CNetMsgMaker msgMaker(node->GetCommonVersion());
14551455
this->m_connman.PushMessage(node, msgMaker.Make(NetMsgType::GETDATA, invs));
14561456
return true;
14571457
});
14581458

1459-
if (!success) return "Node not fully connected";
1459+
if (!success) return "Peer not fully connected";
14601460

14611461
LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
1462-
hash.ToString(), id);
1462+
hash.ToString(), peer_id);
14631463
return std::nullopt;
14641464
}
14651465

src/net_processing.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
4545
/**
4646
* Attempt to manually fetch block from a given peer. We must already have the header.
4747
*
48-
* @param[in] id The peer id
48+
* @param[in] peer_id The peer id
4949
* @param[in] block_index The blockindex
5050
* @returns std::nullopt if a request was successfully made, otherwise an error message
5151
*/
52-
virtual std::optional<std::string> FetchBlock(NodeId id, const CBlockIndex& block_index) = 0;
52+
virtual std::optional<std::string> FetchBlock(NodeId peer_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

+6-6
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,8 @@ static RPCHelpMan getblockfrompeer()
784784
"Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored.\n"
785785
"\nReturns an empty JSON object if the request was successfully scheduled.",
786786
{
787-
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
788-
{"nodeid", RPCArg::Type::NUM, RPCArg::Optional::NO, "The node ID (see getpeerinfo for node IDs)"},
787+
{"block_hash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash to try to fetch"},
788+
{"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer to fetch it from (see getpeerinfo for peer IDs)"},
789789
},
790790
RPCResult{RPCResult::Type::OBJ_EMPTY, "", /*optional=*/ false, "", {}},
791791
RPCExamples{
@@ -798,10 +798,10 @@ static RPCHelpMan getblockfrompeer()
798798
ChainstateManager& chainman = EnsureChainman(node);
799799
PeerManager& peerman = EnsurePeerman(node);
800800

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

804-
const CBlockIndex* const index = WITH_LOCK(cs_main, return chainman.m_blockman.LookupBlockIndex(hash););
804+
const CBlockIndex* const index = WITH_LOCK(cs_main, return chainman.m_blockman.LookupBlockIndex(block_hash););
805805

806806
if (!index) {
807807
throw JSONRPCError(RPC_MISC_ERROR, "Block header missing");
@@ -811,7 +811,7 @@ static RPCHelpMan getblockfrompeer()
811811
throw JSONRPCError(RPC_MISC_ERROR, "Block already downloaded");
812812
}
813813

814-
if (const auto err{peerman.FetchBlock(nodeid, *index)}) {
814+
if (const auto err{peerman.FetchBlock(peer_id, *index)}) {
815815
throw JSONRPCError(RPC_MISC_ERROR, err.value());
816816
}
817817
return UniValue::VOBJ;

src/rpc/client.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
6060
{ "getbalance", 1, "minconf" },
6161
{ "getbalance", 2, "include_watchonly" },
6262
{ "getbalance", 3, "avoid_reuse" },
63-
{ "getblockfrompeer", 1, "nodeid" },
63+
{ "getblockfrompeer", 1, "peer_id" },
6464
{ "getblockhash", 0, "height" },
6565
{ "waitforblockheight", 0, "height" },
6666
{ "waitforblockheight", 1, "timeout" },

0 commit comments

Comments
 (0)