Skip to content

Commit 09dc073

Browse files
committed
[test] Allow AddrReceiver to be used more generally
The `on_addr` functionality of `AddrReceiver` tests logic specific to how the addr messages are set up in the test bodies. To allow other callers to also use `AddrReceiver`, only apply the assertion logic if the caller indicates desirability by setting `test_addr_contents` to true when initializing the class.
1 parent eb63b1d commit 09dc073

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

test/functional/p2p_addr_relay.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@
2323

2424
class AddrReceiver(P2PInterface):
2525
num_ipv4_received = 0
26+
test_addr_contents = False
27+
28+
def __init__(self, test_addr_contents=False):
29+
super().__init__()
30+
self.test_addr_contents = test_addr_contents
2631

2732
def on_addr(self, message):
2833
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.')
3334
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.')
3442

3543

3644
class GetAddrStore(P2PInterface):
@@ -101,7 +109,7 @@ def relay_tests(self):
101109
num_receivers = 7
102110
receivers = []
103111
for _ in range(num_receivers):
104-
receivers.append(self.nodes[0].add_p2p_connection(AddrReceiver()))
112+
receivers.append(self.nodes[0].add_p2p_connection(AddrReceiver(test_addr_contents=True)))
105113

106114
# Keep this with length <= 10. Addresses from larger messages are not
107115
# relayed.
@@ -125,7 +133,7 @@ def relay_tests(self):
125133
self.nodes[0].disconnect_p2ps()
126134

127135
self.log.info('Check relay of addresses received from outbound peers')
128-
inbound_peer = self.nodes[0].add_p2p_connection(AddrReceiver())
136+
inbound_peer = self.nodes[0].add_p2p_connection(AddrReceiver(test_addr_contents=True))
129137
full_outbound_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=0, connection_type="outbound-full-relay")
130138
msg = self.setup_addr_msg(2)
131139
self.send_addr_msg(full_outbound_peer, msg, [inbound_peer])

0 commit comments

Comments
 (0)