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.

bip65-cltv-p2p.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 BIP65 (CHECKLOCKTIMEVERIFY).
  6. Connect to a single node.
  7. Mine 2 (version 3) blocks (save the coinbases for later).
  8. Generate 98 more version 3 blocks, verify the node accepts.
  9. Mine 749 version 4 blocks, verify the node accepts.
  10. Check that the new CLTV rules are not enforced on the 750th version 4 block.
  11. Check that the new CLTV rules are enforced on the 751st version 4 block.
  12. Mine 199 new version blocks.
  13. Mine 1 old-version block.
  14. Mine 1 new version block.
  15. Mine 1 old version block, see that the node rejects.
  16. """
  17. from test_framework.test_framework import ComparisonTestFramework
  18. from test_framework.util import *
  19. from test_framework.mininode import CTransaction, NetworkThread
  20. from test_framework.blocktools import create_coinbase, create_block
  21. from test_framework.comptool import TestInstance, TestManager
  22. from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP
  23. from io import BytesIO
  24. import time
  25. def cltv_invalidate(tx):
  26. '''Modify the signature in vin 0 of the tx to fail CLTV
  27. Prepends -1 CLTV DROP in the scriptSig itself.
  28. '''
  29. tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
  30. list(CScript(tx.vin[0].scriptSig)))
  31. class BIP65Test(ComparisonTestFramework):
  32. def __init__(self):
  33. super().__init__()
  34. self.num_nodes = 1
  35. def setup_network(self):
  36. # Must set the blockversion for this test
  37. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
  38. extra_args=[['-whitelist=127.0.0.1', '-blockversion=3']],
  39. binary=[self.options.testbinary])
  40. def run_test(self):
  41. test = TestManager(self, self.options.tmpdir)
  42. test.add_all_connections(self.nodes)
  43. NetworkThread().start() # Start up network handling in another thread
  44. test.run()
  45. def create_transaction(self, node, coinbase, to_address, amount):
  46. from_txid = node.getblock(coinbase)['tx'][0]
  47. inputs = [{ "txid" : from_txid, "vout" : 0}]
  48. outputs = { to_address : amount }
  49. rawtx = node.createrawtransaction(inputs, outputs)
  50. signresult = node.signrawtransaction(rawtx)
  51. tx = CTransaction()
  52. f = BytesIO(hex_str_to_bytes(signresult['hex']))
  53. tx.deserialize(f)
  54. return tx
  55. def get_tests(self):
  56. self.coinbase_blocks = self.nodes[0].generate(2)
  57. height = 3 # height of the next block to build
  58. self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
  59. self.nodeaddress = self.nodes[0].getnewaddress()
  60. self.last_block_time = int(time.time())
  61. ''' 398 more version 3 blocks '''
  62. test_blocks = []
  63. for i in range(398):
  64. block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
  65. block.nVersion = 3
  66. block.rehash()
  67. block.solve()
  68. test_blocks.append([block, True])
  69. self.last_block_time += 1
  70. self.tip = block.sha256
  71. height += 1
  72. yield TestInstance(test_blocks, sync_every_block=False)
  73. ''' Mine 749 version 4 blocks '''
  74. test_blocks = []
  75. for i in range(749):
  76. block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
  77. block.nVersion = 4
  78. block.rehash()
  79. block.solve()
  80. test_blocks.append([block, True])
  81. self.last_block_time += 1
  82. self.tip = block.sha256
  83. height += 1
  84. yield TestInstance(test_blocks, sync_every_block=False)
  85. '''
  86. Check that the new CLTV rules are not enforced in the 750th
  87. version 3 block.
  88. '''
  89. spendtx = self.create_transaction(self.nodes[0],
  90. self.coinbase_blocks[0], self.nodeaddress, 1.0)
  91. cltv_invalidate(spendtx)
  92. spendtx.rehash()
  93. block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
  94. block.nVersion = 4
  95. block.vtx.append(spendtx)
  96. block.hashMerkleRoot = block.calc_merkle_root()
  97. block.rehash()
  98. block.solve()
  99. self.last_block_time += 1
  100. self.tip = block.sha256
  101. height += 1
  102. yield TestInstance([[block, True]])
  103. ''' Mine 199 new version blocks on last valid tip '''
  104. test_blocks = []
  105. for i in range(199):
  106. block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
  107. block.nVersion = 4
  108. block.rehash()
  109. block.solve()
  110. test_blocks.append([block, True])
  111. self.last_block_time += 1
  112. self.tip = block.sha256
  113. height += 1
  114. yield TestInstance(test_blocks, sync_every_block=False)
  115. ''' Mine 1 old version block '''
  116. block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
  117. block.nVersion = 3
  118. block.rehash()
  119. block.solve()
  120. self.last_block_time += 1
  121. self.tip = block.sha256
  122. height += 1
  123. yield TestInstance([[block, True]])
  124. ''' Mine 1 new version block '''
  125. block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
  126. block.nVersion = 4
  127. block.rehash()
  128. block.solve()
  129. self.last_block_time += 1
  130. self.tip = block.sha256
  131. height += 1
  132. yield TestInstance([[block, True]])
  133. '''
  134. Check that the new CLTV rules are enforced in the 951st version 4
  135. block.
  136. '''
  137. spendtx = self.create_transaction(self.nodes[0],
  138. self.coinbase_blocks[1], self.nodeaddress, 1.0)
  139. cltv_invalidate(spendtx)
  140. spendtx.rehash()
  141. block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
  142. block.nVersion = 4
  143. block.vtx.append(spendtx)
  144. block.hashMerkleRoot = block.calc_merkle_root()
  145. block.rehash()
  146. block.solve()
  147. self.last_block_time += 1
  148. yield TestInstance([[block, False]])
  149. ''' Mine 1 old version block, should be invalid '''
  150. block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
  151. block.nVersion = 3
  152. block.rehash()
  153. block.solve()
  154. self.last_block_time += 1
  155. yield TestInstance([[block, False]])
  156. if __name__ == '__main__':
  157. BIP65Test().main()