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.

nodehandling.py 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 handling."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. import urllib.parse
  9. class NodeHandlingTest (BitcoinTestFramework):
  10. def __init__(self):
  11. super().__init__()
  12. self.num_nodes = 4
  13. self.setup_clean_chain = False
  14. def run_test(self):
  15. ###########################
  16. # setban/listbanned tests #
  17. ###########################
  18. assert_equal(len(self.nodes[2].getpeerinfo()), 4) #we should have 4 nodes at this point
  19. self.nodes[2].setban("127.0.0.1", "add")
  20. time.sleep(3) #wait till the nodes are disconected
  21. assert_equal(len(self.nodes[2].getpeerinfo()), 0) #all nodes must be disconnected at this point
  22. assert_equal(len(self.nodes[2].listbanned()), 1)
  23. self.nodes[2].clearbanned()
  24. assert_equal(len(self.nodes[2].listbanned()), 0)
  25. self.nodes[2].setban("127.0.0.0/24", "add")
  26. assert_equal(len(self.nodes[2].listbanned()), 1)
  27. # This will throw an exception because 127.0.0.1 is within range 127.0.0.0/24
  28. assert_raises_jsonrpc(-23, "IP/Subnet already banned", self.nodes[2].setban, "127.0.0.1", "add")
  29. # This will throw an exception because 127.0.0.1/42 is not a real subnet
  30. assert_raises_jsonrpc(-30, "Error: Invalid IP/Subnet", self.nodes[2].setban, "127.0.0.1/42", "add")
  31. assert_equal(len(self.nodes[2].listbanned()), 1) #still only one banned ip because 127.0.0.1 is within the range of 127.0.0.0/24
  32. # This will throw an exception because 127.0.0.1 was not added above
  33. assert_raises_jsonrpc(-30, "Error: Unban failed", self.nodes[2].setban, "127.0.0.1", "remove")
  34. assert_equal(len(self.nodes[2].listbanned()), 1)
  35. self.nodes[2].setban("127.0.0.0/24", "remove")
  36. assert_equal(len(self.nodes[2].listbanned()), 0)
  37. self.nodes[2].clearbanned()
  38. assert_equal(len(self.nodes[2].listbanned()), 0)
  39. ##test persisted banlist
  40. self.nodes[2].setban("127.0.0.0/32", "add")
  41. self.nodes[2].setban("127.0.0.0/24", "add")
  42. self.nodes[2].setban("192.168.0.1", "add", 1) #ban for 1 seconds
  43. self.nodes[2].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) #ban for 1000 seconds
  44. listBeforeShutdown = self.nodes[2].listbanned()
  45. assert_equal("192.168.0.1/32", listBeforeShutdown[2]['address']) #must be here
  46. time.sleep(2) #make 100% sure we expired 192.168.0.1 node time
  47. #stop node
  48. stop_node(self.nodes[2], 2)
  49. self.nodes[2] = start_node(2, self.options.tmpdir)
  50. listAfterShutdown = self.nodes[2].listbanned()
  51. assert_equal("127.0.0.0/24", listAfterShutdown[0]['address'])
  52. assert_equal("127.0.0.0/32", listAfterShutdown[1]['address'])
  53. assert_equal("/19" in listAfterShutdown[2]['address'], True)
  54. ###########################
  55. # RPC disconnectnode test #
  56. ###########################
  57. url = urllib.parse.urlparse(self.nodes[1].url)
  58. self.nodes[0].disconnectnode(url.hostname+":"+str(p2p_port(1)))
  59. time.sleep(2) #disconnecting a node needs a little bit of time
  60. for node in self.nodes[0].getpeerinfo():
  61. assert(node['addr'] != url.hostname+":"+str(p2p_port(1)))
  62. connect_nodes_bi(self.nodes,0,1) #reconnect the node
  63. found = False
  64. for node in self.nodes[0].getpeerinfo():
  65. if node['addr'] == url.hostname+":"+str(p2p_port(1)):
  66. found = True
  67. assert(found)
  68. if __name__ == '__main__':
  69. NodeHandlingTest ().main ()