Skip to content

Commit 38cbf43

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#26414: test: Move tx creation to create_self_transfer_multi
0b78110 test: Move tx creation to create_self_transfer_multi (kouloumos) Pull request description: Two birds with one stone: replacement of bitcoin/bitcoin#26278 with simplification of the MiniWallet's transaction creation logic. Currently the MiniWallet creates simple txns (1 input, 1 output) with `create_self_transfer`. bitcoin/bitcoin#24637 introduced `create_self_transfer_multi` **which uses** `create_self_transfer` to create a "transaction template" which then adjusts (copy and mutate inputs and outputs) in order to create more complex multi-input multi-output transactions. This can more easily lead to issues such as bitcoin/bitcoin#26278 and is more of a maintenance burden. This PR simplifies the logic by going the other way around. Now `create_self_transfer` **uses** `create_self_transfer_multi`. The transaction creation logic has been moved to `create_self_transfer_multi` which is being called by `create_self_transfer` to construct the simple case of 1 input 1 output transaction. ACKs for top commit: MarcoFalke: ACK 0b78110 👒 Tree-SHA512: 147e577ed5444bee57865bd375b37c9b49d6539e9875c30c2667e70fcba27fe80bcb4552a4e6efb42760d34b40d5dad826883b778eaeefe29425ec081787b4bd
2 parents 5b3f05b + 0b78110 commit 38cbf43

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

test/functional/test_framework/wallet.py

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def create_self_transfer_multi(
244244
utxos_to_spend: Optional[List[dict]] = None,
245245
num_outputs=1,
246246
amount_per_output=0,
247+
locktime=0,
247248
sequence=0,
248249
fee_per_output=1000,
249250
target_weight=0
@@ -256,27 +257,29 @@ def create_self_transfer_multi(
256257
utxos_to_spend = utxos_to_spend or [self.get_utxo()]
257258
sequence = [sequence] * len(utxos_to_spend) if type(sequence) is int else sequence
258259
assert_equal(len(utxos_to_spend), len(sequence))
259-
# create simple tx template (1 input, 1 output)
260-
tx = self.create_self_transfer(
261-
fee_rate=0,
262-
utxo_to_spend=utxos_to_spend[0])["tx"]
263-
264-
# duplicate inputs, witnesses and outputs
265-
tx.vin = [deepcopy(tx.vin[0]) for _ in range(len(utxos_to_spend))]
266-
for txin, seq in zip(tx.vin, sequence):
267-
txin.nSequence = seq
268-
tx.wit.vtxinwit = [deepcopy(tx.wit.vtxinwit[0]) for _ in range(len(utxos_to_spend))]
269-
tx.vout = [deepcopy(tx.vout[0]) for _ in range(num_outputs)]
270-
271-
# adapt input prevouts
272-
for i, utxo in enumerate(utxos_to_spend):
273-
tx.vin[i] = CTxIn(COutPoint(int(utxo['txid'], 16), utxo['vout']))
274-
275-
# adapt output amounts (use fixed fee per output)
260+
261+
# calculate output amount
276262
inputs_value_total = sum([int(COIN * utxo['value']) for utxo in utxos_to_spend])
277263
outputs_value_total = inputs_value_total - fee_per_output * num_outputs
278-
for o in tx.vout:
279-
o.nValue = amount_per_output or (outputs_value_total // num_outputs)
264+
amount_per_output = amount_per_output or (outputs_value_total // num_outputs)
265+
266+
# create tx
267+
tx = CTransaction()
268+
tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), utxo_to_spend['vout']), nSequence=seq) for utxo_to_spend,seq in zip(utxos_to_spend, sequence)]
269+
tx.vout = [CTxOut(amount_per_output, bytearray(self._scriptPubKey)) for _ in range(num_outputs)]
270+
tx.nLockTime = locktime
271+
272+
if self._mode == MiniWalletMode.RAW_P2PK:
273+
self.sign_tx(tx)
274+
elif self._mode == MiniWalletMode.RAW_OP_TRUE:
275+
for i in range(len(utxos_to_spend)):
276+
tx.vin[i].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
277+
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
278+
tx.wit.vtxinwit = [CTxInWitness()] * len(utxos_to_spend)
279+
for i in range(len(utxos_to_spend)):
280+
tx.wit.vtxinwit[i].scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key]
281+
else:
282+
assert False
280283

281284
if target_weight:
282285
self._bulk_tx(tx, target_weight)
@@ -299,6 +302,7 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), fee=Decimal("0"), u
299302
utxo_to_spend = utxo_to_spend or self.get_utxo()
300303
assert fee_rate >= 0
301304
assert fee >= 0
305+
# calculate fee
302306
if self._mode in (MiniWalletMode.RAW_OP_TRUE, MiniWalletMode.ADDRESS_OP_TRUE):
303307
vsize = Decimal(104) # anyone-can-spend
304308
elif self._mode == MiniWalletMode.RAW_P2PK:
@@ -308,29 +312,12 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), fee=Decimal("0"), u
308312
send_value = utxo_to_spend["value"] - (fee or (fee_rate * vsize / 1000))
309313
assert send_value > 0
310314

311-
tx = CTransaction()
312-
tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), utxo_to_spend['vout']), nSequence=sequence)]
313-
tx.vout = [CTxOut(int(COIN * send_value), bytearray(self._scriptPubKey))]
314-
tx.nLockTime = locktime
315-
if self._mode == MiniWalletMode.RAW_P2PK:
316-
self.sign_tx(tx)
317-
elif self._mode == MiniWalletMode.RAW_OP_TRUE:
318-
tx.vin[0].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
319-
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
320-
tx.wit.vtxinwit = [CTxInWitness()]
321-
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key]
322-
else:
323-
assert False
324-
325-
assert_equal(tx.get_vsize(), vsize)
326-
327-
if target_weight:
328-
self._bulk_tx(tx, target_weight)
329-
330-
tx_hex = tx.serialize().hex()
331-
new_utxo = self._create_utxo(txid=tx.rehash(), vout=0, value=send_value, height=0)
315+
# create tx
316+
tx = self.create_self_transfer_multi(utxos_to_spend=[utxo_to_spend], locktime=locktime, sequence=sequence, amount_per_output=int(COIN * send_value), target_weight=target_weight)
317+
if not target_weight:
318+
assert_equal(tx["tx"].get_vsize(), vsize)
332319

333-
return {"txid": new_utxo["txid"], "wtxid": tx.getwtxid(), "hex": tx_hex, "tx": tx, "new_utxo": new_utxo}
320+
return {"txid": tx["txid"], "wtxid": tx["tx"].getwtxid(), "hex": tx["hex"], "tx": tx["tx"], "new_utxo": tx["new_utxos"][0]}
334321

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

0 commit comments

Comments
 (0)