Dash Core  0.12.2.1
P2P Digital Currency
nodehandling.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 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 
6 #
7 # Test node handling
8 #
9 
10 from test_framework.test_framework import BitcoinTestFramework
11 from test_framework.util import *
12 
13 try:
14  import http.client as httplib
15 except ImportError:
16  import httplib
17 try:
18  import urllib.parse as urlparse
19 except ImportError:
20  import urlparse
21 
23  def run_test(self):
24 
27  assert_equal(len(self.nodes[2].getpeerinfo()), 4) #we should have 4 nodes at this point
28  self.nodes[2].setban("127.0.0.1", "add")
29  time.sleep(3) #wait till the nodes are disconected
30  assert_equal(len(self.nodes[2].getpeerinfo()), 0) #all nodes must be disconnected at this point
31  assert_equal(len(self.nodes[2].listbanned()), 1)
32  self.nodes[2].clearbanned()
33  assert_equal(len(self.nodes[2].listbanned()), 0)
34  self.nodes[2].setban("127.0.0.0/24", "add")
35  assert_equal(len(self.nodes[2].listbanned()), 1)
36  try:
37  self.nodes[2].setban("127.0.0.1", "add") #throws exception because 127.0.0.1 is within range 127.0.0.0/24
38  except:
39  pass
40  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
41  try:
42  self.nodes[2].setban("127.0.0.1", "remove")
43  except:
44  pass
45  assert_equal(len(self.nodes[2].listbanned()), 1)
46  self.nodes[2].setban("127.0.0.0/24", "remove")
47  assert_equal(len(self.nodes[2].listbanned()), 0)
48  self.nodes[2].clearbanned()
49  assert_equal(len(self.nodes[2].listbanned()), 0)
50 
51 
52  self.nodes[2].setban("127.0.0.0/32", "add")
53  self.nodes[2].setban("127.0.0.0/24", "add")
54  self.nodes[2].setban("192.168.0.1", "add", 1) #ban for 1 seconds
55  self.nodes[2].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) #ban for 1000 seconds
56  listBeforeShutdown = self.nodes[2].listbanned()
57  assert_equal("192.168.0.1/32", listBeforeShutdown[2]['address']) #must be here
58  time.sleep(2) #make 100% sure we expired 192.168.0.1 node time
59 
60  #stop node
61  stop_node(self.nodes[2], 2)
62 
63  self.nodes[2] = start_node(2, self.options.tmpdir)
64  listAfterShutdown = self.nodes[2].listbanned()
65  assert_equal("127.0.0.0/24", listAfterShutdown[0]['address'])
66  assert_equal("127.0.0.0/32", listAfterShutdown[1]['address'])
67  assert_equal("/19" in listAfterShutdown[2]['address'], True)
68 
69 
72  url = urlparse.urlparse(self.nodes[1].url)
73  self.nodes[0].disconnectnode(url.hostname+":"+str(p2p_port(1)))
74  time.sleep(2) #disconnecting a node needs a little bit of time
75  for node in self.nodes[0].getpeerinfo():
76  assert(node['addr'] != url.hostname+":"+str(p2p_port(1)))
77 
78  connect_nodes_bi(self.nodes,0,1) #reconnect the node
79  found = False
80  for node in self.nodes[0].getpeerinfo():
81  if node['addr'] == url.hostname+":"+str(p2p_port(1)):
82  found = True
83  assert(found)
84 
85 if __name__ == '__main__':
86  NodeHandlingTest ().main ()
UniValue clearbanned(const UniValue &params, bool fHelp)
Definition: net.cpp:562
UniValue listbanned(const UniValue &params, bool fHelp)
Definition: net.cpp:529
UniValue disconnectnode(const UniValue &params, bool fHelp)
Definition: net.cpp:234
UniValue setban(const UniValue &params, bool fHelp)
Definition: net.cpp:465
def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None)
Definition: util.py:281
def stop_node(node, i)
Definition: util.py:323
UniValue getpeerinfo(const UniValue &params, bool fHelp)
Definition: net.cpp:70
def p2p_port(n)
Definition: util.py:93
def assert_equal(thing1, thing2)
Definition: util.py:461
def connect_nodes_bi(nodes, a, b)
Definition: util.py:351