Skip to content

Commit 8973eeb

Browse files
committed
test: use MiniWallet for mining_prioritisetransaction.py
This test can now be run even with the Bitcoin Core wallet disabled.
1 parent 2b5a741 commit 8973eeb

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

test/functional/mining_prioritisetransaction.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@
77
from decimal import Decimal
88
import time
99

10-
from test_framework.blocktools import COINBASE_MATURITY
11-
from test_framework.messages import COIN, MAX_BLOCK_WEIGHT
10+
from test_framework.messages import (
11+
COIN,
12+
MAX_BLOCK_WEIGHT,
13+
)
1214
from test_framework.test_framework import BitcoinTestFramework
13-
from test_framework.util import assert_equal, assert_raises_rpc_error, create_confirmed_utxos, create_lots_of_big_transactions, gen_return_txouts
15+
from test_framework.util import (
16+
assert_equal,
17+
assert_raises_rpc_error,
18+
create_lots_of_big_transactions,
19+
gen_return_txouts,
20+
)
1421
from test_framework.wallet import MiniWallet
1522

1623

1724
class PrioritiseTransactionTest(BitcoinTestFramework):
1825
def set_test_params(self):
19-
self.setup_clean_chain = True
2026
self.num_nodes = 1
2127
self.extra_args = [[
2228
"-printpriority=1",
2329
"-acceptnonstdtxn=1",
2430
]] * self.num_nodes
2531
self.supports_cli = False
2632

27-
def skip_test_if_missing_module(self):
28-
self.skip_if_no_wallet()
29-
3033
def test_diamond(self):
3134
self.log.info("Test diamond-shape package with priority")
32-
self.generate(self.wallet, COINBASE_MATURITY + 1)
3335
mock_time = int(time.time())
3436
self.nodes[0].setmocktime(mock_time)
3537

@@ -104,6 +106,7 @@ def test_diamond(self):
104106

105107
def run_test(self):
106108
self.wallet = MiniWallet(self.nodes[0])
109+
self.wallet.rescan_utxos()
107110

108111
# Test `prioritisetransaction` required parameters
109112
assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction)
@@ -131,7 +134,10 @@ def run_test(self):
131134
self.relayfee = self.nodes[0].getnetworkinfo()['relayfee']
132135

133136
utxo_count = 90
134-
utxos = create_confirmed_utxos(self, self.relayfee, self.nodes[0], utxo_count)
137+
utxos = self.wallet.send_self_transfer_multi(from_node=self.nodes[0], num_outputs=utxo_count)['new_utxos']
138+
self.generate(self.wallet, 1)
139+
assert_equal(len(self.nodes[0].getrawmempool()), 0)
140+
135141
base_fee = self.relayfee*100 # our transactions are smaller than 100kb
136142
txids = []
137143

@@ -141,7 +147,13 @@ def run_test(self):
141147
txids.append([])
142148
start_range = i * range_size
143149
end_range = start_range + range_size
144-
txids[i] = create_lots_of_big_transactions(self.nodes[0], self.txouts, utxos[start_range:end_range], end_range - start_range, (i+1)*base_fee)
150+
txids[i] = create_lots_of_big_transactions(
151+
self.wallet,
152+
self.nodes[0],
153+
(i+1) * base_fee,
154+
end_range - start_range,
155+
self.txouts,
156+
utxos[start_range:end_range])
145157

146158
# Make sure that the size of each group of transactions exceeds
147159
# MAX_BLOCK_WEIGHT // 4 -- otherwise the test needs to be revised to
@@ -200,17 +212,9 @@ def run_test(self):
200212
assert x not in mempool
201213

202214
# Create a free transaction. Should be rejected.
203-
utxo_list = self.nodes[0].listunspent()
204-
assert len(utxo_list) > 0
205-
utxo = utxo_list[0]
206-
207-
inputs = []
208-
outputs = {}
209-
inputs.append({"txid" : utxo["txid"], "vout" : utxo["vout"]})
210-
outputs[self.nodes[0].getnewaddress()] = utxo["amount"]
211-
raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
212-
tx_hex = self.nodes[0].signrawtransactionwithwallet(raw_tx)["hex"]
213-
tx_id = self.nodes[0].decoderawtransaction(tx_hex)["txid"]
215+
tx_res = self.wallet.create_self_transfer(from_node=self.nodes[0], fee_rate=0, mempool_valid=False)
216+
tx_hex = tx_res['hex']
217+
tx_id = tx_res['txid']
214218

215219
# This will raise an exception due to min relay fee not being met
216220
assert_raises_rpc_error(-26, "min relay fee not met", self.nodes[0].sendrawtransaction, tx_hex)

test/functional/test_framework/util.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -552,24 +552,22 @@ def gen_return_txouts():
552552

553553
# Create a spend of each passed-in utxo, splicing in "txouts" to each raw
554554
# transaction to make it large. See gen_return_txouts() above.
555-
def create_lots_of_big_transactions(node, txouts, utxos, num, fee):
556-
addr = node.getnewaddress()
555+
def create_lots_of_big_transactions(mini_wallet, node, fee, tx_batch_size, txouts, utxos=None):
556+
from .messages import COIN
557+
fee_sats = int(fee * COIN)
557558
txids = []
558-
from .messages import tx_from_hex
559-
for _ in range(num):
560-
t = utxos.pop()
561-
inputs = [{"txid": t["txid"], "vout": t["vout"]}]
562-
outputs = {}
563-
change = t['amount'] - fee
564-
outputs[addr] = satoshi_round(change)
565-
rawtx = node.createrawtransaction(inputs, outputs)
566-
tx = tx_from_hex(rawtx)
567-
for txout in txouts:
568-
tx.vout.append(txout)
569-
newtx = tx.serialize().hex()
570-
signresult = node.signrawtransactionwithwallet(newtx, None, "NONE")
571-
txid = node.sendrawtransaction(signresult["hex"], 0)
572-
txids.append(txid)
559+
use_internal_utxos = utxos is None
560+
for _ in range(tx_batch_size):
561+
tx = mini_wallet.create_self_transfer(
562+
from_node=node,
563+
utxo_to_spend=None if use_internal_utxos else utxos.pop(),
564+
fee_rate=0,
565+
mempool_valid=False)['tx']
566+
tx.vout[0].nValue -= fee_sats
567+
tx.vout.extend(txouts)
568+
res = node.testmempoolaccept([tx.serialize().hex()])[0]
569+
assert_equal(res['fees']['base'], fee)
570+
txids.append(node.sendrawtransaction(tx.serialize().hex()))
573571
return txids
574572

575573

0 commit comments

Comments
 (0)