Skip to content

Commit fa066f1

Browse files
author
MarcoFalke
committed
test: Run feature_cltv with MiniWallet
1 parent fa5591d commit fa066f1

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

test/functional/feature_cltv.py

Lines changed: 8 additions & 22 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,15 +35,11 @@
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-
tx = CTransaction()
51-
tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex'])))
52-
5343
tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(tx.vin[0].scriptSig)))
5444
tx.rehash()
5545
return tx
@@ -97,9 +87,6 @@ def set_test_params(self):
9787
self.setup_clean_chain = True
9888
self.rpc_timeout = 480
9989

100-
def skip_test_if_missing_module(self):
101-
self.skip_if_no_wallet()
102-
10390
def test_cltv_info(self, *, is_active):
10491
assert_equal(self.nodes[0].getblockchaininfo()['softforks']['bip65'], {
10592
"active": is_active,
@@ -110,20 +97,20 @@ def test_cltv_info(self, *, is_active):
11097

11198
def run_test(self):
11299
peer = self.nodes[0].add_p2p_connection(P2PInterface())
100+
wallet = MiniWallet(self.nodes[0], raw_script=True)
113101

114102
self.test_cltv_info(is_active=False)
115103

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

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

122110
# create one invalid tx per CLTV failure reason (5 in total) and collect them
123111
invalid_ctlv_txs = []
124112
for i in range(5):
125-
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[i],
126-
self.nodeaddress, amount=1.0)
113+
spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx']
127114
spendtx = cltv_invalidate(self.nodes[0], spendtx, i)
128115
invalid_ctlv_txs.append(spendtx)
129116

@@ -158,8 +145,7 @@ def run_test(self):
158145

159146
# create and test one invalid tx per CLTV failure reason (5 in total)
160147
for i in range(5):
161-
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[10+i],
162-
self.nodeaddress, amount=1.0)
148+
spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx']
163149
spendtx = cltv_invalidate(self.nodes[0], spendtx, i)
164150

165151
expected_cltv_reject_reason = [

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)