Skip to content

Commit

Permalink
rpc: turn already downloaded into error in getblockfrompeer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Dec 24, 2021
1 parent 809d66b commit 60243ca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
15 changes: 6 additions & 9 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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;
},
};
}
Expand Down
6 changes: 2 additions & 4 deletions test/functional/rpc_getblockfrompeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ def run_test(self):
self.log.info("Successful fetch")
result = self.nodes[0].getblockfrompeer(short_tip, peer_0_peer_1_id)
self.wait_until(lambda: self.check_for_block(short_tip), timeout=1)
assert(not "warnings" in result)
assert_equal(result, {})

self.log.info("Don't fetch blocks we already have")
result = self.nodes[0].getblockfrompeer(short_tip, peer_0_peer_1_id)
assert("warnings" in result)
assert_equal(result["warnings"], "Block already downloaded")
assert_raises_rpc_error(-1, "Block already downloaded", self.nodes[0].getblockfrompeer, short_tip, peer_0_peer_1_id)

if __name__ == '__main__':
GetBlockFromPeerTest().main()

0 comments on commit 60243ca

Please sign in to comment.