55
55
assert_equal ,
56
56
assert_greater_than_or_equal ,
57
57
)
58
+ from test_framework .blocktools import COINBASE_MATURITY
58
59
59
60
DEFAULT_FEE = Decimal ("0.0001" )
60
61
@@ -100,8 +101,8 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
100
101
self ._address , self ._internal_key = create_deterministic_address_bcrt1_p2tr_op_true ()
101
102
self ._scriptPubKey = bytes .fromhex (self ._test_node .validateaddress (self ._address )['scriptPubKey' ])
102
103
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 }
105
106
106
107
def _bulk_tx (self , tx , target_weight ):
107
108
"""Pad a transaction with extra outputs until it reaches a target weight (or higher).
@@ -124,7 +125,13 @@ def rescan_utxos(self):
124
125
res = self ._test_node .scantxoutset (action = "start" , scanobjects = [self .get_descriptor ()])
125
126
assert_equal (True , res ['success' ])
126
127
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 ))
128
135
129
136
def scan_tx (self , tx ):
130
137
"""Scan the tx and adjust the internal list of owned utxos"""
@@ -139,7 +146,7 @@ def scan_tx(self, tx):
139
146
pass
140
147
for out in tx ['vout' ]:
141
148
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 ))
143
150
144
151
def scan_txs (self , txs ):
145
152
for tx in txs :
@@ -212,9 +219,13 @@ def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=
212
219
else :
213
220
return self ._utxos [index ]
214
221
215
- def get_utxos (self , * , mark_as_spent = True ):
222
+ def get_utxos (self , * , include_immature_coinbase = False , mark_as_spent = True ):
216
223
"""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 ))
218
229
if mark_as_spent :
219
230
self ._utxos = []
220
231
return utxos
@@ -293,6 +304,8 @@ def create_self_transfer_multi(
293
304
vout = i ,
294
305
value = Decimal (tx .vout [i ].nValue ) / COIN ,
295
306
height = 0 ,
307
+ coinbase = False ,
308
+ confirmations = 0 ,
296
309
) for i in range (len (tx .vout ))],
297
310
"txid" : txid ,
298
311
"hex" : tx .serialize ().hex (),
0 commit comments