Skip to content

Commit d0a909a

Browse files
committed
test: Add "include immature coinbase" flag to MiniWallet get_utxos
1 parent e5b9127 commit d0a909a

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

test/functional/test_framework/wallet.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
assert_equal,
5656
assert_greater_than_or_equal,
5757
)
58+
from test_framework.blocktools import COINBASE_MATURITY
5859

5960
DEFAULT_FEE = Decimal("0.0001")
6061

@@ -100,8 +101,8 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
100101
self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true()
101102
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])
102103

103-
def _create_utxo(self, *, txid, vout, value, height):
104-
return {"txid": txid, "vout": vout, "value": value, "height": height}
104+
def _create_utxo(self, *, txid, vout, value, height, coinbase, confirmations):
105+
return {"txid": txid, "vout": vout, "value": value, "height": height, "coinbase": coinbase, "confirmations": confirmations}
105106

106107
def _bulk_tx(self, tx, target_weight):
107108
"""Pad a transaction with extra outputs until it reaches a target weight (or higher).
@@ -124,7 +125,13 @@ def rescan_utxos(self):
124125
res = self._test_node.scantxoutset(action="start", scanobjects=[self.get_descriptor()])
125126
assert_equal(True, res['success'])
126127
for utxo in res['unspents']:
127-
self._utxos.append(self._create_utxo(txid=utxo["txid"], vout=utxo["vout"], value=utxo["amount"], height=utxo["height"]))
128+
self._utxos.append(
129+
self._create_utxo(txid=utxo["txid"],
130+
vout=utxo["vout"],
131+
value=utxo["amount"],
132+
height=utxo["height"],
133+
coinbase=utxo["coinbase"],
134+
confirmations=res["height"] - utxo["height"] + 1))
128135

129136
def scan_tx(self, tx):
130137
"""Scan the tx and adjust the internal list of owned utxos"""
@@ -139,7 +146,7 @@ def scan_tx(self, tx):
139146
pass
140147
for out in tx['vout']:
141148
if out['scriptPubKey']['hex'] == self._scriptPubKey.hex():
142-
self._utxos.append(self._create_utxo(txid=tx["txid"], vout=out["n"], value=out["value"], height=0))
149+
self._utxos.append(self._create_utxo(txid=tx["txid"], vout=out["n"], value=out["value"], height=0, coinbase=False, confirmations=0))
143150

144151
def scan_txs(self, txs):
145152
for tx in txs:
@@ -212,9 +219,13 @@ def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=
212219
else:
213220
return self._utxos[index]
214221

215-
def get_utxos(self, *, mark_as_spent=True):
222+
def get_utxos(self, *, include_immature_coinbase=False, mark_as_spent=True):
216223
"""Returns the list of all utxos and optionally mark them as spent"""
217-
utxos = deepcopy(self._utxos)
224+
if not include_immature_coinbase:
225+
utxo_filter = filter(lambda utxo: not utxo['coinbase'] or COINBASE_MATURITY <= utxo['confirmations'], self._utxos)
226+
else:
227+
utxo_filter = self._utxos
228+
utxos = deepcopy(list(utxo_filter))
218229
if mark_as_spent:
219230
self._utxos = []
220231
return utxos
@@ -293,6 +304,8 @@ def create_self_transfer_multi(
293304
vout=i,
294305
value=Decimal(tx.vout[i].nValue) / COIN,
295306
height=0,
307+
coinbase=False,
308+
confirmations=0,
296309
) for i in range(len(tx.vout))],
297310
"txid": txid,
298311
"hex": tx.serialize().hex(),

0 commit comments

Comments
 (0)