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.1KB

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