Skip to content

bcli: don't try asking non-full nodes for blocks. #8268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions plugins/bcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,20 @@ static struct command_result *process_getpeerinfo(struct bitcoin_cli *bcli)
json_for_each_arr(i, t, toks)
{
int id;
if (json_scan(tmpctx, bcli->output, t, "{id:%}",
JSON_SCAN(json_to_int, &id)) == NULL) {
// fixme: future optimization: a) filter for full nodes,
// b) sort by last ping
tal_arr_expand(&stash->peers, id);
u8 *services;

if (json_scan(tmpctx, bcli->output, t, "{id:%,services:%}",
JSON_SCAN(json_to_int, &id),
JSON_SCAN_TAL(tmpctx, json_tok_bin_from_hex, &services)) == NULL) {
/* From bitcoin source:
* // NODE_NETWORK means that the node is capable of serving the complete block chain. It is currently
* // set by all Bitcoin Core non pruned nodes, and is unset by SPV clients or other light clients.
* NODE_NETWORK = (1 << 0)
*/
if (tal_count(services) > 0 && (services[tal_count(services)-1] & (1<<0))) {
// fixme: future optimization: sort by last ping
tal_arr_expand(&stash->peers, id);
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,15 @@ def mock_getpeerinfo(r, error=False):
"result": [
{
"id": 1,
"services": "000000000000040d",
},
{
"id": 2,
"services": "0000000000000001",
},
{
"id": 3,
"services": "0000000000000000",
},
],
}
Expand Down Expand Up @@ -210,10 +213,10 @@ def mock_getblockfrompeer_inner(r):
l1.daemon.rpcproxy.mock_rpc("getblockfrompeer", mock_getblockfrompeer())
l1.start(wait_for_bitcoind_sync=False)

# check that we fetched a block from a peer (1st peer (from the back) in this case).
# check that we fetched a block from a peer (1st peer (from the back) in this case, but not from 3 which isn't a full node).
pruned_block = bitcoind.rpc.getblockhash(bitcoind.rpc.getblockcount())
l1.daemon.wait_for_log(f"failed to fetch block {pruned_block} from the bitcoin backend")
l1.daemon.wait_for_log(rf"try to fetch block {pruned_block} from peer 3")
l1.daemon.wait_for_log(rf"try to fetch block {pruned_block} from peer 2")
l1.daemon.wait_for_log(rf"Adding block (\d+): {pruned_block}")

# check that we can also fetch from a peer > 1st (from the back).
Expand All @@ -222,8 +225,8 @@ def mock_getblockfrompeer_inner(r):

pruned_block = bitcoind.rpc.getblockhash(bitcoind.rpc.getblockcount())
l1.daemon.wait_for_log(f"failed to fetch block {pruned_block} from the bitcoin backend")
l1.daemon.wait_for_log(rf"failed to fetch block {pruned_block} from peer 3")
l1.daemon.wait_for_log(rf"try to fetch block {pruned_block} from peer (\d+)")
l1.daemon.wait_for_log(rf"failed to fetch block {pruned_block} from peer 2")
l1.daemon.wait_for_log(rf"try to fetch block {pruned_block} from peer 1")
l1.daemon.wait_for_log(rf"Adding block (\d+): {pruned_block}")

# check that we retry if we could not fetch any block
Expand Down
Loading