Skip to content

Commit bfa8858

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22306: [test] Improvements to p2p_addr_relay.py
6168eb0 [test] Prevent intermittent issue (Amiti Uttarwar) 1d8193e [test] Remove GetAddrStore class (Amiti Uttarwar) ef2f149 [test] Update GetAddrStore callers to use AddrReceiver (Amiti Uttarwar) e8c67ea [test] Add functionality to AddrReceiver (Amiti Uttarwar) 09dc073 [test] Allow AddrReceiver to be used more generally (Amiti Uttarwar) Pull request description: A test refactor broken out from #21528 & a fix to #22243. This PR: 1. consolidates the two helper classes into one, with the intent of making the test logic more clear & usable as we add more subtests to the file 2. hopefully fixes the test flakiness by bumping up the mocktime interval to ensure `m_next_addr_send` timer triggers ACKs for top commit: mzumsande: Code-Review ACK 6168eb0 lsilva01: Tested ACK bitcoin/bitcoin@6168eb0 on Ubuntu 20.04 brunoerg: tACK 6168eb0 Tree-SHA512: 248324f9d37e0e5ffe4acc437cd72ad9a2960abc868a97c6040a36e6ea8b59029127ac4f63fcf67d981a5bb4dbf2334bb2c23c541fae8e910d5523884bcedcba
2 parents a196c89 + 6168eb0 commit bfa8858

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

test/functional/p2p_addr_relay.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,29 @@
2323

2424
class AddrReceiver(P2PInterface):
2525
num_ipv4_received = 0
26+
test_addr_contents = False
2627

27-
def on_addr(self, message):
28-
for addr in message.addrs:
29-
assert_equal(addr.nServices, 9)
30-
if not 8333 <= addr.port < 8343:
31-
raise AssertionError("Invalid addr.port of {} (8333-8342 expected)".format(addr.port))
32-
assert addr.ip.startswith('123.123.123.')
33-
self.num_ipv4_received += 1
34-
35-
36-
class GetAddrStore(P2PInterface):
37-
getaddr_received = False
38-
num_ipv4_received = 0
39-
40-
def on_getaddr(self, message):
41-
self.getaddr_received = True
28+
def __init__(self, test_addr_contents=False):
29+
super().__init__()
30+
self.test_addr_contents = test_addr_contents
4231

4332
def on_addr(self, message):
4433
for addr in message.addrs:
4534
self.num_ipv4_received += 1
35+
if(self.test_addr_contents):
36+
# relay_tests checks the content of the addr messages match
37+
# expectations based on the message creation in setup_addr_msg
38+
assert_equal(addr.nServices, 9)
39+
if not 8333 <= addr.port < 8343:
40+
raise AssertionError("Invalid addr.port of {} (8333-8342 expected)".format(addr.port))
41+
assert addr.ip.startswith('123.123.123.')
4642

4743
def addr_received(self):
4844
return self.num_ipv4_received != 0
4945

46+
def getaddr_received(self):
47+
return self.message_count['getaddr'] > 0
48+
5049

5150
class AddrTest(BitcoinTestFramework):
5251
counter = 0
@@ -79,7 +78,7 @@ def setup_addr_msg(self, num):
7978
def send_addr_msg(self, source, msg, receivers):
8079
source.send_and_ping(msg)
8180
# pop m_next_addr_send timer
82-
self.mocktime += 5 * 60
81+
self.mocktime += 10 * 60
8382
self.nodes[0].setmocktime(self.mocktime)
8483
for peer in receivers:
8584
peer.sync_send_with_ping()
@@ -101,7 +100,7 @@ def relay_tests(self):
101100
num_receivers = 7
102101
receivers = []
103102
for _ in range(num_receivers):
104-
receivers.append(self.nodes[0].add_p2p_connection(AddrReceiver()))
103+
receivers.append(self.nodes[0].add_p2p_connection(AddrReceiver(test_addr_contents=True)))
105104

106105
# Keep this with length <= 10. Addresses from larger messages are not
107106
# relayed.
@@ -125,8 +124,8 @@ def relay_tests(self):
125124
self.nodes[0].disconnect_p2ps()
126125

127126
self.log.info('Check relay of addresses received from outbound peers')
128-
inbound_peer = self.nodes[0].add_p2p_connection(AddrReceiver())
129-
full_outbound_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=0, connection_type="outbound-full-relay")
127+
inbound_peer = self.nodes[0].add_p2p_connection(AddrReceiver(test_addr_contents=True))
128+
full_outbound_peer = self.nodes[0].add_outbound_p2p_connection(AddrReceiver(), p2p_idx=0, connection_type="outbound-full-relay")
130129
msg = self.setup_addr_msg(2)
131130
self.send_addr_msg(full_outbound_peer, msg, [inbound_peer])
132131
self.log.info('Check that the first addr message received from an outbound peer is not relayed')
@@ -142,7 +141,7 @@ def relay_tests(self):
142141
assert_equal(inbound_peer.num_ipv4_received, 2)
143142

144143
self.log.info('Check address relay to outbound peers')
145-
block_relay_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=1, connection_type="block-relay-only")
144+
block_relay_peer = self.nodes[0].add_outbound_p2p_connection(AddrReceiver(), p2p_idx=1, connection_type="block-relay-only")
146145
msg3 = self.setup_addr_msg(2)
147146
self.send_addr_msg(inbound_peer, msg3, [full_outbound_peer, block_relay_peer])
148147

@@ -156,17 +155,17 @@ def relay_tests(self):
156155
def getaddr_tests(self):
157156
self.log.info('Test getaddr behavior')
158157
self.log.info('Check that we send a getaddr message upon connecting to an outbound-full-relay peer')
159-
full_outbound_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=0, connection_type="outbound-full-relay")
158+
full_outbound_peer = self.nodes[0].add_outbound_p2p_connection(AddrReceiver(), p2p_idx=0, connection_type="outbound-full-relay")
160159
full_outbound_peer.sync_with_ping()
161-
assert full_outbound_peer.getaddr_received
160+
assert full_outbound_peer.getaddr_received()
162161

163162
self.log.info('Check that we do not send a getaddr message upon connecting to a block-relay-only peer')
164-
block_relay_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=1, connection_type="block-relay-only")
163+
block_relay_peer = self.nodes[0].add_outbound_p2p_connection(AddrReceiver(), p2p_idx=1, connection_type="block-relay-only")
165164
block_relay_peer.sync_with_ping()
166-
assert_equal(block_relay_peer.getaddr_received, False)
165+
assert_equal(block_relay_peer.getaddr_received(), False)
167166

168167
self.log.info('Check that we answer getaddr messages only from inbound peers')
169-
inbound_peer = self.nodes[0].add_p2p_connection(GetAddrStore())
168+
inbound_peer = self.nodes[0].add_p2p_connection(AddrReceiver())
170169
inbound_peer.sync_with_ping()
171170

172171
# Add some addresses to addrman
@@ -182,7 +181,7 @@ def getaddr_tests(self):
182181

183182
self.mocktime += 5 * 60
184183
self.nodes[0].setmocktime(self.mocktime)
185-
inbound_peer.wait_until(inbound_peer.addr_received)
184+
inbound_peer.wait_until(lambda: inbound_peer.addr_received() is True)
186185

187186
assert_equal(full_outbound_peer.num_ipv4_received, 0)
188187
assert_equal(block_relay_peer.num_ipv4_received, 0)
@@ -196,9 +195,9 @@ def blocksonly_mode_tests(self):
196195
self.mocktime = int(time.time())
197196

198197
self.log.info('Check that we send getaddr messages')
199-
full_outbound_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=0, connection_type="outbound-full-relay")
198+
full_outbound_peer = self.nodes[0].add_outbound_p2p_connection(AddrReceiver(), p2p_idx=0, connection_type="outbound-full-relay")
200199
full_outbound_peer.sync_with_ping()
201-
assert full_outbound_peer.getaddr_received
200+
assert full_outbound_peer.getaddr_received()
202201

203202
self.log.info('Check that we relay address messages')
204203
addr_source = self.nodes[0].add_p2p_connection(P2PInterface())

0 commit comments

Comments
 (0)