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.

maxblocksinflight.py 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2015-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 nodes responses to having many blocks in flight.
  6. In this test we connect to one node over p2p, send it numerous inv's, and
  7. compare the resulting number of getdata requests to a max allowed value. We
  8. test for exceeding 128 blocks in flight, which was the limit an 0.9 client will
  9. reach. [0.10 clients shouldn't request more than 16 from a single peer.]
  10. """
  11. from test_framework.mininode import *
  12. from test_framework.test_framework import BitcoinTestFramework
  13. from test_framework.util import *
  14. MAX_REQUESTS = 128
  15. class TestManager(NodeConnCB):
  16. # set up NodeConnCB callbacks, overriding base class
  17. def on_getdata(self, conn, message):
  18. self.log.debug("got getdata %s" % repr(message))
  19. # Log the requests
  20. for inv in message.inv:
  21. if inv.hash not in self.blockReqCounts:
  22. self.blockReqCounts[inv.hash] = 0
  23. self.blockReqCounts[inv.hash] += 1
  24. def on_close(self, conn):
  25. if not self.disconnectOkay:
  26. raise EarlyDisconnectError(0)
  27. def __init__(self):
  28. NodeConnCB.__init__(self)
  29. def add_new_connection(self, connection):
  30. self.connection = connection
  31. self.blockReqCounts = {}
  32. self.disconnectOkay = False
  33. def run(self):
  34. self.connection.rpc.generate(1) # Leave IBD
  35. numBlocksToGenerate = [8, 16, 128, 1024]
  36. for count in range(len(numBlocksToGenerate)):
  37. current_invs = []
  38. for i in range(numBlocksToGenerate[count]):
  39. current_invs.append(CInv(2, random.randrange(0, 1 << 256)))
  40. if len(current_invs) >= 50000:
  41. self.connection.send_message(msg_inv(current_invs))
  42. current_invs = []
  43. if len(current_invs) > 0:
  44. self.connection.send_message(msg_inv(current_invs))
  45. # Wait and see how many blocks were requested
  46. time.sleep(2)
  47. total_requests = 0
  48. with mininode_lock:
  49. for key in self.blockReqCounts:
  50. total_requests += self.blockReqCounts[key]
  51. if self.blockReqCounts[key] > 1:
  52. raise AssertionError("Error, test failed: block %064x requested more than once" % key)
  53. if total_requests > MAX_REQUESTS:
  54. raise AssertionError("Error, too many blocks (%d) requested" % total_requests)
  55. self.log.info("Round %d: success (total requests: %d)" % (count, total_requests))
  56. self.disconnectOkay = True
  57. self.connection.disconnect_node()
  58. class MaxBlocksInFlightTest(BitcoinTestFramework):
  59. def add_options(self, parser):
  60. parser.add_option("--testbinary", dest="testbinary",
  61. default=os.getenv("BITCOIND", "bitcoind"),
  62. help="Binary to test max block requests behavior")
  63. def __init__(self):
  64. super().__init__()
  65. self.setup_clean_chain = True
  66. self.num_nodes = 1
  67. def setup_network(self):
  68. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
  69. extra_args=[['-whitelist=127.0.0.1']],
  70. binary=[self.options.testbinary])
  71. def run_test(self):
  72. test = TestManager()
  73. # pass log handler through to the test manager object
  74. test.log = self.log
  75. test.add_new_connection(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test))
  76. NetworkThread().start() # Start up network handling in another thread
  77. test.run()
  78. if __name__ == '__main__':
  79. MaxBlocksInFlightTest().main()