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.
12 from test_framework
.test_framework
import BitcoinTestFramework
13 from test_framework
.util
import (
15 assert_raises_jsonrpc
,
22 class NetTest(BitcoinTestFramework
):
25 self
.setup_clean_chain
= True
29 self
._test
_connection
_count
()
30 self
._test
_getnettotals
()
31 self
._test
_getnetworkinginfo
()
32 self
._test
_getaddednodeinfo
()
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
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)
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'
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-existant node returns an error
89 assert_raises_jsonrpc(-24, "Node has not been added",
90 self
.nodes
[0].getaddednodeinfo
, '1.1.1.1')
93 if __name__
== '__main__':