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-acceptblock.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 processing of unrequested blocks.
  6. Setup: two nodes, node0+node1, not connected to each other. Node1 will have
  7. nMinimumChainWork set to 0x10, so it won't process low-work unrequested blocks.
  8. We have one NodeConn connection to node0 called test_node, and one to node1
  9. called min_work_node.
  10. The test:
  11. 1. Generate one block on each node, to leave IBD.
  12. 2. Mine a new block on each tip, and deliver to each node from node's peer.
  13. The tip should advance for node0, but node1 should skip processing due to
  14. nMinimumChainWork.
  15. Node1 is unused in tests 3-7:
  16. 3. Mine a block that forks from the genesis block, and deliver to test_node.
  17. Node0 should not process this block (just accept the header), because it
  18. is unrequested and doesn't have more or equal work to the tip.
  19. 4a,b. Send another two blocks that build on the forking block.
  20. Node0 should process the second block but be stuck on the shorter chain,
  21. because it's missing an intermediate block.
  22. 4c.Send 288 more blocks on the longer chain (the number of blocks ahead
  23. we currently store).
  24. Node0 should process all but the last block (too far ahead in height).
  25. 5. Send a duplicate of the block in #3 to Node0.
  26. Node0 should not process the block because it is unrequested, and stay on
  27. the shorter chain.
  28. 6. Send Node0 an inv for the height 3 block produced in #4 above.
  29. Node0 should figure out that Node0 has the missing height 2 block and send a
  30. getdata.
  31. 7. Send Node0 the missing block again.
  32. Node0 should process and the tip should advance.
  33. 8. Create a fork which is invalid at a height longer than the current chain
  34. (ie to which the node will try to reorg) but which has headers built on top
  35. of the invalid block. Check that we get disconnected if we send more headers
  36. on the chain the node now knows to be invalid.
  37. 9. Test Node1 is able to sync when connected to node0 (which should have sufficient
  38. work on its chain).
  39. """
  40. from test_framework.mininode import *
  41. from test_framework.test_framework import BitcoinTestFramework
  42. from test_framework.util import *
  43. import time
  44. from test_framework.blocktools import create_block, create_coinbase, create_transaction
  45. class AcceptBlockTest(BitcoinTestFramework):
  46. def add_options(self, parser):
  47. parser.add_option("--testbinary", dest="testbinary",
  48. default=os.getenv("BITCOIND", "bitcoind"),
  49. help="bitcoind binary to test")
  50. def set_test_params(self):
  51. self.setup_clean_chain = True
  52. self.num_nodes = 2
  53. self.extra_args = [[], ["-minimumchainwork=0x10"]]
  54. def setup_network(self):
  55. # Node0 will be used to test behavior of processing unrequested blocks
  56. # from peers which are not whitelisted, while Node1 will be used for
  57. # the whitelisted case.
  58. # Node2 will be used for non-whitelisted peers to test the interaction
  59. # with nMinimumChainWork.
  60. self.setup_nodes()
  61. def run_test(self):
  62. # Setup the p2p connections and start up the network thread.
  63. test_node = NodeConnCB() # connects to node0
  64. min_work_node = NodeConnCB() # connects to node1
  65. connections = []
  66. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node))
  67. connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], min_work_node))
  68. test_node.add_connection(connections[0])
  69. min_work_node.add_connection(connections[1])
  70. NetworkThread().start() # Start up network handling in another thread
  71. # Test logic begins here
  72. test_node.wait_for_verack()
  73. min_work_node.wait_for_verack()
  74. # 1. Have nodes mine a block (leave IBD)
  75. [ n.generate(1) for n in self.nodes ]
  76. tips = [ int("0x" + n.getbestblockhash(), 0) for n in self.nodes ]
  77. # 2. Send one block that builds on each tip.
  78. # This should be accepted by node0
  79. blocks_h2 = [] # the height 2 blocks on each node's chain
  80. block_time = int(time.time()) + 1
  81. for i in range(2):
  82. blocks_h2.append(create_block(tips[i], create_coinbase(2), block_time))
  83. blocks_h2[i].solve()
  84. block_time += 1
  85. test_node.send_message(msg_block(blocks_h2[0]))
  86. min_work_node.send_message(msg_block(blocks_h2[1]))
  87. for x in [test_node, min_work_node]:
  88. x.sync_with_ping()
  89. assert_equal(self.nodes[0].getblockcount(), 2)
  90. assert_equal(self.nodes[1].getblockcount(), 1)
  91. self.log.info("First height 2 block accepted by node0; correctly rejected by node1")
  92. # 3. Send another block that builds on genesis.
  93. block_h1f = create_block(int("0x" + self.nodes[0].getblockhash(0), 0), create_coinbase(1), block_time)
  94. block_time += 1
  95. block_h1f.solve()
  96. test_node.send_message(msg_block(block_h1f))
  97. test_node.sync_with_ping()
  98. tip_entry_found = False
  99. for x in self.nodes[0].getchaintips():
  100. if x['hash'] == block_h1f.hash:
  101. assert_equal(x['status'], "headers-only")
  102. tip_entry_found = True
  103. assert(tip_entry_found)
  104. assert_raises_rpc_error(-1, "Block not found on disk", self.nodes[0].getblock, block_h1f.hash)
  105. # 4. Send another two block that build on the fork.
  106. block_h2f = create_block(block_h1f.sha256, create_coinbase(2), block_time)
  107. block_time += 1
  108. block_h2f.solve()
  109. test_node.send_message(msg_block(block_h2f))
  110. test_node.sync_with_ping()
  111. # Since the earlier block was not processed by node, the new block
  112. # can't be fully validated.
  113. tip_entry_found = False
  114. for x in self.nodes[0].getchaintips():
  115. if x['hash'] == block_h2f.hash:
  116. assert_equal(x['status'], "headers-only")
  117. tip_entry_found = True
  118. assert(tip_entry_found)
  119. # But this block should be accepted by node since it has equal work.
  120. self.nodes[0].getblock(block_h2f.hash)
  121. self.log.info("Second height 2 block accepted, but not reorg'ed to")
  122. # 4b. Now send another block that builds on the forking chain.
  123. block_h3 = create_block(block_h2f.sha256, create_coinbase(3), block_h2f.nTime+1)
  124. block_h3.solve()
  125. test_node.send_message(msg_block(block_h3))
  126. test_node.sync_with_ping()
  127. # Since the earlier block was not processed by node, the new block
  128. # can't be fully validated.
  129. tip_entry_found = False
  130. for x in self.nodes[0].getchaintips():
  131. if x['hash'] == block_h3.hash:
  132. assert_equal(x['status'], "headers-only")
  133. tip_entry_found = True
  134. assert(tip_entry_found)
  135. self.nodes[0].getblock(block_h3.hash)
  136. # But this block should be accepted by node since it has more work.
  137. self.nodes[0].getblock(block_h3.hash)
  138. self.log.info("Unrequested more-work block accepted")
  139. # 4c. Now mine 288 more blocks and deliver; all should be processed but
  140. # the last (height-too-high) on node (as long as its not missing any headers)
  141. tip = block_h3
  142. all_blocks = []
  143. for i in range(288):
  144. next_block = create_block(tip.sha256, create_coinbase(i + 4), tip.nTime+1)
  145. next_block.solve()
  146. all_blocks.append(next_block)
  147. tip = next_block
  148. # Now send the block at height 5 and check that it wasn't accepted (missing header)
  149. test_node.send_message(msg_block(all_blocks[1]))
  150. test_node.sync_with_ping()
  151. assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getblock, all_blocks[1].hash)
  152. assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getblockheader, all_blocks[1].hash)
  153. # The block at height 5 should be accepted if we provide the missing header, though
  154. headers_message = msg_headers()
  155. headers_message.headers.append(CBlockHeader(all_blocks[0]))
  156. test_node.send_message(headers_message)
  157. test_node.send_message(msg_block(all_blocks[1]))
  158. test_node.sync_with_ping()
  159. self.nodes[0].getblock(all_blocks[1].hash)
  160. # Now send the blocks in all_blocks
  161. for i in range(288):
  162. test_node.send_message(msg_block(all_blocks[i]))
  163. test_node.sync_with_ping()
  164. # Blocks 1-287 should be accepted, block 288 should be ignored because it's too far ahead
  165. for x in all_blocks[:-1]:
  166. self.nodes[0].getblock(x.hash)
  167. assert_raises_rpc_error(-1, "Block not found on disk", self.nodes[0].getblock, all_blocks[-1].hash)
  168. # 5. Test handling of unrequested block on the node that didn't process
  169. # Should still not be processed (even though it has a child that has more
  170. # work).
  171. # The node should have requested the blocks at some point, so
  172. # disconnect/reconnect first
  173. connections[0].disconnect_node()
  174. test_node.wait_for_disconnect()
  175. test_node = NodeConnCB() # connects to node (not whitelisted)
  176. connections[0] = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node)
  177. test_node.add_connection(connections[0])
  178. test_node.wait_for_verack()
  179. test_node.send_message(msg_block(block_h1f))
  180. test_node.sync_with_ping()
  181. assert_equal(self.nodes[0].getblockcount(), 2)
  182. self.log.info("Unrequested block that would complete more-work chain was ignored")
  183. # 6. Try to get node to request the missing block.
  184. # Poke the node with an inv for block at height 3 and see if that
  185. # triggers a getdata on block 2 (it should if block 2 is missing).
  186. with mininode_lock:
  187. # Clear state so we can check the getdata request
  188. test_node.last_message.pop("getdata", None)
  189. test_node.send_message(msg_inv([CInv(2, block_h3.sha256)]))
  190. test_node.sync_with_ping()
  191. with mininode_lock:
  192. getdata = test_node.last_message["getdata"]
  193. # Check that the getdata includes the right block
  194. assert_equal(getdata.inv[0].hash, block_h1f.sha256)
  195. self.log.info("Inv at tip triggered getdata for unprocessed block")
  196. # 7. Send the missing block for the third time (now it is requested)
  197. test_node.send_message(msg_block(block_h1f))
  198. test_node.sync_with_ping()
  199. assert_equal(self.nodes[0].getblockcount(), 290)
  200. self.nodes[0].getblock(all_blocks[286].hash)
  201. assert_equal(self.nodes[0].getbestblockhash(), all_blocks[286].hash)
  202. assert_raises_rpc_error(-1, "Block not found on disk", self.nodes[0].getblock, all_blocks[287].hash)
  203. self.log.info("Successfully reorged to longer chain from non-whitelisted peer")
  204. # 8. Create a chain which is invalid at a height longer than the
  205. # current chain, but which has more blocks on top of that
  206. block_289f = create_block(all_blocks[284].sha256, create_coinbase(289), all_blocks[284].nTime+1)
  207. block_289f.solve()
  208. block_290f = create_block(block_289f.sha256, create_coinbase(290), block_289f.nTime+1)
  209. block_290f.solve()
  210. block_291 = create_block(block_290f.sha256, create_coinbase(291), block_290f.nTime+1)
  211. # block_291 spends a coinbase below maturity!
  212. block_291.vtx.append(create_transaction(block_290f.vtx[0], 0, b"42", 1))
  213. block_291.hashMerkleRoot = block_291.calc_merkle_root()
  214. block_291.solve()
  215. block_292 = create_block(block_291.sha256, create_coinbase(292), block_291.nTime+1)
  216. block_292.solve()
  217. # Now send all the headers on the chain and enough blocks to trigger reorg
  218. headers_message = msg_headers()
  219. headers_message.headers.append(CBlockHeader(block_289f))
  220. headers_message.headers.append(CBlockHeader(block_290f))
  221. headers_message.headers.append(CBlockHeader(block_291))
  222. headers_message.headers.append(CBlockHeader(block_292))
  223. test_node.send_message(headers_message)
  224. test_node.sync_with_ping()
  225. tip_entry_found = False
  226. for x in self.nodes[0].getchaintips():
  227. if x['hash'] == block_292.hash:
  228. assert_equal(x['status'], "headers-only")
  229. tip_entry_found = True
  230. assert(tip_entry_found)
  231. assert_raises_rpc_error(-1, "Block not found on disk", self.nodes[0].getblock, block_292.hash)
  232. test_node.send_message(msg_block(block_289f))
  233. test_node.send_message(msg_block(block_290f))
  234. test_node.sync_with_ping()
  235. self.nodes[0].getblock(block_289f.hash)
  236. self.nodes[0].getblock(block_290f.hash)
  237. test_node.send_message(msg_block(block_291))
  238. # At this point we've sent an obviously-bogus block, wait for full processing
  239. # without assuming whether we will be disconnected or not
  240. try:
  241. # Only wait a short while so the test doesn't take forever if we do get
  242. # disconnected
  243. test_node.sync_with_ping(timeout=1)
  244. except AssertionError:
  245. test_node.wait_for_disconnect()
  246. test_node = NodeConnCB() # connects to node (not whitelisted)
  247. connections[0] = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node)
  248. test_node.add_connection(connections[0])
  249. NetworkThread().start() # Start up network handling in another thread
  250. test_node.wait_for_verack()
  251. # We should have failed reorg and switched back to 290 (but have block 291)
  252. assert_equal(self.nodes[0].getblockcount(), 290)
  253. assert_equal(self.nodes[0].getbestblockhash(), all_blocks[286].hash)
  254. assert_equal(self.nodes[0].getblock(block_291.hash)["confirmations"], -1)
  255. # Now send a new header on the invalid chain, indicating we're forked off, and expect to get disconnected
  256. block_293 = create_block(block_292.sha256, create_coinbase(293), block_292.nTime+1)
  257. block_293.solve()
  258. headers_message = msg_headers()
  259. headers_message.headers.append(CBlockHeader(block_293))
  260. test_node.send_message(headers_message)
  261. test_node.wait_for_disconnect()
  262. # 9. Connect node1 to node0 and ensure it is able to sync
  263. connect_nodes(self.nodes[0], 1)
  264. sync_blocks([self.nodes[0], self.nodes[1]])
  265. self.log.info("Successfully synced nodes 1 and 0")
  266. [ c.disconnect_node() for c in connections ]
  267. if __name__ == '__main__':
  268. AcceptBlockTest().main()