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.

maxuploadtarget.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 behavior of -maxuploadtarget.
  6. * Verify that getdata requests for old blocks (>1week) are dropped
  7. if uploadtarget has been reached.
  8. * Verify that getdata requests for recent blocks are respecteved even
  9. if uploadtarget has been reached.
  10. * Verify that the upload counters are reset after 24 hours.
  11. """
  12. from collections import defaultdict
  13. import time
  14. from test_framework.mininode import *
  15. from test_framework.test_framework import BitcoinTestFramework
  16. from test_framework.util import *
  17. class TestNode(NodeConnCB):
  18. def __init__(self):
  19. super().__init__()
  20. self.block_receive_map = defaultdict(int)
  21. def on_inv(self, conn, message):
  22. pass
  23. def on_block(self, conn, message):
  24. message.block.calc_sha256()
  25. self.block_receive_map[message.block.sha256] += 1
  26. class MaxUploadTest(BitcoinTestFramework):
  27. def set_test_params(self):
  28. self.setup_clean_chain = True
  29. self.num_nodes = 1
  30. self.extra_args = [["-maxuploadtarget=800", "-blockmaxsize=999000"]]
  31. # Cache for utxos, as the listunspent may take a long time later in the test
  32. self.utxo_cache = []
  33. def run_test(self):
  34. # Before we connect anything, we first set the time on the node
  35. # to be in the past, otherwise things break because the CNode
  36. # time counters can't be reset backward after initialization
  37. old_time = int(time.time() - 2*60*60*24*7)
  38. self.nodes[0].setmocktime(old_time)
  39. # Generate some old blocks
  40. self.nodes[0].generate(130)
  41. # test_nodes[0] will only request old blocks
  42. # test_nodes[1] will only request new blocks
  43. # test_nodes[2] will test resetting the counters
  44. test_nodes = []
  45. connections = []
  46. for i in range(3):
  47. test_nodes.append(TestNode())
  48. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
  49. test_nodes[i].add_connection(connections[i])
  50. NetworkThread().start() # Start up network handling in another thread
  51. [x.wait_for_verack() for x in test_nodes]
  52. # Test logic begins here
  53. # Now mine a big block
  54. mine_large_block(self.nodes[0], self.utxo_cache)
  55. # Store the hash; we'll request this later
  56. big_old_block = self.nodes[0].getbestblockhash()
  57. old_block_size = self.nodes[0].getblock(big_old_block, True)['size']
  58. big_old_block = int(big_old_block, 16)
  59. # Advance to two days ago
  60. self.nodes[0].setmocktime(int(time.time()) - 2*60*60*24)
  61. # Mine one more block, so that the prior block looks old
  62. mine_large_block(self.nodes[0], self.utxo_cache)
  63. # We'll be requesting this new block too
  64. big_new_block = self.nodes[0].getbestblockhash()
  65. big_new_block = int(big_new_block, 16)
  66. # test_nodes[0] will test what happens if we just keep requesting the
  67. # the same big old block too many times (expect: disconnect)
  68. getdata_request = msg_getdata()
  69. getdata_request.inv.append(CInv(2, big_old_block))
  70. max_bytes_per_day = 800*1024*1024
  71. daily_buffer = 144 * 4000000
  72. max_bytes_available = max_bytes_per_day - daily_buffer
  73. success_count = max_bytes_available // old_block_size
  74. # 576MB will be reserved for relaying new blocks, so expect this to
  75. # succeed for ~235 tries.
  76. for i in range(success_count):
  77. test_nodes[0].send_message(getdata_request)
  78. test_nodes[0].sync_with_ping()
  79. assert_equal(test_nodes[0].block_receive_map[big_old_block], i+1)
  80. assert_equal(len(self.nodes[0].getpeerinfo()), 3)
  81. # At most a couple more tries should succeed (depending on how long
  82. # the test has been running so far).
  83. for i in range(3):
  84. test_nodes[0].send_message(getdata_request)
  85. test_nodes[0].wait_for_disconnect()
  86. assert_equal(len(self.nodes[0].getpeerinfo()), 2)
  87. self.log.info("Peer 0 disconnected after downloading old block too many times")
  88. # Requesting the current block on test_nodes[1] should succeed indefinitely,
  89. # even when over the max upload target.
  90. # We'll try 800 times
  91. getdata_request.inv = [CInv(2, big_new_block)]
  92. for i in range(800):
  93. test_nodes[1].send_message(getdata_request)
  94. test_nodes[1].sync_with_ping()
  95. assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
  96. self.log.info("Peer 1 able to repeatedly download new block")
  97. # But if test_nodes[1] tries for an old block, it gets disconnected too.
  98. getdata_request.inv = [CInv(2, big_old_block)]
  99. test_nodes[1].send_message(getdata_request)
  100. test_nodes[1].wait_for_disconnect()
  101. assert_equal(len(self.nodes[0].getpeerinfo()), 1)
  102. self.log.info("Peer 1 disconnected after trying to download old block")
  103. self.log.info("Advancing system time on node to clear counters...")
  104. # If we advance the time by 24 hours, then the counters should reset,
  105. # and test_nodes[2] should be able to retrieve the old block.
  106. self.nodes[0].setmocktime(int(time.time()))
  107. test_nodes[2].sync_with_ping()
  108. test_nodes[2].send_message(getdata_request)
  109. test_nodes[2].sync_with_ping()
  110. assert_equal(test_nodes[2].block_receive_map[big_old_block], 1)
  111. self.log.info("Peer 2 able to download old block")
  112. [c.disconnect_node() for c in connections]
  113. #stop and start node 0 with 1MB maxuploadtarget, whitelist 127.0.0.1
  114. self.log.info("Restarting nodes with -whitelist=127.0.0.1")
  115. self.stop_node(0)
  116. self.start_node(0, ["-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"])
  117. #recreate/reconnect a test node
  118. test_nodes = [TestNode()]
  119. connections = [NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[0])]
  120. test_nodes[0].add_connection(connections[0])
  121. NetworkThread().start() # Start up network handling in another thread
  122. test_nodes[0].wait_for_verack()
  123. #retrieve 20 blocks which should be enough to break the 1MB limit
  124. getdata_request.inv = [CInv(2, big_new_block)]
  125. for i in range(20):
  126. test_nodes[0].send_message(getdata_request)
  127. test_nodes[0].sync_with_ping()
  128. assert_equal(test_nodes[0].block_receive_map[big_new_block], i+1)
  129. getdata_request.inv = [CInv(2, big_old_block)]
  130. test_nodes[0].send_and_ping(getdata_request)
  131. assert_equal(len(self.nodes[0].getpeerinfo()), 1) #node is still connected because of the whitelist
  132. self.log.info("Peer still connected after trying to download old block (whitelisted)")
  133. if __name__ == '__main__':
  134. MaxUploadTest().main()