rpc: getblockfrompeer followups #23706
pull Sjors wants to merge 7 commits into bitcoin:master from Sjors:2021/12/getblockfrompeer_followups changing 7 files +48 −58-
Sjors commented at 1:28 pm on December 8, 2021: memberFollowups from #20295.
-
DrahtBot added the label P2P on Dec 8, 2021
-
DrahtBot added the label RPC/REST/ZMQ on Dec 8, 2021
-
in test/functional/rpc_getblockfrompeer.py:59 in 8e12436823 outdated
55@@ -60,7 +56,7 @@ def run_test(self): 56 assert_raises_rpc_error(-1, "Block header missing", self.nodes[0].getblockfrompeer, "00" * 32, 0) 57 58 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) 60+ assert_raises_rpc_error(-1, f"Peer does not exist", self.nodes[0].getblockfrompeer, short_tip, peer_0_peer_1_id + 1)
MarcoFalke commented at 3:46 pm on December 8, 2021:0 assert_raises_rpc_error(-1, "Peer does not exist", self.nodes[0].getblockfrompeer, short_tip, peer_0_peer_1_id + 1)
typo in the last commit?
in src/rpc/blockchain.cpp:784 in 8e12436823 outdated
780@@ -781,7 +781,8 @@ static RPCHelpMan getblockfrompeer() 781 "getblockfrompeer", 782 "\nAttempt to fetch block from a given peer.\n" 783 "\nWe must have the header for this block, e.g. using submitheader.\n" 784- "\nReturns {} if a block-request was successfully scheduled\n", 785+ "Calling this again for the same block and a new peer, will cause the response from the previous peer to be ignored.\n"
jonatack commented at 3:49 pm on December 8, 2021:4c5505be omit the comma:
s/peer,/peer/
maybe:
0 "Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored.\n"
in src/rpc/blockchain.cpp:804 in 8e12436823 outdated
808- 809- // Check that the peer with nodeid exists 810- if (!connman.ForNode(nodeid, [](CNode* node) {return true;})) { 811- throw JSONRPCError(RPC_MISC_ERROR, strprintf("Peer nodeid %d does not exist", nodeid)); 812- } 813+ const uint256 hash(ParseHashV(request.params[0], "hash"));
jonatack commented at 3:53 pm on December 8, 2021:737cee7
0 const uint256& hash{ParseHashV(request.params[0], "hash")};
fjahr commented at 9:38 pm on January 7, 2022:note to other reviewers: This is resolved in a later commit, could have been done here already probably.in src/rpc/blockchain.cpp:785 in 8e12436823 outdated
780@@ -781,7 +781,8 @@ static RPCHelpMan getblockfrompeer() 781 "getblockfrompeer", 782 "\nAttempt to fetch block from a given peer.\n" 783 "\nWe must have the header for this block, e.g. using submitheader.\n" 784- "\nReturns {} if a block-request was successfully scheduled\n", 785+ "Calling this again for the same block and a new peer, will cause the response from the previous peer to be ignored.\n" 786+ "\nReturns {} if a block-request was successfully scheduled.\n",
jonatack commented at 3:57 pm on December 8, 2021:4c5505be1438a8e0473adc7c96b5f681a9f81b02 maybe
0 "\nReturns an empty JSON object if a block-request was successfully scheduled.\n",
jonatack commented at 3:57 pm on December 8, 2021: memberApproach ACKSjors force-pushed on Dec 9, 2021Sjors force-pushed on Dec 9, 2021Sjors commented at 2:48 am on December 9, 2021: memberRebased and addresses @MarcoFalke’s and @jonatack’s comments.test: fancier Python for getblockfrompeer
Co-authored-by: MarcoFalke <falke.marco@gmail.com>
in src/rpc/blockchain.cpp:785 in 49c18dacfd outdated
780@@ -781,7 +781,8 @@ static RPCHelpMan getblockfrompeer() 781 "getblockfrompeer", 782 "\nAttempt to fetch block from a given peer.\n" 783 "\nWe must have the header for this block, e.g. using submitheader.\n" 784- "\nReturns {} if a block-request was successfully scheduled\n", 785+ "Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored.\n" 786+ "\nReturns an empty JSON object if a block-request was successfully scheduled.\n",
MarcoFalke commented at 8:22 am on December 9, 2021:0 "\nReturns an empty JSON object if a block-request was successfully scheduled.",
Sjors commented at 8:53 am on December 9, 2021:Done, and rebased.Sjors force-pushed on Dec 9, 2021in src/net_processing.cpp:1459 in 00e23db610 outdated
1460- LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n", 1461- hash.ToString(), id); 1462- } else { 1463- RemoveBlockRequest(hash); 1464- LogPrint(BCLog::NET, "Failed to request block %s from peer=%d\n", 1465+ if (!success) return "Node not fully connected";
jonatack commented at 6:07 pm on December 9, 2021:Maybe add a test assertion for this in the functional test.
Sjors commented at 2:23 am on December 10, 2021:I have no idea how to produce that scenario; IIUC there’s only a very brief interval between when a peer id is assigned and the handshake is done.
jonatack commented at 9:21 am on December 10, 2021:0 if (!success) return "Peer not fully connected";
(a peer is more specific, as it is a node that is not our own)
jonatack commented at 6:10 pm on December 9, 2021: memberLightly tested ACK 00e23db610d3ed206b8d34cf296540b594264732
0$ ./src/bitcoin-cli -signet getblockfrompeer 00000152031b708d8b29013d55aedd24e8db90af7fcf7f1e51881ad97cf0e9a4 0 1{ 2 "warnings": "Block already downloaded" 3}
0$ ./src/bitcoin-cli -signet help getblockfrompeer 1getblockfrompeer "blockhash" nodeid 2 3Attempt to fetch block from a given peer. 4 5We must have the header for this block, e.g. using submitheader. 6Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored. 7 8Returns an empty JSON object if a block-request was successfully scheduled. 9 10Arguments: 111. blockhash (string, required) The block hash 122. nodeid (numeric, required) The node ID (see getpeerinfo for node IDs) 13 14Result: 15{ (json object) 16 "warnings" : "str" (string, optional) any warnings 17} 18 19Examples: 20> bitcoin-cli getblockfrompeer "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09" 0 21> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockfrompeer", "params": ["00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09" 0]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
in src/rpc/blockchain.cpp:788 in 00e23db610 outdated
780@@ -781,7 +781,8 @@ static RPCHelpMan getblockfrompeer() 781 "getblockfrompeer", 782 "\nAttempt to fetch block from a given peer.\n" 783 "\nWe must have the header for this block, e.g. using submitheader.\n" 784- "\nReturns {} if a block-request was successfully scheduled\n", 785+ "Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored.\n" 786+ "\nReturns an empty JSON object if a block-request was successfully scheduled.", 787 { 788 {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"}, 789 {"nodeid", RPCArg::Type::NUM, RPCArg::Optional::NO, "The node ID (see getpeerinfo for node IDs)"},
jonatack commented at 9:20 am on December 10, 2021:maybe
s/nodeid/peer_id/
? the call is named getblockfrompeer, the help description uses “peer”, and snakecase is current rpc args naming style (doc/developer-notes.md::L1163), and a peer is more specific, as it is a node that is not our own0 {"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer ID (see getpeerinfo for node IDs)"},
Sjors commented at 1:12 pm on December 10, 2021:Done, and also changed the variable names.
jonatack commented at 1:26 pm on December 10, 2021:Nice. May need to update client.cpp as well:{ "getblockfrompeer", 1, "nodeid" },
Sjors commented at 2:30 am on December 11, 2021:Oops, fixed.MarcoFalke commented at 1:50 pm on December 10, 2021: memberAssertionError: RPC client conversion table (/tmp/cirrus-ci-build/ci/scratch/build/bitcoin-i686-pc-linux-gnu/src/rpc/client.cpp) and RPC server named arguments mismatch! {('getblockfrompeer', 1, 'peer_id'), ('getblockfrompeer', 1, 'nodeid')}
Sjors force-pushed on Dec 11, 2021jonatack commented at 12:19 pm on December 11, 2021: memberACK 009ccf8d3b173b44b05a7784bb4d053f770b8619
0$ ./src/bitcoin-cli -signet getblockfrompeer 00000152031b708d8b29013d55aedd24e8db90af7fcf7f1e51881ad97cf0e9a4 0 1{ 2 "warnings": "Block already downloaded" 3}
0$ ./src/bitcoin-cli -signet help getblockfrompeer 1getblockfrompeer "blockhash" peer_id 2 3Attempt to fetch block from a given peer. 4 5We must have the header for this block, e.g. using submitheader. 6Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored. 7 8Returns an empty JSON object if a block-request was successfully scheduled. 9 10Arguments: 111. blockhash (string, required) The block hash 122. peer_id (numeric, required) The peer ID (see getpeerinfo for peer IDs) 13 14Result: 15{ (json object) 16 "warnings" : "str" (string, optional) any warnings 17}
If you have to retouch, maybe
s/a block-request/the request/
in the help and snakecase naming for the blockhash param, e.g. block_hash like peer_id (sorry for the incremental review, iteration works for me :D)DrahtBot commented at 12:21 pm on December 11, 2021: memberThe following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Conflicts
Reviewers, this pull request conflicts with the following ones:
- #23927 (rpc: Pruning nodes can not fetch blocks before syncing past their height by fjahr)
- #23813 (Add test and docs for getblockfrompeer by fjahr)
- #22932 (Add CBlockIndex lock annotations, guard nStatus/nFile/nDataPos/nUndoPos by cs_main by jonatack)
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
in src/rpc/blockchain.cpp:811 in 009ccf8d3b outdated
807- const NodeId nodeid = static_cast<NodeId>(request.params[1].get_int64()); 808- 809- // Check that the peer with nodeid exists 810- if (!connman.ForNode(nodeid, [](CNode* node) {return true;})) { 811- throw JSONRPCError(RPC_MISC_ERROR, strprintf("Peer nodeid %d does not exist", nodeid)); 812- }
luke-jr commented at 5:18 pm on December 12, 2021:Why are you removing this? Where is the peer id checked now?
Sjors commented at 2:41 am on December 13, 2021:It’s checked in
FetchBlock
: https://github.com/Sjors/bitcoin/blob/009ccf8d3b173b44b05a7784bb4d053f770b8619/src/net_processing.cpp#L1437-L1439As suggested here: #20295 (review)
luke-jr commented at 3:24 am on December 13, 2021:But this check was for the case where FetchBlock is not called (block already downloaded)
Sjors commented at 4:10 am on December 13, 2021:I see. But we only return one error, and it seems to me that the “block already downloaded” error is more relevant anyway.
luke-jr commented at 4:12 am on December 13, 2021:That’s currently just a warning. Maybe it should be an error?
Sjors commented at 5:39 am on December 13, 2021:Good point, let’s just make it an error, so it’s easier to reason about.
jonatack commented at 10:40 am on December 16, 2021:my previous test:
0$ ./src/bitcoin-cli -signet getblockfrompeer 00000152031b708d8b29013d55aedd24e8db90af7fcf7f1e51881ad97cf0e9a4 0 1{ 2 "warnings": "Block already downloaded" 3}
now
0$ ./src/bitcoin-cli -signet getblockfrompeer 00000152031b708d8b29013d55aedd24e8db90af7fcf7f1e51881ad97cf0e9a4 0 1error code: -1 2error message: 3Block already downloaded
in src/rpc/blockchain.cpp:784 in 009ccf8d3b outdated
780@@ -781,10 +781,11 @@ static RPCHelpMan getblockfrompeer() 781 "getblockfrompeer", 782 "\nAttempt to fetch block from a given peer.\n" 783 "\nWe must have the header for this block, e.g. using submitheader.\n" 784- "\nReturns {} if a block-request was successfully scheduled\n", 785+ "Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored.\n"
luke-jr commented at 5:25 pm on December 12, 2021:Nit: This change appears to be part of the wrong commit
Sjors commented at 2:47 am on December 13, 2021:Moved.Sjors force-pushed on Dec 13, 2021in src/rpc/blockchain.cpp:801 in 45ed85e9a8 outdated
808- 809- // Check that the peer with nodeid exists 810- if (!connman.ForNode(nodeid, [](CNode* node) {return true;})) { 811- throw JSONRPCError(RPC_MISC_ERROR, strprintf("Peer nodeid %d does not exist", nodeid)); 812- } 813+ const uint256& block_hash{ParseHashV(request.params[0], "block_hash")};
Sjors force-pushed on Dec 13, 2021Sjors commented at 6:36 am on December 13, 2021: member@MarcoFalke I added a commit to allow the RPC result to be an empty JSON object (rather thannull
or no result).in src/rpc/util.cpp:850 in f76065f9bd outdated
845@@ -846,6 +846,10 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const 846 sections.PushSection({indent + "}" + maybe_separator, ""}); 847 return; 848 } 849+ case Type::EMPTY: { 850+ sections.PushSection({indent + maybe_key + "{}", Description("empty JSON object")});
MarcoFalke commented at 8:43 am on December 13, 2021:why does this need a new case? Why can’t the above case be re-used?
Sjors commented at 11:58 am on December 13, 2021:Because of
CHECK_NONFATAL(!m_inner.empty());
The rest of the
Type::OBJ
code block doesn’t seem to do anything useful for an empty object either, so it seemed more readable to just add a new case. But I could use anif
statement instead.
Sjors commented at 12:01 pm on December 13, 2021:I found a more compact solution…
Sjors force-pushed on Dec 13, 2021luke-jr referenced this in commit b0f2ed40e6 on Dec 14, 2021luke-jr referenced this in commit 721ecb5841 on Dec 14, 2021in src/rpc/blockchain.cpp:790 in 86c9863a07 outdated
792 }, 793- RPCResult{RPCResult::Type::OBJ, "", "", 794- { 795- {RPCResult::Type::STR, "warnings", /*optional=*/true, "any warnings"}, 796- }}, 797+ RPCResult{RPCResult::Type::EMPTY, "", /* optional */ false, "", {}},
jonatack commented at 10:38 am on December 16, 2021:Is the help now missing a brace?
previous reviews
0Result: 1{ (json object) 2 "warnings" : "str" (string, optional) any warnings 3}
now
0Result: 1 (json object) 2}
maybe this would be nice
0Result: 1{ 2 (empty json object) 3}
jonatack commented at 10:38 am on December 16, 2021:2158b9f (nit, seems new since my last review) format for clang-tidy verification of this named arg in the RPCResult constructor
0 RPCResult{RPCResult::Type::EMPTY, "", /*optional=*/ false, "", {}},
Sjors commented at 11:02 am on December 16, 2021:I’ll bring back the braces and fix the nitin src/rpc/blockchain.cpp:788 in 86c9863a07 outdated
786+ "\nReturns an empty JSON object if the request was successfully scheduled.", 787 { 788- {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"}, 789- {"nodeid", RPCArg::Type::NUM, RPCArg::Optional::NO, "The node ID (see getpeerinfo for node IDs)"}, 790+ {"block_hash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"}, 791+ {"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer ID (see getpeerinfo for peer IDs)"},
jonatack commented at 10:39 am on December 16, 2021:2158b9f perhaps better descriptions
0- {"block_hash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"}, 1- {"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer ID (see getpeerinfo for peer IDs)"}, 2+ {"block_hash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash to try to fetch"}, 3+ {"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer to fetch it from (see getpeerinfo for peer IDs)"},
Sjors force-pushed on Dec 16, 2021in src/rpc/util.h:246 in a7bd4aaf17 outdated
242@@ -243,6 +243,7 @@ struct RPCResult { 243 ARR_FIXED, //!< Special array that has a fixed number of entries 244 NUM_TIME, //!< Special numeric to denote unix epoch time 245 ELISION, //!< Special type to denote elision (...) 246+ EMPTY, //!< Special type to allow empty OBJ
jonatack commented at 8:02 pm on December 16, 2021:pico-nit, wonder if
EMPTY_OBJ
would be clearer, as there areOBJ
andOBJ_DYN
enums (feel free to ignore)0 EMPTY_OBJ, //!< Special type to allow empty JSON object
Sjors commented at 2:39 am on December 23, 2021:Maybe, @MarcoFalke?
MarcoFalke commented at 11:20 am on December 24, 2021:OBJ_EMPTY :ballot_box:
Also, if you retouch, pls put it next to OBJ_DYN in the list
Sjors commented at 3:30 pm on December 24, 2021:OBJ_EMPTY
it is!jonatack commented at 8:04 pm on December 16, 2021: memberLightly tested diff-review ACK a7bd4aaf17fdbfc2cbc5080467b3d17977b8b6b5 per
git diff 86c9863 a7bd4aa
0$ ./src/bitcoin-cli -signet help getblockfrompeer 1getblockfrompeer "block_hash" peer_id 2 3Attempt to fetch block from a given peer. 4 5We must have the header for this block, e.g. using submitheader. 6Subsequent calls for the same block and a new peer will cause the response from the previous peer to be ignored. 7 8Returns an empty JSON object if the request was successfully scheduled. 9 10Arguments: 111. block_hash (string, required) The block hash to try to fetch 122. peer_id (numeric, required) The peer to fetch it from (see getpeerinfo for peer IDs) 13 14Result: 15{} (empty JSON object)
0$ ./src/bitcoin-cli -signet getblockfrompeer 00000152031b708d8b29013d55aedd24e8db90af7fcf7f1e51881ad97cf0e9a4 0 1error code: -1 2error message: 3Block already downloaded
MarcoFalke added this to the milestone 23.0 on Dec 24, 2021rpc: allow empty JSON object result 8d1a3e6498refactor: drop redundant hash argument from FetchBlock 0e3d7c5ee1rpc: clarify getblockfrompeer behavior when called multiple times 809d66bb65rpc: turn already downloaded into error in getblockfrompeer 60243cac72rpc: more detailed errors for getblockfrompeer 34d5399211rpc: use peer_id, block_hash for FetchBlock 923312fbf6Sjors force-pushed on Dec 24, 2021jonatack approvedjonatack commented at 2:52 pm on January 7, 2022: memberACK 923312fbf6a89efde1739da0b7209694d4f892ba :package:fjahr commented at 9:58 pm on January 7, 2022: membertested ACK 923312fbf6a89efde1739da0b7209694d4f892ba
Reviewed code, built and tested the rpc locally.
MarcoFalke merged this on Jan 25, 2022MarcoFalke closed this on Jan 25, 2022
MarcoFalke commented at 7:26 pm on January 25, 2022: memberThis broke the rpc docs: #24155 (comment)Sjors deleted the branch on Jan 25, 2022sidhujag referenced this in commit eb07514e5a on Jan 28, 2022Fabcien referenced this in commit 93ae2058ec on Dec 3, 2022Fabcien referenced this in commit 392507352a on Dec 3, 2022Fabcien referenced this in commit 46ff54fbde on Dec 3, 2022Fabcien referenced this in commit 9132c4dcfd on Dec 3, 2022DrahtBot locked this on Jan 25, 2023
github-metadata-mirror
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-12-18 18:12 UTC
This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me