Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

p2p-timeouts.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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(SingleNodeConnCB):
  25. def __init__(self):
  26. SingleNodeConnCB.__init__(self)
  27. self.connected = False
  28. self.received_version = False
  29. def on_open(self, conn):
  30. self.connected = True
  31. def on_close(self, conn):
  32. self.connected = False
  33. def on_version(self, conn, message):
  34. # Don't send a verack in response
  35. self.received_version = True
  36. class TimeoutsTest(BitcoinTestFramework):
  37. def __init__(self):
  38. super().__init__()
  39. self.setup_clean_chain = True
  40. self.num_nodes = 1
  41. def setup_network(self):
  42. self.nodes = []
  43. # Start up node0 to be a version 1, pre-segwit node.
  44. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
  45. def run_test(self):
  46. # Setup the p2p connections and start up the network thread.
  47. self.no_verack_node = TestNode() # never send verack
  48. self.no_version_node = TestNode() # never send version (just ping)
  49. self.no_send_node = TestNode() # never send anything
  50. connections = []
  51. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_verack_node))
  52. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_version_node, send_version=False))
  53. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_send_node, send_version=False))
  54. self.no_verack_node.add_connection(connections[0])
  55. self.no_version_node.add_connection(connections[1])
  56. self.no_send_node.add_connection(connections[2])
  57. NetworkThread().start() # Start up network handling in another thread
  58. sleep(1)
  59. assert(self.no_verack_node.connected)
  60. assert(self.no_version_node.connected)
  61. assert(self.no_send_node.connected)
  62. ping_msg = msg_ping()
  63. connections[0].send_message(ping_msg)
  64. connections[1].send_message(ping_msg)
  65. sleep(30)
  66. assert(self.no_verack_node.received_version)
  67. assert(self.no_verack_node.connected)
  68. assert(self.no_version_node.connected)
  69. assert(self.no_send_node.connected)
  70. connections[0].send_message(ping_msg)
  71. connections[1].send_message(ping_msg)
  72. sleep(31)
  73. assert(not self.no_verack_node.connected)
  74. assert(not self.no_version_node.connected)
  75. assert(not self.no_send_node.connected)
  76. if __name__ == '__main__':
  77. TimeoutsTest().main()