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.

rpc_net.py 4.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.
  6. Tests correspond to code in rpc/net.cpp.
  7. """
  8. from test_framework.test_framework import BitcoinTestFramework
  9. from test_framework.util import (
  10. assert_equal,
  11. assert_raises_rpc_error,
  12. connect_nodes_bi,
  13. p2p_port,
  14. wait_until,
  15. )
  16. class NetTest(BitcoinTestFramework):
  17. def set_test_params(self):
  18. self.setup_clean_chain = True
  19. self.num_nodes = 2
  20. def run_test(self):
  21. self._test_connection_count()
  22. self._test_getnettotals()
  23. self._test_getnetworkinginfo()
  24. self._test_getaddednodeinfo()
  25. self._test_getpeerinfo()
  26. def _test_connection_count(self):
  27. # connect_nodes_bi connects each node to the other
  28. assert_equal(self.nodes[0].getconnectioncount(), 2)
  29. def _test_getnettotals(self):
  30. # check that getnettotals totalbytesrecv and totalbytessent
  31. # are consistent with getpeerinfo
  32. peer_info = self.nodes[0].getpeerinfo()
  33. assert_equal(len(peer_info), 2)
  34. net_totals = self.nodes[0].getnettotals()
  35. assert_equal(sum([peer['bytesrecv'] for peer in peer_info]),
  36. net_totals['totalbytesrecv'])
  37. assert_equal(sum([peer['bytessent'] for peer in peer_info]),
  38. net_totals['totalbytessent'])
  39. # test getnettotals and getpeerinfo by doing a ping
  40. # the bytes sent/received should change
  41. # note ping and pong are 32 bytes each
  42. self.nodes[0].ping()
  43. wait_until(lambda: (net_totals['totalbytessent'] + 32*2) == self.nodes[0].getnettotals()['totalbytessent'], timeout=1)
  44. wait_until(lambda: (net_totals['totalbytesrecv'] + 32*2) == self.nodes[0].getnettotals()['totalbytesrecv'], timeout=1)
  45. peer_info_after_ping = self.nodes[0].getpeerinfo()
  46. for before, after in zip(peer_info, peer_info_after_ping):
  47. assert_equal(before['bytesrecv_per_msg']['pong'] + 32, after['bytesrecv_per_msg']['pong'])
  48. assert_equal(before['bytessent_per_msg']['ping'] + 32, after['bytessent_per_msg']['ping'])
  49. def _test_getnetworkinginfo(self):
  50. assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
  51. assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
  52. self.nodes[0].setnetworkactive(False)
  53. assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
  54. # Wait a bit for all sockets to close
  55. wait_until(lambda: self.nodes[0].getnetworkinfo()['connections'] == 0, timeout=3)
  56. self.nodes[0].setnetworkactive(True)
  57. connect_nodes_bi(self.nodes, 0, 1)
  58. assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
  59. assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
  60. def _test_getaddednodeinfo(self):
  61. assert_equal(self.nodes[0].getaddednodeinfo(), [])
  62. # add a node (node2) to node0
  63. ip_port = "127.0.0.1:{}".format(p2p_port(2))
  64. self.nodes[0].addnode(ip_port, 'add')
  65. # check that the node has indeed been added
  66. added_nodes = self.nodes[0].getaddednodeinfo(ip_port)
  67. assert_equal(len(added_nodes), 1)
  68. assert_equal(added_nodes[0]['addednode'], ip_port)
  69. # check that a non-existent node returns an error
  70. assert_raises_rpc_error(-24, "Node has not been added",
  71. self.nodes[0].getaddednodeinfo, '1.1.1.1')
  72. def _test_getpeerinfo(self):
  73. peer_info = [x.getpeerinfo() for x in self.nodes]
  74. # check both sides of bidirectional connection between nodes
  75. # the address bound to on one side will be the source address for the other node
  76. assert_equal(peer_info[0][0]['addrbind'], peer_info[1][0]['addr'])
  77. assert_equal(peer_info[1][0]['addrbind'], peer_info[0][0]['addr'])
  78. if __name__ == '__main__':
  79. NetTest().main()