Skip to content

Commit aa26797

Browse files
committed
test: MiniWallet: add send_to method to create arbitrary txouts
With this new method, outputs to an arbitrary scriptPubKey/amount can be created. Note that the implementation was already present in the test feature_rbf.py and is just moved to the MiniWallet interface, in order to enable other tests to also use it.
1 parent 632be55 commit aa26797

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

test/functional/feature_rbf.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from test_framework.test_framework import BitcoinTestFramework
2020
from test_framework.util import (
2121
assert_equal,
22-
assert_greater_than,
2322
assert_raises_rpc_error,
2423
)
2524
from test_framework.script_util import (
@@ -96,23 +95,10 @@ def run_test(self):
9695
def make_utxo(self, node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
9796
"""Create a txout with a given amount and scriptPubKey
9897
99-
Assumes that MiniWallet has enough funds to cover the amount and the fixed fee
100-
(from it's internal utxos, the one with the largest value is taken).
101-
10298
confirmed - txouts created will be confirmed in the blockchain;
10399
unconfirmed otherwise.
104100
"""
105-
# MiniWallet only supports sweeping utxos to its own internal scriptPubKey, so in
106-
# order to create an output with arbitrary amount/scriptPubKey, we have to add it
107-
# manually after calling the create_self_transfer method. The MiniWallet output's
108-
# nValue has to be adapted accordingly (amount and fee deduction). To keep things
109-
# simple, we use a fixed fee of 1000 Satoshis here.
110-
fee = 1000
111-
tx = self.wallet.create_self_transfer(from_node=node, fee_rate=0, mempool_valid=False)['tx']
112-
assert_greater_than(tx.vout[0].nValue, amount + fee)
113-
tx.vout[0].nValue -= (amount + fee) # change output -> MiniWallet
114-
tx.vout.append(CTxOut(amount, scriptPubKey)) # desired output -> to be returned
115-
txid = self.wallet.sendrawtransaction(from_node=node, tx_hex=tx.serialize().hex())
101+
txid, n = self.wallet.send_to(from_node=node, scriptPubKey=scriptPubKey, amount=amount)
116102

117103
# If requested, ensure txouts are confirmed.
118104
if confirmed:
@@ -125,7 +111,7 @@ def make_utxo(self, node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRI
125111
assert new_size < mempool_size
126112
mempool_size = new_size
127113

128-
return COutPoint(int(txid, 16), 1)
114+
return COutPoint(int(txid, 16), n)
129115

130116
def test_simple_doublespend(self):
131117
"""Simple doublespend"""

test/functional/test_framework/wallet.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,25 @@ def send_self_transfer(self, **kwargs):
146146
self.sendrawtransaction(from_node=kwargs['from_node'], tx_hex=tx['hex'])
147147
return tx
148148

149+
def send_to(self, *, from_node, scriptPubKey, amount, fee=1000):
150+
"""
151+
Create and send a tx with an output to a given scriptPubKey/amount,
152+
plus a change output to our internal address. To keep things simple, a
153+
fixed fee given in Satoshi is used.
154+
155+
Note that this method fails if there is no single internal utxo
156+
available that can cover the cost for the amount and the fixed fee
157+
(the utxo with the largest value is taken).
158+
159+
Returns a tuple (txid, n) referring to the created external utxo outpoint.
160+
"""
161+
tx = self.create_self_transfer(from_node=from_node, fee_rate=0, mempool_valid=False)['tx']
162+
assert_greater_than_or_equal(tx.vout[0].nValue, amount + fee)
163+
tx.vout[0].nValue -= (amount + fee) # change output -> MiniWallet
164+
tx.vout.append(CTxOut(amount, scriptPubKey)) # arbitrary output -> to be returned
165+
txid = self.sendrawtransaction(from_node=from_node, tx_hex=tx.serialize().hex())
166+
return txid, 1
167+
149168
def create_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_spend=None, mempool_valid=True, locktime=0, sequence=0):
150169
"""Create and return a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
151170
self._utxos = sorted(self._utxos, key=lambda k: k['value'])

0 commit comments

Comments
 (0)