mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-13 11:25:02 -05:00
Merge bitcoin/bitcoin#25430: test: refactor: save MiniWallet mode explicitly
be8d0dba15
test: refactor: save MiniWallet mode explicitly (Sebastian Falbesoner) Pull request description: Rather than abusing the member variables `self._priv_key` and `self._address` to determine the MiniWallet mode, save it explicitly (`self._mode`) in the constructor and use that instead to increase the readability and maintainability of the code. ACKs for top commit: MarcoFalke: review ACKbe8d0dba15
🔳 Tree-SHA512: 882c635e39c89911d995917a0603395158ee86dc46b26a49815756db67c61a7aa35059eddf1dc6f2933a77169941426b98bd463e60e39515a596b1b69edf89df
This commit is contained in:
commit
b178807265
1 changed files with 14 additions and 14 deletions
|
@ -86,8 +86,7 @@ class MiniWallet:
|
||||||
def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
|
def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
|
||||||
self._test_node = test_node
|
self._test_node = test_node
|
||||||
self._utxos = []
|
self._utxos = []
|
||||||
self._priv_key = None
|
self._mode = mode
|
||||||
self._address = None
|
|
||||||
|
|
||||||
assert isinstance(mode, MiniWalletMode)
|
assert isinstance(mode, MiniWalletMode)
|
||||||
if mode == MiniWalletMode.RAW_OP_TRUE:
|
if mode == MiniWalletMode.RAW_OP_TRUE:
|
||||||
|
@ -121,7 +120,7 @@ class MiniWallet:
|
||||||
|
|
||||||
def sign_tx(self, tx, fixed_length=True):
|
def sign_tx(self, tx, fixed_length=True):
|
||||||
"""Sign tx that has been created by MiniWallet in P2PK mode"""
|
"""Sign tx that has been created by MiniWallet in P2PK mode"""
|
||||||
assert self._priv_key is not None
|
assert_equal(self._mode, MiniWalletMode.RAW_P2PK)
|
||||||
(sighash, err) = LegacySignatureHash(CScript(self._scriptPubKey), tx, 0, SIGHASH_ALL)
|
(sighash, err) = LegacySignatureHash(CScript(self._scriptPubKey), tx, 0, SIGHASH_ALL)
|
||||||
assert err is None
|
assert err is None
|
||||||
# for exact fee calculation, create only signatures with fixed size by default (>49.89% probability):
|
# for exact fee calculation, create only signatures with fixed size by default (>49.89% probability):
|
||||||
|
@ -151,6 +150,7 @@ class MiniWallet:
|
||||||
return descsum_create(f'raw({self._scriptPubKey.hex()})')
|
return descsum_create(f'raw({self._scriptPubKey.hex()})')
|
||||||
|
|
||||||
def get_address(self):
|
def get_address(self):
|
||||||
|
assert_equal(self._mode, MiniWalletMode.ADDRESS_OP_TRUE)
|
||||||
return self._address
|
return self._address
|
||||||
|
|
||||||
def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=True) -> dict:
|
def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=True) -> dict:
|
||||||
|
@ -257,10 +257,12 @@ class MiniWallet:
|
||||||
"""Create and return a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
|
"""Create and return a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
|
||||||
from_node = from_node or self._test_node
|
from_node = from_node or self._test_node
|
||||||
utxo_to_spend = utxo_to_spend or self.get_utxo()
|
utxo_to_spend = utxo_to_spend or self.get_utxo()
|
||||||
if self._priv_key is None:
|
if self._mode in (MiniWalletMode.RAW_OP_TRUE, MiniWalletMode.ADDRESS_OP_TRUE):
|
||||||
vsize = Decimal(104) # anyone-can-spend
|
vsize = Decimal(104) # anyone-can-spend
|
||||||
else:
|
elif self._mode == MiniWalletMode.RAW_P2PK:
|
||||||
vsize = Decimal(168) # P2PK (73 bytes scriptSig + 35 bytes scriptPubKey + 60 bytes other)
|
vsize = Decimal(168) # P2PK (73 bytes scriptSig + 35 bytes scriptPubKey + 60 bytes other)
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
send_value = int(COIN * (utxo_to_spend['value'] - fee_rate * (vsize / 1000)))
|
send_value = int(COIN * (utxo_to_spend['value'] - fee_rate * (vsize / 1000)))
|
||||||
assert send_value > 0
|
assert send_value > 0
|
||||||
|
|
||||||
|
@ -268,17 +270,15 @@ class MiniWallet:
|
||||||
tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), utxo_to_spend['vout']), nSequence=sequence)]
|
tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), utxo_to_spend['vout']), nSequence=sequence)]
|
||||||
tx.vout = [CTxOut(send_value, self._scriptPubKey)]
|
tx.vout = [CTxOut(send_value, self._scriptPubKey)]
|
||||||
tx.nLockTime = locktime
|
tx.nLockTime = locktime
|
||||||
if not self._address:
|
if self._mode == MiniWalletMode.RAW_P2PK:
|
||||||
# raw script
|
|
||||||
if self._priv_key is not None:
|
|
||||||
# P2PK, need to sign
|
|
||||||
self.sign_tx(tx)
|
self.sign_tx(tx)
|
||||||
else:
|
elif self._mode == MiniWalletMode.RAW_OP_TRUE:
|
||||||
# anyone-can-spend
|
|
||||||
tx.vin[0].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
|
tx.vin[0].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
|
||||||
else:
|
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
|
||||||
tx.wit.vtxinwit = [CTxInWitness()]
|
tx.wit.vtxinwit = [CTxInWitness()]
|
||||||
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key]
|
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key]
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
tx_hex = tx.serialize().hex()
|
tx_hex = tx.serialize().hex()
|
||||||
|
|
||||||
assert_equal(tx.get_vsize(), vsize)
|
assert_equal(tx.get_vsize(), vsize)
|
||||||
|
|
Loading…
Add table
Reference in a new issue