feature_csv_activation.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 CSV soft fork activation. 6 7 This soft fork will activate the following BIPS: 8 BIP 68 - nSequence relative lock times 9 BIP 112 - CHECKSEQUENCEVERIFY 10 BIP 113 - MedianTimePast semantics for nLockTime 11 12 mine 83 blocks whose coinbases will be used to generate inputs for our tests 13 mine 344 blocks and seed block chain with the 83 inputs used for our tests at height 427 14 mine 2 blocks and verify soft fork not yet activated 15 mine 1 block and test that soft fork is activated (rules enforced for next block) 16 Test BIP 113 is enforced 17 Mine 4 blocks so next height is 580 and test BIP 68 is enforced for time and height 18 Mine 1 block so next height is 581 and test BIP 68 now passes time but not height 19 Mine 1 block so next height is 582 and test BIP 68 now passes time and height 20 Test that BIP 112 is enforced 21 22 Various transactions will be used to test that the BIPs rules are not enforced before the soft fork activates 23 And that after the soft fork activates transactions pass and fail as they should according to the rules. 24 For each BIP, transactions of versions 1 and 2 will be tested. 25 ---------------- 26 BIP 113: 27 bip113tx - modify the nLocktime variable 28 29 BIP 68: 30 bip68txs - 16 txs with nSequence relative locktime of 10 with various bits set as per the relative_locktimes below 31 32 BIP 112: 33 bip112txs_vary_nSequence - 16 txs with nSequence relative_locktimes of 10 evaluated against 10 OP_CSV OP_DROP 34 bip112txs_vary_nSequence_9 - 16 txs with nSequence relative_locktimes of 9 evaluated against 10 OP_CSV OP_DROP 35 bip112txs_vary_OP_CSV - 16 txs with nSequence = 10 evaluated against varying {relative_locktimes of 10} OP_CSV OP_DROP 36 bip112txs_vary_OP_CSV_9 - 16 txs with nSequence = 9 evaluated against varying {relative_locktimes of 10} OP_CSV OP_DROP 37 bip112tx_special - test negative argument to OP_CSV 38 bip112tx_emptystack - test empty stack (= no argument) OP_CSV 39 """ 40 from itertools import product 41 import time 42 43 from test_framework.blocktools import ( 44 create_block, 45 ) 46 from test_framework.p2p import P2PDataStore 47 from test_framework.script import ( 48 CScript, 49 OP_CHECKSEQUENCEVERIFY, 50 OP_DROP, 51 ) 52 from test_framework.test_framework import BitcoinTestFramework 53 from test_framework.util import ( 54 assert_equal, 55 softfork_active, 56 ) 57 from test_framework.wallet import ( 58 MiniWallet, 59 MiniWalletMode, 60 ) 61 62 TESTING_TX_COUNT = 83 # Number of testing transactions: 1 BIP113 tx, 16 BIP68 txs, 66 BIP112 txs (see comments above) 63 COINBASE_BLOCK_COUNT = TESTING_TX_COUNT # Number of coinbase blocks we need to generate as inputs for our txs 64 BASE_RELATIVE_LOCKTIME = 10 65 SEQ_DISABLE_FLAG = 1 << 31 66 SEQ_RANDOM_HIGH_BIT = 1 << 25 67 SEQ_TYPE_FLAG = 1 << 22 68 SEQ_RANDOM_LOW_BIT = 1 << 18 69 70 71 def relative_locktime(sdf, srhb, stf, srlb): 72 """Returns a locktime with certain bits set.""" 73 74 locktime = BASE_RELATIVE_LOCKTIME 75 if sdf: 76 locktime |= SEQ_DISABLE_FLAG 77 if srhb: 78 locktime |= SEQ_RANDOM_HIGH_BIT 79 if stf: 80 locktime |= SEQ_TYPE_FLAG 81 if srlb: 82 locktime |= SEQ_RANDOM_LOW_BIT 83 return locktime 84 85 86 def all_rlt_txs(txs): 87 return [tx['tx'] for tx in txs] 88 89 90 CSV_ACTIVATION_HEIGHT = 432 91 92 93 class BIP68_112_113Test(BitcoinTestFramework): 94 def set_test_params(self): 95 self.num_nodes = 1 96 self.setup_clean_chain = True 97 # whitelist peers to speed up tx relay / mempool sync 98 self.noban_tx_relay = True 99 self.extra_args = [[ 100 f'-testactivationheight=csv@{CSV_ACTIVATION_HEIGHT}', 101 ]] 102 103 def create_self_transfer_from_utxo(self, input_tx): 104 utxo = self.miniwallet.get_utxo(txid=input_tx.txid_hex, mark_as_spent=False) 105 tx = self.miniwallet.create_self_transfer(utxo_to_spend=utxo)['tx'] 106 return tx 107 108 def create_bip112special(self, input, txversion): 109 tx = self.create_self_transfer_from_utxo(input) 110 tx.version = txversion 111 self.miniwallet.sign_tx(tx) 112 tx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig))) 113 return tx 114 115 def create_bip112emptystack(self, input, txversion): 116 tx = self.create_self_transfer_from_utxo(input) 117 tx.version = txversion 118 self.miniwallet.sign_tx(tx) 119 tx.vin[0].scriptSig = CScript([OP_CHECKSEQUENCEVERIFY] + list(CScript(tx.vin[0].scriptSig))) 120 return tx 121 122 def send_generic_input_tx(self, coinbases): 123 input_txid = self.nodes[0].getblock(coinbases.pop(), 2)['tx'][0]['txid'] 124 utxo_to_spend = self.miniwallet.get_utxo(txid=input_txid) 125 return self.miniwallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_to_spend)['tx'] 126 127 def create_bip68txs(self, bip68inputs, txversion, locktime_delta=0): 128 """Returns a list of bip68 transactions with different bits set.""" 129 txs = [] 130 assert len(bip68inputs) >= 16 131 for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)): 132 locktime = relative_locktime(sdf, srhb, stf, srlb) 133 tx = self.create_self_transfer_from_utxo(bip68inputs[i]) 134 tx.version = txversion 135 tx.vin[0].nSequence = locktime + locktime_delta 136 self.miniwallet.sign_tx(tx) 137 txs.append({'tx': tx, 'sdf': sdf, 'stf': stf}) 138 139 return txs 140 141 def create_bip112txs(self, bip112inputs, varyOP_CSV, txversion, locktime_delta=0): 142 """Returns a list of bip68 transactions with different bits set.""" 143 txs = [] 144 assert len(bip112inputs) >= 16 145 for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)): 146 locktime = relative_locktime(sdf, srhb, stf, srlb) 147 tx = self.create_self_transfer_from_utxo(bip112inputs[i]) 148 if varyOP_CSV: # if varying OP_CSV, nSequence is fixed 149 tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME + locktime_delta 150 else: # vary nSequence instead, OP_CSV is fixed 151 tx.vin[0].nSequence = locktime + locktime_delta 152 tx.version = txversion 153 self.miniwallet.sign_tx(tx) 154 if varyOP_CSV: 155 tx.vin[0].scriptSig = CScript([locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig))) 156 else: 157 tx.vin[0].scriptSig = CScript([BASE_RELATIVE_LOCKTIME, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(tx.vin[0].scriptSig))) 158 txs.append({'tx': tx, 'sdf': sdf, 'stf': stf}) 159 return txs 160 161 def generate_blocks(self, number): 162 test_blocks = [] 163 for _ in range(number): 164 block = self.create_test_block([]) 165 test_blocks.append(block) 166 self.last_block_time += 600 167 self.tip = block.hash_int 168 self.tipheight += 1 169 return test_blocks 170 171 def create_test_block(self, txs): 172 block = create_block(self.tip, height=self.tipheight + 1, ntime=self.last_block_time + 600, txlist=txs) 173 block.solve() 174 return block 175 176 def send_blocks(self, blocks, success=True, reject_reason=None): 177 """Sends blocks to test node. Syncs and verifies that tip has advanced to most recent block. 178 179 Call with success = False if the tip shouldn't advance to the most recent block.""" 180 self.helper_peer.send_blocks_and_test(blocks, self.nodes[0], success=success, reject_reason=reject_reason) 181 182 def run_test(self): 183 self.helper_peer = self.nodes[0].add_p2p_connection(P2PDataStore()) 184 self.miniwallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_P2PK) 185 186 self.log.info("Generate blocks in the past for coinbase outputs.") 187 long_past_time = int(time.time()) - 600 * 1000 # enough to build up to 1000 blocks 10 minutes apart without worrying about getting into the future 188 self.nodes[0].setmocktime(long_past_time - 100) # enough so that the generated blocks will still all be before long_past_time 189 self.coinbase_blocks = self.generate(self.miniwallet, COINBASE_BLOCK_COUNT) # blocks generated for inputs 190 self.nodes[0].setmocktime(0) # set time back to present so yielded blocks aren't in the future as we advance last_block_time 191 self.tipheight = COINBASE_BLOCK_COUNT # height of the next block to build 192 self.last_block_time = long_past_time 193 self.tip = int(self.nodes[0].getbestblockhash(), 16) 194 195 # Activation height is hardcoded 196 # We advance to block height five below BIP112 activation for the following tests 197 test_blocks = self.generate_blocks(CSV_ACTIVATION_HEIGHT - 5 - COINBASE_BLOCK_COUNT) 198 self.send_blocks(test_blocks) 199 assert not softfork_active(self.nodes[0], 'csv') 200 201 # Inputs at height = 431 202 # 203 # Put inputs for all tests in the chain at height 431 (tip now = 430) (time increases by 600s per block) 204 # Note we reuse inputs for v1 and v2 txs so must test these separately 205 # 16 normal inputs 206 bip68inputs = [] 207 for _ in range(16): 208 bip68inputs.append(self.send_generic_input_tx(self.coinbase_blocks)) 209 210 # 2 sets of 16 inputs with 10 OP_CSV OP_DROP (actually will be prepended to spending scriptSig) 211 bip112basicinputs = [] 212 for _ in range(2): 213 inputs = [] 214 for _ in range(16): 215 inputs.append(self.send_generic_input_tx(self.coinbase_blocks)) 216 bip112basicinputs.append(inputs) 217 218 # 2 sets of 16 varied inputs with (relative_lock_time) OP_CSV OP_DROP (actually will be prepended to spending scriptSig) 219 bip112diverseinputs = [] 220 for _ in range(2): 221 inputs = [] 222 for _ in range(16): 223 inputs.append(self.send_generic_input_tx(self.coinbase_blocks)) 224 bip112diverseinputs.append(inputs) 225 226 # 1 special input with -1 OP_CSV OP_DROP (actually will be prepended to spending scriptSig) 227 bip112specialinput = self.send_generic_input_tx(self.coinbase_blocks) 228 # 1 special input with (empty stack) OP_CSV (actually will be prepended to spending scriptSig) 229 bip112emptystackinput = self.send_generic_input_tx(self.coinbase_blocks) 230 231 # 1 normal input 232 bip113input = self.send_generic_input_tx(self.coinbase_blocks) 233 234 self.nodes[0].setmocktime(self.last_block_time + 600) 235 inputblockhash = self.generate(self.nodes[0], 1)[0] # 1 block generated for inputs to be in chain at height 431 236 self.nodes[0].setmocktime(0) 237 self.tip = int(inputblockhash, 16) 238 self.tipheight += 1 239 self.last_block_time += 600 240 assert_equal(len(self.nodes[0].getblock(inputblockhash, True)["tx"]), TESTING_TX_COUNT + 1) 241 242 # 2 more version 4 blocks 243 test_blocks = self.generate_blocks(2) 244 self.send_blocks(test_blocks) 245 246 assert_equal(self.tipheight, CSV_ACTIVATION_HEIGHT - 2) 247 self.log.info(f"Height = {self.tipheight}, CSV not yet active (will activate for block {CSV_ACTIVATION_HEIGHT}, not {CSV_ACTIVATION_HEIGHT - 1})") 248 assert not softfork_active(self.nodes[0], 'csv') 249 250 # Test both version 1 and version 2 transactions for all tests 251 # BIP113 test transaction will be modified before each use to put in appropriate block time 252 bip113tx_v1 = self.create_self_transfer_from_utxo(bip113input) 253 bip113tx_v1.vin[0].nSequence = 0xFFFFFFFE 254 bip113tx_v1.version = 1 255 bip113tx_v2 = self.create_self_transfer_from_utxo(bip113input) 256 bip113tx_v2.vin[0].nSequence = 0xFFFFFFFE 257 bip113tx_v2.version = 2 258 259 # For BIP68 test all 16 relative sequence locktimes 260 bip68txs_v1 = self.create_bip68txs(bip68inputs, 1) 261 bip68txs_v2 = self.create_bip68txs(bip68inputs, 2) 262 263 # For BIP112 test: 264 # 16 relative sequence locktimes of 10 against 10 OP_CSV OP_DROP inputs 265 bip112txs_vary_nSequence_v1 = self.create_bip112txs(bip112basicinputs[0], False, 1) 266 bip112txs_vary_nSequence_v2 = self.create_bip112txs(bip112basicinputs[0], False, 2) 267 # 16 relative sequence locktimes of 9 against 10 OP_CSV OP_DROP inputs 268 bip112txs_vary_nSequence_9_v1 = self.create_bip112txs(bip112basicinputs[1], False, 1, -1) 269 bip112txs_vary_nSequence_9_v2 = self.create_bip112txs(bip112basicinputs[1], False, 2, -1) 270 # sequence lock time of 10 against 16 (relative_lock_time) OP_CSV OP_DROP inputs 271 bip112txs_vary_OP_CSV_v1 = self.create_bip112txs(bip112diverseinputs[0], True, 1) 272 bip112txs_vary_OP_CSV_v2 = self.create_bip112txs(bip112diverseinputs[0], True, 2) 273 # sequence lock time of 9 against 16 (relative_lock_time) OP_CSV OP_DROP inputs 274 bip112txs_vary_OP_CSV_9_v1 = self.create_bip112txs(bip112diverseinputs[1], True, 1, -1) 275 bip112txs_vary_OP_CSV_9_v2 = self.create_bip112txs(bip112diverseinputs[1], True, 2, -1) 276 # -1 OP_CSV OP_DROP input 277 bip112tx_special_v1 = self.create_bip112special(bip112specialinput, 1) 278 bip112tx_special_v2 = self.create_bip112special(bip112specialinput, 2) 279 # (empty stack) OP_CSV input 280 bip112tx_emptystack_v1 = self.create_bip112emptystack(bip112emptystackinput, 1) 281 bip112tx_emptystack_v2 = self.create_bip112emptystack(bip112emptystackinput, 2) 282 283 self.log.info("TESTING") 284 285 self.log.info("Pre-Soft Fork Tests. All txs should pass.") 286 self.log.info("Test version 1 txs") 287 288 success_txs = [] 289 # BIP113 tx, -1 CSV tx and empty stack CSV tx should succeed 290 bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block 291 self.miniwallet.sign_tx(bip113tx_v1) 292 success_txs.append(bip113tx_v1) 293 success_txs.append(bip112tx_special_v1) 294 success_txs.append(bip112tx_emptystack_v1) 295 # add BIP 68 txs 296 success_txs.extend(all_rlt_txs(bip68txs_v1)) 297 # add BIP 112 with seq=10 txs 298 success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_v1)) 299 success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_v1)) 300 # try BIP 112 with seq=9 txs 301 success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v1)) 302 success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_9_v1)) 303 self.send_blocks([self.create_test_block(success_txs)]) 304 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 305 306 self.log.info("Test version 2 txs") 307 308 success_txs = [] 309 # BIP113 tx, -1 CSV tx and empty stack CSV tx should succeed 310 bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block 311 self.miniwallet.sign_tx(bip113tx_v2) 312 success_txs.append(bip113tx_v2) 313 success_txs.append(bip112tx_special_v2) 314 success_txs.append(bip112tx_emptystack_v2) 315 # add BIP 68 txs 316 success_txs.extend(all_rlt_txs(bip68txs_v2)) 317 # add BIP 112 with seq=10 txs 318 success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_v2)) 319 success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_v2)) 320 # try BIP 112 with seq=9 txs 321 success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v2)) 322 success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_9_v2)) 323 self.send_blocks([self.create_test_block(success_txs)]) 324 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 325 326 # 1 more version 4 block to get us to height 432 so the fork should now be active for the next block 327 assert not softfork_active(self.nodes[0], 'csv') 328 test_blocks = self.generate_blocks(1) 329 self.send_blocks(test_blocks) 330 assert softfork_active(self.nodes[0], 'csv') 331 332 self.log.info("Post-Soft Fork Tests.") 333 334 self.log.info("BIP 113 tests") 335 # BIP 113 tests should now fail regardless of version number if nLockTime isn't satisfied by new rules 336 bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block 337 self.miniwallet.sign_tx(bip113tx_v1) 338 bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block 339 self.miniwallet.sign_tx(bip113tx_v2) 340 for bip113tx in [bip113tx_v1, bip113tx_v2]: 341 self.send_blocks([self.create_test_block([bip113tx])], success=False, reject_reason='bad-txns-nonfinal') 342 343 # BIP 113 tests should now pass if the locktime is < MTP 344 bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block 345 self.miniwallet.sign_tx(bip113tx_v1) 346 bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block 347 self.miniwallet.sign_tx(bip113tx_v2) 348 for bip113tx in [bip113tx_v1, bip113tx_v2]: 349 self.send_blocks([self.create_test_block([bip113tx])]) 350 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 351 352 # Next block height = 437 after 4 blocks of random version 353 test_blocks = self.generate_blocks(4) 354 self.send_blocks(test_blocks) 355 356 self.log.info("BIP 68 tests") 357 self.log.info("Test version 1 txs - all should still pass") 358 359 success_txs = [] 360 success_txs.extend(all_rlt_txs(bip68txs_v1)) 361 self.send_blocks([self.create_test_block(success_txs)]) 362 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 363 364 self.log.info("Test version 2 txs") 365 366 # All txs with SEQUENCE_LOCKTIME_DISABLE_FLAG set pass 367 bip68success_txs = [tx['tx'] for tx in bip68txs_v2 if tx['sdf']] 368 self.send_blocks([self.create_test_block(bip68success_txs)]) 369 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 370 371 # All txs without flag fail as we are at delta height = 8 < 10 and delta time = 8 * 600 < 10 * 512 372 bip68timetxs = [tx['tx'] for tx in bip68txs_v2 if not tx['sdf'] and tx['stf']] 373 for tx in bip68timetxs: 374 self.send_blocks([self.create_test_block([tx])], success=False, reject_reason='bad-txns-nonfinal') 375 376 bip68heighttxs = [tx['tx'] for tx in bip68txs_v2 if not tx['sdf'] and not tx['stf']] 377 for tx in bip68heighttxs: 378 self.send_blocks([self.create_test_block([tx])], success=False, reject_reason='bad-txns-nonfinal') 379 380 # Advance one block to 438 381 test_blocks = self.generate_blocks(1) 382 self.send_blocks(test_blocks) 383 384 # Height txs should fail and time txs should now pass 9 * 600 > 10 * 512 385 bip68success_txs.extend(bip68timetxs) 386 self.send_blocks([self.create_test_block(bip68success_txs)]) 387 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 388 for tx in bip68heighttxs: 389 self.send_blocks([self.create_test_block([tx])], success=False, reject_reason='bad-txns-nonfinal') 390 391 # Advance one block to 439 392 test_blocks = self.generate_blocks(1) 393 self.send_blocks(test_blocks) 394 395 # All BIP 68 txs should pass 396 bip68success_txs.extend(bip68heighttxs) 397 self.send_blocks([self.create_test_block(bip68success_txs)]) 398 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 399 400 self.log.info("BIP 112 tests") 401 self.log.info("Test version 1 txs") 402 403 # -1 OP_CSV tx and (empty stack) OP_CSV tx should fail 404 self.send_blocks([self.create_test_block([bip112tx_special_v1])], success=False, 405 reject_reason='block-script-verify-flag-failed (Negative locktime)') 406 self.send_blocks([self.create_test_block([bip112tx_emptystack_v1])], success=False, 407 reject_reason='block-script-verify-flag-failed (Operation not valid with the current stack size)') 408 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 1 txs should still pass 409 410 success_txs = [tx['tx'] for tx in bip112txs_vary_OP_CSV_v1 if tx['sdf']] 411 success_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_9_v1 if tx['sdf']] 412 self.send_blocks([self.create_test_block(success_txs)]) 413 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 414 415 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV, version 1 txs should now fail 416 fail_txs = all_rlt_txs(bip112txs_vary_nSequence_v1) 417 fail_txs += all_rlt_txs(bip112txs_vary_nSequence_9_v1) 418 fail_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_v1 if not tx['sdf']] 419 fail_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_9_v1 if not tx['sdf']] 420 for tx in fail_txs: 421 self.send_blocks([self.create_test_block([tx])], success=False, 422 reject_reason='block-script-verify-flag-failed (Locktime requirement not satisfied)') 423 424 self.log.info("Test version 2 txs") 425 426 # -1 OP_CSV tx and (empty stack) OP_CSV tx should fail 427 self.send_blocks([self.create_test_block([bip112tx_special_v2])], success=False, 428 reject_reason='block-script-verify-flag-failed (Negative locktime)') 429 self.send_blocks([self.create_test_block([bip112tx_emptystack_v2])], success=False, 430 reject_reason='block-script-verify-flag-failed (Operation not valid with the current stack size)') 431 432 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 2 txs should pass (all sequence locks are met) 433 success_txs = [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if tx['sdf']] 434 success_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_9_v2 if tx['sdf']] 435 436 self.send_blocks([self.create_test_block(success_txs)]) 437 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 438 439 # SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV for all remaining txs ## 440 441 # All txs with nSequence 9 should fail either due to earlier mismatch or failing the CSV check 442 fail_txs = all_rlt_txs(bip112txs_vary_nSequence_9_v2) 443 fail_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_9_v2 if not tx['sdf']] 444 for tx in fail_txs: 445 self.send_blocks([self.create_test_block([tx])], success=False, 446 reject_reason='block-script-verify-flag-failed (Locktime requirement not satisfied)') 447 448 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in nSequence, tx should fail 449 fail_txs = [tx['tx'] for tx in bip112txs_vary_nSequence_v2 if tx['sdf']] 450 for tx in fail_txs: 451 self.send_blocks([self.create_test_block([tx])], success=False, 452 reject_reason='block-script-verify-flag-failed (Locktime requirement not satisfied)') 453 454 # If sequencelock types mismatch, tx should fail 455 fail_txs = [tx['tx'] for tx in bip112txs_vary_nSequence_v2 if not tx['sdf'] and tx['stf']] 456 fail_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if not tx['sdf'] and tx['stf']] 457 for tx in fail_txs: 458 self.send_blocks([self.create_test_block([tx])], success=False, 459 reject_reason='block-script-verify-flag-failed (Locktime requirement not satisfied)') 460 461 # Remaining txs should pass, just test masking works properly 462 success_txs = [tx['tx'] for tx in bip112txs_vary_nSequence_v2 if not tx['sdf'] and not tx['stf']] 463 success_txs += [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if not tx['sdf'] and not tx['stf']] 464 self.send_blocks([self.create_test_block(success_txs)]) 465 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 466 467 # Additional test, of checking that comparison of two time types works properly 468 time_txs = [] 469 for tx in [tx['tx'] for tx in bip112txs_vary_OP_CSV_v2 if not tx['sdf'] and tx['stf']]: 470 tx.vin[0].nSequence = BASE_RELATIVE_LOCKTIME | SEQ_TYPE_FLAG 471 self.miniwallet.sign_tx(tx) 472 time_txs.append(tx) 473 474 self.send_blocks([self.create_test_block(time_txs)]) 475 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 476 477 478 if __name__ == '__main__': 479 BIP68_112_113Test(__file__).main()