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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. Test that the CHECKLOCKTIMEVERIFY soft-fork activates at (regtest) block height
  7. 1351.
  8. """
  9. from test_framework.test_framework import BitcoinTestFramework
  10. from test_framework.util import *
  11. from test_framework.mininode import *
  12. from test_framework.blocktools import create_coinbase, create_block
  13. from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP, CScriptNum
  14. from io import BytesIO
  15. CLTV_HEIGHT = 1351
  16. # Reject codes that we might receive in this test
  17. REJECT_INVALID = 16
  18. REJECT_OBSOLETE = 17
  19. REJECT_NONSTANDARD = 64
  20. def cltv_invalidate(tx):
  21. '''Modify the signature in vin 0 of the tx to fail CLTV
  22. Prepends -1 CLTV DROP in the scriptSig itself.
  23. TODO: test more ways that transactions using CLTV could be invalid (eg
  24. locktime requirements fail, sequence time requirements fail, etc).
  25. '''
  26. tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
  27. list(CScript(tx.vin[0].scriptSig)))
  28. def cltv_validate(node, tx, height):
  29. '''Modify the signature in vin 0 of the tx to pass CLTV
  30. Prepends <height> CLTV DROP in the scriptSig, and sets
  31. the locktime to height'''
  32. tx.vin[0].nSequence = 0
  33. tx.nLockTime = height
  34. # Need to re-sign, since nSequence and nLockTime changed
  35. signed_result = node.signrawtransaction(ToHex(tx))
  36. new_tx = CTransaction()
  37. new_tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex'])))
  38. new_tx.vin[0].scriptSig = CScript([CScriptNum(height), OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
  39. list(CScript(new_tx.vin[0].scriptSig)))
  40. return new_tx
  41. def create_transaction(node, coinbase, to_address, amount):
  42. from_txid = node.getblock(coinbase)['tx'][0]
  43. inputs = [{ "txid" : from_txid, "vout" : 0}]
  44. outputs = { to_address : amount }
  45. rawtx = node.createrawtransaction(inputs, outputs)
  46. signresult = node.signrawtransaction(rawtx)
  47. tx = CTransaction()
  48. tx.deserialize(BytesIO(hex_str_to_bytes(signresult['hex'])))
  49. return tx
  50. class BIP65Test(BitcoinTestFramework):
  51. def set_test_params(self):
  52. self.num_nodes = 1
  53. self.extra_args = [['-promiscuousmempoolflags=1', '-whitelist=127.0.0.1']]
  54. self.setup_clean_chain = True
  55. def run_test(self):
  56. node0 = NodeConnCB()
  57. connections = []
  58. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], node0))
  59. node0.add_connection(connections[0])
  60. NetworkThread().start() # Start up network handling in another thread
  61. # wait_for_verack ensures that the P2P connection is fully up.
  62. node0.wait_for_verack()
  63. self.log.info("Mining %d blocks", CLTV_HEIGHT - 2)
  64. self.coinbase_blocks = self.nodes[0].generate(CLTV_HEIGHT - 2)
  65. self.nodeaddress = self.nodes[0].getnewaddress()
  66. self.log.info("Test that an invalid-according-to-CLTV transaction can still appear in a block")
  67. spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[0],
  68. self.nodeaddress, 1.0)
  69. cltv_invalidate(spendtx)
  70. spendtx.rehash()
  71. tip = self.nodes[0].getbestblockhash()
  72. block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1
  73. block = create_block(int(tip, 16), create_coinbase(CLTV_HEIGHT - 1), block_time)
  74. block.nVersion = 3
  75. block.vtx.append(spendtx)
  76. block.hashMerkleRoot = block.calc_merkle_root()
  77. block.solve()
  78. node0.send_and_ping(msg_block(block))
  79. assert_equal(self.nodes[0].getbestblockhash(), block.hash)
  80. self.log.info("Test that blocks must now be at least version 4")
  81. tip = block.sha256
  82. block_time += 1
  83. block = create_block(tip, create_coinbase(CLTV_HEIGHT), block_time)
  84. block.nVersion = 3
  85. block.solve()
  86. node0.send_and_ping(msg_block(block))
  87. assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
  88. wait_until(lambda: "reject" in node0.last_message.keys(), lock=mininode_lock)
  89. with mininode_lock:
  90. assert_equal(node0.last_message["reject"].code, REJECT_OBSOLETE)
  91. assert_equal(node0.last_message["reject"].reason, b'bad-version(0x00000003)')
  92. assert_equal(node0.last_message["reject"].data, block.sha256)
  93. del node0.last_message["reject"]
  94. self.log.info("Test that invalid-according-to-cltv transactions cannot appear in a block")
  95. block.nVersion = 4
  96. spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[1],
  97. self.nodeaddress, 1.0)
  98. cltv_invalidate(spendtx)
  99. spendtx.rehash()
  100. # First we show that this tx is valid except for CLTV by getting it
  101. # accepted to the mempool (which we can achieve with
  102. # -promiscuousmempoolflags).
  103. node0.send_and_ping(msg_tx(spendtx))
  104. assert spendtx.hash in self.nodes[0].getrawmempool()
  105. # Now we verify that a block with this transaction is invalid.
  106. block.vtx.append(spendtx)
  107. block.hashMerkleRoot = block.calc_merkle_root()
  108. block.solve()
  109. node0.send_and_ping(msg_block(block))
  110. assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
  111. wait_until(lambda: "reject" in node0.last_message.keys(), lock=mininode_lock)
  112. with mininode_lock:
  113. assert node0.last_message["reject"].code in [REJECT_INVALID, REJECT_NONSTANDARD]
  114. assert_equal(node0.last_message["reject"].data, block.sha256)
  115. if node0.last_message["reject"].code == REJECT_INVALID:
  116. # Generic rejection when a block is invalid
  117. assert_equal(node0.last_message["reject"].reason, b'block-validation-failed')
  118. else:
  119. assert b'Negative locktime' in node0.last_message["reject"].reason
  120. self.log.info("Test that a version 4 block with a valid-according-to-CLTV transaction is accepted")
  121. spendtx = cltv_validate(self.nodes[0], spendtx, CLTV_HEIGHT - 1)
  122. spendtx.rehash()
  123. block.vtx.pop(1)
  124. block.vtx.append(spendtx)
  125. block.hashMerkleRoot = block.calc_merkle_root()
  126. block.solve()
  127. node0.send_and_ping(msg_block(block))
  128. assert_equal(int(self.nodes[0].getbestblockhash(), 16), block.sha256)
  129. if __name__ == '__main__':
  130. BIP65Test().main()