Skip to content

Commit d086961

Browse files
committed
[test framework] add ability to spend only confirmed utxos
Useful to ensure that the topologies of packages/transactions are as expected, preventing bugs caused by having unexpected mempool ancestors.
1 parent 3ea71fe commit d086961

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
@@ -208,7 +208,7 @@ def get_address(self):
208208
assert_equal(self._mode, MiniWalletMode.ADDRESS_OP_TRUE)
209209
return self._address
210210

211-
def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=True) -> dict:
211+
def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=True, confirmed_only=False) -> dict:
212212
"""
213213
Returns a utxo and marks it as spent (pops it from the internal list)
214214
@@ -224,19 +224,23 @@ def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=
224224
utxo_filter = reversed(mature_coins) # By default the largest utxo
225225
if vout is not None:
226226
utxo_filter = filter(lambda utxo: vout == utxo['vout'], utxo_filter)
227+
if confirmed_only:
228+
utxo_filter = filter(lambda utxo: utxo['confirmations'] > 0, utxo_filter)
227229
index = self._utxos.index(next(utxo_filter))
228230
if mark_as_spent:
229231
return self._utxos.pop(index)
230232
else:
231233
return self._utxos[index]
232234

233-
def get_utxos(self, *, include_immature_coinbase=False, mark_as_spent=True):
235+
def get_utxos(self, *, include_immature_coinbase=False, mark_as_spent=True, confirmed_only=False):
234236
"""Returns the list of all utxos and optionally mark them as spent"""
235237
if not include_immature_coinbase:
236238
blocks_height = self._test_node.getblockchaininfo()['blocks']
237239
utxo_filter = filter(lambda utxo: not utxo['coinbase'] or COINBASE_MATURITY - 1 <= blocks_height - utxo['height'], self._utxos)
238240
else:
239241
utxo_filter = self._utxos
242+
if confirmed_only:
243+
utxo_filter = filter(lambda utxo: utxo['confirmations'] > 0, utxo_filter)
240244
utxos = deepcopy(list(utxo_filter))
241245
if mark_as_spent:
242246
self._utxos = []
@@ -286,14 +290,15 @@ def create_self_transfer_multi(
286290
locktime=0,
287291
sequence=0,
288292
fee_per_output=1000,
289-
target_weight=0
293+
target_weight=0,
294+
confirmed_only=False
290295
):
291296
"""
292297
Create and return a transaction that spends the given UTXOs and creates a
293298
certain number of outputs with equal amounts. The output amounts can be
294299
set by amount_per_output or automatically calculated with a fee_per_output.
295300
"""
296-
utxos_to_spend = utxos_to_spend or [self.get_utxo()]
301+
utxos_to_spend = utxos_to_spend or [self.get_utxo(confirmed_only=confirmed_only)]
297302
sequence = [sequence] * len(utxos_to_spend) if type(sequence) is int else sequence
298303
assert_equal(len(utxos_to_spend), len(sequence))
299304

@@ -333,9 +338,17 @@ def create_self_transfer_multi(
333338
"tx": tx,
334339
}
335340

336-
def create_self_transfer(self, *, fee_rate=Decimal("0.003"), fee=Decimal("0"), utxo_to_spend=None, locktime=0, sequence=0, target_weight=0):
341+
def create_self_transfer(self, *,
342+
fee_rate=Decimal("0.003"),
343+
fee=Decimal("0"),
344+
utxo_to_spend=None,
345+
locktime=0,
346+
sequence=0,
347+
target_weight=0,
348+
confirmed_only=False
349+
):
337350
"""Create and return a tx with the specified fee. If fee is 0, use fee_rate, where the resulting fee may be exact or at most one satoshi higher than needed."""
338-
utxo_to_spend = utxo_to_spend or self.get_utxo()
351+
utxo_to_spend = utxo_to_spend or self.get_utxo(confirmed_only=confirmed_only)
339352
assert fee_rate >= 0
340353
assert fee >= 0
341354
# calculate fee

0 commit comments

Comments
 (0)