Skip to content

Commit 7a74805

Browse files
MarcoFalkeknst
authored andcommitted
Merge bitcoin#22048: test: MiniWallet: introduce enum type for output mode
6cebac5 test: MiniWallet: introduce enum type for output mode (Sebastian Falbesoner) Pull request description: This is a follow-up PR to bitcoin#21945 which lifted the number of MiniWallet's tx output modes from 2 to 3 (by adding P2PK Support). Since the current way of specifying the mode on the ctor via two booleans is ugly and error-prone (see table in comment bitcoin#21945 (comment)), a new Enum type `MiniWalletMode` is introduced that can hold the following values: - ADDRESS_OP_TRUE - RAW_OP_TRUE - RAW_P2PK Also adds documentation that should guide the user on which mode is useful for what etc. with a summary table. (Can also be split up in a separate commit or shortened if that is desired, maybe it's considered to be too verbose). ACKs for top commit: MarcoFalke: cr ACK 6cebac5 Tree-SHA512: cbbc10806d9d9e62829548094415e9f1a281cd247b9a9fc7f7f33b923c723aa03e7bc3024623a77fb1f7da4d73455fa8244840f746980d32acdad97ee12100da
1 parent 2e3d084 commit 7a74805

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

test/functional/feature_cltv.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
assert_equal,
3030
assert_raises_rpc_error,
3131
)
32-
from test_framework.wallet import MiniWallet
32+
from test_framework.wallet import (
33+
MiniWallet,
34+
MiniWalletMode,
35+
)
3336

3437
CLTV_HEIGHT = 1351
3538

@@ -101,7 +104,7 @@ def test_cltv_info(self, *, is_active):
101104

102105
def run_test(self):
103106
peer = self.nodes[0].add_p2p_connection(P2PInterface())
104-
wallet = MiniWallet(self.nodes[0], raw_script=True)
107+
wallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_OP_TRUE)
105108

106109
self.test_cltv_info(is_active=False)
107110

test/functional/feature_csv_activation.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@
5555
assert_equal,
5656
softfork_active,
5757
)
58-
from test_framework.wallet import MiniWallet
58+
from test_framework.wallet import (
59+
MiniWallet,
60+
MiniWalletMode,
61+
)
5962

6063
TESTING_TX_COUNT = 83 # Number of testing transactions: 1 BIP113 tx, 16 BIP68 txs, 66 BIP112 txs (see comments above)
6164
COINBASE_BLOCK_COUNT = TESTING_TX_COUNT # Number of coinbase blocks we need to generate as inputs for our txs
@@ -187,7 +190,7 @@ def send_blocks(self, blocks, success=True, reject_reason=None):
187190

188191
def run_test(self):
189192
self.helper_peer = self.nodes[0].add_p2p_connection(P2PDataStore())
190-
self.miniwallet = MiniWallet(self.nodes[0], use_p2pk=True)
193+
self.miniwallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_P2PK)
191194

192195
self.log.info("Generate blocks in the past for coinbase outputs.")
193196
self.coinbase_blocks = self.miniwallet.generate(COINBASE_BLOCK_COUNT) # blocks generated for inputs

test/functional/test_framework/wallet.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""A limited-functionality wallet, which may replace a real wallet in tests"""
66

77
from decimal import Decimal
8+
from enum import Enum
89
from test_framework.address import ADDRESS_BCRT1_P2SH_OP_TRUE
910
from test_framework.key import ECKey
1011
from typing import Optional
@@ -30,22 +31,46 @@
3031
)
3132

3233

34+
class MiniWalletMode(Enum):
35+
"""Determines the transaction type the MiniWallet is creating and spending.
36+
37+
For most purposes, the default mode ADDRESS_OP_TRUE should be sufficient;
38+
it simply uses a fixed P2SH address whose coins are spent with a
39+
witness stack of OP_TRUE, i.e. following an anyone-can-spend policy.
40+
However, if the transactions need to be modified by the user (e.g. prepending
41+
scriptSig for testing opcodes that are activated by a soft-fork), or the txs
42+
should contain an actual signature, the raw modes RAW_OP_TRUE and RAW_P2PK
43+
can be useful. Summary of modes:
44+
45+
| output | | tx is | can modify | needs
46+
mode | description | address | standard | scriptSig | signing
47+
----------------+-------------------+-----------+----------+------------+----------
48+
ADDRESS_OP_TRUE | anyone-can-spend | bech32 | yes | no | no
49+
RAW_OP_TRUE | anyone-can-spend | - (raw) | no | yes | no
50+
RAW_P2PK | pay-to-public-key | - (raw) | yes | yes | yes
51+
"""
52+
ADDRESS_OP_TRUE = 1
53+
RAW_OP_TRUE = 2
54+
RAW_P2PK = 3
55+
56+
3357
class MiniWallet:
34-
def __init__(self, test_node, *, raw_script=False, use_p2pk=False):
58+
def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
3559
self._test_node = test_node
3660
self._utxos = []
3761
self._priv_key = None
3862
self._address = None
3963

40-
if raw_script:
64+
assert isinstance(mode, MiniWalletMode)
65+
if mode == MiniWalletMode.RAW_OP_TRUE:
4166
self._scriptPubKey = bytes(CScript([OP_TRUE]))
42-
elif use_p2pk:
67+
elif mode == MiniWalletMode.RAW_P2PK:
4368
# use simple deterministic private key (k=1)
4469
self._priv_key = ECKey()
4570
self._priv_key.set((1).to_bytes(32, 'big'), True)
4671
pub_key = self._priv_key.get_pubkey()
4772
self._scriptPubKey = bytes(CScript([pub_key.get_bytes(), OP_CHECKSIG]))
48-
else:
73+
elif mode == MiniWalletMode.ADDRESS_OP_TRUE:
4974
self._address = ADDRESS_BCRT1_P2SH_OP_TRUE
5075
self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey'])
5176

0 commit comments

Comments
 (0)