@@ -204,43 +204,54 @@ def test_service_flags(self):
204204 def test_getnodeaddresses (self ):
205205 self .log .info ("Test getnodeaddresses" )
206206 self .nodes [0 ].add_p2p_connection (P2PInterface ())
207+ services = NODE_NETWORK
207208
208- # Add some addresses to the Address Manager over RPC. Due to the way
209- # bucket and bucket position are calculated, some of these addresses
210- # will collide.
209+ # Add an IPv6 address to the address manager.
210+ ipv6_addr = "1233:3432:2434:2343:3234:2345:6546:4534"
211+ self .nodes [0 ].addpeeraddress (address = ipv6_addr , port = 8333 )
212+
213+ # Add 10,000 IPv4 addresses to the address manager. Due to the way bucket
214+ # and bucket positions are calculated, some of these addresses will collide.
211215 imported_addrs = []
212216 for i in range (10000 ):
213217 first_octet = i >> 8
214218 second_octet = i % 256
215- a = "{ }.{}.1.1". format ( first_octet , second_octet ) # IPV4
219+ a = f" { first_octet } .{ second_octet } .1.1"
216220 imported_addrs .append (a )
217221 self .nodes [0 ].addpeeraddress (a , 8333 )
218222
219- # Obtain addresses via rpc call and check they were ones sent in before.
220- #
221- # Maximum possible addresses in addrman is 10000, although actual
222- # number will usually be less due to bucket and bucket position
223- # collisions.
224- node_addresses = self .nodes [0 ].getnodeaddresses (0 )
223+ # Fetch the addresses via the RPC and test the results.
224+ assert_equal (len (self .nodes [0 ].getnodeaddresses ()), 1 ) # default count is 1
225+ assert_equal (len (self .nodes [0 ].getnodeaddresses (count = 2 )), 2 )
226+ assert_equal (len (self .nodes [0 ].getnodeaddresses (network = "ipv4" , count = 8 )), 8 )
227+
228+ # Maximum possible addresses in AddrMan is 10000. The actual number will
229+ # usually be less due to bucket and bucket position collisions.
230+ node_addresses = self .nodes [0 ].getnodeaddresses (0 , "ipv4" )
225231 assert_greater_than (len (node_addresses ), 5000 )
226232 assert_greater_than (10000 , len (node_addresses ))
227233 for a in node_addresses :
228234 assert_equal (a ["time" ], self .mocktime )
229- assert_equal (a ["services" ], NODE_NETWORK )
235+ assert_equal (a ["services" ], services )
230236 assert a ["address" ] in imported_addrs
231237 assert_equal (a ["port" ], 8333 )
232238 assert_equal (a ["network" ], "ipv4" )
233239
234- node_addresses = self .nodes [0 ].getnodeaddresses (1 )
235- assert_equal (len (node_addresses ), 1 )
240+ # Test the IPv6 address.
241+ res = self .nodes [0 ].getnodeaddresses (0 , "ipv6" )
242+ assert_equal (len (res ), 1 )
243+ assert_equal (res [0 ]["address" ], ipv6_addr )
244+ assert_equal (res [0 ]["network" ], "ipv6" )
245+ assert_equal (res [0 ]["port" ], 8333 )
246+ assert_equal (res [0 ]["services" ], services )
236247
237- assert_raises_rpc_error (- 8 , "Address count out of range" , self .nodes [0 ].getnodeaddresses , - 1 )
248+ # Test for the absence of onion and I2P addresses.
249+ for network in ["onion" , "i2p" ]:
250+ assert_equal (self .nodes [0 ].getnodeaddresses (0 , network ), [])
238251
239- # addrman's size cannot be known reliably after insertion, as hash collisions may occur
240- # so only test that requesting a large number of addresses returns less than that
241- LARGE_REQUEST_COUNT = 10000
242- node_addresses = self .nodes [0 ].getnodeaddresses (LARGE_REQUEST_COUNT )
243- assert_greater_than (LARGE_REQUEST_COUNT , len (node_addresses ))
252+ # Test invalid arguments.
253+ assert_raises_rpc_error (- 8 , "Address count out of range" , self .nodes [0 ].getnodeaddresses , - 1 )
254+ assert_raises_rpc_error (- 8 , "Network not recognized: Foo" , self .nodes [0 ].getnodeaddresses , 1 , "Foo" )
244255
245256
246257if __name__ == '__main__' :
0 commit comments