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