Skip to content

Commit 73b8f84

Browse files
committed
merge bitcoin#22147: p2p: Protect last outbound HB compact block peer
1 parent 2ce4818 commit 73b8f84

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
@@ -1058,12 +1058,27 @@ void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
10581058
return;
10591059
}
10601060
if (nodestate->fProvidesHeaderAndIDs) {
1061+
int num_outbound_hb_peers = 0;
10611062
for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
10621063
if (*it == nodeid) {
10631064
lNodesAnnouncingHeaderAndIDs.erase(it);
10641065
lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
10651066
return;
10661067
}
1068+
CNodeState *state = State(*it);
1069+
if (state != nullptr && !state->m_is_inbound) ++num_outbound_hb_peers;
1070+
}
1071+
if (nodestate->m_is_inbound) {
1072+
// If we're adding an inbound HB peer, make sure we're not removing
1073+
// our last outbound HB peer in the process.
1074+
if (lNodesAnnouncingHeaderAndIDs.size() >= 3 && num_outbound_hb_peers == 1) {
1075+
CNodeState *remove_node = State(lNodesAnnouncingHeaderAndIDs.front());
1076+
if (remove_node != nullptr && !remove_node->m_is_inbound) {
1077+
// Put the HB outbound peer in the second slot, so that it
1078+
// doesn't get removed.
1079+
std::swap(lNodesAnnouncingHeaderAndIDs.front(), *std::next(lNodesAnnouncingHeaderAndIDs.begin()));
1080+
}
1081+
}
10671082
}
10681083
m_connman.ForNode(nodeid, [this](CNode* pfrom){
10691084
LockAssertion lock(::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
@@ -194,6 +194,7 @@
194194
'p2p_addrv2_relay.py',
195195
'wallet_groups.py --legacy-wallet',
196196
'wallet_groups.py --descriptors',
197+
'p2p_compactblocks_hb.py',
197198
'p2p_disconnect_ban.py',
198199
'feature_addressindex.py',
199200
'feature_timestampindex.py',

0 commit comments

Comments
 (0)