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.

p2p-timeouts.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2016 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 various net timeouts.
  6. - Create three bitcoind nodes:
  7. no_verack_node - we never send a verack in response to their version
  8. no_version_node - we never send a version (only a ping)
  9. no_send_node - we never send any P2P message.
  10. - Start all three nodes
  11. - Wait 1 second
  12. - Assert that we're connected
  13. - Send a ping to no_verack_node and no_version_node
  14. - Wait 30 seconds
  15. - Assert that we're still connected
  16. - Send a ping to no_verack_node and no_version_node
  17. - Wait 31 seconds
  18. - Assert that we're no longer connected (timeout to receive version/verack is 60 seconds)
  19. """
  20. from time import sleep
  21. from test_framework.mininode import *
  22. from test_framework.test_framework import BitcoinTestFramework
  23. from test_framework.util import *
  24. class TestNode(NodeConnCB):
  25. def on_version(self, conn, message):
  26. # Don't send a verack in response
  27. pass
  28. class TimeoutsTest(BitcoinTestFramework):
  29. def set_test_params(self):
  30. self.setup_clean_chain = True
  31. self.num_nodes = 1
  32. def run_test(self):
  33. # Setup the p2p connections and start up the network thread.
  34. self.no_verack_node = TestNode() # never send verack
  35. self.no_version_node = TestNode() # never send version (just ping)
  36. self.no_send_node = TestNode() # never send anything
  37. connections = []
  38. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_verack_node))
  39. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_version_node, send_version=False))
  40. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_send_node, send_version=False))
  41. self.no_verack_node.add_connection(connections[0])
  42. self.no_version_node.add_connection(connections[1])
  43. self.no_send_node.add_connection(connections[2])
  44. NetworkThread().start() # Start up network handling in another thread
  45. sleep(1)
  46. assert(self.no_verack_node.connected)
  47. assert(self.no_version_node.connected)
  48. assert(self.no_send_node.connected)
  49. ping_msg = msg_ping()
  50. connections[0].send_message(ping_msg)
  51. connections[1].send_message(ping_msg)
  52. sleep(30)
  53. assert "version" in self.no_verack_node.last_message
  54. assert(self.no_verack_node.connected)
  55. assert(self.no_version_node.connected)
  56. assert(self.no_send_node.connected)
  57. connections[0].send_message(ping_msg)
  58. connections[1].send_message(ping_msg)
  59. sleep(31)
  60. assert(not self.no_verack_node.connected)
  61. assert(not self.no_version_node.connected)
  62. assert(not self.no_send_node.connected)
  63. if __name__ == '__main__':
  64. TimeoutsTest().main()