@@ -792,15 +792,13 @@ static RPCHelpMan getblockfrompeer()
792
792
" getblockfrompeer" ,
793
793
" \n Attempt to fetch block from a given peer.\n "
794
794
" \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." ,
796
797
{
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)" },
799
800
},
800
- RPCResult{RPCResult::Type::OBJ, " " , " " ,
801
- {
802
- {RPCResult::Type::STR, " warnings" , /* optional=*/ true , " any warnings" },
803
- }},
801
+ RPCResult{RPCResult::Type::OBJ_EMPTY, " " , /* optional=*/ false , " " , {}},
804
802
RPCExamples{
805
803
HelpExampleCli (" getblockfrompeer" , " \" 00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0" )
806
804
+ HelpExampleRpc (" getblockfrompeer" , " \" 00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" 0" )
@@ -810,31 +808,24 @@ static RPCHelpMan getblockfrompeer()
810
808
const NodeContext& node = EnsureAnyNodeContext (request.context );
811
809
ChainstateManager& chainman = EnsureChainman (node);
812
810
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 ());
818
811
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 ()};
823
814
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 ););
825
816
826
817
if (!index ) {
827
818
throw JSONRPCError (RPC_MISC_ERROR, " Block header missing" );
828
819
}
829
820
830
- UniValue result = UniValue::VOBJ;
831
-
832
821
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" );
836
823
}
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;
838
829
},
839
830
};
840
831
}
0 commit comments