Merge #11726: Cleanups + nit fixes for walletdir PR
[bitcoinplatinum.git] / test / functional / bip68-112-113-p2p.py
blobd3c7d8fc1138a1d6fba060bde14b6f17c87f915b
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 activation of the first version bits soft fork.
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
12 regtest lock-in with 108/144 block signalling
13 activation after a further 144 blocks
15 mine 82 blocks whose coinbases will be used to generate inputs for our tests
16 mine 61 blocks to transition from DEFINED to STARTED
17 mine 144 blocks only 100 of which are signaling readiness in order to fail to change state this period
18 mine 144 blocks with 108 signaling and verify STARTED->LOCKED_IN
19 mine 140 blocks and seed block chain with the 82 inputs will use for our tests at height 572
20 mine 3 blocks and verify still at LOCKED_IN and test that enforcement has not triggered
21 mine 1 block and test that enforcement has triggered (which triggers ACTIVE)
22 Test BIP 113 is enforced
23 Mine 4 blocks so next height is 580 and test BIP 68 is enforced for time and height
24 Mine 1 block so next height is 581 and test BIP 68 now passes time but not height
25 Mine 1 block so next height is 582 and test BIP 68 now passes time and height
26 Test that BIP 112 is enforced
28 Various transactions will be used to test that the BIPs rules are not enforced before the soft fork activates
29 And that after the soft fork activates transactions pass and fail as they should according to the rules.
30 For each BIP, transactions of versions 1 and 2 will be tested.
31 ----------------
32 BIP 113:
33 bip113tx - modify the nLocktime variable
35 BIP 68:
36 bip68txs - 16 txs with nSequence relative locktime of 10 with various bits set as per the relative_locktimes below
38 BIP 112:
39 bip112txs_vary_nSequence - 16 txs with nSequence relative_locktimes of 10 evaluated against 10 OP_CSV OP_DROP
40 bip112txs_vary_nSequence_9 - 16 txs with nSequence relative_locktimes of 9 evaluated against 10 OP_CSV OP_DROP
41 bip112txs_vary_OP_CSV - 16 txs with nSequence = 10 evaluated against varying {relative_locktimes of 10} OP_CSV OP_DROP
42 bip112txs_vary_OP_CSV_9 - 16 txs with nSequence = 9 evaluated against varying {relative_locktimes of 10} OP_CSV OP_DROP
43 bip112tx_special - test negative argument to OP_CSV
44 """
46 from test_framework.test_framework import ComparisonTestFramework
47 from test_framework.util import *
48 from test_framework.mininode import ToHex, CTransaction, network_thread_start
49 from test_framework.blocktools import create_coinbase, create_block
50 from test_framework.comptool import TestInstance, TestManager
51 from test_framework.script import *
52 from io import BytesIO
53 import time
55 base_relative_locktime = 10
56 seq_disable_flag = 1<<31
57 seq_random_high_bit = 1<<25
58 seq_type_flag = 1<<22
59 seq_random_low_bit = 1<<18
61 # b31,b25,b22,b18 represent the 31st, 25th, 22nd and 18th bits respectively in the nSequence field
62 # relative_locktimes[b31][b25][b22][b18] is a base_relative_locktime with the indicated bits set if their indices are 1
63 relative_locktimes = []
64 for b31 in range(2):
65 b25times = []
66 for b25 in range(2):
67 b22times = []
68 for b22 in range(2):
69 b18times = []
70 for b18 in range(2):
71 rlt = base_relative_locktime
72 if (b31):
73 rlt = rlt | seq_disable_flag
74 if (b25):
75 rlt = rlt | seq_random_high_bit
76 if (b22):
77 rlt = rlt | seq_type_flag
78 if (b18):
79 rlt = rlt | seq_random_low_bit
80 b18times.append(rlt)
81 b22times.append(b18times)
82 b25times.append(b22times)
83 relative_locktimes.append(b25times)
85 def all_rlt_txs(txarray):
86 txs = []
87 for b31 in range(2):
88 for b25 in range(2):
89 for b22 in range(2):
90 for b18 in range(2):
91 txs.append(txarray[b31][b25][b22][b18])
92 return txs
94 class BIP68_112_113Test(ComparisonTestFramework):
95 def set_test_params(self):
96 self.num_nodes = 1
97 self.setup_clean_chain = True
98 self.extra_args = [['-whitelist=127.0.0.1', '-blockversion=4']]
100 def run_test(self):
101 test = TestManager(self, self.options.tmpdir)
102 test.add_all_connections(self.nodes)
103 network_thread_start()
104 test.run()
106 def send_generic_input_tx(self, node, coinbases):
107 amount = Decimal("49.99")
108 return node.sendrawtransaction(ToHex(self.sign_transaction(node, self.create_transaction(node, node.getblock(coinbases.pop())['tx'][0], self.nodeaddress, amount))))
110 def create_transaction(self, node, txid, to_address, amount):
111 inputs = [{ "txid" : txid, "vout" : 0}]
112 outputs = { to_address : amount }
113 rawtx = node.createrawtransaction(inputs, outputs)
114 tx = CTransaction()
115 f = BytesIO(hex_str_to_bytes(rawtx))
116 tx.deserialize(f)
117 return tx
119 def sign_transaction(self, node, unsignedtx):
120 rawtx = ToHex(unsignedtx)
121 signresult = node.signrawtransaction(rawtx)
122 tx = CTransaction()
123 f = BytesIO(hex_str_to_bytes(signresult['hex']))
124 tx.deserialize(f)
125 return tx
127 def generate_blocks(self, number, version, test_blocks = []):
128 for i in range(number):
129 block = self.create_test_block([], version)
130 test_blocks.append([block, True])
131 self.last_block_time += 600
132 self.tip = block.sha256
133 self.tipheight += 1
134 return test_blocks
136 def create_test_block(self, txs, version = 536870912):
137 block = create_block(self.tip, create_coinbase(self.tipheight + 1), self.last_block_time + 600)
138 block.nVersion = version
139 block.vtx.extend(txs)
140 block.hashMerkleRoot = block.calc_merkle_root()
141 block.rehash()
142 block.solve()
143 return block
145 def create_bip68txs(self, bip68inputs, txversion, locktime_delta = 0):
146 txs = []
147 assert(len(bip68inputs) >= 16)
148 i = 0
149 for b31 in range(2):
150 b25txs = []
151 for b25 in range(2):
152 b22txs = []
153 for b22 in range(2):
154 b18txs = []
155 for b18 in range(2):
156 tx = self.create_transaction(self.nodes[0], bip68inputs[i], self.nodeaddress, Decimal("49.98"))
157 i += 1
158 tx.nVersion = txversion
159 tx.vin[0].nSequence = relative_locktimes[b31][b25][b22][b18] + locktime_delta
160 b18txs.append(self.sign_transaction(self.nodes[0], tx))
161 b22txs.append(b18txs)
162 b25txs.append(b22txs)
163 txs.append(b25txs)
164 return txs
166 def create_bip112special(self, input, txversion):
167 tx = self.create_transaction(self.nodes[0], input, self.nodeaddress, Decimal("49.98"))
168 tx.nVersion = txversion
169 signtx = self.sign_transaction(self.nodes[0], tx)
170 signtx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
171 return signtx
173 def create_bip112txs(self, bip112inputs, varyOP_CSV, txversion, locktime_delta = 0):
174 txs = []
175 assert(len(bip112inputs) >= 16)
176 i = 0
177 for b31 in range(2):
178 b25txs = []
179 for b25 in range(2):
180 b22txs = []
181 for b22 in range(2):
182 b18txs = []
183 for b18 in range(2):
184 tx = self.create_transaction(self.nodes[0], bip112inputs[i], self.nodeaddress, Decimal("49.98"))
185 i += 1
186 if (varyOP_CSV): # if varying OP_CSV, nSequence is fixed
187 tx.vin[0].nSequence = base_relative_locktime + locktime_delta
188 else: # vary nSequence instead, OP_CSV is fixed
189 tx.vin[0].nSequence = relative_locktimes[b31][b25][b22][b18] + locktime_delta
190 tx.nVersion = txversion
191 signtx = self.sign_transaction(self.nodes[0], tx)
192 if (varyOP_CSV):
193 signtx.vin[0].scriptSig = CScript([relative_locktimes[b31][b25][b22][b18], OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
194 else:
195 signtx.vin[0].scriptSig = CScript([base_relative_locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
196 b18txs.append(signtx)
197 b22txs.append(b18txs)
198 b25txs.append(b22txs)
199 txs.append(b25txs)
200 return txs
202 def get_tests(self):
203 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
204 self.nodes[0].setmocktime(long_past_time - 100) # enough so that the generated blocks will still all be before long_past_time
205 self.coinbase_blocks = self.nodes[0].generate(1 + 16 + 2*32 + 1) # 82 blocks generated for inputs
206 self.nodes[0].setmocktime(0) # set time back to present so yielded blocks aren't in the future as we advance last_block_time
207 self.tipheight = 82 # height of the next block to build
208 self.last_block_time = long_past_time
209 self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
210 self.nodeaddress = self.nodes[0].getnewaddress()
212 assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'defined')
213 test_blocks = self.generate_blocks(61, 4)
214 yield TestInstance(test_blocks, sync_every_block=False) # 1
215 # Advanced from DEFINED to STARTED, height = 143
216 assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'started')
218 # Fail to achieve LOCKED_IN 100 out of 144 signal bit 0
219 # using a variety of bits to simulate multiple parallel softforks
220 test_blocks = self.generate_blocks(50, 536870913) # 0x20000001 (signalling ready)
221 test_blocks = self.generate_blocks(20, 4, test_blocks) # 0x00000004 (signalling not)
222 test_blocks = self.generate_blocks(50, 536871169, test_blocks) # 0x20000101 (signalling ready)
223 test_blocks = self.generate_blocks(24, 536936448, test_blocks) # 0x20010000 (signalling not)
224 yield TestInstance(test_blocks, sync_every_block=False) # 2
225 # Failed to advance past STARTED, height = 287
226 assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'started')
228 # 108 out of 144 signal bit 0 to achieve lock-in
229 # using a variety of bits to simulate multiple parallel softforks
230 test_blocks = self.generate_blocks(58, 536870913) # 0x20000001 (signalling ready)
231 test_blocks = self.generate_blocks(26, 4, test_blocks) # 0x00000004 (signalling not)
232 test_blocks = self.generate_blocks(50, 536871169, test_blocks) # 0x20000101 (signalling ready)
233 test_blocks = self.generate_blocks(10, 536936448, test_blocks) # 0x20010000 (signalling not)
234 yield TestInstance(test_blocks, sync_every_block=False) # 3
235 # Advanced from STARTED to LOCKED_IN, height = 431
236 assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'locked_in')
238 # 140 more version 4 blocks
239 test_blocks = self.generate_blocks(140, 4)
240 yield TestInstance(test_blocks, sync_every_block=False) # 4
242 ### Inputs at height = 572
243 # Put inputs for all tests in the chain at height 572 (tip now = 571) (time increases by 600s per block)
244 # Note we reuse inputs for v1 and v2 txs so must test these separately
245 # 16 normal inputs
246 bip68inputs = []
247 for i in range(16):
248 bip68inputs.append(self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
249 # 2 sets of 16 inputs with 10 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
250 bip112basicinputs = []
251 for j in range(2):
252 inputs = []
253 for i in range(16):
254 inputs.append(self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
255 bip112basicinputs.append(inputs)
256 # 2 sets of 16 varied inputs with (relative_lock_time) OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
257 bip112diverseinputs = []
258 for j in range(2):
259 inputs = []
260 for i in range(16):
261 inputs.append(self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
262 bip112diverseinputs.append(inputs)
263 # 1 special input with -1 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
264 bip112specialinput = self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
265 # 1 normal input
266 bip113input = self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
268 self.nodes[0].setmocktime(self.last_block_time + 600)
269 inputblockhash = self.nodes[0].generate(1)[0] # 1 block generated for inputs to be in chain at height 572
270 self.nodes[0].setmocktime(0)
271 self.tip = int("0x" + inputblockhash, 0)
272 self.tipheight += 1
273 self.last_block_time += 600
274 assert_equal(len(self.nodes[0].getblock(inputblockhash,True)["tx"]), 82+1)
276 # 2 more version 4 blocks
277 test_blocks = self.generate_blocks(2, 4)
278 yield TestInstance(test_blocks, sync_every_block=False) # 5
279 # Not yet advanced to ACTIVE, height = 574 (will activate for block 576, not 575)
280 assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'locked_in')
282 # Test both version 1 and version 2 transactions for all tests
283 # BIP113 test transaction will be modified before each use to put in appropriate block time
284 bip113tx_v1 = self.create_transaction(self.nodes[0], bip113input, self.nodeaddress, Decimal("49.98"))
285 bip113tx_v1.vin[0].nSequence = 0xFFFFFFFE
286 bip113tx_v1.nVersion = 1
287 bip113tx_v2 = self.create_transaction(self.nodes[0], bip113input, self.nodeaddress, Decimal("49.98"))
288 bip113tx_v2.vin[0].nSequence = 0xFFFFFFFE
289 bip113tx_v2.nVersion = 2
291 # For BIP68 test all 16 relative sequence locktimes
292 bip68txs_v1 = self.create_bip68txs(bip68inputs, 1)
293 bip68txs_v2 = self.create_bip68txs(bip68inputs, 2)
295 # For BIP112 test:
296 # 16 relative sequence locktimes of 10 against 10 OP_CSV OP_DROP inputs
297 bip112txs_vary_nSequence_v1 = self.create_bip112txs(bip112basicinputs[0], False, 1)
298 bip112txs_vary_nSequence_v2 = self.create_bip112txs(bip112basicinputs[0], False, 2)
299 # 16 relative sequence locktimes of 9 against 10 OP_CSV OP_DROP inputs
300 bip112txs_vary_nSequence_9_v1 = self.create_bip112txs(bip112basicinputs[1], False, 1, -1)
301 bip112txs_vary_nSequence_9_v2 = self.create_bip112txs(bip112basicinputs[1], False, 2, -1)
302 # sequence lock time of 10 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
303 bip112txs_vary_OP_CSV_v1 = self.create_bip112txs(bip112diverseinputs[0], True, 1)
304 bip112txs_vary_OP_CSV_v2 = self.create_bip112txs(bip112diverseinputs[0], True, 2)
305 # sequence lock time of 9 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
306 bip112txs_vary_OP_CSV_9_v1 = self.create_bip112txs(bip112diverseinputs[1], True, 1, -1)
307 bip112txs_vary_OP_CSV_9_v2 = self.create_bip112txs(bip112diverseinputs[1], True, 2, -1)
308 # -1 OP_CSV OP_DROP input
309 bip112tx_special_v1 = self.create_bip112special(bip112specialinput, 1)
310 bip112tx_special_v2 = self.create_bip112special(bip112specialinput, 2)
313 ### TESTING ###
314 ##################################
315 ### Before Soft Forks Activate ###
316 ##################################
317 # All txs should pass
318 ### Version 1 txs ###
319 success_txs = []
320 # add BIP113 tx and -1 CSV tx
321 bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
322 bip113signed1 = self.sign_transaction(self.nodes[0], bip113tx_v1)
323 success_txs.append(bip113signed1)
324 success_txs.append(bip112tx_special_v1)
325 # add BIP 68 txs
326 success_txs.extend(all_rlt_txs(bip68txs_v1))
327 # add BIP 112 with seq=10 txs
328 success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_v1))
329 success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_v1))
330 # try BIP 112 with seq=9 txs
331 success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v1))
332 success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_9_v1))
333 yield TestInstance([[self.create_test_block(success_txs), True]]) # 6
334 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
336 ### Version 2 txs ###
337 success_txs = []
338 # add BIP113 tx and -1 CSV tx
339 bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
340 bip113signed2 = self.sign_transaction(self.nodes[0], bip113tx_v2)
341 success_txs.append(bip113signed2)
342 success_txs.append(bip112tx_special_v2)
343 # add BIP 68 txs
344 success_txs.extend(all_rlt_txs(bip68txs_v2))
345 # add BIP 112 with seq=10 txs
346 success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_v2))
347 success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_v2))
348 # try BIP 112 with seq=9 txs
349 success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v2))
350 success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_9_v2))
351 yield TestInstance([[self.create_test_block(success_txs), True]]) # 7
352 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
355 # 1 more version 4 block to get us to height 575 so the fork should now be active for the next block
356 test_blocks = self.generate_blocks(1, 4)
357 yield TestInstance(test_blocks, sync_every_block=False) # 8
358 assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'active')
361 #################################
362 ### After Soft Forks Activate ###
363 #################################
364 ### BIP 113 ###
365 # BIP 113 tests should now fail regardless of version number if nLockTime isn't satisfied by new rules
366 bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
367 bip113signed1 = self.sign_transaction(self.nodes[0], bip113tx_v1)
368 bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
369 bip113signed2 = self.sign_transaction(self.nodes[0], bip113tx_v2)
370 for bip113tx in [bip113signed1, bip113signed2]:
371 yield TestInstance([[self.create_test_block([bip113tx]), False]]) # 9,10
372 # BIP 113 tests should now pass if the locktime is < MTP
373 bip113tx_v1.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block
374 bip113signed1 = self.sign_transaction(self.nodes[0], bip113tx_v1)
375 bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 - 1 # < MTP of prior block
376 bip113signed2 = self.sign_transaction(self.nodes[0], bip113tx_v2)
377 for bip113tx in [bip113signed1, bip113signed2]:
378 yield TestInstance([[self.create_test_block([bip113tx]), True]]) # 11,12
379 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
381 # Next block height = 580 after 4 blocks of random version
382 test_blocks = self.generate_blocks(4, 1234)
383 yield TestInstance(test_blocks, sync_every_block=False) # 13
385 ### BIP 68 ###
386 ### Version 1 txs ###
387 # All still pass
388 success_txs = []
389 success_txs.extend(all_rlt_txs(bip68txs_v1))
390 yield TestInstance([[self.create_test_block(success_txs), True]]) # 14
391 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
393 ### Version 2 txs ###
394 bip68success_txs = []
395 # All txs with SEQUENCE_LOCKTIME_DISABLE_FLAG set pass
396 for b25 in range(2):
397 for b22 in range(2):
398 for b18 in range(2):
399 bip68success_txs.append(bip68txs_v2[1][b25][b22][b18])
400 yield TestInstance([[self.create_test_block(bip68success_txs), True]]) # 15
401 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
402 # All txs without flag fail as we are at delta height = 8 < 10 and delta time = 8 * 600 < 10 * 512
403 bip68timetxs = []
404 for b25 in range(2):
405 for b18 in range(2):
406 bip68timetxs.append(bip68txs_v2[0][b25][1][b18])
407 for tx in bip68timetxs:
408 yield TestInstance([[self.create_test_block([tx]), False]]) # 16 - 19
409 bip68heighttxs = []
410 for b25 in range(2):
411 for b18 in range(2):
412 bip68heighttxs.append(bip68txs_v2[0][b25][0][b18])
413 for tx in bip68heighttxs:
414 yield TestInstance([[self.create_test_block([tx]), False]]) # 20 - 23
416 # Advance one block to 581
417 test_blocks = self.generate_blocks(1, 1234)
418 yield TestInstance(test_blocks, sync_every_block=False) # 24
420 # Height txs should fail and time txs should now pass 9 * 600 > 10 * 512
421 bip68success_txs.extend(bip68timetxs)
422 yield TestInstance([[self.create_test_block(bip68success_txs), True]]) # 25
423 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
424 for tx in bip68heighttxs:
425 yield TestInstance([[self.create_test_block([tx]), False]]) # 26 - 29
427 # Advance one block to 582
428 test_blocks = self.generate_blocks(1, 1234)
429 yield TestInstance(test_blocks, sync_every_block=False) # 30
431 # All BIP 68 txs should pass
432 bip68success_txs.extend(bip68heighttxs)
433 yield TestInstance([[self.create_test_block(bip68success_txs), True]]) # 31
434 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
437 ### BIP 112 ###
438 ### Version 1 txs ###
439 # -1 OP_CSV tx should fail
440 yield TestInstance([[self.create_test_block([bip112tx_special_v1]), False]]) #32
441 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 1 txs should still pass
442 success_txs = []
443 for b25 in range(2):
444 for b22 in range(2):
445 for b18 in range(2):
446 success_txs.append(bip112txs_vary_OP_CSV_v1[1][b25][b22][b18])
447 success_txs.append(bip112txs_vary_OP_CSV_9_v1[1][b25][b22][b18])
448 yield TestInstance([[self.create_test_block(success_txs), True]]) # 33
449 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
451 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV, version 1 txs should now fail
452 fail_txs = []
453 fail_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_v1))
454 fail_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v1))
455 for b25 in range(2):
456 for b22 in range(2):
457 for b18 in range(2):
458 fail_txs.append(bip112txs_vary_OP_CSV_v1[0][b25][b22][b18])
459 fail_txs.append(bip112txs_vary_OP_CSV_9_v1[0][b25][b22][b18])
461 for tx in fail_txs:
462 yield TestInstance([[self.create_test_block([tx]), False]]) # 34 - 81
464 ### Version 2 txs ###
465 # -1 OP_CSV tx should fail
466 yield TestInstance([[self.create_test_block([bip112tx_special_v2]), False]]) #82
468 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 2 txs should pass (all sequence locks are met)
469 success_txs = []
470 for b25 in range(2):
471 for b22 in range(2):
472 for b18 in range(2):
473 success_txs.append(bip112txs_vary_OP_CSV_v2[1][b25][b22][b18]) # 8/16 of vary_OP_CSV
474 success_txs.append(bip112txs_vary_OP_CSV_9_v2[1][b25][b22][b18]) # 8/16 of vary_OP_CSV_9
476 yield TestInstance([[self.create_test_block(success_txs), True]]) # 83
477 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
479 ## SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV for all remaining txs ##
480 # All txs with nSequence 9 should fail either due to earlier mismatch or failing the CSV check
481 fail_txs = []
482 fail_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v2)) # 16/16 of vary_nSequence_9
483 for b25 in range(2):
484 for b22 in range(2):
485 for b18 in range(2):
486 fail_txs.append(bip112txs_vary_OP_CSV_9_v2[0][b25][b22][b18]) # 16/16 of vary_OP_CSV_9
488 for tx in fail_txs:
489 yield TestInstance([[self.create_test_block([tx]), False]]) # 84 - 107
491 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in nSequence, tx should fail
492 fail_txs = []
493 for b25 in range(2):
494 for b22 in range(2):
495 for b18 in range(2):
496 fail_txs.append(bip112txs_vary_nSequence_v2[1][b25][b22][b18]) # 8/16 of vary_nSequence
497 for tx in fail_txs:
498 yield TestInstance([[self.create_test_block([tx]), False]]) # 108-115
500 # If sequencelock types mismatch, tx should fail
501 fail_txs = []
502 for b25 in range(2):
503 for b18 in range(2):
504 fail_txs.append(bip112txs_vary_nSequence_v2[0][b25][1][b18]) # 12/16 of vary_nSequence
505 fail_txs.append(bip112txs_vary_OP_CSV_v2[0][b25][1][b18]) # 12/16 of vary_OP_CSV
506 for tx in fail_txs:
507 yield TestInstance([[self.create_test_block([tx]), False]]) # 116-123
509 # Remaining txs should pass, just test masking works properly
510 success_txs = []
511 for b25 in range(2):
512 for b18 in range(2):
513 success_txs.append(bip112txs_vary_nSequence_v2[0][b25][0][b18]) # 16/16 of vary_nSequence
514 success_txs.append(bip112txs_vary_OP_CSV_v2[0][b25][0][b18]) # 16/16 of vary_OP_CSV
515 yield TestInstance([[self.create_test_block(success_txs), True]]) # 124
516 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
518 # Additional test, of checking that comparison of two time types works properly
519 time_txs = []
520 for b25 in range(2):
521 for b18 in range(2):
522 tx = bip112txs_vary_OP_CSV_v2[0][b25][1][b18]
523 tx.vin[0].nSequence = base_relative_locktime | seq_type_flag
524 signtx = self.sign_transaction(self.nodes[0], tx)
525 time_txs.append(signtx)
526 yield TestInstance([[self.create_test_block(time_txs), True]]) # 125
527 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
529 ### Missing aspects of test
530 ## Testing empty stack fails
533 if __name__ == '__main__':
534 BIP68_112_113Test().main()