您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

bip68-112-113-p2p.py 26KB

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