Skip to content

Commit c991943

Browse files
amitiuttarwarmzumsande
authored andcommitted
[test] Refactor the addr relay test to prepare for new tests
Moves setting up the addr message into a repeatable function, and breaks up the existing tests into separate functions for legibility.
1 parent f6c44e9 commit c991943

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

test/functional/p2p_addr_relay.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,6 @@
1919
)
2020
import time
2121

22-
# Keep this with length <= 10. Addresses from larger messages are not relayed.
23-
ADDRS = []
24-
num_ipv4_addrs = 10
25-
26-
for i in range(num_ipv4_addrs):
27-
addr = CAddress()
28-
addr.time = int(time.time()) + i
29-
addr.nServices = NODE_NETWORK | NODE_WITNESS
30-
addr.ip = "123.123.123.{}".format(i % 256)
31-
addr.port = 8333 + i
32-
ADDRS.append(addr)
33-
3422

3523
class AddrReceiver(P2PInterface):
3624
num_ipv4_received = 0
@@ -45,33 +33,62 @@ def on_addr(self, message):
4533

4634

4735
class AddrTest(BitcoinTestFramework):
36+
counter = 0
37+
mocktime = int(time.time())
38+
4839
def set_test_params(self):
4940
self.num_nodes = 1
5041

5142
def run_test(self):
52-
self.log.info('Create connection that sends addr messages')
53-
addr_source = self.nodes[0].add_p2p_connection(P2PInterface())
43+
self.oversized_addr_test()
44+
self.relay_tests()
45+
46+
def setup_addr_msg(self, num):
47+
addrs = []
48+
for i in range(num):
49+
addr = CAddress()
50+
addr.time = self.mocktime + i
51+
addr.nServices = NODE_NETWORK | NODE_WITNESS
52+
addr.ip = f"123.123.123.{self.counter % 256}"
53+
addr.port = 8333 + i
54+
addrs.append(addr)
55+
self.counter += 1
56+
5457
msg = msg_addr()
58+
msg.addrs = addrs
59+
return msg
5560

56-
self.log.info('Send too-large addr message')
57-
msg.addrs = ADDRS * 101 # more than 1000 addresses in one message
61+
def oversized_addr_test(self):
62+
self.log.info('Send an addr message that is too large')
63+
addr_source = self.nodes[0].add_p2p_connection(P2PInterface())
64+
65+
msg = self.setup_addr_msg(1010)
5866
with self.nodes[0].assert_debug_log(['addr message size = 1010']):
5967
addr_source.send_and_ping(msg)
6068

69+
self.nodes[0].disconnect_p2ps()
70+
71+
def relay_tests(self):
6172
self.log.info('Check that addr message content is relayed and added to addrman')
73+
addr_source = self.nodes[0].add_p2p_connection(P2PInterface())
6274
num_receivers = 7
6375
receivers = []
6476
for _ in range(num_receivers):
6577
receivers.append(self.nodes[0].add_p2p_connection(AddrReceiver()))
66-
msg.addrs = ADDRS
78+
79+
# Keep this with length <= 10. Addresses from larger messages are not
80+
# relayed.
81+
num_ipv4_addrs = 10
82+
msg = self.setup_addr_msg(num_ipv4_addrs)
6783
with self.nodes[0].assert_debug_log(
6884
[
6985
'Added {} addresses from 127.0.0.1: 0 tried'.format(num_ipv4_addrs),
70-
'received: addr (301 bytes) peer=0',
86+
'received: addr (301 bytes) peer=1',
7187
]
7288
):
7389
addr_source.send_and_ping(msg)
74-
self.nodes[0].setmocktime(int(time.time()) + 30 * 60)
90+
self.mocktime += 30 * 60
91+
self.nodes[0].setmocktime(self.mocktime)
7592
for receiver in receivers:
7693
receiver.sync_with_ping()
7794

@@ -82,6 +99,8 @@ def run_test(self):
8299
ipv4_branching_factor = 2
83100
assert_equal(total_ipv4_received, num_ipv4_addrs * ipv4_branching_factor)
84101

102+
self.nodes[0].disconnect_p2ps()
103+
85104

86105
if __name__ == '__main__':
87106
AddrTest().main()

0 commit comments

Comments
 (0)