Skip to content

Commit 779aaa7

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#21754: test: Run feature_cltv with MiniWallet
fa066f1 test: Run feature_cltv with MiniWallet (MarcoFalke) fa5591d test: Hide tx rehash in helper (MarcoFalke) fa5f938 test: Remove new_tx reference (MarcoFalke) Pull request description: Allows to run the test even with no wallet compiled in ACKs for top commit: theStack: ACK fa066f1 💽 Tree-SHA512: 3f659a178ba3ee0baffd70fddf8b8a68e5551d85626c7f254b234d7f75e6a16430a32a7952037db358b579f045b4d296b46156f72e5d226f3e80334dc635ca10
2 parents dc8da2a + fa066f1 commit 779aaa7

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

test/functional/feature_cltv.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
from test_framework.blocktools import (
1212
create_block,
1313
create_coinbase,
14-
create_transaction,
1514
)
1615
from test_framework.messages import (
1716
CTransaction,
18-
ToHex,
1917
msg_block,
2018
)
2119
from test_framework.p2p import P2PInterface
@@ -27,12 +25,8 @@
2725
OP_DROP,
2826
)
2927
from test_framework.test_framework import BitcoinTestFramework
30-
from test_framework.util import (
31-
assert_equal,
32-
hex_str_to_bytes,
33-
)
34-
35-
from io import BytesIO
28+
from test_framework.util import assert_equal
29+
from test_framework.wallet import MiniWallet
3630

3731
CLTV_HEIGHT = 1351
3832

@@ -41,19 +35,14 @@
4135
# 1) prepending a given script to the scriptSig of vin 0 and
4236
# 2) (optionally) modify the nSequence of vin 0 and the tx's nLockTime
4337
def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None):
38+
assert_equal(len(tx.vin), 1)
4439
if nsequence is not None:
4540
tx.vin[0].nSequence = nsequence
4641
tx.nLockTime = nlocktime
4742

48-
# Need to re-sign, since nSequence and nLockTime changed
49-
signed_result = node.signrawtransactionwithwallet(ToHex(tx))
50-
new_tx = CTransaction()
51-
new_tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex'])))
52-
else:
53-
new_tx = tx
54-
55-
new_tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(new_tx.vin[0].scriptSig)))
56-
return new_tx
43+
tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(tx.vin[0].scriptSig)))
44+
tx.rehash()
45+
return tx
5746

5847

5948
def cltv_invalidate(node, tx, failure_reason):
@@ -98,9 +87,6 @@ def set_test_params(self):
9887
self.setup_clean_chain = True
9988
self.rpc_timeout = 480
10089

101-
def skip_test_if_missing_module(self):
102-
self.skip_if_no_wallet()
103-
10490
def test_cltv_info(self, *, is_active):
10591
assert_equal(self.nodes[0].getblockchaininfo()['softforks']['bip65'], {
10692
"active": is_active,
@@ -111,22 +97,21 @@ def test_cltv_info(self, *, is_active):
11197

11298
def run_test(self):
11399
peer = self.nodes[0].add_p2p_connection(P2PInterface())
100+
wallet = MiniWallet(self.nodes[0], raw_script=True)
114101

115102
self.test_cltv_info(is_active=False)
116103

117104
self.log.info("Mining %d blocks", CLTV_HEIGHT - 2)
118-
self.coinbase_txids = [self.nodes[0].getblock(b)['tx'][0] for b in self.nodes[0].generate(CLTV_HEIGHT - 2)]
119-
self.nodeaddress = self.nodes[0].getnewaddress()
105+
wallet.generate(10)
106+
self.nodes[0].generate(CLTV_HEIGHT - 2 - 10)
120107

121108
self.log.info("Test that invalid-according-to-CLTV transactions can still appear in a block")
122109

123110
# create one invalid tx per CLTV failure reason (5 in total) and collect them
124111
invalid_ctlv_txs = []
125112
for i in range(5):
126-
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[i],
127-
self.nodeaddress, amount=1.0)
113+
spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx']
128114
spendtx = cltv_invalidate(self.nodes[0], spendtx, i)
129-
spendtx.rehash()
130115
invalid_ctlv_txs.append(spendtx)
131116

132117
tip = self.nodes[0].getbestblockhash()
@@ -160,10 +145,8 @@ def run_test(self):
160145

161146
# create and test one invalid tx per CLTV failure reason (5 in total)
162147
for i in range(5):
163-
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[10+i],
164-
self.nodeaddress, amount=1.0)
148+
spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx']
165149
spendtx = cltv_invalidate(self.nodes[0], spendtx, i)
166-
spendtx.rehash()
167150

168151
expected_cltv_reject_reason = [
169152
"non-mandatory-script-verify-flag (Operation not valid with the current stack size)",
@@ -197,7 +180,6 @@ def run_test(self):
197180

198181
self.log.info("Test that a version 4 block with a valid-according-to-CLTV transaction is accepted")
199182
spendtx = cltv_validate(self.nodes[0], spendtx, CLTV_HEIGHT - 1)
200-
spendtx.rehash()
201183

202184
block.vtx.pop(1)
203185
block.vtx.append(spendtx)

test/functional/test_framework/wallet.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from test_framework.script import (
1818
CScript,
1919
OP_TRUE,
20+
OP_NOP,
2021
)
2122
from test_framework.util import (
2223
assert_equal,
@@ -26,11 +27,15 @@
2627

2728

2829
class MiniWallet:
29-
def __init__(self, test_node):
30+
def __init__(self, test_node, *, raw_script=False):
3031
self._test_node = test_node
3132
self._utxos = []
32-
self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE
33-
self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey'])
33+
if raw_script:
34+
self._address = None
35+
self._scriptPubKey = bytes(CScript([OP_TRUE]))
36+
else:
37+
self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE
38+
self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey'])
3439

3540
def scan_blocks(self, *, start=1, num):
3641
"""Scan the blocks for self._address outputs and add them to self._utxos"""
@@ -47,7 +52,7 @@ def scan_tx(self, tx):
4752

4853
def generate(self, num_blocks):
4954
"""Generate blocks with coinbase outputs to the internal address, and append the outputs to the internal list"""
50-
blocks = self._test_node.generatetoaddress(num_blocks, self._address)
55+
blocks = self._test_node.generatetodescriptor(num_blocks, f'raw({self._scriptPubKey.hex()})')
5156
for b in blocks:
5257
cb_tx = self._test_node.getblock(blockhash=b, verbosity=2)['tx'][0]
5358
self._utxos.append({'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']})
@@ -89,16 +94,20 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_
8994
tx = CTransaction()
9095
tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), utxo_to_spend['vout']))]
9196
tx.vout = [CTxOut(int(send_value * COIN), self._scriptPubKey)]
92-
tx.wit.vtxinwit = [CTxInWitness()]
93-
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]
97+
if not self._address:
98+
# raw script
99+
tx.vin[0].scriptSig = CScript([OP_NOP] * 35) # pad to identical size
100+
else:
101+
tx.wit.vtxinwit = [CTxInWitness()]
102+
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]
94103
tx_hex = tx.serialize().hex()
95104

96105
tx_info = from_node.testmempoolaccept([tx_hex])[0]
97106
assert_equal(mempool_valid, tx_info['allowed'])
98107
if mempool_valid:
99108
assert_equal(tx_info['vsize'], vsize)
100109
assert_equal(tx_info['fees']['base'], fee)
101-
return {'txid': tx_info['txid'], 'wtxid': tx_info['wtxid'], 'hex': tx_hex}
110+
return {'txid': tx_info['txid'], 'wtxid': tx_info['wtxid'], 'hex': tx_hex, 'tx': tx}
102111

103112
def sendrawtransaction(self, *, from_node, tx_hex):
104113
from_node.sendrawtransaction(tx_hex)

0 commit comments

Comments
 (0)