test: use MiniWallet for mining_prioritisetransaction.py

This test can now be run even with the Bitcoin Core wallet disabled.
pull/24839/head
Sebastian Falbesoner 3 years ago
parent 2b5a741e98
commit 8973eeb412

@ -7,16 +7,22 @@
from decimal import Decimal from decimal import Decimal
import time import time
from test_framework.blocktools import COINBASE_MATURITY from test_framework.messages import (
from test_framework.messages import COIN, MAX_BLOCK_WEIGHT COIN,
MAX_BLOCK_WEIGHT,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error, create_confirmed_utxos, create_lots_of_big_transactions, gen_return_txouts from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
create_lots_of_big_transactions,
gen_return_txouts,
)
from test_framework.wallet import MiniWallet from test_framework.wallet import MiniWallet
class PrioritiseTransactionTest(BitcoinTestFramework): class PrioritiseTransactionTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1 self.num_nodes = 1
self.extra_args = [[ self.extra_args = [[
"-printpriority=1", "-printpriority=1",
@ -24,12 +30,8 @@ class PrioritiseTransactionTest(BitcoinTestFramework):
]] * self.num_nodes ]] * self.num_nodes
self.supports_cli = False self.supports_cli = False
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
def test_diamond(self): def test_diamond(self):
self.log.info("Test diamond-shape package with priority") self.log.info("Test diamond-shape package with priority")
self.generate(self.wallet, COINBASE_MATURITY + 1)
mock_time = int(time.time()) mock_time = int(time.time())
self.nodes[0].setmocktime(mock_time) self.nodes[0].setmocktime(mock_time)
@ -104,6 +106,7 @@ class PrioritiseTransactionTest(BitcoinTestFramework):
def run_test(self): def run_test(self):
self.wallet = MiniWallet(self.nodes[0]) self.wallet = MiniWallet(self.nodes[0])
self.wallet.rescan_utxos()
# Test `prioritisetransaction` required parameters # Test `prioritisetransaction` required parameters
assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction) assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction)
@ -131,7 +134,10 @@ class PrioritiseTransactionTest(BitcoinTestFramework):
self.relayfee = self.nodes[0].getnetworkinfo()['relayfee'] self.relayfee = self.nodes[0].getnetworkinfo()['relayfee']
utxo_count = 90 utxo_count = 90
utxos = create_confirmed_utxos(self, self.relayfee, self.nodes[0], utxo_count) utxos = self.wallet.send_self_transfer_multi(from_node=self.nodes[0], num_outputs=utxo_count)['new_utxos']
self.generate(self.wallet, 1)
assert_equal(len(self.nodes[0].getrawmempool()), 0)
base_fee = self.relayfee*100 # our transactions are smaller than 100kb base_fee = self.relayfee*100 # our transactions are smaller than 100kb
txids = [] txids = []
@ -141,7 +147,13 @@ class PrioritiseTransactionTest(BitcoinTestFramework):
txids.append([]) txids.append([])
start_range = i * range_size start_range = i * range_size
end_range = start_range + range_size end_range = start_range + range_size
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) txids[i] = create_lots_of_big_transactions(
self.wallet,
self.nodes[0],
(i+1) * base_fee,
end_range - start_range,
self.txouts,
utxos[start_range:end_range])
# Make sure that the size of each group of transactions exceeds # Make sure that the size of each group of transactions exceeds
# MAX_BLOCK_WEIGHT // 4 -- otherwise the test needs to be revised to # MAX_BLOCK_WEIGHT // 4 -- otherwise the test needs to be revised to
@ -200,17 +212,9 @@ class PrioritiseTransactionTest(BitcoinTestFramework):
assert x not in mempool assert x not in mempool
# Create a free transaction. Should be rejected. # Create a free transaction. Should be rejected.
utxo_list = self.nodes[0].listunspent() tx_res = self.wallet.create_self_transfer(from_node=self.nodes[0], fee_rate=0, mempool_valid=False)
assert len(utxo_list) > 0 tx_hex = tx_res['hex']
utxo = utxo_list[0] tx_id = tx_res['txid']
inputs = []
outputs = {}
inputs.append({"txid" : utxo["txid"], "vout" : utxo["vout"]})
outputs[self.nodes[0].getnewaddress()] = utxo["amount"]
raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
tx_hex = self.nodes[0].signrawtransactionwithwallet(raw_tx)["hex"]
tx_id = self.nodes[0].decoderawtransaction(tx_hex)["txid"]
# This will raise an exception due to min relay fee not being met # This will raise an exception due to min relay fee not being met
assert_raises_rpc_error(-26, "min relay fee not met", self.nodes[0].sendrawtransaction, tx_hex) assert_raises_rpc_error(-26, "min relay fee not met", self.nodes[0].sendrawtransaction, tx_hex)

@ -552,24 +552,22 @@ def gen_return_txouts():
# Create a spend of each passed-in utxo, splicing in "txouts" to each raw # Create a spend of each passed-in utxo, splicing in "txouts" to each raw
# transaction to make it large. See gen_return_txouts() above. # transaction to make it large. See gen_return_txouts() above.
def create_lots_of_big_transactions(node, txouts, utxos, num, fee): def create_lots_of_big_transactions(mini_wallet, node, fee, tx_batch_size, txouts, utxos=None):
addr = node.getnewaddress() from .messages import COIN
fee_sats = int(fee * COIN)
txids = [] txids = []
from .messages import tx_from_hex use_internal_utxos = utxos is None
for _ in range(num): for _ in range(tx_batch_size):
t = utxos.pop() tx = mini_wallet.create_self_transfer(
inputs = [{"txid": t["txid"], "vout": t["vout"]}] from_node=node,
outputs = {} utxo_to_spend=None if use_internal_utxos else utxos.pop(),
change = t['amount'] - fee fee_rate=0,
outputs[addr] = satoshi_round(change) mempool_valid=False)['tx']
rawtx = node.createrawtransaction(inputs, outputs) tx.vout[0].nValue -= fee_sats
tx = tx_from_hex(rawtx) tx.vout.extend(txouts)
for txout in txouts: res = node.testmempoolaccept([tx.serialize().hex()])[0]
tx.vout.append(txout) assert_equal(res['fees']['base'], fee)
newtx = tx.serialize().hex() txids.append(node.sendrawtransaction(tx.serialize().hex()))
signresult = node.signrawtransactionwithwallet(newtx, None, "NONE")
txid = node.sendrawtransaction(signresult["hex"], 0)
txids.append(txid)
return txids return txids

Loading…
Cancel
Save