You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bip68-112-113-p2p.py 27KB

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