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.
pull/23720/head
Martin Zumsande 3 years ago
parent aeeccd9aa6
commit 261dddb924

@ -44,7 +44,7 @@ class AddrReceiver(P2PInterface):
assert_equal(addr.nServices, 9) assert_equal(addr.nServices, 9)
if not 8333 <= addr.port < 8343: if not 8333 <= addr.port < 8343:
raise AssertionError("Invalid addr.port of {} (8333-8342 expected)".format(addr.port)) raise AssertionError("Invalid addr.port of {} (8333-8342 expected)".format(addr.port))
assert addr.ip.startswith('123.123.123.') assert addr.ip.startswith('123.123.')
def on_getaddr(self, message): def on_getaddr(self, message):
# When the node sends us a getaddr, it increments the addr relay tokens for the connection by 1000 # When the node sends us a getaddr, it increments the addr relay tokens for the connection by 1000
@ -91,30 +91,21 @@ class AddrTest(BitcoinTestFramework):
self.blocksonly_mode_tests() self.blocksonly_mode_tests()
self.rate_limit_tests() self.rate_limit_tests()
def setup_addr_msg(self, num): def setup_addr_msg(self, num, sequential_ips=True):
addrs = [] addrs = []
for i in range(num): for i in range(num):
addr = CAddress() addr = CAddress()
addr.time = self.mocktime + i addr.time = self.mocktime + random.randrange(-100, 100)
addr.nServices = P2P_SERVICES addr.nServices = P2P_SERVICES
addr.ip = f"123.123.123.{self.counter % 256}" if sequential_ips:
assert self.counter < 256 ** 2 # Don't allow the returned ip addresses to wrap.
addr.ip = f"123.123.{self.counter // 256}.{self.counter % 256}"
self.counter += 1
else:
addr.ip = f"{random.randrange(128,169)}.{random.randrange(1,255)}.{random.randrange(1,255)}.{random.randrange(1,255)}"
addr.port = 8333 + i addr.port = 8333 + i
addrs.append(addr) addrs.append(addr)
self.counter += 1
msg = msg_addr()
msg.addrs = addrs
return msg
def setup_rand_addr_msg(self, num):
addrs = []
for i in range(num):
addr = CAddress()
addr.time = self.mocktime + i
addr.nServices = P2P_SERVICES
addr.ip = f"{random.randrange(128,169)}.{random.randrange(1,255)}.{random.randrange(1,255)}.{random.randrange(1,255)}"
addr.port = 8333
addrs.append(addr)
msg = msg_addr() msg = msg_addr()
msg.addrs = addrs msg.addrs = addrs
return msg return msg
@ -317,7 +308,7 @@ class AddrTest(BitcoinTestFramework):
def send_addrs_and_test_rate_limiting(self, peer, no_relay, *, new_addrs, total_addrs): def send_addrs_and_test_rate_limiting(self, peer, no_relay, *, new_addrs, total_addrs):
"""Send an addr message and check that the number of addresses processed and rate-limited is as expected""" """Send an addr message and check that the number of addresses processed and rate-limited is as expected"""
peer.send_and_ping(self.setup_rand_addr_msg(new_addrs)) peer.send_and_ping(self.setup_addr_msg(new_addrs, sequential_ips=False))
peerinfo = self.nodes[0].getpeerinfo()[0] peerinfo = self.nodes[0].getpeerinfo()[0]
addrs_processed = peerinfo['addr_processed'] addrs_processed = peerinfo['addr_processed']

Loading…
Cancel
Save