23
23
24
24
class AddrReceiver (P2PInterface ):
25
25
num_ipv4_received = 0
26
+ test_addr_contents = False
26
27
27
- def on_addr (self , message ):
28
- for addr in message .addrs :
29
- assert_equal (addr .nServices , 9 )
30
- if not 8333 <= addr .port < 8343 :
31
- raise AssertionError ("Invalid addr.port of {} (8333-8342 expected)" .format (addr .port ))
32
- assert addr .ip .startswith ('123.123.123.' )
33
- self .num_ipv4_received += 1
34
-
35
-
36
- class GetAddrStore (P2PInterface ):
37
- getaddr_received = False
38
- num_ipv4_received = 0
39
-
40
- def on_getaddr (self , message ):
41
- self .getaddr_received = True
28
+ def __init__ (self , test_addr_contents = False ):
29
+ super ().__init__ ()
30
+ self .test_addr_contents = test_addr_contents
42
31
43
32
def on_addr (self , message ):
44
33
for addr in message .addrs :
45
34
self .num_ipv4_received += 1
35
+ if (self .test_addr_contents ):
36
+ # relay_tests checks the content of the addr messages match
37
+ # expectations based on the message creation in setup_addr_msg
38
+ assert_equal (addr .nServices , 9 )
39
+ if not 8333 <= addr .port < 8343 :
40
+ raise AssertionError ("Invalid addr.port of {} (8333-8342 expected)" .format (addr .port ))
41
+ assert addr .ip .startswith ('123.123.123.' )
46
42
47
43
def addr_received (self ):
48
44
return self .num_ipv4_received != 0
49
45
46
+ def getaddr_received (self ):
47
+ return self .message_count ['getaddr' ] > 0
48
+
50
49
51
50
class AddrTest (BitcoinTestFramework ):
52
51
counter = 0
@@ -79,7 +78,7 @@ def setup_addr_msg(self, num):
79
78
def send_addr_msg (self , source , msg , receivers ):
80
79
source .send_and_ping (msg )
81
80
# pop m_next_addr_send timer
82
- self .mocktime += 5 * 60
81
+ self .mocktime += 10 * 60
83
82
self .nodes [0 ].setmocktime (self .mocktime )
84
83
for peer in receivers :
85
84
peer .sync_send_with_ping ()
@@ -101,7 +100,7 @@ def relay_tests(self):
101
100
num_receivers = 7
102
101
receivers = []
103
102
for _ in range (num_receivers ):
104
- receivers .append (self .nodes [0 ].add_p2p_connection (AddrReceiver ()))
103
+ receivers .append (self .nodes [0 ].add_p2p_connection (AddrReceiver (test_addr_contents = True )))
105
104
106
105
# Keep this with length <= 10. Addresses from larger messages are not
107
106
# relayed.
@@ -125,8 +124,8 @@ def relay_tests(self):
125
124
self .nodes [0 ].disconnect_p2ps ()
126
125
127
126
self .log .info ('Check relay of addresses received from outbound peers' )
128
- inbound_peer = self .nodes [0 ].add_p2p_connection (AddrReceiver ())
129
- full_outbound_peer = self .nodes [0 ].add_outbound_p2p_connection (GetAddrStore (), p2p_idx = 0 , connection_type = "outbound-full-relay" )
127
+ inbound_peer = self .nodes [0 ].add_p2p_connection (AddrReceiver (test_addr_contents = True ))
128
+ full_outbound_peer = self .nodes [0 ].add_outbound_p2p_connection (AddrReceiver (), p2p_idx = 0 , connection_type = "outbound-full-relay" )
130
129
msg = self .setup_addr_msg (2 )
131
130
self .send_addr_msg (full_outbound_peer , msg , [inbound_peer ])
132
131
self .log .info ('Check that the first addr message received from an outbound peer is not relayed' )
@@ -142,7 +141,7 @@ def relay_tests(self):
142
141
assert_equal (inbound_peer .num_ipv4_received , 2 )
143
142
144
143
self .log .info ('Check address relay to outbound peers' )
145
- block_relay_peer = self .nodes [0 ].add_outbound_p2p_connection (GetAddrStore (), p2p_idx = 1 , connection_type = "block-relay-only" )
144
+ block_relay_peer = self .nodes [0 ].add_outbound_p2p_connection (AddrReceiver (), p2p_idx = 1 , connection_type = "block-relay-only" )
146
145
msg3 = self .setup_addr_msg (2 )
147
146
self .send_addr_msg (inbound_peer , msg3 , [full_outbound_peer , block_relay_peer ])
148
147
@@ -156,17 +155,17 @@ def relay_tests(self):
156
155
def getaddr_tests (self ):
157
156
self .log .info ('Test getaddr behavior' )
158
157
self .log .info ('Check that we send a getaddr message upon connecting to an outbound-full-relay peer' )
159
- full_outbound_peer = self .nodes [0 ].add_outbound_p2p_connection (GetAddrStore (), p2p_idx = 0 , connection_type = "outbound-full-relay" )
158
+ full_outbound_peer = self .nodes [0 ].add_outbound_p2p_connection (AddrReceiver (), p2p_idx = 0 , connection_type = "outbound-full-relay" )
160
159
full_outbound_peer .sync_with_ping ()
161
- assert full_outbound_peer .getaddr_received
160
+ assert full_outbound_peer .getaddr_received ()
162
161
163
162
self .log .info ('Check that we do not send a getaddr message upon connecting to a block-relay-only peer' )
164
- block_relay_peer = self .nodes [0 ].add_outbound_p2p_connection (GetAddrStore (), p2p_idx = 1 , connection_type = "block-relay-only" )
163
+ block_relay_peer = self .nodes [0 ].add_outbound_p2p_connection (AddrReceiver (), p2p_idx = 1 , connection_type = "block-relay-only" )
165
164
block_relay_peer .sync_with_ping ()
166
- assert_equal (block_relay_peer .getaddr_received , False )
165
+ assert_equal (block_relay_peer .getaddr_received () , False )
167
166
168
167
self .log .info ('Check that we answer getaddr messages only from inbound peers' )
169
- inbound_peer = self .nodes [0 ].add_p2p_connection (GetAddrStore ())
168
+ inbound_peer = self .nodes [0 ].add_p2p_connection (AddrReceiver ())
170
169
inbound_peer .sync_with_ping ()
171
170
172
171
# Add some addresses to addrman
@@ -182,7 +181,7 @@ def getaddr_tests(self):
182
181
183
182
self .mocktime += 5 * 60
184
183
self .nodes [0 ].setmocktime (self .mocktime )
185
- inbound_peer .wait_until (inbound_peer .addr_received )
184
+ inbound_peer .wait_until (lambda : inbound_peer .addr_received () is True )
186
185
187
186
assert_equal (full_outbound_peer .num_ipv4_received , 0 )
188
187
assert_equal (block_relay_peer .num_ipv4_received , 0 )
@@ -196,9 +195,9 @@ def blocksonly_mode_tests(self):
196
195
self .mocktime = int (time .time ())
197
196
198
197
self .log .info ('Check that we send getaddr messages' )
199
- full_outbound_peer = self .nodes [0 ].add_outbound_p2p_connection (GetAddrStore (), p2p_idx = 0 , connection_type = "outbound-full-relay" )
198
+ full_outbound_peer = self .nodes [0 ].add_outbound_p2p_connection (AddrReceiver (), p2p_idx = 0 , connection_type = "outbound-full-relay" )
200
199
full_outbound_peer .sync_with_ping ()
201
- assert full_outbound_peer .getaddr_received
200
+ assert full_outbound_peer .getaddr_received ()
202
201
203
202
self .log .info ('Check that we relay address messages' )
204
203
addr_source = self .nodes [0 ].add_p2p_connection (P2PInterface ())
0 commit comments