You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

disconnect_ban.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. """Test node disconnect and ban behavior"""
  6. import time
  7. from test_framework.test_framework import BitcoinTestFramework
  8. from test_framework.util import (
  9. assert_equal,
  10. assert_raises_rpc_error,
  11. connect_nodes_bi,
  12. wait_until,
  13. )
  14. class DisconnectBanTest(BitcoinTestFramework):
  15. def set_test_params(self):
  16. self.num_nodes = 2
  17. def run_test(self):
  18. self.log.info("Test setban and listbanned RPCs")
  19. self.log.info("setban: successfully ban single IP address")
  20. assert_equal(len(self.nodes[1].getpeerinfo()), 2) # node1 should have 2 connections to node0 at this point
  21. self.nodes[1].setban("127.0.0.1", "add")
  22. wait_until(lambda: len(self.nodes[1].getpeerinfo()) == 0, timeout=10)
  23. assert_equal(len(self.nodes[1].getpeerinfo()), 0) # all nodes must be disconnected at this point
  24. assert_equal(len(self.nodes[1].listbanned()), 1)
  25. self.log.info("clearbanned: successfully clear ban list")
  26. self.nodes[1].clearbanned()
  27. assert_equal(len(self.nodes[1].listbanned()), 0)
  28. self.nodes[1].setban("127.0.0.0/24", "add")
  29. self.log.info("setban: fail to ban an already banned subnet")
  30. assert_equal(len(self.nodes[1].listbanned()), 1)
  31. assert_raises_rpc_error(-23, "IP/Subnet already banned", self.nodes[1].setban, "127.0.0.1", "add")
  32. self.log.info("setban: fail to ban an invalid subnet")
  33. assert_raises_rpc_error(-30, "Error: Invalid IP/Subnet", self.nodes[1].setban, "127.0.0.1/42", "add")
  34. assert_equal(len(self.nodes[1].listbanned()), 1) # still only one banned ip because 127.0.0.1 is within the range of 127.0.0.0/24
  35. self.log.info("setban remove: fail to unban a non-banned subnet")
  36. assert_raises_rpc_error(-30, "Error: Unban failed", self.nodes[1].setban, "127.0.0.1", "remove")
  37. assert_equal(len(self.nodes[1].listbanned()), 1)
  38. self.log.info("setban remove: successfully unban subnet")
  39. self.nodes[1].setban("127.0.0.0/24", "remove")
  40. assert_equal(len(self.nodes[1].listbanned()), 0)
  41. self.nodes[1].clearbanned()
  42. assert_equal(len(self.nodes[1].listbanned()), 0)
  43. self.log.info("setban: test persistence across node restart")
  44. self.nodes[1].setban("127.0.0.0/32", "add")
  45. self.nodes[1].setban("127.0.0.0/24", "add")
  46. # Set the mocktime so we can control when bans expire
  47. old_time = int(time.time())
  48. self.nodes[1].setmocktime(old_time)
  49. self.nodes[1].setban("192.168.0.1", "add", 1) # ban for 1 seconds
  50. self.nodes[1].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) # ban for 1000 seconds
  51. listBeforeShutdown = self.nodes[1].listbanned()
  52. assert_equal("192.168.0.1/32", listBeforeShutdown[2]['address'])
  53. # Move time forward by 3 seconds so the third ban has expired
  54. self.nodes[1].setmocktime(old_time + 3)
  55. assert_equal(len(self.nodes[1].listbanned()), 3)
  56. self.stop_node(1)
  57. self.start_node(1)
  58. listAfterShutdown = self.nodes[1].listbanned()
  59. assert_equal("127.0.0.0/24", listAfterShutdown[0]['address'])
  60. assert_equal("127.0.0.0/32", listAfterShutdown[1]['address'])
  61. assert_equal("/19" in listAfterShutdown[2]['address'], True)
  62. # Clear ban lists
  63. self.nodes[1].clearbanned()
  64. connect_nodes_bi(self.nodes, 0, 1)
  65. self.log.info("Test disconnectnode RPCs")
  66. self.log.info("disconnectnode: fail to disconnect when calling with address and nodeid")
  67. address1 = self.nodes[0].getpeerinfo()[0]['addr']
  68. node1 = self.nodes[0].getpeerinfo()[0]['addr']
  69. assert_raises_rpc_error(-32602, "Only one of address and nodeid should be provided.", self.nodes[0].disconnectnode, address=address1, nodeid=node1)
  70. self.log.info("disconnectnode: fail to disconnect when calling with junk address")
  71. assert_raises_rpc_error(-29, "Node not found in connected nodes", self.nodes[0].disconnectnode, address="221B Baker Street")
  72. self.log.info("disconnectnode: successfully disconnect node by address")
  73. address1 = self.nodes[0].getpeerinfo()[0]['addr']
  74. self.nodes[0].disconnectnode(address=address1)
  75. wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10)
  76. assert not [node for node in self.nodes[0].getpeerinfo() if node['addr'] == address1]
  77. self.log.info("disconnectnode: successfully reconnect node")
  78. connect_nodes_bi(self.nodes, 0, 1) # reconnect the node
  79. assert_equal(len(self.nodes[0].getpeerinfo()), 2)
  80. assert [node for node in self.nodes[0].getpeerinfo() if node['addr'] == address1]
  81. self.log.info("disconnectnode: successfully disconnect node by node id")
  82. id1 = self.nodes[0].getpeerinfo()[0]['id']
  83. self.nodes[0].disconnectnode(nodeid=id1)
  84. wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10)
  85. assert not [node for node in self.nodes[0].getpeerinfo() if node['id'] == id1]
  86. if __name__ == '__main__':
  87. DisconnectBanTest().main()