Skip to content

Commit 8b7ea28

Browse files
kwvgUdjinM6
andcommitted
merge bitcoin#21754: Run feature_cltv with MiniWallet
Co-authored-by: UdjinM6 <[email protected]>
1 parent bd75014 commit 8b7ea28

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

test/functional/feature_cltv.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from test_framework.blocktools import (
1212
create_block,
1313
create_coinbase,
14-
create_transaction,
1514
)
1615
from test_framework.messages import (
1716
CTransaction,
@@ -29,10 +28,8 @@
2928
from test_framework.util import (
3029
assert_equal,
3130
assert_raises_rpc_error,
32-
hex_str_to_bytes,
3331
)
34-
35-
from io import BytesIO
32+
from test_framework.wallet import MiniWallet
3633

3734
CLTV_HEIGHT = 1351
3835

@@ -41,19 +38,14 @@
4138
# 1) prepending a given script to the scriptSig of vin 0 and
4239
# 2) (optionally) modify the nSequence of vin 0 and the tx's nLockTime
4340
def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None):
41+
assert_equal(len(tx.vin), 1)
4442
if nsequence is not None:
4543
tx.vin[0].nSequence = nsequence
4644
tx.nLockTime = nlocktime
4745

48-
# Need to re-sign, since nSequence and nLockTime changed
49-
signed_result = node.signrawtransactionwithwallet(tx.serialize().hex())
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
46+
tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(tx.vin[0].scriptSig)))
47+
tx.rehash()
48+
return tx
5749

5850

5951
def cltv_invalidate(node, tx, failure_reason):
@@ -108,27 +100,23 @@ def test_cltv_info(self, *, is_active):
108100
},
109101
)
110102

111-
def skip_test_if_missing_module(self):
112-
self.skip_if_no_wallet()
113-
114103
def run_test(self):
115104
peer = self.nodes[0].add_p2p_connection(P2PInterface())
105+
wallet = MiniWallet(self.nodes[0], raw_script=True)
116106

117107
self.test_cltv_info(is_active=False)
118108

119109
self.log.info("Mining %d blocks", CLTV_HEIGHT - 2)
120-
self.coinbase_txids = [self.nodes[0].getblock(b)['tx'][0] for b in self.nodes[0].generate(CLTV_HEIGHT - 2)]
121-
self.nodeaddress = self.nodes[0].getnewaddress()
110+
wallet.generate(10)
111+
self.nodes[0].generate(CLTV_HEIGHT - 2 - 10)
122112

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

125115
# create one invalid tx per CLTV failure reason (5 in total) and collect them
126116
invalid_ctlv_txs = []
127117
for i in range(5):
128-
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[i],
129-
self.nodeaddress, amount=1.0)
118+
spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx']
130119
spendtx = cltv_invalidate(self.nodes[0], spendtx, i)
131-
spendtx.rehash()
132120
invalid_ctlv_txs.append(spendtx)
133121

134122
tip = self.nodes[0].getbestblockhash()
@@ -162,10 +150,8 @@ def run_test(self):
162150

163151
# create and test one invalid tx per CLTV failure reason (5 in total)
164152
for i in range(5):
165-
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[10+i],
166-
self.nodeaddress, amount=1.0)
153+
spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx']
167154
spendtx = cltv_invalidate(self.nodes[0], spendtx, i)
168-
spendtx.rehash()
169155

170156
expected_cltv_reject_reason = [
171157
"non-mandatory-script-verify-flag (Operation not valid with the current stack size)",
@@ -191,7 +177,6 @@ def run_test(self):
191177

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

196181
block.vtx.pop(1)
197182
block.vtx.append(spendtx)

test/functional/test_framework/wallet.py

Lines changed: 15 additions & 6 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_P2SH_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_P2SH_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,15 +94,19 @@ 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.vin[0].scriptSig = CScript([CScript([OP_TRUE])])
97+
if not self._address:
98+
# raw script
99+
tx.vin[0].scriptSig = CScript([OP_NOP] * 24) # pad to identical size
100+
else:
101+
tx.vin[0].scriptSig = CScript([CScript([OP_TRUE])])
93102
tx_hex = tx.serialize().hex()
94103

95104
tx_info = from_node.testmempoolaccept([tx_hex])[0]
96105
assert_equal(mempool_valid, tx_info['allowed'])
97106
if mempool_valid:
98107
assert_equal(len(tx_hex) // 2, vsize)
99108
assert_equal(tx_info['fees']['base'], fee)
100-
return {'txid': tx_info['txid'], 'hex': tx_hex}
109+
return {'txid': tx_info['txid'], 'hex': tx_hex, 'tx': tx}
101110

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

0 commit comments

Comments
 (0)