Skip to content

Commit dee8549

Browse files
committed
test: simplify and speedup mempool_updatefromblock.py by using MiniWallet
1 parent aaa5597 commit dee8549

File tree

1 file changed

+15
-42
lines changed

1 file changed

+15
-42
lines changed

test/functional/mempool_updatefromblock.py

+15-42
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,20 @@
77
Test mempool update of transaction descendants/ancestors information (count, size)
88
when transactions have been re-added from a disconnected block to the mempool.
99
"""
10+
from math import ceil
1011
import time
1112

12-
from decimal import Decimal
1313
from test_framework.test_framework import BitcoinTestFramework
1414
from test_framework.util import assert_equal
15-
from test_framework.address import key_to_p2pkh
16-
from test_framework.wallet_util import bytes_to_wif
17-
from test_framework.key import ECKey
15+
from test_framework.wallet import MiniWallet
1816

1917

2018
class MempoolUpdateFromBlockTest(BitcoinTestFramework):
2119
def set_test_params(self):
2220
self.num_nodes = 1
2321
self.extra_args = [['-limitdescendantsize=1000', '-limitancestorsize=1000', '-limitancestorcount=100']]
2422

25-
def get_new_address(self):
26-
key = ECKey()
27-
key.generate()
28-
pubkey = key.get_pubkey().get_bytes()
29-
address = key_to_p2pkh(pubkey)
30-
self.priv_keys.append(bytes_to_wif(key.get_bytes()))
31-
return address
32-
33-
def transaction_graph_test(self, size, n_tx_to_mine=None, start_input_txid='', end_address='', fee=Decimal(0.00100000)):
23+
def transaction_graph_test(self, size, n_tx_to_mine=None, fee=100_000):
3424
"""Create an acyclic tournament (a type of directed graph) of transactions and use it for testing.
3525
3626
Keyword arguments:
@@ -45,14 +35,7 @@ def transaction_graph_test(self, size, n_tx_to_mine=None, start_input_txid='', e
4535
4636
More details: https://en.wikipedia.org/wiki/Tournament_(graph_theory)
4737
"""
48-
49-
self.priv_keys = [self.nodes[0].get_deterministic_priv_key().key]
50-
if not start_input_txid:
51-
start_input_txid = self.nodes[0].getblock(self.nodes[0].getblockhash(1))['tx'][0]
52-
53-
if not end_address:
54-
end_address = self.get_new_address()
55-
38+
wallet = MiniWallet(self.nodes[0])
5639
first_block_hash = ''
5740
tx_id = []
5841
tx_size = []
@@ -61,41 +44,31 @@ def transaction_graph_test(self, size, n_tx_to_mine=None, start_input_txid='', e
6144
self.log.debug('Preparing transaction #{}...'.format(i))
6245
# Prepare inputs.
6346
if i == 0:
64-
inputs = [{'txid': start_input_txid, 'vout': 0}]
65-
inputs_value = self.nodes[0].gettxout(start_input_txid, 0)['value']
47+
inputs = [wallet.get_utxo()] # let MiniWallet provide a start UTXO
6648
else:
6749
inputs = []
68-
inputs_value = 0
6950
for j, tx in enumerate(tx_id[0:i]):
7051
# Transaction tx[K] is a child of each of previous transactions tx[0]..tx[K-1] at their output K-1.
7152
vout = i - j - 1
72-
inputs.append({'txid': tx_id[j], 'vout': vout})
73-
inputs_value += self.nodes[0].gettxout(tx, vout)['value']
74-
75-
self.log.debug('inputs={}'.format(inputs))
76-
self.log.debug('inputs_value={}'.format(inputs_value))
53+
inputs.append(wallet.get_utxo(txid=tx_id[j], vout=vout))
7754

7855
# Prepare outputs.
7956
tx_count = i + 1
8057
if tx_count < size:
8158
# Transaction tx[K] is an ancestor of each of subsequent transactions tx[K+1]..tx[N-1].
8259
n_outputs = size - tx_count
83-
output_value = ((inputs_value - fee) / Decimal(n_outputs)).quantize(Decimal('0.00000001'))
84-
outputs = {}
85-
for _ in range(n_outputs):
86-
outputs[self.get_new_address()] = output_value
8760
else:
88-
output_value = (inputs_value - fee).quantize(Decimal('0.00000001'))
89-
outputs = {end_address: output_value}
90-
91-
self.log.debug('output_value={}'.format(output_value))
92-
self.log.debug('outputs={}'.format(outputs))
61+
n_outputs = 1
9362

9463
# Create a new transaction.
95-
unsigned_raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
96-
signed_raw_tx = self.nodes[0].signrawtransactionwithkey(unsigned_raw_tx, self.priv_keys)
97-
tx_id.append(self.nodes[0].sendrawtransaction(signed_raw_tx['hex']))
98-
tx_size.append(self.nodes[0].getmempoolentry(tx_id[-1])['vsize'])
64+
new_tx = wallet.send_self_transfer_multi(
65+
from_node=self.nodes[0],
66+
utxos_to_spend=inputs,
67+
num_outputs=n_outputs,
68+
fee_per_output=ceil(fee / n_outputs)
69+
)
70+
tx_id.append(new_tx['txid'])
71+
tx_size.append(new_tx['tx'].get_vsize())
9972

10073
if tx_count in n_tx_to_mine:
10174
# The created transactions are mined into blocks by batches.

0 commit comments

Comments
 (0)