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.

net.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. start_nodes,
  13. connect_nodes_bi,
  14. )
  15. class NetTest(BitcoinTestFramework):
  16. def __init__(self):
  17. super().__init__()
  18. self.setup_clean_chain = True
  19. self.num_nodes = 2
  20. def setup_network(self):
  21. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
  22. connect_nodes_bi(self.nodes, 0, 1)
  23. self.is_network_split = False
  24. self.sync_all()
  25. def run_test(self):
  26. assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
  27. assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2) # bilateral connection
  28. self.nodes[0].setnetworkactive(False)
  29. assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
  30. timeout = 3
  31. while self.nodes[0].getnetworkinfo()['connections'] != 0:
  32. # Wait a bit for all sockets to close
  33. assert timeout > 0, 'not all connections closed in time'
  34. timeout -= 0.1
  35. time.sleep(0.1)
  36. self.nodes[0].setnetworkactive(True)
  37. connect_nodes_bi(self.nodes, 0, 1)
  38. assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
  39. assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
  40. if __name__ == '__main__':
  41. NetTest().main()