mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-04 08:51:00 +02:00
test: Add "include immature coinbase" flag to MiniWallet get_utxos
This commit is contained in:
parent
e5b9127d9e
commit
d0a909ae54
@ -55,6 +55,7 @@ from test_framework.util import (
|
|||||||
assert_equal,
|
assert_equal,
|
||||||
assert_greater_than_or_equal,
|
assert_greater_than_or_equal,
|
||||||
)
|
)
|
||||||
|
from test_framework.blocktools import COINBASE_MATURITY
|
||||||
|
|
||||||
DEFAULT_FEE = Decimal("0.0001")
|
DEFAULT_FEE = Decimal("0.0001")
|
||||||
|
|
||||||
@ -100,8 +101,8 @@ class MiniWallet:
|
|||||||
self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true()
|
self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true()
|
||||||
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])
|
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])
|
||||||
|
|
||||||
def _create_utxo(self, *, txid, vout, value, height):
|
def _create_utxo(self, *, txid, vout, value, height, coinbase, confirmations):
|
||||||
return {"txid": txid, "vout": vout, "value": value, "height": height}
|
return {"txid": txid, "vout": vout, "value": value, "height": height, "coinbase": coinbase, "confirmations": confirmations}
|
||||||
|
|
||||||
def _bulk_tx(self, tx, target_weight):
|
def _bulk_tx(self, tx, target_weight):
|
||||||
"""Pad a transaction with extra outputs until it reaches a target weight (or higher).
|
"""Pad a transaction with extra outputs until it reaches a target weight (or higher).
|
||||||
@ -124,7 +125,13 @@ class MiniWallet:
|
|||||||
res = self._test_node.scantxoutset(action="start", scanobjects=[self.get_descriptor()])
|
res = self._test_node.scantxoutset(action="start", scanobjects=[self.get_descriptor()])
|
||||||
assert_equal(True, res['success'])
|
assert_equal(True, res['success'])
|
||||||
for utxo in res['unspents']:
|
for utxo in res['unspents']:
|
||||||
self._utxos.append(self._create_utxo(txid=utxo["txid"], vout=utxo["vout"], value=utxo["amount"], height=utxo["height"]))
|
self._utxos.append(
|
||||||
|
self._create_utxo(txid=utxo["txid"],
|
||||||
|
vout=utxo["vout"],
|
||||||
|
value=utxo["amount"],
|
||||||
|
height=utxo["height"],
|
||||||
|
coinbase=utxo["coinbase"],
|
||||||
|
confirmations=res["height"] - utxo["height"] + 1))
|
||||||
|
|
||||||
def scan_tx(self, tx):
|
def scan_tx(self, tx):
|
||||||
"""Scan the tx and adjust the internal list of owned utxos"""
|
"""Scan the tx and adjust the internal list of owned utxos"""
|
||||||
@ -139,7 +146,7 @@ class MiniWallet:
|
|||||||
pass
|
pass
|
||||||
for out in tx['vout']:
|
for out in tx['vout']:
|
||||||
if out['scriptPubKey']['hex'] == self._scriptPubKey.hex():
|
if out['scriptPubKey']['hex'] == self._scriptPubKey.hex():
|
||||||
self._utxos.append(self._create_utxo(txid=tx["txid"], vout=out["n"], value=out["value"], height=0))
|
self._utxos.append(self._create_utxo(txid=tx["txid"], vout=out["n"], value=out["value"], height=0, coinbase=False, confirmations=0))
|
||||||
|
|
||||||
def scan_txs(self, txs):
|
def scan_txs(self, txs):
|
||||||
for tx in txs:
|
for tx in txs:
|
||||||
@ -212,9 +219,13 @@ class MiniWallet:
|
|||||||
else:
|
else:
|
||||||
return self._utxos[index]
|
return self._utxos[index]
|
||||||
|
|
||||||
def get_utxos(self, *, mark_as_spent=True):
|
def get_utxos(self, *, include_immature_coinbase=False, mark_as_spent=True):
|
||||||
"""Returns the list of all utxos and optionally mark them as spent"""
|
"""Returns the list of all utxos and optionally mark them as spent"""
|
||||||
utxos = deepcopy(self._utxos)
|
if not include_immature_coinbase:
|
||||||
|
utxo_filter = filter(lambda utxo: not utxo['coinbase'] or COINBASE_MATURITY <= utxo['confirmations'], self._utxos)
|
||||||
|
else:
|
||||||
|
utxo_filter = self._utxos
|
||||||
|
utxos = deepcopy(list(utxo_filter))
|
||||||
if mark_as_spent:
|
if mark_as_spent:
|
||||||
self._utxos = []
|
self._utxos = []
|
||||||
return utxos
|
return utxos
|
||||||
@ -293,6 +304,8 @@ class MiniWallet:
|
|||||||
vout=i,
|
vout=i,
|
||||||
value=Decimal(tx.vout[i].nValue) / COIN,
|
value=Decimal(tx.vout[i].nValue) / COIN,
|
||||||
height=0,
|
height=0,
|
||||||
|
coinbase=False,
|
||||||
|
confirmations=0,
|
||||||
) for i in range(len(tx.vout))],
|
) for i in range(len(tx.vout))],
|
||||||
"txid": txid,
|
"txid": txid,
|
||||||
"hex": tx.serialize().hex(),
|
"hex": tx.serialize().hex(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user