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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_greater_than_or_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. # getnettotals totalbytesrecv and totalbytessent should be
  32. # consistent with getpeerinfo. Since the RPC calls are not atomic,
  33. # and messages might have been recvd or sent between RPC calls, call
  34. # getnettotals before and after and verify that the returned values
  35. # from getpeerinfo are bounded by those values.
  36. net_totals_before = self.nodes[0].getnettotals()
  37. peer_info = self.nodes[0].getpeerinfo()
  38. net_totals_after = self.nodes[0].getnettotals()
  39. assert_equal(len(peer_info), 2)
  40. peers_recv = sum([peer['bytesrecv'] for peer in peer_info])
  41. peers_sent = sum([peer['bytessent'] for peer in peer_info])
  42. assert_greater_than_or_equal(peers_recv, net_totals_before['totalbytesrecv'])
  43. assert_greater_than_or_equal(net_totals_after['totalbytesrecv'], peers_recv)
  44. assert_greater_than_or_equal(peers_sent, net_totals_before['totalbytessent'])
  45. assert_greater_than_or_equal(net_totals_after['totalbytessent'], peers_sent)
  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. wait_until(lambda: (self.nodes[0].getnettotals()['totalbytessent'] >= net_totals_after['totalbytessent'] + 32 * 2), timeout=1)
  51. wait_until(lambda: (self.nodes[0].getnettotals()['totalbytesrecv'] >= net_totals_after['totalbytesrecv'] + 32 * 2), timeout=1)
  52. peer_info_after_ping = self.nodes[0].getpeerinfo()
  53. for before, after in zip(peer_info, peer_info_after_ping):
  54. assert_greater_than_or_equal(after['bytesrecv_per_msg']['pong'], before['bytesrecv_per_msg']['pong'] + 32)
  55. assert_greater_than_or_equal(after['bytessent_per_msg']['ping'], before['bytessent_per_msg']['ping'] + 32)
  56. def _test_getnetworkinginfo(self):
  57. assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
  58. assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
  59. self.nodes[0].setnetworkactive(False)
  60. assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
  61. # Wait a bit for all sockets to close
  62. wait_until(lambda: self.nodes[0].getnetworkinfo()['connections'] == 0, timeout=3)
  63. self.nodes[0].setnetworkactive(True)
  64. connect_nodes_bi(self.nodes, 0, 1)
  65. assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
  66. assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
  67. def _test_getaddednodeinfo(self):
  68. assert_equal(self.nodes[0].getaddednodeinfo(), [])
  69. # add a node (node2) to node0
  70. ip_port = "127.0.0.1:{}".format(p2p_port(2))
  71. self.nodes[0].addnode(ip_port, 'add')
  72. # check that the node has indeed been added
  73. added_nodes = self.nodes[0].getaddednodeinfo(ip_port)
  74. assert_equal(len(added_nodes), 1)
  75. assert_equal(added_nodes[0]['addednode'], ip_port)
  76. # check that a non-existent node returns an error
  77. assert_raises_rpc_error(-24, "Node has not been added", self.nodes[0].getaddednodeinfo, '1.1.1.1')
  78. def _test_getpeerinfo(self):
  79. peer_info = [x.getpeerinfo() for x in self.nodes]
  80. # check both sides of bidirectional connection between nodes
  81. # the address bound to on one side will be the source address for the other node
  82. assert_equal(peer_info[0][0]['addrbind'], peer_info[1][0]['addr'])
  83. assert_equal(peer_info[1][0]['addrbind'], peer_info[0][0]['addr'])
  84. if __name__ == '__main__':
  85. NetTest().main()