[qa] TestNode: Add wait_until_stopped helper method
[bitcoinplatinum.git] / test / functional / net.py
blob830aeb45b486023d4b295d61ceb41a0793028e7c
1 #!/usr/bin/env python3
2 # Copyright (c) 2017 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 RPC calls related to net.
7 Tests correspond to code in rpc/net.cpp.
8 """
10 import time
12 from test_framework.test_framework import BitcoinTestFramework
13 from test_framework.util import (
14 assert_equal,
15 assert_raises_jsonrpc,
16 connect_nodes_bi,
17 p2p_port,
20 class NetTest(BitcoinTestFramework):
21 def set_test_params(self):
22 self.setup_clean_chain = True
23 self.num_nodes = 2
25 def run_test(self):
26 self._test_connection_count()
27 self._test_getnettotals()
28 self._test_getnetworkinginfo()
29 self._test_getaddednodeinfo()
30 self._test_getpeerinfo()
32 def _test_connection_count(self):
33 # connect_nodes_bi connects each node to the other
34 assert_equal(self.nodes[0].getconnectioncount(), 2)
36 def _test_getnettotals(self):
37 # check that getnettotals totalbytesrecv and totalbytessent
38 # are consistent with getpeerinfo
39 peer_info = self.nodes[0].getpeerinfo()
40 assert_equal(len(peer_info), 2)
41 net_totals = self.nodes[0].getnettotals()
42 assert_equal(sum([peer['bytesrecv'] for peer in peer_info]),
43 net_totals['totalbytesrecv'])
44 assert_equal(sum([peer['bytessent'] for peer in peer_info]),
45 net_totals['totalbytessent'])
46 # test getnettotals and getpeerinfo by doing a ping
47 # the bytes sent/received should change
48 # note ping and pong are 32 bytes each
49 self.nodes[0].ping()
50 time.sleep(0.1)
51 peer_info_after_ping = self.nodes[0].getpeerinfo()
52 net_totals_after_ping = self.nodes[0].getnettotals()
53 for before, after in zip(peer_info, peer_info_after_ping):
54 assert_equal(before['bytesrecv_per_msg']['pong'] + 32, after['bytesrecv_per_msg']['pong'])
55 assert_equal(before['bytessent_per_msg']['ping'] + 32, after['bytessent_per_msg']['ping'])
56 assert_equal(net_totals['totalbytesrecv'] + 32*2, net_totals_after_ping['totalbytesrecv'])
57 assert_equal(net_totals['totalbytessent'] + 32*2, net_totals_after_ping['totalbytessent'])
59 def _test_getnetworkinginfo(self):
60 assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
61 assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
63 self.nodes[0].setnetworkactive(False)
64 assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
65 timeout = 3
66 while self.nodes[0].getnetworkinfo()['connections'] != 0:
67 # Wait a bit for all sockets to close
68 assert timeout > 0, 'not all connections closed in time'
69 timeout -= 0.1
70 time.sleep(0.1)
72 self.nodes[0].setnetworkactive(True)
73 connect_nodes_bi(self.nodes, 0, 1)
74 assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
75 assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
77 def _test_getaddednodeinfo(self):
78 assert_equal(self.nodes[0].getaddednodeinfo(), [])
79 # add a node (node2) to node0
80 ip_port = "127.0.0.1:{}".format(p2p_port(2))
81 self.nodes[0].addnode(ip_port, 'add')
82 # check that the node has indeed been added
83 added_nodes = self.nodes[0].getaddednodeinfo(ip_port)
84 assert_equal(len(added_nodes), 1)
85 assert_equal(added_nodes[0]['addednode'], ip_port)
86 # check that a non-existent node returns an error
87 assert_raises_jsonrpc(-24, "Node has not been added",
88 self.nodes[0].getaddednodeinfo, '1.1.1.1')
90 def _test_getpeerinfo(self):
91 peer_info = [x.getpeerinfo() for x in self.nodes]
92 # check both sides of bidirectional connection between nodes
93 # the address bound to on one side will be the source address for the other node
94 assert_equal(peer_info[0][0]['addrbind'], peer_info[1][0]['addr'])
95 assert_equal(peer_info[1][0]['addrbind'], peer_info[0][0]['addr'])
97 if __name__ == '__main__':
98 NetTest().main()