Skip to content

Commit f6a25be

Browse files
committed
Merge bitcoin/bitcoin#22147: p2p: Protect last outbound HB compact block peer
30aee2d tests: Add test for compact block HB selection (Pieter Wuille) 6efbcec Protect last outbound HB compact block peer (Suhas Daftuar) Pull request description: If all our high-bandwidth compact block serving peers (BIP 152) stall block download, then we can be denied a block for (potentially) a long time. As inbound connections are much more likely to be adversarial than outbound connections, mitigate this risk by never removing our last outbound HB peer if it would be replaced by an inbound. ACKs for top commit: achow101: ACK 30aee2d ariard: Code ACK 30aee2d jonatack: ACK 30aee2d Tree-SHA512: 5c6c9326e3667b97e0864c371ae2174d2be9054dad479f4366127b9cd3ac60ffa01ec9707b16ef29cac122db6916cf56fd9985733390017134ace483278921d5
2 parents a305a68 + 30aee2d commit f6a25be

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

src/net_processing.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,12 +836,27 @@ void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
836836
return;
837837
}
838838
if (nodestate->fProvidesHeaderAndIDs) {
839+
int num_outbound_hb_peers = 0;
839840
for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
840841
if (*it == nodeid) {
841842
lNodesAnnouncingHeaderAndIDs.erase(it);
842843
lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
843844
return;
844845
}
846+
CNodeState *state = State(*it);
847+
if (state != nullptr && !state->m_is_inbound) ++num_outbound_hb_peers;
848+
}
849+
if (nodestate->m_is_inbound) {
850+
// If we're adding an inbound HB peer, make sure we're not removing
851+
// our last outbound HB peer in the process.
852+
if (lNodesAnnouncingHeaderAndIDs.size() >= 3 && num_outbound_hb_peers == 1) {
853+
CNodeState *remove_node = State(lNodesAnnouncingHeaderAndIDs.front());
854+
if (remove_node != nullptr && !remove_node->m_is_inbound) {
855+
// Put the HB outbound peer in the second slot, so that it
856+
// doesn't get removed.
857+
std::swap(lNodesAnnouncingHeaderAndIDs.front(), *std::next(lNodesAnnouncingHeaderAndIDs.begin()));
858+
}
859+
}
845860
}
846861
m_connman.ForNode(nodeid, [this](CNode* pfrom) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
847862
AssertLockHeld(::cs_main);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2021 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test compact blocks HB selection logic."""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.util import assert_equal
9+
10+
11+
class CompactBlocksConnectionTest(BitcoinTestFramework):
12+
"""Test class for verifying selection of HB peer connections."""
13+
14+
def set_test_params(self):
15+
self.setup_clean_chain = True
16+
self.num_nodes = 6
17+
18+
def peer_info(self, from_node, to_node):
19+
"""Query from_node for its getpeerinfo about to_node."""
20+
for peerinfo in self.nodes[from_node].getpeerinfo():
21+
if "(testnode%i)" % to_node in peerinfo['subver']:
22+
return peerinfo
23+
return None
24+
25+
def setup_network(self):
26+
self.setup_nodes()
27+
# Start network with everyone disconnected
28+
self.sync_all()
29+
30+
def relay_block_through(self, peer):
31+
"""Relay a new block through peer peer, and return HB status between 1 and [2,3,4,5]."""
32+
self.connect_nodes(peer, 0)
33+
self.nodes[0].generate(1)
34+
self.sync_blocks()
35+
self.disconnect_nodes(peer, 0)
36+
status_to = [self.peer_info(1, i)['bip152_hb_to'] for i in range(2, 6)]
37+
status_from = [self.peer_info(i, 1)['bip152_hb_from'] for i in range(2, 6)]
38+
assert_equal(status_to, status_from)
39+
return status_to
40+
41+
def run_test(self):
42+
self.log.info("Testing reserved high-bandwidth mode slot for outbound peer...")
43+
44+
# Connect everyone to node 0, and mine some blocks to get all nodes out of IBD.
45+
for i in range(1, 6):
46+
self.connect_nodes(i, 0)
47+
self.nodes[0].generate(2)
48+
self.sync_blocks()
49+
for i in range(1, 6):
50+
self.disconnect_nodes(i, 0)
51+
52+
# Construct network topology:
53+
# - Node 0 is the block producer
54+
# - Node 1 is the "target" node being tested
55+
# - Nodes 2-5 are intermediaries.
56+
# - Node 1 has an outbound connection to node 2
57+
# - Node 1 has inbound connections from nodes 3-5
58+
self.connect_nodes(3, 1)
59+
self.connect_nodes(4, 1)
60+
self.connect_nodes(5, 1)
61+
self.connect_nodes(1, 2)
62+
63+
# Mine blocks subsequently relaying through nodes 3,4,5 (inbound to node 1)
64+
for nodeid in range(3, 6):
65+
status = self.relay_block_through(nodeid)
66+
assert_equal(status, [False, nodeid >= 3, nodeid >= 4, nodeid >= 5])
67+
68+
# And again through each. This should not change HB status.
69+
for nodeid in range(3, 6):
70+
status = self.relay_block_through(nodeid)
71+
assert_equal(status, [False, True, True, True])
72+
73+
# Now relay one block through peer 2 (outbound from node 1), so it should take HB status
74+
# from one of the inbounds.
75+
status = self.relay_block_through(2)
76+
assert_equal(status[0], True)
77+
assert_equal(sum(status), 3)
78+
79+
# Now relay again through nodes 3,4,5. Since 2 is outbound, it should remain HB.
80+
for nodeid in range(3, 6):
81+
status = self.relay_block_through(nodeid)
82+
assert status[0]
83+
assert status[nodeid - 2]
84+
assert_equal(sum(status), 3)
85+
86+
# Reconnect peer 2, and retry. Now the three inbounds should be HB again.
87+
self.disconnect_nodes(1, 2)
88+
self.connect_nodes(1, 2)
89+
for nodeid in range(3, 6):
90+
status = self.relay_block_through(nodeid)
91+
assert not status[0]
92+
assert status[nodeid - 2]
93+
assert_equal(status, [False, True, True, True])
94+
95+
96+
if __name__ == '__main__':
97+
CompactBlocksConnectionTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
'wallet_groups.py --legacy-wallet',
175175
'p2p_addrv2_relay.py',
176176
'wallet_groups.py --descriptors',
177+
'p2p_compactblocks_hb.py',
177178
'p2p_disconnect_ban.py',
178179
'rpc_decodescript.py',
179180
'rpc_blockchain.py',

0 commit comments

Comments
 (0)