Skip to content

Commit 6cebac5

Browse files
committed
test: MiniWallet: introduce enum type for output mode
For the MiniWallet constructor, the two boolean parameters "raw_script" and "use_p2pk" are replaced by a single parameter of the newly introduced type MiniWalletMode (derived by enum.Enum), which can hold the following values: - ADDRESS_OP_TRUE - RAW_OP_TRUE - RAW_P2PK
1 parent ce4a852 commit 6cebac5

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
@@ -26,7 +26,10 @@
2626
)
2727
from test_framework.test_framework import BitcoinTestFramework
2828
from test_framework.util import assert_equal
29-
from test_framework.wallet import MiniWallet
29+
from test_framework.wallet import (
30+
MiniWallet,
31+
MiniWalletMode,
32+
)
3033

3134
CLTV_HEIGHT = 1351
3235

@@ -97,7 +100,7 @@ def test_cltv_info(self, *, is_active):
97100

98101
def run_test(self):
99102
peer = self.nodes[0].add_p2p_connection(P2PInterface())
100-
wallet = MiniWallet(self.nodes[0], raw_script=True)
103+
wallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_OP_TRUE)
101104

102105
self.test_cltv_info(is_active=False)
103106

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
@@ -181,7 +184,7 @@ def send_blocks(self, blocks, success=True, reject_reason=None):
181184

182185
def run_test(self):
183186
self.helper_peer = self.nodes[0].add_p2p_connection(P2PDataStore())
184-
self.miniwallet = MiniWallet(self.nodes[0], use_p2pk=True)
187+
self.miniwallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_P2PK)
185188

186189
self.log.info("Generate blocks in the past for coinbase outputs.")
187190
long_past_time = int(time.time()) - 600 * 1000 # enough to build up to 1000 blocks 10 minutes apart without worrying about getting into the future

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_P2WSH_OP_TRUE
910
from test_framework.key import ECKey
1011
from test_framework.messages import (
@@ -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 bech32 P2WSH 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_P2WSH_OP_TRUE
5075
self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey'])
5176

0 commit comments

Comments
 (0)