Skip to content

Commit 89fb354

Browse files
committed
Merge bitcoin/bitcoin#26625: test: Run mempool_packages.py with MiniWallet
fa6b402 test: Run mempool_packages.py with MiniWallet (MarcoFalke) fa448c2 test: Return fee from MiniWallet (MarcoFalke) faec09f test: Return chain of MiniWallet txs from MiniWallet chain method (MarcoFalke) faa12d4 test: Refactor MiniWallet sign_tx (MarcoFalke) fa2d821 test: Return wtxid from create_self_transfer_multi (MarcoFalke) Pull request description: This allows to run the test even when no wallet is compiled in. Also, it is a lot nicer to read now. ACKs for top commit: glozow: reACK fa6b402 Tree-SHA512: de0338068fd51db01d64ce270f94fd2982a63a6de597325cd1e1f11127e9075bd4aeacace0ed76d09a2db8b962b27237cf11edb4c1fe1a01134d397f8a11bd05
2 parents f41252f + fa6b402 commit 89fb354

File tree

5 files changed

+72
-134
lines changed

5 files changed

+72
-134
lines changed

test/functional/mempool_package_limits.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_chain_limits_helper(self, mempool_count, package_count):
4545
assert_equal(0, node.getmempoolinfo()["size"])
4646
chain_hex = []
4747

48-
chaintip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=mempool_count)
48+
chaintip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=mempool_count)[-1]["new_utxo"]
4949
# in-package transactions
5050
for _ in range(package_count):
5151
tx = self.wallet.create_self_transfer(utxo_to_spend=chaintip_utxo)
@@ -100,13 +100,13 @@ def test_desc_count_limits(self):
100100

101101
package_hex = []
102102
# Chain A (M2a... M12a)
103-
chain_a_tip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=11, utxo_to_spend=m1_utxos[0])
103+
chain_a_tip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=11, utxo_to_spend=m1_utxos[0])[-1]["new_utxo"]
104104
# Pa
105105
pa_hex = self.wallet.create_self_transfer(utxo_to_spend=chain_a_tip_utxo)["hex"]
106106
package_hex.append(pa_hex)
107107

108108
# Chain B (M2b... M13b)
109-
chain_b_tip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=12, utxo_to_spend=m1_utxos[1])
109+
chain_b_tip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=12, utxo_to_spend=m1_utxos[1])[-1]["new_utxo"]
110110
# Pb
111111
pb_hex = self.wallet.create_self_transfer(utxo_to_spend=chain_b_tip_utxo)["hex"]
112112
package_hex.append(pb_hex)
@@ -145,7 +145,7 @@ def test_desc_count_limits_2(self):
145145
m1_utxos = self.wallet.send_self_transfer_multi(from_node=node, num_outputs=2)['new_utxos']
146146

147147
# Chain M2...M24
148-
self.wallet.send_self_transfer_chain(from_node=node, chain_length=23, utxo_to_spend=m1_utxos[0])
148+
self.wallet.send_self_transfer_chain(from_node=node, chain_length=23, utxo_to_spend=m1_utxos[0])[-1]["new_utxo"]
149149

150150
# P1
151151
p1_tx = self.wallet.create_self_transfer(utxo_to_spend=m1_utxos[1])
@@ -191,7 +191,7 @@ def test_anc_count_limits(self):
191191

192192
# Two chains of 13 transactions each
193193
for _ in range(2):
194-
chain_tip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=12)
194+
chain_tip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=12)[-1]["new_utxo"]
195195
# Save the 13th transaction for the package
196196
tx = self.wallet.create_self_transfer(utxo_to_spend=chain_tip_utxo)
197197
package_hex.append(tx["hex"])
@@ -234,7 +234,7 @@ def test_anc_count_limits_2(self):
234234
self.log.info("Check that in-mempool and in-package ancestors are calculated properly in packages")
235235
# Two chains of 12 transactions each
236236
for _ in range(2):
237-
chaintip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=12)
237+
chaintip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=12)[-1]["new_utxo"]
238238
# last 2 transactions will be the parents of Pc
239239
pc_parent_utxos.append(chaintip_utxo)
240240

test/functional/mempool_packages.py

Lines changed: 40 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from decimal import Decimal
88

9-
from test_framework.blocktools import COINBASE_MATURITY
109
from test_framework.messages import (
1110
COIN,
1211
DEFAULT_ANCESTOR_LIMIT,
@@ -17,9 +16,8 @@
1716
from test_framework.util import (
1817
assert_equal,
1918
assert_raises_rpc_error,
20-
chain_transaction,
2119
)
22-
20+
from test_framework.wallet import MiniWallet
2321

2422
# custom limits for node1
2523
CUSTOM_ANCESTOR_LIMIT = 5
@@ -45,43 +43,33 @@ def set_test_params(self):
4543
],
4644
]
4745

48-
def skip_test_if_missing_module(self):
49-
self.skip_if_no_wallet()
50-
5146
def run_test(self):
52-
# Mine some blocks and have them mature.
47+
self.wallet = MiniWallet(self.nodes[0])
48+
self.wallet.rescan_utxos()
49+
50+
if self.is_specified_wallet_compiled():
51+
self.nodes[0].createwallet("watch_wallet", disable_private_keys=True)
52+
self.nodes[0].importaddress(self.wallet.get_address())
53+
5354
peer_inv_store = self.nodes[0].add_p2p_connection(P2PTxInvStore()) # keep track of invs
54-
self.generate(self.nodes[0], COINBASE_MATURITY + 1)
55-
utxo = self.nodes[0].listunspent(10)
56-
txid = utxo[0]['txid']
57-
vout = utxo[0]['vout']
58-
value = utxo[0]['amount']
59-
assert 'ancestorcount' not in utxo[0]
60-
assert 'ancestorsize' not in utxo[0]
61-
assert 'ancestorfees' not in utxo[0]
62-
63-
fee = Decimal("0.0001")
55+
6456
# DEFAULT_ANCESTOR_LIMIT transactions off a confirmed tx should be fine
65-
chain = []
66-
witness_chain = []
57+
chain = self.wallet.create_self_transfer_chain(chain_length=DEFAULT_ANCESTOR_LIMIT)
58+
witness_chain = [t["wtxid"] for t in chain]
6759
ancestor_vsize = 0
6860
ancestor_fees = Decimal(0)
69-
for i in range(DEFAULT_ANCESTOR_LIMIT):
70-
(txid, sent_value) = chain_transaction(self.nodes[0], [txid], [0], value, fee, 1)
71-
value = sent_value
72-
chain.append(txid)
73-
# We need the wtxids to check P2P announcements
74-
witnesstx = self.nodes[0].gettransaction(txid=txid, verbose=True)['decoded']
75-
witness_chain.append(witnesstx['hash'])
7661

62+
for i, t in enumerate(chain):
63+
ancestor_vsize += t["tx"].get_vsize()
64+
ancestor_fees += t["fee"]
65+
self.wallet.sendrawtransaction(from_node=self.nodes[0], tx_hex=t["hex"])
7766
# Check that listunspent ancestor{count, size, fees} yield the correct results
78-
wallet_unspent = self.nodes[0].listunspent(minconf=0)
79-
this_unspent = next(utxo_info for utxo_info in wallet_unspent if utxo_info['txid'] == txid)
80-
assert_equal(this_unspent['ancestorcount'], i + 1)
81-
ancestor_vsize += self.nodes[0].getrawtransaction(txid=txid, verbose=True)['vsize']
82-
assert_equal(this_unspent['ancestorsize'], ancestor_vsize)
83-
ancestor_fees -= self.nodes[0].gettransaction(txid=txid)['fee']
84-
assert_equal(this_unspent['ancestorfees'], ancestor_fees * COIN)
67+
if self.is_specified_wallet_compiled():
68+
wallet_unspent = self.nodes[0].listunspent(minconf=0)
69+
this_unspent = next(utxo_info for utxo_info in wallet_unspent if utxo_info["txid"] == t["txid"])
70+
assert_equal(this_unspent['ancestorcount'], i + 1)
71+
assert_equal(this_unspent['ancestorsize'], ancestor_vsize)
72+
assert_equal(this_unspent['ancestorfees'], ancestor_fees * COIN)
8573

8674
# Wait until mempool transactions have passed initial broadcast (sent inv and received getdata)
8775
# Otherwise, getrawmempool may be inconsistent with getmempoolentry if unbroadcast changes in between
@@ -99,15 +87,20 @@ def run_test(self):
9987
ancestor_count = DEFAULT_ANCESTOR_LIMIT
10088
assert_equal(ancestor_fees, sum([mempool[tx]['fees']['base'] for tx in mempool]))
10189

90+
# Adding one more transaction on to the chain should fail.
91+
next_hop = self.wallet.create_self_transfer(utxo_to_spend=chain[-1]["new_utxo"])["hex"]
92+
assert_raises_rpc_error(-26, "too-long-mempool-chain", lambda: self.nodes[0].sendrawtransaction(next_hop))
93+
10294
descendants = []
103-
ancestors = list(chain)
95+
ancestors = [t["txid"] for t in chain]
96+
chain = [t["txid"] for t in chain]
10497
for x in reversed(chain):
10598
# Check that getmempoolentry is consistent with getrawmempool
10699
entry = self.nodes[0].getmempoolentry(x)
107100
assert_equal(entry, mempool[x])
108101

109102
# Check that gettxspendingprevout is consistent with getrawmempool
110-
witnesstx = self.nodes[0].gettransaction(txid=x, verbose=True)['decoded']
103+
witnesstx = self.nodes[0].getrawtransaction(txid=x, verbose=True)
111104
for tx_in in witnesstx["vin"]:
112105
spending_result = self.nodes[0].gettxspendingprevout([ {'txid' : tx_in["txid"], 'vout' : tx_in["vout"]} ])
113106
assert_equal(spending_result, [ {'txid' : tx_in["txid"], 'vout' : tx_in["vout"], 'spendingtxid' : x} ])
@@ -193,9 +186,6 @@ def run_test(self):
193186
descendant_fees += entry['fees']['base']
194187
assert_equal(entry['fees']['descendant'], descendant_fees + Decimal('0.00001'))
195188

196-
# Adding one more transaction on to the chain should fail.
197-
assert_raises_rpc_error(-26, "too-long-mempool-chain", chain_transaction, self.nodes[0], [txid], [vout], value, fee, 1)
198-
199189
# Check that prioritising a tx before it's added to the mempool works
200190
# First clear the mempool by mining a block.
201191
self.generate(self.nodes[0], 1)
@@ -232,28 +222,23 @@ def run_test(self):
232222
# TODO: test ancestor size limits
233223

234224
# Now test descendant chain limits
235-
txid = utxo[1]['txid']
236-
value = utxo[1]['amount']
237-
vout = utxo[1]['vout']
238225

239-
transaction_package = []
240226
tx_children = []
241227
# First create one parent tx with 10 children
242-
(txid, sent_value) = chain_transaction(self.nodes[0], [txid], [vout], value, fee, 10)
243-
parent_transaction = txid
244-
for i in range(10):
245-
transaction_package.append({'txid': txid, 'vout': i, 'amount': sent_value})
228+
tx_with_children = self.wallet.send_self_transfer_multi(from_node=self.nodes[0], num_outputs=10)
229+
parent_transaction = tx_with_children["txid"]
230+
transaction_package = tx_with_children["new_utxos"]
246231

247232
# Sign and send up to MAX_DESCENDANT transactions chained off the parent tx
248233
chain = [] # save sent txs for the purpose of checking node1's mempool later (see below)
249234
for _ in range(DEFAULT_DESCENDANT_LIMIT - 1):
250235
utxo = transaction_package.pop(0)
251-
(txid, sent_value) = chain_transaction(self.nodes[0], [utxo['txid']], [utxo['vout']], utxo['amount'], fee, 10)
236+
new_tx = self.wallet.send_self_transfer_multi(from_node=self.nodes[0], num_outputs=10, utxos_to_spend=[utxo])
237+
txid = new_tx["txid"]
252238
chain.append(txid)
253239
if utxo['txid'] is parent_transaction:
254240
tx_children.append(txid)
255-
for j in range(10):
256-
transaction_package.append({'txid': txid, 'vout': j, 'amount': sent_value})
241+
transaction_package.extend(new_tx["new_utxos"])
257242

258243
mempool = self.nodes[0].getrawmempool(True)
259244
assert_equal(mempool[parent_transaction]['descendantcount'], DEFAULT_DESCENDANT_LIMIT)
@@ -263,8 +248,8 @@ def run_test(self):
263248
assert_equal(mempool[child]['depends'], [parent_transaction])
264249

265250
# Sending one more chained transaction will fail
266-
utxo = transaction_package.pop(0)
267-
assert_raises_rpc_error(-26, "too-long-mempool-chain", chain_transaction, self.nodes[0], [utxo['txid']], [utxo['vout']], utxo['amount'], fee, 10)
251+
next_hop = self.wallet.create_self_transfer(utxo_to_spend=transaction_package.pop(0))["hex"]
252+
assert_raises_rpc_error(-26, "too-long-mempool-chain", lambda: self.nodes[0].sendrawtransaction(next_hop))
268253

269254
# Check that node1's mempool is as expected, containing:
270255
# - txs from previous ancestor test (-> custom ancestor limit)
@@ -304,42 +289,19 @@ def run_test(self):
304289
# last block.
305290

306291
# Create tx0 with 2 outputs
307-
utxo = self.nodes[0].listunspent()
308-
txid = utxo[0]['txid']
309-
value = utxo[0]['amount']
310-
vout = utxo[0]['vout']
311-
312-
send_value = (value - fee) / 2
313-
inputs = [ {'txid' : txid, 'vout' : vout} ]
314-
outputs = {}
315-
for _ in range(2):
316-
outputs[self.nodes[0].getnewaddress()] = send_value
317-
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
318-
signedtx = self.nodes[0].signrawtransactionwithwallet(rawtx)
319-
txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
320-
tx0_id = txid
321-
value = send_value
292+
tx0 = self.wallet.send_self_transfer_multi(from_node=self.nodes[0], num_outputs=2)
322293

323294
# Create tx1
324-
tx1_id, _ = chain_transaction(self.nodes[0], [tx0_id], [0], value, fee, 1)
295+
tx1 = self.wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=tx0["new_utxos"][0])
325296

326297
# Create tx2-7
327-
vout = 1
328-
txid = tx0_id
329-
for _ in range(6):
330-
(txid, sent_value) = chain_transaction(self.nodes[0], [txid], [vout], value, fee, 1)
331-
vout = 0
332-
value = sent_value
298+
tx7 = self.wallet.send_self_transfer_chain(from_node=self.nodes[0], utxo_to_spend=tx0["new_utxos"][1], chain_length=6)[-1]
333299

334300
# Mine these in a block
335301
self.generate(self.nodes[0], 1)
336302

337303
# Now generate tx8, with a big fee
338-
inputs = [ {'txid' : tx1_id, 'vout': 0}, {'txid' : txid, 'vout': 0} ]
339-
outputs = { self.nodes[0].getnewaddress() : send_value + value - 4*fee }
340-
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
341-
signedtx = self.nodes[0].signrawtransactionwithwallet(rawtx)
342-
txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
304+
self.wallet.send_self_transfer_multi(from_node=self.nodes[0], utxos_to_spend=[tx1["new_utxo"], tx7["new_utxo"]], fee_per_output=40000)
343305
self.sync_mempools()
344306

345307
# Now try to disconnect the tip on each node...

test/functional/rpc_packages.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ def test_chain(self):
128128
node = self.nodes[0]
129129

130130
chain = self.wallet.create_self_transfer_chain(chain_length=25)
131-
chain_hex = chain["chain_hex"]
132-
chain_txns = chain["chain_txns"]
131+
chain_hex = [t["hex"] for t in chain]
132+
chain_txns = [t["tx"] for t in chain]
133133

134134
self.log.info("Check that testmempoolaccept requires packages to be sorted by dependency")
135135
assert_equal(node.testmempoolaccept(rawtxs=chain_hex[::-1]),
@@ -374,7 +374,7 @@ def test_submitpackage(self):
374374

375375
self.log.info("Submitpackage only allows packages of 1 child with its parents")
376376
# Chain of 3 transactions has too many generations
377-
chain_hex = self.wallet.create_self_transfer_chain(chain_length=25)["chain_hex"]
377+
chain_hex = [t["hex"] for t in self.wallet.create_self_transfer_chain(chain_length=25)]
378378
assert_raises_rpc_error(-25, "not-child-with-parents", node.submitpackage, chain_hex)
379379

380380

test/functional/test_framework/util.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -488,28 +488,6 @@ def find_output(node, txid, amount, *, blockhash=None):
488488
raise RuntimeError("find_output txid %s : %s not found" % (txid, str(amount)))
489489

490490

491-
def chain_transaction(node, parent_txids, vouts, value, fee, num_outputs):
492-
"""Build and send a transaction that spends the given inputs (specified
493-
by lists of parent_txid:vout each), with the desired total value and fee,
494-
equally divided up to the desired number of outputs.
495-
496-
Returns a tuple with the txid and the amount sent per output.
497-
"""
498-
send_value = satoshi_round((value - fee)/num_outputs)
499-
inputs = []
500-
for (txid, vout) in zip(parent_txids, vouts):
501-
inputs.append({'txid' : txid, 'vout' : vout})
502-
outputs = {}
503-
for _ in range(num_outputs):
504-
outputs[node.getnewaddress()] = send_value
505-
rawtx = node.createrawtransaction(inputs, outputs, 0, True)
506-
signedtx = node.signrawtransactionwithwallet(rawtx)
507-
txid = node.sendrawtransaction(signedtx['hex'])
508-
fulltx = node.getrawtransaction(txid, 1)
509-
assert len(fulltx['vout']) == num_outputs # make sure we didn't generate a change output
510-
return (txid, send_value)
511-
512-
513491
# Create large OP_RETURN txouts that can be appended to a transaction
514492
# to make it large (helper for constructing large transactions). The
515493
# total serialized size of the txouts is about 66k vbytes.

0 commit comments

Comments
 (0)