Skip to content

Commit 0d04784

Browse files
committed
Refactor the functional test
1 parent 83ad65f commit 0d04784

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

test/functional/p2p_getaddr_caching.py

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
"""Test addr response caching"""
66

77
import time
8-
from test_framework.messages import (
9-
CAddress,
10-
NODE_NETWORK,
11-
NODE_WITNESS,
12-
msg_addr,
13-
msg_getaddr,
14-
)
8+
9+
from test_framework.messages import msg_getaddr
1510
from test_framework.p2p import (
1611
P2PInterface,
1712
p2p_lock
@@ -21,21 +16,9 @@
2116
assert_equal,
2217
)
2318

19+
# As defined in net_processing.
2420
MAX_ADDR_TO_SEND = 1000
25-
26-
def gen_addrs(n):
27-
addrs = []
28-
for i in range(n):
29-
addr = CAddress()
30-
addr.time = int(time.time())
31-
addr.nServices = NODE_NETWORK | NODE_WITNESS
32-
# Use first octets to occupy different AddrMan buckets
33-
first_octet = i >> 8
34-
second_octet = i % 256
35-
addr.ip = "{}.{}.1.1".format(first_octet, second_octet)
36-
addr.port = 8333
37-
addrs.append(addr)
38-
return addrs
21+
MAX_PCT_ADDR_TO_SEND = 23
3922

4023
class AddrReceiver(P2PInterface):
4124

@@ -62,18 +45,16 @@ def set_test_params(self):
6245
self.num_nodes = 1
6346

6447
def run_test(self):
65-
self.log.info('Create connection that sends and requests addr messages')
66-
addr_source = self.nodes[0].add_p2p_connection(P2PInterface())
67-
68-
msg_send_addrs = msg_addr()
6948
self.log.info('Fill peer AddrMan with a lot of records')
70-
# Since these addrs are sent from the same source, not all of them will be stored,
71-
# because we allocate a limited number of AddrMan buckets per addr source.
72-
total_addrs = 10000
73-
addrs = gen_addrs(total_addrs)
74-
for i in range(int(total_addrs/MAX_ADDR_TO_SEND)):
75-
msg_send_addrs.addrs = addrs[i * MAX_ADDR_TO_SEND:(i + 1) * MAX_ADDR_TO_SEND]
76-
addr_source.send_and_ping(msg_send_addrs)
49+
for i in range(10000):
50+
first_octet = i >> 8
51+
second_octet = i % 256
52+
a = "{}.{}.1.1".format(first_octet, second_octet)
53+
self.nodes[0].addpeeraddress(a, 8333)
54+
55+
# Need to make sure we hit MAX_ADDR_TO_SEND records in the addr response later because
56+
# only a fraction of all known addresses can be cached and returned.
57+
assert(len(self.nodes[0].getnodeaddresses(0)) > int(MAX_ADDR_TO_SEND / (MAX_PCT_ADDR_TO_SEND / 100)))
7758

7859
responses = []
7960
self.log.info('Send many addr requests within short time to receive same response')
@@ -89,7 +70,7 @@ def run_test(self):
8970
responses.append(addr_receiver.get_received_addrs())
9071
for response in responses[1:]:
9172
assert_equal(response, responses[0])
92-
assert(len(response) < MAX_ADDR_TO_SEND)
73+
assert(len(response) == MAX_ADDR_TO_SEND)
9374

9475
cur_mock_time += 3 * 24 * 60 * 60
9576
self.nodes[0].setmocktime(cur_mock_time)

0 commit comments

Comments
 (0)