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.

assumevalid.py 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-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 logic for skipping signature validation on old blocks.
  6. Test logic for skipping signature validation on blocks which we've assumed
  7. valid (https://github.com/bitcoin/bitcoin/pull/9484)
  8. We build a chain that includes and invalid signature for one of the
  9. transactions:
  10. 0: genesis block
  11. 1: block 1 with coinbase transaction output.
  12. 2-101: bury that block with 100 blocks so the coinbase transaction
  13. output can be spent
  14. 102: a block containing a transaction spending the coinbase
  15. transaction output. The transaction has an invalid signature.
  16. 103-2202: bury the bad block with just over two weeks' worth of blocks
  17. (2100 blocks)
  18. Start three nodes:
  19. - node0 has no -assumevalid parameter. Try to sync to block 2202. It will
  20. reject block 102 and only sync as far as block 101
  21. - node1 has -assumevalid set to the hash of block 102. Try to sync to
  22. block 2202. node1 will sync all the way to block 2202.
  23. - node2 has -assumevalid set to the hash of block 102. Try to sync to
  24. block 200. node2 will reject block 102 since it's assumed valid, but it
  25. isn't buried by at least two weeks' work.
  26. """
  27. from test_framework.mininode import *
  28. from test_framework.test_framework import BitcoinTestFramework
  29. from test_framework.util import *
  30. from test_framework.blocktools import create_block, create_coinbase
  31. from test_framework.key import CECKey
  32. from test_framework.script import *
  33. class BaseNode(SingleNodeConnCB):
  34. def __init__(self):
  35. SingleNodeConnCB.__init__(self)
  36. self.last_inv = None
  37. self.last_headers = None
  38. self.last_block = None
  39. self.last_getdata = None
  40. self.block_announced = False
  41. self.last_getheaders = None
  42. self.disconnected = False
  43. self.last_blockhash_announced = None
  44. def on_close(self, conn):
  45. self.disconnected = True
  46. def wait_for_disconnect(self, timeout=60):
  47. test_function = lambda: self.disconnected
  48. assert(wait_until(test_function, timeout=timeout))
  49. return
  50. def send_header_for_blocks(self, new_blocks):
  51. headers_message = msg_headers()
  52. headers_message.headers = [ CBlockHeader(b) for b in new_blocks ]
  53. self.send_message(headers_message)
  54. class SendHeadersTest(BitcoinTestFramework):
  55. def __init__(self):
  56. super().__init__()
  57. self.setup_clean_chain = True
  58. self.num_nodes = 3
  59. def setup_network(self):
  60. # Start node0. We don't start the other nodes yet since
  61. # we need to pre-mine a block with an invalid transaction
  62. # signature so we can pass in the block hash as assumevalid.
  63. self.nodes = []
  64. self.nodes.append(start_node(0, self.options.tmpdir))
  65. def run_test(self):
  66. # Connect to node0
  67. node0 = BaseNode()
  68. connections = []
  69. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], node0))
  70. node0.add_connection(connections[0])
  71. NetworkThread().start() # Start up network handling in another thread
  72. node0.wait_for_verack()
  73. # Build the blockchain
  74. self.tip = int(self.nodes[0].getbestblockhash(), 16)
  75. self.block_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time'] + 1
  76. self.blocks = []
  77. # Get a pubkey for the coinbase TXO
  78. coinbase_key = CECKey()
  79. coinbase_key.set_secretbytes(b"horsebattery")
  80. coinbase_pubkey = coinbase_key.get_pubkey()
  81. # Create the first block with a coinbase output to our key
  82. height = 1
  83. block = create_block(self.tip, create_coinbase(height, coinbase_pubkey), self.block_time)
  84. self.blocks.append(block)
  85. self.block_time += 1
  86. block.solve()
  87. # Save the coinbase for later
  88. self.block1 = block
  89. self.tip = block.sha256
  90. height += 1
  91. # Bury the block 100 deep so the coinbase output is spendable
  92. for i in range(100):
  93. block = create_block(self.tip, create_coinbase(height), self.block_time)
  94. block.solve()
  95. self.blocks.append(block)
  96. self.tip = block.sha256
  97. self.block_time += 1
  98. height += 1
  99. # Create a transaction spending the coinbase output with an invalid (null) signature
  100. tx = CTransaction()
  101. tx.vin.append(CTxIn(COutPoint(self.block1.vtx[0].sha256, 0), scriptSig=b""))
  102. tx.vout.append(CTxOut(49*100000000, CScript([OP_TRUE])))
  103. tx.calc_sha256()
  104. block102 = create_block(self.tip, create_coinbase(height), self.block_time)
  105. self.block_time += 1
  106. block102.vtx.extend([tx])
  107. block102.hashMerkleRoot = block102.calc_merkle_root()
  108. block102.rehash()
  109. block102.solve()
  110. self.blocks.append(block102)
  111. self.tip = block102.sha256
  112. self.block_time += 1
  113. height += 1
  114. # Bury the assumed valid block 2100 deep
  115. for i in range(2100):
  116. block = create_block(self.tip, create_coinbase(height), self.block_time)
  117. block.nVersion = 4
  118. block.solve()
  119. self.blocks.append(block)
  120. self.tip = block.sha256
  121. self.block_time += 1
  122. height += 1
  123. # Start node1 and node2 with assumevalid so they accept a block with a bad signature.
  124. self.nodes.append(start_node(1, self.options.tmpdir,
  125. ["-assumevalid=" + hex(block102.sha256)]))
  126. node1 = BaseNode() # connects to node1
  127. connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], node1))
  128. node1.add_connection(connections[1])
  129. node1.wait_for_verack()
  130. self.nodes.append(start_node(2, self.options.tmpdir,
  131. ["-assumevalid=" + hex(block102.sha256)]))
  132. node2 = BaseNode() # connects to node2
  133. connections.append(NodeConn('127.0.0.1', p2p_port(2), self.nodes[2], node2))
  134. node2.add_connection(connections[2])
  135. node2.wait_for_verack()
  136. # send header lists to all three nodes
  137. node0.send_header_for_blocks(self.blocks[0:2000])
  138. node0.send_header_for_blocks(self.blocks[2000:])
  139. node1.send_header_for_blocks(self.blocks[0:2000])
  140. node1.send_header_for_blocks(self.blocks[2000:])
  141. node2.send_header_for_blocks(self.blocks[0:200])
  142. # Send 102 blocks to node0. Block 102 will be rejected.
  143. for i in range(101):
  144. node0.send_message(msg_block(self.blocks[i]))
  145. node0.sync_with_ping() # make sure the most recent block is synced
  146. node0.send_message(msg_block(self.blocks[101]))
  147. assert_equal(self.nodes[0].getblock(self.nodes[0].getbestblockhash())['height'], 101)
  148. # Send 3102 blocks to node1. All blocks will be accepted.
  149. for i in range(2202):
  150. node1.send_message(msg_block(self.blocks[i]))
  151. node1.sync_with_ping() # make sure the most recent block is synced
  152. assert_equal(self.nodes[1].getblock(self.nodes[1].getbestblockhash())['height'], 2202)
  153. # Send 102 blocks to node2. Block 102 will be rejected.
  154. for i in range(101):
  155. node2.send_message(msg_block(self.blocks[i]))
  156. node2.sync_with_ping() # make sure the most recent block is synced
  157. node2.send_message(msg_block(self.blocks[101]))
  158. assert_equal(self.nodes[2].getblock(self.nodes[2].getbestblockhash())['height'], 101)
  159. if __name__ == '__main__':
  160. SendHeadersTest().main()