@@ -792,15 +792,13 @@ static RPCHelpMan getblockfrompeer()
792792 " getblockfrompeer" ,
793793 " \n Attempt to fetch block from a given peer.\n "
794794 " \n We must have the header for this block, e.g. using submitheader.\n "
795- " \n Returns {} if a block-request was successfully scheduled\n " ,
795+ " Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored.\n "
796+ " \n Returns an empty JSON object if the request was successfully scheduled." ,
796797 {
797- {" blockhash " , RPCArg::Type::STR_HEX, RPCArg::Optional::NO, " The block hash" },
798- {" nodeid " , RPCArg::Type::NUM, RPCArg::Optional::NO, " The node ID (see getpeerinfo for node IDs)" },
798+ {" block_hash " , RPCArg::Type::STR_HEX, RPCArg::Optional::NO, " The block hash to try to fetch " },
799+ {" peer_id " , RPCArg::Type::NUM, RPCArg::Optional::NO, " The peer to fetch it from (see getpeerinfo for peer IDs)" },
799800 },
800- RPCResult{RPCResult::Type::OBJ, " " , " " ,
801- {
802- {RPCResult::Type::STR, " warnings" , /* optional=*/ true , " any warnings" },
803- }},
801+ RPCResult{RPCResult::Type::OBJ_EMPTY, " " , /* optional=*/ false , " " , {}},
804802 RPCExamples{
805803 HelpExampleCli (" getblockfrompeer" , " \" 00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0" )
806804 + HelpExampleRpc (" getblockfrompeer" , " \" 00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0" )
@@ -810,31 +808,24 @@ static RPCHelpMan getblockfrompeer()
810808 const NodeContext& node = EnsureAnyNodeContext (request.context );
811809 ChainstateManager& chainman = EnsureChainman (node);
812810 PeerManager& peerman = EnsurePeerman (node);
813- CConnman& connman = EnsureConnman (node);
814-
815- uint256 hash (ParseHashV (request.params [0 ], " hash" ));
816-
817- const NodeId nodeid = static_cast <NodeId>(request.params [1 ].get_int64 ());
818811
819- // Check that the peer with nodeid exists
820- if (!connman.ForNode (nodeid, [](CNode* node) {return true ;})) {
821- throw JSONRPCError (RPC_MISC_ERROR, strprintf (" Peer nodeid %d does not exist" , nodeid));
822- }
812+ const uint256& block_hash{ParseHashV (request.params [0 ], " block_hash" )};
813+ const NodeId peer_id{request.params [1 ].get_int64 ()};
823814
824- const CBlockIndex* const index = WITH_LOCK (cs_main, return chainman.m_blockman .LookupBlockIndex (hash ););
815+ const CBlockIndex* const index = WITH_LOCK (cs_main, return chainman.m_blockman .LookupBlockIndex (block_hash ););
825816
826817 if (!index) {
827818 throw JSONRPCError (RPC_MISC_ERROR, " Block header missing" );
828819 }
829820
830- UniValue result = UniValue::VOBJ;
831-
832821 if (index->nStatus & BLOCK_HAVE_DATA) {
833- result.pushKV (" warnings" , " Block already downloaded" );
834- } else if (!peerman.FetchBlock (nodeid, hash, *index)) {
835- throw JSONRPCError (RPC_MISC_ERROR, " Failed to fetch block from peer" );
822+ throw JSONRPCError (RPC_MISC_ERROR, " Block already downloaded" );
836823 }
837- return result;
824+
825+ if (const auto err{peerman.FetchBlock (peer_id, *index)}) {
826+ throw JSONRPCError (RPC_MISC_ERROR, err.value ());
827+ }
828+ return UniValue::VOBJ;
838829},
839830 };
840831}
0 commit comments