Skip to content

Commit be8d0db

Browse files
committed
test: refactor: save MiniWallet mode explicitly
Rather than abusing the member variables self._priv_key and self._address to determine the MiniWallet mode, save it explicitly instead in the constructor to increase the readability and maintainability of the code.
1 parent a09033e commit be8d0db

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

test/functional/test_framework/wallet.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ class MiniWallet:
8686
def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
8787
self._test_node = test_node
8888
self._utxos = []
89-
self._priv_key = None
90-
self._address = None
89+
self._mode = mode
9190

9291
assert isinstance(mode, MiniWalletMode)
9392
if mode == MiniWalletMode.RAW_OP_TRUE:
@@ -121,7 +120,7 @@ def scan_tx(self, tx):
121120

122121
def sign_tx(self, tx, fixed_length=True):
123122
"""Sign tx that has been created by MiniWallet in P2PK mode"""
124-
assert self._priv_key is not None
123+
assert_equal(self._mode, MiniWalletMode.RAW_P2PK)
125124
(sighash, err) = LegacySignatureHash(CScript(self._scriptPubKey), tx, 0, SIGHASH_ALL)
126125
assert err is None
127126
# for exact fee calculation, create only signatures with fixed size by default (>49.89% probability):
@@ -151,6 +150,7 @@ def get_descriptor(self):
151150
return descsum_create(f'raw({self._scriptPubKey.hex()})')
152151

153152
def get_address(self):
153+
assert_equal(self._mode, MiniWalletMode.ADDRESS_OP_TRUE)
154154
return self._address
155155

156156
def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=True) -> dict:
@@ -257,28 +257,28 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node=None, utx
257257
"""Create and return a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
258258
from_node = from_node or self._test_node
259259
utxo_to_spend = utxo_to_spend or self.get_utxo()
260-
if self._priv_key is None:
260+
if self._mode in (MiniWalletMode.RAW_OP_TRUE, MiniWalletMode.ADDRESS_OP_TRUE):
261261
vsize = Decimal(104) # anyone-can-spend
262-
else:
262+
elif self._mode == MiniWalletMode.RAW_P2PK:
263263
vsize = Decimal(168) # P2PK (73 bytes scriptSig + 35 bytes scriptPubKey + 60 bytes other)
264+
else:
265+
assert False
264266
send_value = int(COIN * (utxo_to_spend['value'] - fee_rate * (vsize / 1000)))
265267
assert send_value > 0
266268

267269
tx = CTransaction()
268270
tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), utxo_to_spend['vout']), nSequence=sequence)]
269271
tx.vout = [CTxOut(send_value, self._scriptPubKey)]
270272
tx.nLockTime = locktime
271-
if not self._address:
272-
# raw script
273-
if self._priv_key is not None:
274-
# P2PK, need to sign
275-
self.sign_tx(tx)
276-
else:
277-
# anyone-can-spend
278-
tx.vin[0].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
279-
else:
273+
if self._mode == MiniWalletMode.RAW_P2PK:
274+
self.sign_tx(tx)
275+
elif self._mode == MiniWalletMode.RAW_OP_TRUE:
276+
tx.vin[0].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
277+
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
280278
tx.wit.vtxinwit = [CTxInWitness()]
281279
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key]
280+
else:
281+
assert False
282282
tx_hex = tx.serialize().hex()
283283

284284
assert_equal(tx.get_vsize(), vsize)

0 commit comments

Comments
 (0)