Skip to content

Commit fa34e44

Browse files
author
MacroFake
committed
test: Return new_utxo from create_self_transfer in MiniWallet
1 parent dde7205 commit fa34e44

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

test/functional/mempool_reorg.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ def run_test(self):
6868
assert_raises_rpc_error(-26, 'non-final', self.nodes[0].sendrawtransaction, timelock_tx)
6969

7070
self.log.info("Create spend_2_1 and spend_3_1")
71-
spend_2_utxo = wallet.get_utxo(txid=spend_2['txid'])
72-
spend_2_1 = wallet.create_self_transfer(utxo_to_spend=spend_2_utxo)
73-
spend_3_utxo = wallet.get_utxo(txid=spend_3['txid'])
74-
spend_3_1 = wallet.create_self_transfer(utxo_to_spend=spend_3_utxo)
71+
spend_2_1 = wallet.create_self_transfer(utxo_to_spend=spend_2["new_utxo"])
72+
spend_3_1 = wallet.create_self_transfer(utxo_to_spend=spend_3["new_utxo"])
7573

7674
self.log.info("Broadcast and mine spend_3_1")
7775
spend_3_1_id = self.nodes[0].sendrawtransaction(spend_3_1['hex'])

test/functional/test_framework/wallet.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
101101
self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true()
102102
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])
103103

104+
def _create_utxo(self, *, txid, vout, value, height):
105+
return {"txid": txid, "vout": vout, "value": value, "height": height}
106+
104107
def get_balance(self):
105108
return sum(u['value'] for u in self._utxos)
106109

@@ -110,13 +113,13 @@ def rescan_utxos(self):
110113
res = self._test_node.scantxoutset(action="start", scanobjects=[self.get_descriptor()])
111114
assert_equal(True, res['success'])
112115
for utxo in res['unspents']:
113-
self._utxos.append({'txid': utxo['txid'], 'vout': utxo['vout'], 'value': utxo['amount'], 'height': utxo['height']})
116+
self._utxos.append(self._create_utxo(txid=utxo["txid"], vout=utxo["vout"], value=utxo["amount"], height=utxo["height"]))
114117

115118
def scan_tx(self, tx):
116119
"""Scan the tx for self._scriptPubKey outputs and add them to self._utxos"""
117120
for out in tx['vout']:
118121
if out['scriptPubKey']['hex'] == self._scriptPubKey.hex():
119-
self._utxos.append({'txid': tx['txid'], 'vout': out['n'], 'value': out['value'], 'height': 0})
122+
self._utxos.append(self._create_utxo(txid=tx["txid"], vout=out["n"], value=out["value"], height=0))
120123

121124
def sign_tx(self, tx, fixed_length=True):
122125
"""Sign tx that has been created by MiniWallet in P2PK mode"""
@@ -140,7 +143,7 @@ def generate(self, num_blocks, **kwargs):
140143
for b in blocks:
141144
block_info = self._test_node.getblock(blockhash=b, verbosity=2)
142145
cb_tx = block_info['tx'][0]
143-
self._utxos.append({'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value'], 'height': block_info['height']})
146+
self._utxos.append(self._create_utxo(txid=cb_tx["txid"], vout=0, value=cb_tx["vout"][0]["value"], height=block_info["height"]))
144147
return blocks
145148

146149
def get_scriptPubKey(self):
@@ -264,12 +267,12 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), utxo_to_spend=None,
264267
vsize = Decimal(168) # P2PK (73 bytes scriptSig + 35 bytes scriptPubKey + 60 bytes other)
265268
else:
266269
assert False
267-
send_value = int(COIN * (utxo_to_spend['value'] - fee_rate * (vsize / 1000)))
270+
send_value = utxo_to_spend["value"] - (fee_rate * vsize / 1000)
268271
assert send_value > 0
269272

270273
tx = CTransaction()
271274
tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), utxo_to_spend['vout']), nSequence=sequence)]
272-
tx.vout = [CTxOut(send_value, self._scriptPubKey)]
275+
tx.vout = [CTxOut(int(COIN * send_value), self._scriptPubKey)]
273276
tx.nLockTime = locktime
274277
if self._mode == MiniWalletMode.RAW_P2PK:
275278
self.sign_tx(tx)
@@ -283,8 +286,9 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), utxo_to_spend=None,
283286
tx_hex = tx.serialize().hex()
284287

285288
assert_equal(tx.get_vsize(), vsize)
289+
new_utxo = self._create_utxo(txid=tx.rehash(), vout=0, value=send_value, height=0)
286290

287-
return {'txid': tx.rehash(), 'wtxid': tx.getwtxid(), 'hex': tx_hex, 'tx': tx}
291+
return {"txid": new_utxo["txid"], "wtxid": tx.getwtxid(), "hex": tx_hex, "tx": tx, "new_utxo": new_utxo}
288292

289293
def sendrawtransaction(self, *, from_node, tx_hex, maxfeerate=0, **kwargs):
290294
txid = from_node.sendrawtransaction(hexstring=tx_hex, maxfeerate=maxfeerate, **kwargs)

0 commit comments

Comments
 (0)