14
14
from test_framework .test_framework import BitcoinTestFramework
15
15
from test_framework .util import (
16
16
assert_equal ,
17
+ PORT_MIN ,
18
+ PORT_RANGE ,
17
19
)
18
20
19
21
# As defined in net_processing.
@@ -42,6 +44,13 @@ def addr_received(self):
42
44
class AddrTest (BitcoinTestFramework ):
43
45
def set_test_params (self ):
44
46
self .num_nodes = 1
47
+ # Start onion ports after p2p and rpc ports.
48
+ port = PORT_MIN + 2 * PORT_RANGE
49
+ self .onion_port1 = port
50
+ self .onion_port2 = port + 1
51
+ self .extra_args = [
52
+ [f"-bind=127.0.0.1:{ self .onion_port1 } =onion" , f"-bind=127.0.0.1:{ self .onion_port2 } =onion" ],
53
+ ]
45
54
46
55
def run_test (self ):
47
56
self .log .info ('Fill peer AddrMan with a lot of records' )
@@ -55,35 +64,66 @@ def run_test(self):
55
64
# only a fraction of all known addresses can be cached and returned.
56
65
assert (len (self .nodes [0 ].getnodeaddresses (0 )) > int (MAX_ADDR_TO_SEND / (MAX_PCT_ADDR_TO_SEND / 100 )))
57
66
58
- responses = []
67
+ last_response_on_local_bind = None
68
+ last_response_on_onion_bind1 = None
69
+ last_response_on_onion_bind2 = None
59
70
self .log .info ('Send many addr requests within short time to receive same response' )
60
71
N = 5
61
72
cur_mock_time = int (time .time ())
62
73
for i in range (N ):
63
- addr_receiver = self .nodes [0 ].add_p2p_connection (AddrReceiver ())
64
- addr_receiver .send_and_ping (msg_getaddr ())
74
+ addr_receiver_local = self .nodes [0 ].add_p2p_connection (AddrReceiver ())
75
+ addr_receiver_local .send_and_ping (msg_getaddr ())
76
+ addr_receiver_onion1 = self .nodes [0 ].add_p2p_connection (AddrReceiver (), dstport = self .onion_port1 )
77
+ addr_receiver_onion1 .send_and_ping (msg_getaddr ())
78
+ addr_receiver_onion2 = self .nodes [0 ].add_p2p_connection (AddrReceiver (), dstport = self .onion_port2 )
79
+ addr_receiver_onion2 .send_and_ping (msg_getaddr ())
80
+
65
81
# Trigger response
66
82
cur_mock_time += 5 * 60
67
83
self .nodes [0 ].setmocktime (cur_mock_time )
68
- addr_receiver .wait_until (addr_receiver .addr_received )
69
- responses .append (addr_receiver .get_received_addrs ())
70
- for response in responses [1 :]:
71
- assert_equal (response , responses [0 ])
72
- assert (len (response ) == MAX_ADDR_TO_SEND )
84
+ addr_receiver_local .wait_until (addr_receiver_local .addr_received )
85
+ addr_receiver_onion1 .wait_until (addr_receiver_onion1 .addr_received )
86
+ addr_receiver_onion2 .wait_until (addr_receiver_onion2 .addr_received )
87
+
88
+ if i > 0 :
89
+ # Responses from different binds should be unique
90
+ assert (last_response_on_local_bind != addr_receiver_onion1 .get_received_addrs ())
91
+ assert (last_response_on_local_bind != addr_receiver_onion2 .get_received_addrs ())
92
+ assert (last_response_on_onion_bind1 != addr_receiver_onion2 .get_received_addrs ())
93
+ # Responses on from the same bind should be the same
94
+ assert_equal (last_response_on_local_bind , addr_receiver_local .get_received_addrs ())
95
+ assert_equal (last_response_on_onion_bind1 , addr_receiver_onion1 .get_received_addrs ())
96
+ assert_equal (last_response_on_onion_bind2 , addr_receiver_onion2 .get_received_addrs ())
97
+
98
+ last_response_on_local_bind = addr_receiver_local .get_received_addrs ()
99
+ last_response_on_onion_bind1 = addr_receiver_onion1 .get_received_addrs ()
100
+ last_response_on_onion_bind2 = addr_receiver_onion2 .get_received_addrs ()
101
+
102
+ for response in [last_response_on_local_bind , last_response_on_onion_bind1 , last_response_on_onion_bind2 ]:
103
+ assert_equal (len (response ), MAX_ADDR_TO_SEND )
73
104
74
105
cur_mock_time += 3 * 24 * 60 * 60
75
106
self .nodes [0 ].setmocktime (cur_mock_time )
76
107
77
108
self .log .info ('After time passed, see a new response to addr request' )
78
- last_addr_receiver = self .nodes [0 ].add_p2p_connection (AddrReceiver ())
79
- last_addr_receiver .send_and_ping (msg_getaddr ())
109
+ addr_receiver_local = self .nodes [0 ].add_p2p_connection (AddrReceiver ())
110
+ addr_receiver_local .send_and_ping (msg_getaddr ())
111
+ addr_receiver_onion1 = self .nodes [0 ].add_p2p_connection (AddrReceiver (), dstport = self .onion_port1 )
112
+ addr_receiver_onion1 .send_and_ping (msg_getaddr ())
113
+ addr_receiver_onion2 = self .nodes [0 ].add_p2p_connection (AddrReceiver (), dstport = self .onion_port2 )
114
+ addr_receiver_onion2 .send_and_ping (msg_getaddr ())
115
+
80
116
# Trigger response
81
117
cur_mock_time += 5 * 60
82
118
self .nodes [0 ].setmocktime (cur_mock_time )
83
- last_addr_receiver .wait_until (last_addr_receiver .addr_received )
84
- # new response is different
85
- assert ( set ( responses [ 0 ]) != set ( last_addr_receiver . get_received_addrs ()) )
119
+ addr_receiver_local .wait_until (addr_receiver_local .addr_received )
120
+ addr_receiver_onion1 . wait_until ( addr_receiver_onion1 . addr_received )
121
+ addr_receiver_onion2 . wait_until ( addr_receiver_onion2 . addr_received )
86
122
123
+ # new response is different
124
+ assert (set (last_response_on_local_bind ) != set (addr_receiver_local .get_received_addrs ()))
125
+ assert (set (last_response_on_onion_bind1 ) != set (addr_receiver_onion1 .get_received_addrs ()))
126
+ assert (set (last_response_on_onion_bind2 ) != set (addr_receiver_onion2 .get_received_addrs ()))
87
127
88
128
if __name__ == '__main__' :
89
129
AddrTest ().main ()
0 commit comments