From fa5f938cfe4264054e9017fbe710c81e7b3bb150 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Thu, 22 Apr 2021 14:16:42 +0200 Subject: test: Remove new_tx reference new_tx is a reference to tx. To avoid confusion and code-bloat, remove it --- test/functional/feature_cltv.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'test/functional') diff --git a/test/functional/feature_cltv.py b/test/functional/feature_cltv.py index f2130fb588..5c4512c387 100755 --- a/test/functional/feature_cltv.py +++ b/test/functional/feature_cltv.py @@ -47,13 +47,11 @@ def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None): # Need to re-sign, since nSequence and nLockTime changed signed_result = node.signrawtransactionwithwallet(ToHex(tx)) - new_tx = CTransaction() - new_tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex']))) - else: - new_tx = tx + tx = CTransaction() + tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex']))) - new_tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(new_tx.vin[0].scriptSig))) - return new_tx + tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(tx.vin[0].scriptSig))) + return tx def cltv_invalidate(node, tx, failure_reason): -- cgit v1.2.3 From fa5591d38cfc917235a3dffd61c36dbf1f781a4d Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Thu, 22 Apr 2021 13:57:12 +0200 Subject: test: Hide tx rehash in helper This avoids code-bloat and accidentally forgetting to call it --- test/functional/feature_cltv.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'test/functional') diff --git a/test/functional/feature_cltv.py b/test/functional/feature_cltv.py index 5c4512c387..49dea28c4e 100755 --- a/test/functional/feature_cltv.py +++ b/test/functional/feature_cltv.py @@ -51,6 +51,7 @@ def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None): tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex']))) tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(tx.vin[0].scriptSig))) + tx.rehash() return tx @@ -124,7 +125,6 @@ class BIP65Test(BitcoinTestFramework): spendtx = create_transaction(self.nodes[0], self.coinbase_txids[i], self.nodeaddress, amount=1.0) spendtx = cltv_invalidate(self.nodes[0], spendtx, i) - spendtx.rehash() invalid_ctlv_txs.append(spendtx) tip = self.nodes[0].getbestblockhash() @@ -161,7 +161,6 @@ class BIP65Test(BitcoinTestFramework): spendtx = create_transaction(self.nodes[0], self.coinbase_txids[10+i], self.nodeaddress, amount=1.0) spendtx = cltv_invalidate(self.nodes[0], spendtx, i) - spendtx.rehash() expected_cltv_reject_reason = [ "non-mandatory-script-verify-flag (Operation not valid with the current stack size)", @@ -195,7 +194,6 @@ class BIP65Test(BitcoinTestFramework): self.log.info("Test that a version 4 block with a valid-according-to-CLTV transaction is accepted") spendtx = cltv_validate(self.nodes[0], spendtx, CLTV_HEIGHT - 1) - spendtx.rehash() block.vtx.pop(1) block.vtx.append(spendtx) -- cgit v1.2.3 From fa066f1b66ab75e6c88e11922bbd085c9d3211de Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Thu, 22 Apr 2021 18:04:57 +0200 Subject: test: Run feature_cltv with MiniWallet --- test/functional/feature_cltv.py | 30 ++++++++---------------------- test/functional/test_framework/wallet.py | 23 ++++++++++++++++------- 2 files changed, 24 insertions(+), 29 deletions(-) (limited to 'test/functional') diff --git a/test/functional/feature_cltv.py b/test/functional/feature_cltv.py index 49dea28c4e..6c51944d81 100755 --- a/test/functional/feature_cltv.py +++ b/test/functional/feature_cltv.py @@ -11,11 +11,9 @@ Test that the CHECKLOCKTIMEVERIFY soft-fork activates at (regtest) block height from test_framework.blocktools import ( create_block, create_coinbase, - create_transaction, ) from test_framework.messages import ( CTransaction, - ToHex, msg_block, ) from test_framework.p2p import P2PInterface @@ -27,12 +25,8 @@ from test_framework.script import ( OP_DROP, ) from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import ( - assert_equal, - hex_str_to_bytes, -) - -from io import BytesIO +from test_framework.util import assert_equal +from test_framework.wallet import MiniWallet CLTV_HEIGHT = 1351 @@ -41,15 +35,11 @@ CLTV_HEIGHT = 1351 # 1) prepending a given script to the scriptSig of vin 0 and # 2) (optionally) modify the nSequence of vin 0 and the tx's nLockTime def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None): + assert_equal(len(tx.vin), 1) if nsequence is not None: tx.vin[0].nSequence = nsequence tx.nLockTime = nlocktime - # Need to re-sign, since nSequence and nLockTime changed - signed_result = node.signrawtransactionwithwallet(ToHex(tx)) - tx = CTransaction() - tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex']))) - tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(tx.vin[0].scriptSig))) tx.rehash() return tx @@ -97,9 +87,6 @@ class BIP65Test(BitcoinTestFramework): self.setup_clean_chain = True self.rpc_timeout = 480 - def skip_test_if_missing_module(self): - self.skip_if_no_wallet() - def test_cltv_info(self, *, is_active): assert_equal(self.nodes[0].getblockchaininfo()['softforks']['bip65'], { "active": is_active, @@ -110,20 +97,20 @@ class BIP65Test(BitcoinTestFramework): def run_test(self): peer = self.nodes[0].add_p2p_connection(P2PInterface()) + wallet = MiniWallet(self.nodes[0], raw_script=True) self.test_cltv_info(is_active=False) self.log.info("Mining %d blocks", CLTV_HEIGHT - 2) - self.coinbase_txids = [self.nodes[0].getblock(b)['tx'][0] for b in self.nodes[0].generate(CLTV_HEIGHT - 2)] - self.nodeaddress = self.nodes[0].getnewaddress() + wallet.generate(10) + self.nodes[0].generate(CLTV_HEIGHT - 2 - 10) self.log.info("Test that invalid-according-to-CLTV transactions can still appear in a block") # create one invalid tx per CLTV failure reason (5 in total) and collect them invalid_ctlv_txs = [] for i in range(5): - spendtx = create_transaction(self.nodes[0], self.coinbase_txids[i], - self.nodeaddress, amount=1.0) + spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx'] spendtx = cltv_invalidate(self.nodes[0], spendtx, i) invalid_ctlv_txs.append(spendtx) @@ -158,8 +145,7 @@ class BIP65Test(BitcoinTestFramework): # create and test one invalid tx per CLTV failure reason (5 in total) for i in range(5): - spendtx = create_transaction(self.nodes[0], self.coinbase_txids[10+i], - self.nodeaddress, amount=1.0) + spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx'] spendtx = cltv_invalidate(self.nodes[0], spendtx, i) expected_cltv_reject_reason = [ diff --git a/test/functional/test_framework/wallet.py b/test/functional/test_framework/wallet.py index 59ef18635b..395b50c4d8 100644 --- a/test/functional/test_framework/wallet.py +++ b/test/functional/test_framework/wallet.py @@ -17,6 +17,7 @@ from test_framework.messages import ( from test_framework.script import ( CScript, OP_TRUE, + OP_NOP, ) from test_framework.util import ( assert_equal, @@ -26,11 +27,15 @@ from test_framework.util import ( class MiniWallet: - def __init__(self, test_node): + def __init__(self, test_node, *, raw_script=False): self._test_node = test_node self._utxos = [] - self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE - self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey']) + if raw_script: + self._address = None + self._scriptPubKey = bytes(CScript([OP_TRUE])) + else: + self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE + self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey']) def scan_blocks(self, *, start=1, num): """Scan the blocks for self._address outputs and add them to self._utxos""" @@ -47,7 +52,7 @@ class MiniWallet: def generate(self, num_blocks): """Generate blocks with coinbase outputs to the internal address, and append the outputs to the internal list""" - blocks = self._test_node.generatetoaddress(num_blocks, self._address) + blocks = self._test_node.generatetodescriptor(num_blocks, f'raw({self._scriptPubKey.hex()})') for b in blocks: cb_tx = self._test_node.getblock(blockhash=b, verbosity=2)['tx'][0] self._utxos.append({'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']}) @@ -89,8 +94,12 @@ class MiniWallet: tx = CTransaction() tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), utxo_to_spend['vout']))] tx.vout = [CTxOut(int(send_value * COIN), self._scriptPubKey)] - tx.wit.vtxinwit = [CTxInWitness()] - tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])] + if not self._address: + # raw script + tx.vin[0].scriptSig = CScript([OP_NOP] * 35) # pad to identical size + else: + tx.wit.vtxinwit = [CTxInWitness()] + tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])] tx_hex = tx.serialize().hex() tx_info = from_node.testmempoolaccept([tx_hex])[0] @@ -98,7 +107,7 @@ class MiniWallet: if mempool_valid: assert_equal(tx_info['vsize'], vsize) assert_equal(tx_info['fees']['base'], fee) - return {'txid': tx_info['txid'], 'wtxid': tx_info['wtxid'], 'hex': tx_hex} + return {'txid': tx_info['txid'], 'wtxid': tx_info['wtxid'], 'hex': tx_hex, 'tx': tx} def sendrawtransaction(self, *, from_node, tx_hex): from_node.sendrawtransaction(tx_hex) -- cgit v1.2.3