Skip to content

Commit 3f89c0e

Browse files
committed
test: improve getnodeaddresses coverage, test by network
1 parent 6c98c09 commit 3f89c0e

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

test/functional/rpc_net.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -187,43 +187,54 @@ def test_service_flags(self):
187187
def test_getnodeaddresses(self):
188188
self.log.info("Test getnodeaddresses")
189189
self.nodes[0].add_p2p_connection(P2PInterface())
190+
services = NODE_NETWORK | NODE_WITNESS
190191

191-
# Add some addresses to the Address Manager over RPC. Due to the way
192-
# bucket and bucket position are calculated, some of these addresses
193-
# will collide.
192+
# Add an IPv6 address to the address manager.
193+
ipv6_addr = "1233:3432:2434:2343:3234:2345:6546:4534"
194+
self.nodes[0].addpeeraddress(address=ipv6_addr, port=8333)
195+
196+
# Add 10,000 IPv4 addresses to the address manager. Due to the way bucket
197+
# and bucket positions are calculated, some of these addresses will collide.
194198
imported_addrs = []
195199
for i in range(10000):
196200
first_octet = i >> 8
197201
second_octet = i % 256
198-
a = "{}.{}.1.1".format(first_octet, second_octet) # IPV4
202+
a = f"{first_octet}.{second_octet}.1.1"
199203
imported_addrs.append(a)
200204
self.nodes[0].addpeeraddress(a, 8333)
201205

202-
# Obtain addresses via rpc call and check they were ones sent in before.
203-
#
204-
# Maximum possible addresses in addrman is 10000, although actual
205-
# number will usually be less due to bucket and bucket position
206-
# collisions.
207-
node_addresses = self.nodes[0].getnodeaddresses(0)
206+
# Fetch the addresses via the RPC and test the results.
207+
assert_equal(len(self.nodes[0].getnodeaddresses()), 1) # default count is 1
208+
assert_equal(len(self.nodes[0].getnodeaddresses(count=2)), 2)
209+
assert_equal(len(self.nodes[0].getnodeaddresses(network="ipv4", count=8)), 8)
210+
211+
# Maximum possible addresses in AddrMan is 10000. The actual number will
212+
# usually be less due to bucket and bucket position collisions.
213+
node_addresses = self.nodes[0].getnodeaddresses(0, "ipv4")
208214
assert_greater_than(len(node_addresses), 5000)
209215
assert_greater_than(10000, len(node_addresses))
210216
for a in node_addresses:
211217
assert_greater_than(a["time"], 1527811200) # 1st June 2018
212-
assert_equal(a["services"], NODE_NETWORK | NODE_WITNESS)
218+
assert_equal(a["services"], services)
213219
assert a["address"] in imported_addrs
214220
assert_equal(a["port"], 8333)
215221
assert_equal(a["network"], "ipv4")
216222

217-
node_addresses = self.nodes[0].getnodeaddresses(1)
218-
assert_equal(len(node_addresses), 1)
223+
# Test the IPv6 address.
224+
res = self.nodes[0].getnodeaddresses(0, "ipv6")
225+
assert_equal(len(res), 1)
226+
assert_equal(res[0]["address"], ipv6_addr)
227+
assert_equal(res[0]["network"], "ipv6")
228+
assert_equal(res[0]["port"], 8333)
229+
assert_equal(res[0]["services"], services)
219230

220-
assert_raises_rpc_error(-8, "Address count out of range", self.nodes[0].getnodeaddresses, -1)
231+
# Test for the absence of onion and I2P addresses.
232+
for network in ["onion", "i2p"]:
233+
assert_equal(self.nodes[0].getnodeaddresses(0, network), [])
221234

222-
# addrman's size cannot be known reliably after insertion, as hash collisions may occur
223-
# so only test that requesting a large number of addresses returns less than that
224-
LARGE_REQUEST_COUNT = 10000
225-
node_addresses = self.nodes[0].getnodeaddresses(LARGE_REQUEST_COUNT)
226-
assert_greater_than(LARGE_REQUEST_COUNT, len(node_addresses))
235+
# Test invalid arguments.
236+
assert_raises_rpc_error(-8, "Address count out of range", self.nodes[0].getnodeaddresses, -1)
237+
assert_raises_rpc_error(-8, "Network not recognized: Foo", self.nodes[0].getnodeaddresses, 1, "Foo")
227238

228239

229240
if __name__ == '__main__':

0 commit comments

Comments
 (0)