Skip to content

Commit

Permalink
bcli: change iteration order on peerlist
Browse files Browse the repository at this point in the history
It is simpler to just iterate through the peerlist backwards.

Signed-off-by: Peter Neuroth <[email protected]>
  • Loading branch information
nepet authored and rustyrussell committed Apr 23, 2024
1 parent 8894455 commit 9ae5c04
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
6 changes: 2 additions & 4 deletions plugins/bcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,12 @@ static struct command_result *process_getblockfrompeer(struct bitcoin_cli *bcli)
* block from peer */
plugin_log(bcli->cmd->plugin, LOG_DBG,
"failed to fetch block %s from peer %i, skip.",
stash->block_hash, stash->peers[0]);
stash->block_hash, stash->peers[tal_count(stash->peers) - 1]);
} else {
plugin_log(bcli->cmd->plugin, LOG_DBG,
"try to fetch block %s from peer %i.",
stash->block_hash, stash->peers[0]);
stash->block_hash, stash->peers[tal_count(stash->peers) - 1]);
}

stash->peers[0] = stash->peers[tal_count(stash->peers) - 1];
tal_resize(&stash->peers, tal_count(stash->peers) - 1);

/* `getblockfrompeer` is an async call. sleep for a second to allow the
Expand Down
8 changes: 4 additions & 4 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,19 @@ 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 in this case).
# check that we fetched a block from a peer (1st peer (from the back) in this case).
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 1")
l1.daemon.wait_for_log(rf"try to fetch block {pruned_block} from peer 3")
l1.daemon.wait_for_log(rf"Adding block (\d+): {pruned_block}")

# check that we can also fetch from a peer > 1st.
# check that we can also fetch from a peer > 1st (from the back).
l1.daemon.rpcproxy.mock_rpc("getblockfrompeer", mock_getblockfrompeer(error=True, release_after=2))
bitcoind.generate_block(1)

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 1")
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"Adding block (\d+): {pruned_block}")

Expand Down

0 comments on commit 9ae5c04

Please sign in to comment.