/ test / functional / feature_cltv.py
feature_cltv.py
  1  #!/usr/bin/env python3
  2  # Copyright (c) 2015-present 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  
  7  Test that the CHECKLOCKTIMEVERIFY soft-fork activates.
  8  """
  9  
 10  from test_framework.blocktools import (
 11      TIME_GENESIS_BLOCK,
 12      create_block,
 13      create_coinbase,
 14  )
 15  from test_framework.messages import (
 16      CTransaction,
 17      SEQUENCE_FINAL,
 18      msg_block,
 19  )
 20  from test_framework.p2p import P2PInterface
 21  from test_framework.script import (
 22      CScript,
 23      CScriptNum,
 24      OP_1NEGATE,
 25      OP_CHECKLOCKTIMEVERIFY,
 26      OP_DROP,
 27  )
 28  from test_framework.test_framework import BitcoinTestFramework
 29  from test_framework.util import assert_equal
 30  from test_framework.wallet import (
 31      MiniWallet,
 32      MiniWalletMode,
 33  )
 34  
 35  
 36  # Helper function to modify a transaction by
 37  # 1) prepending a given script to the scriptSig of vin 0 and
 38  # 2) (optionally) modify the nSequence of vin 0 and the tx's nLockTime
 39  def cltv_modify_tx(tx, prepend_scriptsig, nsequence=None, nlocktime=None):
 40      assert_equal(len(tx.vin), 1)
 41      if nsequence is not None:
 42          tx.vin[0].nSequence = nsequence
 43          tx.nLockTime = nlocktime
 44  
 45      tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(tx.vin[0].scriptSig)))
 46  
 47  
 48  def cltv_invalidate(tx, failure_reason):
 49      # Modify the signature in vin 0 and nSequence/nLockTime of the tx to fail CLTV
 50      #
 51      # According to BIP65, OP_CHECKLOCKTIMEVERIFY can fail due the following reasons:
 52      # 1) the stack is empty
 53      # 2) the top item on the stack is less than 0
 54      # 3) the lock-time type (height vs. timestamp) of the top stack item and the
 55      #    nLockTime field are not the same
 56      # 4) the top stack item is greater than the transaction's nLockTime field
 57      # 5) the nSequence field of the txin is 0xffffffff (SEQUENCE_FINAL)
 58      assert failure_reason in range(5)
 59      scheme = [
 60          # | Script to prepend to scriptSig                  | nSequence  | nLockTime    |
 61          # +-------------------------------------------------+------------+--------------+
 62          [[OP_CHECKLOCKTIMEVERIFY],                            None,       None],
 63          [[OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP],       None,       None],
 64          [[CScriptNum(100), OP_CHECKLOCKTIMEVERIFY, OP_DROP],  0,          TIME_GENESIS_BLOCK],
 65          [[CScriptNum(100), OP_CHECKLOCKTIMEVERIFY, OP_DROP],  0,          50],
 66          [[CScriptNum(50),  OP_CHECKLOCKTIMEVERIFY, OP_DROP],  SEQUENCE_FINAL, 50],
 67      ][failure_reason]
 68  
 69      cltv_modify_tx(tx, prepend_scriptsig=scheme[0], nsequence=scheme[1], nlocktime=scheme[2])
 70  
 71  
 72  def cltv_validate(tx, height):
 73      # Modify the signature in vin 0 and nSequence/nLockTime of the tx to pass CLTV
 74      scheme = [[CScriptNum(height), OP_CHECKLOCKTIMEVERIFY, OP_DROP], 0, height]
 75  
 76      cltv_modify_tx(tx, prepend_scriptsig=scheme[0], nsequence=scheme[1], nlocktime=scheme[2])
 77  
 78  
 79  CLTV_HEIGHT = 111
 80  
 81  
 82  class BIP65Test(BitcoinTestFramework):
 83      def set_test_params(self):
 84          self.num_nodes = 1
 85          # whitelist peers to speed up tx relay / mempool sync
 86          self.noban_tx_relay = True
 87          self.extra_args = [[
 88              f'-testactivationheight=cltv@{CLTV_HEIGHT}',
 89              '-acceptnonstdtxn=1',  # cltv_invalidate is nonstandard
 90          ]]
 91          self.setup_clean_chain = True
 92          self.rpc_timeout = 480
 93  
 94      def test_cltv_info(self, *, is_active):
 95          assert_equal(self.nodes[0].getdeploymentinfo()['deployments']['bip65'], {
 96                  "active": is_active,
 97                  "height": CLTV_HEIGHT,
 98                  "type": "buried",
 99              },
100          )
101  
102      def run_test(self):
103          peer = self.nodes[0].add_p2p_connection(P2PInterface())
104          wallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_OP_TRUE)
105  
106          self.test_cltv_info(is_active=False)
107  
108          self.log.info("Mining %d blocks", CLTV_HEIGHT - 2)
109          self.generate(wallet, 10)
110          self.generate(self.nodes[0], CLTV_HEIGHT - 2 - 10)
111          assert_equal(self.nodes[0].getblockcount(), CLTV_HEIGHT - 2)
112  
113          self.log.info("Test that invalid-according-to-CLTV transactions can still appear in a block")
114  
115          # create one invalid tx per CLTV failure reason (5 in total) and collect them
116          invalid_cltv_txs = []
117          for i in range(5):
118              spendtx = wallet.create_self_transfer()['tx']
119              cltv_invalidate(spendtx, i)
120              invalid_cltv_txs.append(spendtx)
121  
122          tip = self.nodes[0].getbestblockhash()
123          block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1
124          block = create_block(int(tip, 16), create_coinbase(CLTV_HEIGHT - 1), block_time, version=3, txlist=invalid_cltv_txs)
125          block.solve()
126  
127          self.test_cltv_info(is_active=False)  # Not active as of current tip and next block does not need to obey rules
128          peer.send_and_ping(msg_block(block))
129          self.test_cltv_info(is_active=True)  # Not active as of current tip, but next block must obey rules
130          assert_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
131  
132          self.log.info("Test that blocks must now be at least version 4")
133          tip = block.hash_int
134          block_time += 1
135          block = create_block(tip, create_coinbase(CLTV_HEIGHT), block_time, version=3)
136          block.solve()
137  
138          with self.nodes[0].assert_debug_log(expected_msgs=[f'{block.hash_hex}, bad-version(0x00000003)']):
139              peer.send_and_ping(msg_block(block))
140              assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
141              peer.sync_with_ping()
142  
143          self.log.info("Test that invalid-according-to-CLTV transactions cannot appear in a block")
144          block.nVersion = 4
145          block.vtx.append(CTransaction()) # dummy tx after coinbase that will be replaced later
146  
147          # create and test one invalid tx per CLTV failure reason (5 in total)
148          for i in range(5):
149              spendtx = wallet.create_self_transfer()['tx']
150              assert_equal(len(spendtx.vin), 1)
151              coin = spendtx.vin[0]
152              coin_txid = format(coin.prevout.hash, '064x')
153              coin_vout = coin.prevout.n
154              cltv_invalidate(spendtx, i)
155  
156              blk_rej = "block-script-verify-flag-failed"
157              tx_rej = "mempool-script-verify-flag-failed"
158              expected_cltv_reject_reason = [
159                  " (Operation not valid with the current stack size)",
160                  " (Negative locktime)",
161                  " (Locktime requirement not satisfied)",
162                  " (Locktime requirement not satisfied)",
163                  " (Locktime requirement not satisfied)",
164              ][i]
165              # First we show that this tx is valid except for CLTV by getting it
166              # rejected from the mempool for exactly that reason.
167              spendtx_txid = spendtx.txid_hex
168              spendtx_wtxid = spendtx.wtxid_hex
169              assert_equal(
170                  [{
171                      'txid': spendtx_txid,
172                      'wtxid': spendtx_wtxid,
173                      'allowed': False,
174                      'reject-reason': tx_rej + expected_cltv_reject_reason,
175                      'reject-details': tx_rej + expected_cltv_reject_reason + f", input 0 of {spendtx_txid} (wtxid {spendtx_wtxid}), spending {coin_txid}:{coin_vout}"
176                  }],
177                  self.nodes[0].testmempoolaccept(rawtxs=[spendtx.serialize().hex()], maxfeerate=0),
178              )
179  
180              # Now we verify that a block with this transaction is also invalid.
181              block.vtx[1] = spendtx
182              block.hashMerkleRoot = block.calc_merkle_root()
183              block.solve()
184  
185              with self.nodes[0].assert_debug_log(expected_msgs=[f'Block validation error: {blk_rej + expected_cltv_reject_reason}']):
186                  peer.send_and_ping(msg_block(block))
187                  assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
188                  peer.sync_with_ping()
189  
190          self.log.info("Test that a version 4 block with a valid-according-to-CLTV transaction is accepted")
191          cltv_validate(spendtx, CLTV_HEIGHT - 1)
192  
193          block.vtx.pop(1)
194          block.vtx.append(spendtx)
195          block.hashMerkleRoot = block.calc_merkle_root()
196          block.solve()
197  
198          self.test_cltv_info(is_active=True)  # Not active as of current tip, but next block must obey rules
199          peer.send_and_ping(msg_block(block))
200          self.test_cltv_info(is_active=True)  # Active as of current tip
201          assert_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
202  
203  
204  if __name__ == '__main__':
205      BIP65Test(__file__).main()