Skip to content

Commit 261dddb

Browse files
committed
test: Combine addr generation helper functions
This combines the addr generation helper functions setup_addr_msg and setup_rand_addr_msg. It also changes the way addr.time is filled to random, because before, if too many addresses (>600) were created in a batch, they would stop being relayed because their timestamp would be too far in the future.
1 parent aeeccd9 commit 261dddb

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

test/functional/p2p_addr_relay.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def on_addr(self, message):
4444
assert_equal(addr.nServices, 9)
4545
if not 8333 <= addr.port < 8343:
4646
raise AssertionError("Invalid addr.port of {} (8333-8342 expected)".format(addr.port))
47-
assert addr.ip.startswith('123.123.123.')
47+
assert addr.ip.startswith('123.123.')
4848

4949
def on_getaddr(self, message):
5050
# When the node sends us a getaddr, it increments the addr relay tokens for the connection by 1000
@@ -91,30 +91,21 @@ def run_test(self):
9191
self.blocksonly_mode_tests()
9292
self.rate_limit_tests()
9393

94-
def setup_addr_msg(self, num):
94+
def setup_addr_msg(self, num, sequential_ips=True):
9595
addrs = []
9696
for i in range(num):
9797
addr = CAddress()
98-
addr.time = self.mocktime + i
98+
addr.time = self.mocktime + random.randrange(-100, 100)
9999
addr.nServices = P2P_SERVICES
100-
addr.ip = f"123.123.123.{self.counter % 256}"
100+
if sequential_ips:
101+
assert self.counter < 256 ** 2 # Don't allow the returned ip addresses to wrap.
102+
addr.ip = f"123.123.{self.counter // 256}.{self.counter % 256}"
103+
self.counter += 1
104+
else:
105+
addr.ip = f"{random.randrange(128,169)}.{random.randrange(1,255)}.{random.randrange(1,255)}.{random.randrange(1,255)}"
101106
addr.port = 8333 + i
102107
addrs.append(addr)
103-
self.counter += 1
104-
105-
msg = msg_addr()
106-
msg.addrs = addrs
107-
return msg
108108

109-
def setup_rand_addr_msg(self, num):
110-
addrs = []
111-
for i in range(num):
112-
addr = CAddress()
113-
addr.time = self.mocktime + i
114-
addr.nServices = P2P_SERVICES
115-
addr.ip = f"{random.randrange(128,169)}.{random.randrange(1,255)}.{random.randrange(1,255)}.{random.randrange(1,255)}"
116-
addr.port = 8333
117-
addrs.append(addr)
118109
msg = msg_addr()
119110
msg.addrs = addrs
120111
return msg
@@ -317,7 +308,7 @@ def blocksonly_mode_tests(self):
317308
def send_addrs_and_test_rate_limiting(self, peer, no_relay, *, new_addrs, total_addrs):
318309
"""Send an addr message and check that the number of addresses processed and rate-limited is as expected"""
319310

320-
peer.send_and_ping(self.setup_rand_addr_msg(new_addrs))
311+
peer.send_and_ping(self.setup_addr_msg(new_addrs, sequential_ips=False))
321312

322313
peerinfo = self.nodes[0].getpeerinfo()[0]
323314
addrs_processed = peerinfo['addr_processed']

0 commit comments

Comments
 (0)