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