Skip to content

Commit f680d27

Browse files
committed
test: use MiniWallet for make_utxo helper in feature_rbf.py
1 parent 0f27524 commit f680d27

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

test/functional/feature_rbf.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from copy import deepcopy
88
from decimal import Decimal
99

10-
from test_framework.blocktools import COINBASE_MATURITY
1110
from test_framework.messages import (
1211
BIP125_SEQUENCE_NUMBER,
1312
COIN,
@@ -18,10 +17,18 @@
1817
)
1918
from test_framework.script import CScript, OP_DROP
2019
from test_framework.test_framework import BitcoinTestFramework
21-
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round
22-
from test_framework.script_util import DUMMY_P2WPKH_SCRIPT, DUMMY_2_P2WPKH_SCRIPT
20+
from test_framework.util import (
21+
assert_equal,
22+
assert_greater_than,
23+
assert_raises_rpc_error,
24+
)
25+
from test_framework.script_util import (
26+
DUMMY_P2WPKH_SCRIPT,
27+
DUMMY_2_P2WPKH_SCRIPT,
28+
)
2329
from test_framework.wallet import MiniWallet
2430

31+
2532
MAX_REPLACEMENT_LIMIT = 100
2633
class ReplaceByFeeTest(BitcoinTestFramework):
2734
def set_test_params(self):
@@ -89,29 +96,23 @@ def run_test(self):
8996
def make_utxo(self, node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
9097
"""Create a txout with a given amount and scriptPubKey
9198
92-
Mines coins as needed.
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).
93101
94102
confirmed - txouts created will be confirmed in the blockchain;
95103
unconfirmed otherwise.
96104
"""
97-
fee = 1 * COIN
98-
while node.getbalance() < satoshi_round((amount + fee) / COIN):
99-
self.generate(node, COINBASE_MATURITY)
100-
101-
new_addr = node.getnewaddress()
102-
txid = node.sendtoaddress(new_addr, satoshi_round((amount + fee) / COIN))
103-
tx1 = node.getrawtransaction(txid, 1)
104-
txid = int(txid, 16)
105-
i, _ = next(filter(lambda vout: new_addr == vout[1]['scriptPubKey']['address'], enumerate(tx1['vout'])))
106-
107-
tx2 = CTransaction()
108-
tx2.vin = [CTxIn(COutPoint(txid, i))]
109-
tx2.vout = [CTxOut(amount, scriptPubKey)]
110-
tx2.rehash()
111-
112-
signed_tx = node.signrawtransactionwithwallet(tx2.serialize().hex())
113-
114-
txid = node.sendrawtransaction(signed_tx['hex'], 0)
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())
115116

116117
# If requested, ensure txouts are confirmed.
117118
if confirmed:
@@ -124,7 +125,7 @@ def make_utxo(self, node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRI
124125
assert new_size < mempool_size
125126
mempool_size = new_size
126127

127-
return COutPoint(int(txid, 16), 0)
128+
return COutPoint(int(txid, 16), 1)
128129

129130
def test_simple_doublespend(self):
130131
"""Simple doublespend"""

test/functional/test_framework/wallet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_
179179
return {'txid': tx_info['txid'], 'wtxid': tx_info['wtxid'], 'hex': tx_hex, 'tx': tx}
180180

181181
def sendrawtransaction(self, *, from_node, tx_hex):
182-
from_node.sendrawtransaction(tx_hex)
182+
txid = from_node.sendrawtransaction(tx_hex)
183183
self.scan_tx(from_node.decoderawtransaction(tx_hex))
184+
return txid
185+
184186

185187
def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, parent_locking_script=None, fee=DEFAULT_FEE):
186188
"""Build a transaction that spends parent_txid.vout[n] and produces one output with

0 commit comments

Comments
 (0)