diff --git a/test/functional/bip68-112-113-p2p.py b/test/functional/bip68-112-113-p2p.py --- a/test/functional/bip68-112-113-p2p.py +++ b/test/functional/bip68-112-113-p2p.py @@ -42,11 +42,10 @@ from test_framework.test_framework import ComparisonTestFramework from test_framework.util import * -from test_framework.mininode import ToHex, CTransaction, NetworkThread +from test_framework.mininode import ToHex, FromHex, CTransaction, NetworkThread from test_framework.blocktools import create_coinbase, create_block from test_framework.comptool import TestInstance, TestManager from test_framework.script import * -from io import BytesIO import time # far in the future @@ -111,7 +110,8 @@ def run_test(self): test = TestManager(self, self.options.tmpdir) test.add_all_connections(self.nodes) - NetworkThread().start() # Start up network handling in another thread + # Start up network handling in another thread + NetworkThread().start() test.run() def send_generic_input_tx(self, node, coinbases): @@ -122,17 +122,13 @@ inputs = [{"txid": txid, "vout": 0}] outputs = {to_address: amount} rawtx = node.createrawtransaction(inputs, outputs) - tx = CTransaction() - f = BytesIO(hex_str_to_bytes(rawtx)) - tx.deserialize(f) + tx = FromHex(CTransaction(), rawtx) return tx def sign_transaction(self, node, unsignedtx): rawtx = ToHex(unsignedtx) signresult = node.signrawtransaction(rawtx) - tx = CTransaction() - f = BytesIO(hex_str_to_bytes(signresult['hex'])) - tx.deserialize(f) + tx = FromHex(CTransaction(), signresult['hex']) return tx def generate_blocks(self, number): @@ -201,10 +197,12 @@ tx = self.create_transaction( self.nodes[0], bip112inputs[i], self.nodeaddress, Decimal("49.98")) i += 1 - if (varyOP_CSV): # if varying OP_CSV, nSequence is fixed + # if varying OP_CSV, nSequence is fixed + if (varyOP_CSV): tx.vin[0].nSequence = base_relative_locktime + \ locktime_delta - else: # vary nSequence instead, OP_CSV is fixed + # vary nSequence instead, OP_CSV is fixed + else: tx.vin[0].nSequence = relative_locktimes[b31][b25][b22][b18] + \ locktime_delta tx.nVersion = txversion @@ -232,7 +230,8 @@ # Set time back to present so yielded blocks aren't in the future as # we advance last_block_time self.nodes[0].setmocktime(0) - self.tipheight = 82 # height of the next block to build + # height of the next block to build + self.tipheight = 82 self.last_block_time = long_past_time self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0) self.nodeaddress = self.nodes[0].getnewaddress() @@ -242,7 +241,8 @@ # 489 more version 4 blocks test_blocks = self.generate_blocks(489) - yield TestInstance(test_blocks, sync_every_block=False) # 1 + # Test #1 + yield TestInstance(test_blocks, sync_every_block=False) # Still not activated. assert_equal(get_csv_status(self.nodes[0]), False) @@ -290,7 +290,8 @@ # 2 more version 4 blocks test_blocks = self.generate_blocks(2) - yield TestInstance(test_blocks, sync_every_block=False) # 5 + # Test #2 + yield TestInstance(test_blocks, sync_every_block=False) # Not yet activated, height = 574 (will activate for block 576, not 575) assert_equal(get_csv_status(self.nodes[0]), False) @@ -356,7 +357,8 @@ # try BIP 112 with seq=9 txs success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v1)) success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_9_v1)) - yield TestInstance([[self.create_test_block(success_txs), True]]) # 6 + # Test #3 + yield TestInstance([[self.create_test_block(success_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) ### Version 2 txs ### @@ -375,13 +377,15 @@ # try BIP 112 with seq=9 txs success_txs.extend(all_rlt_txs(bip112txs_vary_nSequence_9_v2)) success_txs.extend(all_rlt_txs(bip112txs_vary_OP_CSV_9_v2)) - yield TestInstance([[self.create_test_block(success_txs), True]]) # 7 + # Test #4 + yield TestInstance([[self.create_test_block(success_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) # 1 more version 4 block to get us to height 575 so the fork should # now be active for the next block test_blocks = self.generate_blocks(1) - yield TestInstance(test_blocks, sync_every_block=False) # 8 + # Test #5 + yield TestInstance(test_blocks, sync_every_block=False) assert_equal(get_csv_status(self.nodes[0]), False) self.nodes[0].generate(1) @@ -401,7 +405,7 @@ bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 bip113signed2 = self.sign_transaction(self.nodes[0], bip113tx_v2) for bip113tx in [bip113signed1, bip113signed2]: - # 9,10 + # Test #6, Test #7 yield TestInstance([[self.create_test_block([bip113tx]), False]]) # BIP 113 tests should now pass if the locktime is < MTP @@ -412,20 +416,22 @@ bip113tx_v2.nLockTime = self.last_block_time - 600 * 5 - 1 bip113signed2 = self.sign_transaction(self.nodes[0], bip113tx_v2) for bip113tx in [bip113signed1, bip113signed2]: - # 11,12 + # Test #8, Test #9 yield TestInstance([[self.create_test_block([bip113tx]), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) # Next block height = 580 after 4 blocks of random version test_blocks = self.generate_blocks(4) - yield TestInstance(test_blocks, sync_every_block=False) # 13 + # Test #10 + yield TestInstance(test_blocks, sync_every_block=False) ### BIP 68 ### ### Version 1 txs ### # All still pass success_txs = [] success_txs.extend(all_rlt_txs(bip68txs_v1)) - yield TestInstance([[self.create_test_block(success_txs), True]]) # 14 + # Test #11 + yield TestInstance([[self.create_test_block(success_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) ### Version 2 txs ### @@ -435,7 +441,7 @@ for b22 in range(2): for b18 in range(2): bip68success_txs.append(bip68txs_v2[1][b25][b22][b18]) - # 15 + # Test #12 yield TestInstance([[self.create_test_block(bip68success_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) # All txs without flag fail as we are at delta height = 8 < 10 and @@ -445,43 +451,45 @@ for b18 in range(2): bip68timetxs.append(bip68txs_v2[0][b25][1][b18]) for tx in bip68timetxs: - # 16 - 19 + # Test #13 - Test #16 yield TestInstance([[self.create_test_block([tx]), False]]) bip68heighttxs = [] for b25 in range(2): for b18 in range(2): bip68heighttxs.append(bip68txs_v2[0][b25][0][b18]) for tx in bip68heighttxs: - # 20 - 23 + # Test #17 - Test #20 yield TestInstance([[self.create_test_block([tx]), False]]) # Advance one block to 581 test_blocks = self.generate_blocks(1) - yield TestInstance(test_blocks, sync_every_block=False) # 24 + # Test #21 + yield TestInstance(test_blocks, sync_every_block=False) # Height txs should fail and time txs should now pass 9 * 600 > 10 * 512 bip68success_txs.extend(bip68timetxs) - # 25 + # Test #22 yield TestInstance([[self.create_test_block(bip68success_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) for tx in bip68heighttxs: - # 26 - 29 + # Test #23 - Test #26 yield TestInstance([[self.create_test_block([tx]), False]]) # Advance one block to 582 test_blocks = self.generate_blocks(1) - yield TestInstance(test_blocks, sync_every_block=False) # 30 + # Test #27 + yield TestInstance(test_blocks, sync_every_block=False) # All BIP 68 txs should pass bip68success_txs.extend(bip68heighttxs) - # 31 + # Test #28 yield TestInstance([[self.create_test_block(bip68success_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) ### BIP 112 ### ### Version 1 txs ### # -1 OP_CSV tx should fail - # 32 + # Test #29 yield TestInstance([[self.create_test_block([bip112tx_special_v1]), False]]) # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, # version 1 txs should still pass @@ -493,7 +501,8 @@ bip112txs_vary_OP_CSV_v1[1][b25][b22][b18]) success_txs.append( bip112txs_vary_OP_CSV_9_v1[1][b25][b22][b18]) - yield TestInstance([[self.create_test_block(success_txs), True]]) # 33 + # Test #30 + yield TestInstance([[self.create_test_block(success_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) # If SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV, @@ -509,12 +518,12 @@ bip112txs_vary_OP_CSV_9_v1[0][b25][b22][b18]) for tx in fail_txs: - # 34 - 81 + # Test #31 - Test #78 yield TestInstance([[self.create_test_block([tx]), False]]) ### Version 2 txs ### # -1 OP_CSV tx should fail - # 82 + # Test #79 yield TestInstance([[self.create_test_block([bip112tx_special_v2]), False]]) # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, @@ -530,7 +539,8 @@ success_txs.append( bip112txs_vary_OP_CSV_9_v2[1][b25][b22][b18]) - yield TestInstance([[self.create_test_block(success_txs), True]]) # 83 + # Test #80 + yield TestInstance([[self.create_test_block(success_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) ## SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV for all remaining txs ## @@ -547,7 +557,7 @@ bip112txs_vary_OP_CSV_9_v2[0][b25][b22][b18]) for tx in fail_txs: - # 84 - 107 + # Test #81 - Test #104 yield TestInstance([[self.create_test_block([tx]), False]]) # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in nSequence, tx should fail @@ -559,7 +569,7 @@ fail_txs.append( bip112txs_vary_nSequence_v2[1][b25][b22][b18]) for tx in fail_txs: - # 108-115 + # Test #105 - Test #112 yield TestInstance([[self.create_test_block([tx]), False]]) # If sequencelock types mismatch, tx should fail @@ -571,7 +581,7 @@ # 12/16 of vary_OP_CSV fail_txs.append(bip112txs_vary_OP_CSV_v2[0][b25][1][b18]) for tx in fail_txs: - # 116-123 + # Test #113 - Test #120 yield TestInstance([[self.create_test_block([tx]), False]]) # Remaining txs should pass, just test masking works properly @@ -582,7 +592,7 @@ success_txs.append(bip112txs_vary_nSequence_v2[0][b25][0][b18]) # 16/16 of vary_OP_CSV success_txs.append(bip112txs_vary_OP_CSV_v2[0][b25][0][b18]) - # 124 + # Test #121 yield TestInstance([[self.create_test_block(success_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) @@ -594,7 +604,8 @@ tx.vin[0].nSequence = base_relative_locktime | seq_type_flag signtx = self.sign_transaction(self.nodes[0], tx) time_txs.append(signtx) - yield TestInstance([[self.create_test_block(time_txs), True]]) # 125 + # Test #122 + yield TestInstance([[self.create_test_block(time_txs), True]]) self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) # Missing aspects of test