Skip to content

Commit a65d225

Browse files
Merge bitcoin/bitcoin#27035: test: simplify and speedup mempool_updatefromblock.py by using MiniWallet
dee8549 test: simplify and speedup mempool_updatefromblock.py by using MiniWallet (Sebastian Falbesoner) Pull request description: This PR simplifies the functional test mempool_updatefromblock.py by using MiniWallet in order to avoid manual low-level tx creation (signing, outputs selection, fee calculation). Most of the tedious work is done by the method `MiniWallet.send_self_transfer_multi` (calling `create_self_transfer_multi` internally) which supports spending a given set of UTXOs and creating a certain number of outputs. As a nice side-effect, the test's performance increases significantly (~3.5x on my system): ``` master 1m56.80s real 1m50.10s user 0m06.36s system PR 0m32.34s real 0m30.26s user 0m01.41s system ``` The arguments `start_input_txid` and `end_address` have been removed from the `transaction_graph_test` method, as they are currently unused and I don't see them being needed for future tests. ACKs for top commit: brunoerg: crACK dee8549 MarcoFalke: lgtm ACK dee8549 🚏 Tree-SHA512: 9f6da634bdc8c272f9a2af1cddaa364ee371d4e95554463a066249eecebb668d8c6cb123ec8a5404c41b3291010c0c8806a8a01dd227733cec03e73aa93b0103
2 parents 1e0198b + dee8549 commit a65d225

File tree

1 file changed

+15
-42
lines changed

1 file changed

+15
-42
lines changed

test/functional/mempool_updatefromblock.py

Lines changed: 15 additions & 42 deletions
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)