doc: spelling fixes
[bitcoinplatinum.git] / test / functional / net.py
blob1e63d38035ae82c6f56b25e80b8d52b90eb427ea
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,
21 class NetTest(BitcoinTestFramework):
22 def __init__(self):
23 super().__init__()
24 self.setup_clean_chain = True
25 self.num_nodes = 2
27 def run_test(self):
28 self._test_connection_count()
29 self._test_getnettotals()
30 self._test_getnetworkinginfo()
31 self._test_getaddednodeinfo()
32 self._test_getpeerinfo()
34 def _test_connection_count(self):
35 # connect_nodes_bi connects each node to the other
36 assert_equal(self.nodes[0].getconnectioncount(), 2)
38 def _test_getnettotals(self):
39 # check that getnettotals totalbytesrecv and totalbytessent
40 # are consistent with getpeerinfo
41 peer_info = self.nodes[0].getpeerinfo()
42 assert_equal(len(peer_info), 2)
43 net_totals = self.nodes[0].getnettotals()
44 assert_equal(sum([peer['bytesrecv'] for peer in peer_info]),
45 net_totals['totalbytesrecv'])
46 assert_equal(sum([peer['bytessent'] for peer in peer_info]),
47 net_totals['totalbytessent'])
48 # test getnettotals and getpeerinfo by doing a ping
49 # the bytes sent/received should change
50 # note ping and pong are 32 bytes each
51 self.nodes[0].ping()
52 time.sleep(0.1)
53 peer_info_after_ping = self.nodes[0].getpeerinfo()
54 net_totals_after_ping = self.nodes[0].getnettotals()
55 for before, after in zip(peer_info, peer_info_after_ping):
56 assert_equal(before['bytesrecv_per_msg']['pong'] + 32, after['bytesrecv_per_msg']['pong'])
57 assert_equal(before['bytessent_per_msg']['ping'] + 32, after['bytessent_per_msg']['ping'])
58 assert_equal(net_totals['totalbytesrecv'] + 32*2, net_totals_after_ping['totalbytesrecv'])
59 assert_equal(net_totals['totalbytessent'] + 32*2, net_totals_after_ping['totalbytessent'])
61 def _test_getnetworkinginfo(self):
62 assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
63 assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
65 self.nodes[0].setnetworkactive(False)
66 assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
67 timeout = 3
68 while self.nodes[0].getnetworkinfo()['connections'] != 0:
69 # Wait a bit for all sockets to close
70 assert timeout > 0, 'not all connections closed in time'
71 timeout -= 0.1
72 time.sleep(0.1)
74 self.nodes[0].setnetworkactive(True)
75 connect_nodes_bi(self.nodes, 0, 1)
76 assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
77 assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
79 def _test_getaddednodeinfo(self):
80 assert_equal(self.nodes[0].getaddednodeinfo(), [])
81 # add a node (node2) to node0
82 ip_port = "127.0.0.1:{}".format(p2p_port(2))
83 self.nodes[0].addnode(ip_port, 'add')
84 # check that the node has indeed been added
85 added_nodes = self.nodes[0].getaddednodeinfo(ip_port)
86 assert_equal(len(added_nodes), 1)
87 assert_equal(added_nodes[0]['addednode'], ip_port)
88 # check that a non-existent node returns an error
89 assert_raises_jsonrpc(-24, "Node has not been added",
90 self.nodes[0].getaddednodeinfo, '1.1.1.1')
92 def _test_getpeerinfo(self):
93 peer_info = [x.getpeerinfo() for x in self.nodes]
94 # check both sides of bidirectional connection between nodes
95 # the address bound to on one side will be the source address for the other node
96 assert_equal(peer_info[0][0]['addrbind'], peer_info[1][0]['addr'])
97 assert_equal(peer_info[1][0]['addrbind'], peer_info[0][0]['addr'])
99 if __name__ == '__main__':
100 NetTest().main()