Skip to content

Commit 869f136

Browse files
jonatackamitiuttarwarjnewberymzumsande
committed
Add test for rpc addpeeraddress with "tried" argument
Co-authored-by: Amiti Uttarwar <[email protected]> Co-authored-by: John Newbery <[email protected]> Co-authored-by: Martin Zumsande <[email protected]>
1 parent ef242f5 commit 869f136

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

test/functional/rpc_net.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,16 @@ def test_getnodeaddresses(self):
239239
assert_raises_rpc_error(-8, "Network not recognized: Foo", self.nodes[0].getnodeaddresses, 1, "Foo")
240240

241241
def test_addpeeraddress(self):
242+
"""RPC addpeeraddress sets the source address equal to the destination address.
243+
If an address with the same /16 as an existing new entry is passed, it will be
244+
placed in the same new bucket and have a 1/64 chance of the bucket positions
245+
colliding (depending on the value of nKey in the addrman), in which case the
246+
new address won't be added. The probability of collision can be reduced to
247+
1/2^16 = 1/65536 by using an address from a different /16. We avoid this here
248+
by first testing adding a tried table entry before testing adding a new table one.
249+
"""
242250
self.log.info("Test addpeeraddress")
251+
self.restart_node(1, ["-checkaddrman=1"])
243252
node = self.nodes[1]
244253

245254
self.log.debug("Test that addpeerinfo is a hidden RPC")
@@ -251,17 +260,25 @@ def test_addpeeraddress(self):
251260
assert_equal(node.addpeeraddress(address="", port=8333), {"success": False})
252261
assert_equal(node.getnodeaddresses(count=0), [])
253262

254-
self.log.debug("Test that adding a valid address succeeds")
255-
assert_equal(node.addpeeraddress(address="1.2.3.4", port=8333), {"success": True})
256-
addrs = node.getnodeaddresses(count=0)
257-
assert_equal(len(addrs), 1)
258-
assert_equal(addrs[0]["address"], "1.2.3.4")
259-
assert_equal(addrs[0]["port"], 8333)
260-
261-
self.log.debug("Test that adding the same address again when already present fails")
262-
assert_equal(node.addpeeraddress(address="1.2.3.4", port=8333), {"success": False})
263+
self.log.debug("Test that adding a valid address to the tried table succeeds")
264+
assert_equal(node.addpeeraddress(address="1.2.3.4", tried=True, port=8333), {"success": True})
265+
with node.assert_debug_log(expected_msgs=["Addrman checks started: new 0, tried 1, total 1"]):
266+
addrs = node.getnodeaddresses(count=0) # getnodeaddresses re-runs the addrman checks
267+
assert_equal(len(addrs), 1)
268+
assert_equal(addrs[0]["address"], "1.2.3.4")
269+
assert_equal(addrs[0]["port"], 8333)
270+
271+
self.log.debug("Test that adding an already-present tried address to the new and tried tables fails")
272+
for value in [True, False]:
273+
assert_equal(node.addpeeraddress(address="1.2.3.4", tried=value, port=8333), {"success": False})
263274
assert_equal(len(node.getnodeaddresses(count=0)), 1)
264275

276+
self.log.debug("Test that adding a second address, this time to the new table, succeeds")
277+
assert_equal(node.addpeeraddress(address="2.0.0.0", port=8333), {"success": True})
278+
with node.assert_debug_log(expected_msgs=["Addrman checks started: new 1, tried 1, total 2"]):
279+
addrs = node.getnodeaddresses(count=0) # getnodeaddresses re-runs the addrman checks
280+
assert_equal(len(addrs), 2)
281+
265282

266283
if __name__ == '__main__':
267284
NetTest().main()

0 commit comments

Comments
 (0)