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-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.
  6. This soft fork will activate the following BIPS:
  7. BIP 68 - nSequence relative lock times
  8. BIP 112 - CHECKSEQUENCEVERIFY
  9. BIP 113 - MedianTimePast semantics for nLockTime
  10. regtest lock-in with 108/144 block signalling
  11. activation after a further 144 blocks
  12. mine 82 blocks whose coinbases will be used to generate inputs for our tests
  13. mine 61 blocks to transition from DEFINED to STARTED
  14. mine 144 blocks only 100 of which are signaling readiness in order to fail to change state this period
  15. mine 144 blocks with 108 signaling and verify STARTED->LOCKED_IN
  16. mine 140 blocks and seed block chain with the 82 inputs will use for our tests at height 572
  17. mine 3 blocks and verify still at LOCKED_IN and test that enforcement has not triggered
  18. mine 1 block and test that enforcement has triggered (which triggers ACTIVE)
  19. Test BIP 113 is enforced
  20. Mine 4 blocks so next height is 580 and test BIP 68 is enforced for time and height
  21. Mine 1 block so next height is 581 and test BIP 68 now passes time but not height
  22. Mine 1 block so next height is 582 and test BIP 68 now passes time and height
  23. Test that BIP 112 is enforced
  24. Various transactions will be used to test that the BIPs rules are not enforced before the soft fork activates
  25. And that after the soft fork activates transactions pass and fail as they should according to the rules.
  26. For each BIP, transactions of versions 1 and 2 will be tested.
  27. ----------------
  28. BIP 113:
  29. bip113tx - modify the nLocktime variable
  30. BIP 68:
  31. bip68txs - 16 txs with nSequence relative locktime of 10 with various bits set as per the relative_locktimes below
  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. """
  39. from test_framework.test_framework import ComparisonTestFramework
  40. from test_framework.util import *
  41. from test_framework.mininode import ToHex, CTransaction, NetworkThread
  42. from test_framework.blocktools import create_coinbase, create_block
  43. from test_framework.comptool import TestInstance, TestManager
  44. from test_framework.script import *
  45. from io import BytesIO
  46. import time
  47. base_relative_locktime = 10
  48. seq_disable_flag = 1<<31
  49. seq_random_high_bit = 1<<25
  50. seq_type_flag = 1<<22
  51. seq_random_low_bit = 1<<18
  52. # b31,b25,b22,b18 represent the 31st, 25th, 22nd and 18th bits respectively in the nSequence field
  53. # relative_locktimes[b31][b25][b22][b18] is a base_relative_locktime with the indicated bits set if their indices are 1
  54. relative_locktimes = []
  55. for b31 in range(2):
  56. b25times = []
  57. for b25 in range(2):
  58. b22times = []
  59. for b22 in range(2):
  60. b18times = []
  61. for b18 in range(2):
  62. rlt = base_relative_locktime
  63. if (b31):
  64. rlt = rlt | seq_disable_flag
  65. if (b25):
  66. rlt = rlt | seq_random_high_bit
  67. if (b22):
  68. rlt = rlt | seq_type_flag
  69. if (b18):
  70. rlt = rlt | seq_random_low_bit
  71. b18times.append(rlt)
  72. b22times.append(b18times)
  73. b25times.append(b22times)
  74. relative_locktimes.append(b25times)
  75. def all_rlt_txs(txarray):
  76. txs = []
  77. for b31 in range(2):
  78. for b25 in range(2):
  79. for b22 in range(2):
  80. for b18 in range(2):
  81. txs.append(txarray[b31][b25][b22][b18])
  82. return txs
  83. class BIP68_112_113Test(ComparisonTestFramework):
  84. def __init__(self):
  85. super().__init__()
  86. self.num_nodes = 1
  87. def setup_network(self):
  88. # Must set the blockversion for this test
  89. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
  90. extra_args=[['-whitelist=127.0.0.1', '-blockversion=4']],
  91. binary=[self.options.testbinary])
  92. def run_test(self):
  93. test = TestManager(self, self.options.tmpdir)
  94. test.add_all_connections(self.nodes)
  95. NetworkThread().start() # Start up network handling in another thread
  96. test.run()
  97. def send_generic_input_tx(self, node, coinbases):
  98. amount = Decimal("49.99")
  99. return node.sendrawtransaction(ToHex(self.sign_transaction(node, self.create_transaction(node, node.getblock(coinbases.pop())['tx'][0], self.nodeaddress, amount))))
  100. def create_transaction(self, node, txid, to_address, amount):
  101. inputs = [{ "txid" : txid, "vout" : 0}]
  102. outputs = { to_address : amount }
  103. rawtx = node.createrawtransaction(inputs, outputs)
  104. tx = CTransaction()
  105. f = BytesIO(hex_str_to_bytes(rawtx))
  106. tx.deserialize(f)
  107. return tx
  108. def sign_transaction(self, node, unsignedtx):
  109. rawtx = ToHex(unsignedtx)
  110. signresult = node.signrawtransaction(rawtx)
  111. tx = CTransaction()
  112. f = BytesIO(hex_str_to_bytes(signresult['hex']))
  113. tx.deserialize(f)
  114. return tx
  115. def generate_blocks(self, number, version, test_blocks = []):
  116. for i in range(number):
  117. block = self.create_test_block([], version)
  118. test_blocks.append([block, True])
  119. self.last_block_time += 600
  120. self.tip = block.sha256
  121. self.tipheight += 1
  122. return test_blocks
  123. def create_test_block(self, txs, version = 536870912):
  124. block = create_block(self.tip, create_coinbase(self.tipheight + 1), self.last_block_time + 600)
  125. block.nVersion = version
  126. block.vtx.extend(txs)
  127. block.hashMerkleRoot = block.calc_merkle_root()
  128. block.rehash()
  129. block.solve()
  130. return block
  131. def create_bip68txs(self, bip68inputs, txversion, locktime_delta = 0):
  132. txs = []
  133. assert(len(bip68inputs) >= 16)
  134. i = 0
  135. for b31 in range(2):
  136. b25txs = []
  137. for b25 in range(2):
  138. b22txs = []
  139. for b22 in range(2):
  140. b18txs = []
  141. for b18 in range(2):
  142. tx = self.create_transaction(self.nodes[0], bip68inputs[i], self.nodeaddress, Decimal("49.98"))
  143. i += 1
  144. tx.nVersion = txversion
  145. tx.vin[0].nSequence = relative_locktimes[b31][b25][b22][b18] + locktime_delta
  146. b18txs.append(self.sign_transaction(self.nodes[0], tx))
  147. b22txs.append(b18txs)
  148. b25txs.append(b22txs)
  149. txs.append(b25txs)
  150. return txs
  151. def create_bip112special(self, input, txversion):
  152. tx = self.create_transaction(self.nodes[0], input, self.nodeaddress, Decimal("49.98"))
  153. tx.nVersion = txversion
  154. signtx = self.sign_transaction(self.nodes[0], tx)
  155. signtx.vin[0].scriptSig = CScript([-1, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
  156. return signtx
  157. def create_bip112txs(self, bip112inputs, varyOP_CSV, txversion, locktime_delta = 0):
  158. txs = []
  159. assert(len(bip112inputs) >= 16)
  160. i = 0
  161. for b31 in range(2):
  162. b25txs = []
  163. for b25 in range(2):
  164. b22txs = []
  165. for b22 in range(2):
  166. b18txs = []
  167. for b18 in range(2):
  168. tx = self.create_transaction(self.nodes[0], bip112inputs[i], self.nodeaddress, Decimal("49.98"))
  169. i += 1
  170. if (varyOP_CSV): # if varying OP_CSV, nSequence is fixed
  171. tx.vin[0].nSequence = base_relative_locktime + locktime_delta
  172. else: # vary nSequence instead, OP_CSV is fixed
  173. tx.vin[0].nSequence = relative_locktimes[b31][b25][b22][b18] + locktime_delta
  174. tx.nVersion = txversion
  175. signtx = self.sign_transaction(self.nodes[0], tx)
  176. if (varyOP_CSV):
  177. signtx.vin[0].scriptSig = CScript([relative_locktimes[b31][b25][b22][b18], OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
  178. else:
  179. signtx.vin[0].scriptSig = CScript([base_relative_locktime, OP_CHECKSEQUENCEVERIFY, OP_DROP] + list(CScript(signtx.vin[0].scriptSig)))
  180. b18txs.append(signtx)
  181. b22txs.append(b18txs)
  182. b25txs.append(b22txs)
  183. txs.append(b25txs)
  184. return txs
  185. def get_tests(self):
  186. 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
  187. self.nodes[0].setmocktime(long_past_time - 100) # enough so that the generated blocks will still all be before long_past_time
  188. self.coinbase_blocks = self.nodes[0].generate(1 + 16 + 2*32 + 1) # 82 blocks generated for inputs
  189. self.nodes[0].setmocktime(0) # set time back to present so yielded blocks aren't in the future as we advance last_block_time
  190. self.tipheight = 82 # height of the next block to build
  191. self.last_block_time = long_past_time
  192. self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
  193. self.nodeaddress = self.nodes[0].getnewaddress()
  194. assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'defined')
  195. test_blocks = self.generate_blocks(61, 4)
  196. yield TestInstance(test_blocks, sync_every_block=False) # 1
  197. # Advanced from DEFINED to STARTED, height = 143
  198. assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'started')
  199. # Fail to achieve LOCKED_IN 100 out of 144 signal bit 0
  200. # using a variety of bits to simulate multiple parallel softforks
  201. test_blocks = self.generate_blocks(50, 536870913) # 0x20000001 (signalling ready)
  202. test_blocks = self.generate_blocks(20, 4, test_blocks) # 0x00000004 (signalling not)
  203. test_blocks = self.generate_blocks(50, 536871169, test_blocks) # 0x20000101 (signalling ready)
  204. test_blocks = self.generate_blocks(24, 536936448, test_blocks) # 0x20010000 (signalling not)
  205. yield TestInstance(test_blocks, sync_every_block=False) # 2
  206. # Failed to advance past STARTED, height = 287
  207. assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'started')
  208. # 108 out of 144 signal bit 0 to achieve lock-in
  209. # using a variety of bits to simulate multiple parallel softforks
  210. test_blocks = self.generate_blocks(58, 536870913) # 0x20000001 (signalling ready)
  211. test_blocks = self.generate_blocks(26, 4, test_blocks) # 0x00000004 (signalling not)
  212. test_blocks = self.generate_blocks(50, 536871169, test_blocks) # 0x20000101 (signalling ready)
  213. test_blocks = self.generate_blocks(10, 536936448, test_blocks) # 0x20010000 (signalling not)
  214. yield TestInstance(test_blocks, sync_every_block=False) # 3
  215. # Advanced from STARTED to LOCKED_IN, height = 431
  216. assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'locked_in')
  217. # 140 more version 4 blocks
  218. test_blocks = self.generate_blocks(140, 4)
  219. yield TestInstance(test_blocks, sync_every_block=False) # 4
  220. ### Inputs at height = 572
  221. # Put inputs for all tests in the chain at height 572 (tip now = 571) (time increases by 600s per block)
  222. # Note we reuse inputs for v1 and v2 txs so must test these separately
  223. # 16 normal inputs
  224. bip68inputs = []
  225. for i in range(16):
  226. bip68inputs.append(self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
  227. # 2 sets of 16 inputs with 10 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
  228. bip112basicinputs = []
  229. for j in range(2):
  230. inputs = []
  231. for i in range(16):
  232. inputs.append(self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
  233. bip112basicinputs.append(inputs)
  234. # 2 sets of 16 varied inputs with (relative_lock_time) OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
  235. bip112diverseinputs = []
  236. for j in range(2):
  237. inputs = []
  238. for i in range(16):
  239. inputs.append(self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks))
  240. bip112diverseinputs.append(inputs)
  241. # 1 special input with -1 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
  242. bip112specialinput = self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
  243. # 1 normal input
  244. bip113input = self.send_generic_input_tx(self.nodes[0], self.coinbase_blocks)
  245. self.nodes[0].setmocktime(self.last_block_time + 600)
  246. inputblockhash = self.nodes[0].generate(1)[0] # 1 block generated for inputs to be in chain at height 572
  247. self.nodes[0].setmocktime(0)
  248. self.tip = int("0x" + inputblockhash, 0)
  249. self.tipheight += 1
  250. self.last_block_time += 600
  251. assert_equal(len(self.nodes[0].getblock(inputblockhash,True)["tx"]), 82+1)
  252. # 2 more version 4 blocks
  253. test_blocks = self.generate_blocks(2, 4)
  254. yield TestInstance(test_blocks, sync_every_block=False) # 5
  255. # Not yet advanced to ACTIVE, height = 574 (will activate for block 576, not 575)
  256. assert_equal(get_bip9_status(self.nodes[0], 'csv')['status'], 'locked_in')
  257. # Test both version 1 and version 2 transactions for all tests
  258. # BIP113 test transaction will be modified before each use to put in appropriate block time
  259. bip113tx_v1 = self.create_transaction(self.nodes[0], bip113input, self.nodeaddress, Decimal("49.98"))
  260. bip113tx_v1.vin[0].nSequence = 0xFFFFFFFE
  261. bip113tx_v1.nVersion = 1
  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()