test: improve getnodeaddresses coverage, test by network

pull/826/head
Jon Atack 4 years ago
parent 6c98c09991
commit 3f89c0e990
No known key found for this signature in database
GPG Key ID: 4F5721B3D0E3921D

@ -187,43 +187,54 @@ class NetTest(BitcoinTestFramework):
def test_getnodeaddresses(self): def test_getnodeaddresses(self):
self.log.info("Test getnodeaddresses") self.log.info("Test getnodeaddresses")
self.nodes[0].add_p2p_connection(P2PInterface()) self.nodes[0].add_p2p_connection(P2PInterface())
services = NODE_NETWORK | NODE_WITNESS
# Add some addresses to the Address Manager over RPC. Due to the way # Add an IPv6 address to the address manager.
# bucket and bucket position are calculated, some of these addresses ipv6_addr = "1233:3432:2434:2343:3234:2345:6546:4534"
# will collide. self.nodes[0].addpeeraddress(address=ipv6_addr, port=8333)
# Add 10,000 IPv4 addresses to the address manager. Due to the way bucket
# and bucket positions are calculated, some of these addresses will collide.
imported_addrs = [] imported_addrs = []
for i in range(10000): for i in range(10000):
first_octet = i >> 8 first_octet = i >> 8
second_octet = i % 256 second_octet = i % 256
a = "{}.{}.1.1".format(first_octet, second_octet) # IPV4 a = f"{first_octet}.{second_octet}.1.1"
imported_addrs.append(a) imported_addrs.append(a)
self.nodes[0].addpeeraddress(a, 8333) self.nodes[0].addpeeraddress(a, 8333)
# Obtain addresses via rpc call and check they were ones sent in before. # Fetch the addresses via the RPC and test the results.
# assert_equal(len(self.nodes[0].getnodeaddresses()), 1) # default count is 1
# Maximum possible addresses in addrman is 10000, although actual assert_equal(len(self.nodes[0].getnodeaddresses(count=2)), 2)
# number will usually be less due to bucket and bucket position assert_equal(len(self.nodes[0].getnodeaddresses(network="ipv4", count=8)), 8)
# collisions.
node_addresses = self.nodes[0].getnodeaddresses(0) # Maximum possible addresses in AddrMan is 10000. The actual number will
# usually be less due to bucket and bucket position collisions.
node_addresses = self.nodes[0].getnodeaddresses(0, "ipv4")
assert_greater_than(len(node_addresses), 5000) assert_greater_than(len(node_addresses), 5000)
assert_greater_than(10000, len(node_addresses)) assert_greater_than(10000, len(node_addresses))
for a in node_addresses: for a in node_addresses:
assert_greater_than(a["time"], 1527811200) # 1st June 2018 assert_greater_than(a["time"], 1527811200) # 1st June 2018
assert_equal(a["services"], NODE_NETWORK | NODE_WITNESS) assert_equal(a["services"], services)
assert a["address"] in imported_addrs assert a["address"] in imported_addrs
assert_equal(a["port"], 8333) assert_equal(a["port"], 8333)
assert_equal(a["network"], "ipv4") assert_equal(a["network"], "ipv4")
node_addresses = self.nodes[0].getnodeaddresses(1) # Test the IPv6 address.
assert_equal(len(node_addresses), 1) res = self.nodes[0].getnodeaddresses(0, "ipv6")
assert_equal(len(res), 1)
assert_equal(res[0]["address"], ipv6_addr)
assert_equal(res[0]["network"], "ipv6")
assert_equal(res[0]["port"], 8333)
assert_equal(res[0]["services"], services)
assert_raises_rpc_error(-8, "Address count out of range", self.nodes[0].getnodeaddresses, -1) # Test for the absence of onion and I2P addresses.
for network in ["onion", "i2p"]:
assert_equal(self.nodes[0].getnodeaddresses(0, network), [])
# addrman's size cannot be known reliably after insertion, as hash collisions may occur # Test invalid arguments.
# so only test that requesting a large number of addresses returns less than that assert_raises_rpc_error(-8, "Address count out of range", self.nodes[0].getnodeaddresses, -1)
LARGE_REQUEST_COUNT = 10000 assert_raises_rpc_error(-8, "Network not recognized: Foo", self.nodes[0].getnodeaddresses, 1, "Foo")
node_addresses = self.nodes[0].getnodeaddresses(LARGE_REQUEST_COUNT)
assert_greater_than(LARGE_REQUEST_COUNT, len(node_addresses))
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save