7
7
Test mempool update of transaction descendants/ancestors information (count, size)
8
8
when transactions have been re-added from a disconnected block to the mempool.
9
9
"""
10
+ from math import ceil
10
11
import time
11
12
12
- from decimal import Decimal
13
13
from test_framework .test_framework import BitcoinTestFramework
14
14
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
18
16
19
17
20
18
class MempoolUpdateFromBlockTest (BitcoinTestFramework ):
21
19
def set_test_params (self ):
22
20
self .num_nodes = 1
23
21
self .extra_args = [['-limitdescendantsize=1000' , '-limitancestorsize=1000' , '-limitancestorcount=100' ]]
24
22
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 ):
34
24
"""Create an acyclic tournament (a type of directed graph) of transactions and use it for testing.
35
25
36
26
Keyword arguments:
@@ -45,14 +35,7 @@ def transaction_graph_test(self, size, n_tx_to_mine=None, start_input_txid='', e
45
35
46
36
More details: https://en.wikipedia.org/wiki/Tournament_(graph_theory)
47
37
"""
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 ])
56
39
first_block_hash = ''
57
40
tx_id = []
58
41
tx_size = []
@@ -61,41 +44,31 @@ def transaction_graph_test(self, size, n_tx_to_mine=None, start_input_txid='', e
61
44
self .log .debug ('Preparing transaction #{}...' .format (i ))
62
45
# Prepare inputs.
63
46
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
66
48
else :
67
49
inputs = []
68
- inputs_value = 0
69
50
for j , tx in enumerate (tx_id [0 :i ]):
70
51
# Transaction tx[K] is a child of each of previous transactions tx[0]..tx[K-1] at their output K-1.
71
52
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 ))
77
54
78
55
# Prepare outputs.
79
56
tx_count = i + 1
80
57
if tx_count < size :
81
58
# Transaction tx[K] is an ancestor of each of subsequent transactions tx[K+1]..tx[N-1].
82
59
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
87
60
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
93
62
94
63
# 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 ())
99
72
100
73
if tx_count in n_tx_to_mine :
101
74
# The created transactions are mined into blocks by batches.
0 commit comments