5
5
"""Test addr response caching"""
6
6
7
7
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
15
10
from test_framework .p2p import (
16
11
P2PInterface ,
17
12
p2p_lock
21
16
assert_equal ,
22
17
)
23
18
19
+ # As defined in net_processing.
24
20
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
39
22
40
23
class AddrReceiver (P2PInterface ):
41
24
@@ -62,18 +45,16 @@ def set_test_params(self):
62
45
self .num_nodes = 1
63
46
64
47
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 ()
69
48
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 )))
77
58
78
59
responses = []
79
60
self .log .info ('Send many addr requests within short time to receive same response' )
@@ -89,7 +70,7 @@ def run_test(self):
89
70
responses .append (addr_receiver .get_received_addrs ())
90
71
for response in responses [1 :]:
91
72
assert_equal (response , responses [0 ])
92
- assert (len (response ) < MAX_ADDR_TO_SEND )
73
+ assert (len (response ) == MAX_ADDR_TO_SEND )
93
74
94
75
cur_mock_time += 3 * 24 * 60 * 60
95
76
self .nodes [0 ].setmocktime (cur_mock_time )
0 commit comments