Skip to content

Commit 6e63e36

Browse files
committed
test: refactor: dedup utility function chain_transaction()
1 parent e638acf commit 6e63e36

File tree

3 files changed

+41
-50
lines changed

3 files changed

+41
-50
lines changed

test/functional/mempool_package_onemore.py

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
from test_framework.blocktools import COINBASE_MATURITY
1313
from test_framework.test_framework import BitcoinTestFramework
14-
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round
14+
from test_framework.util import (
15+
assert_equal,
16+
assert_raises_rpc_error,
17+
chain_transaction,
18+
)
1519

1620
MAX_ANCESTORS = 25
1721
MAX_DESCENDANTS = 25
@@ -24,23 +28,6 @@ def set_test_params(self):
2428
def skip_test_if_missing_module(self):
2529
self.skip_if_no_wallet()
2630

27-
# Build a transaction that spends parent_txid:vout
28-
# Return amount sent
29-
def chain_transaction(self, node, parent_txids, vouts, value, fee, num_outputs):
30-
send_value = satoshi_round((value - fee)/num_outputs)
31-
inputs = []
32-
for (txid, vout) in zip(parent_txids, vouts):
33-
inputs.append({'txid' : txid, 'vout' : vout})
34-
outputs = {}
35-
for _ in range(num_outputs):
36-
outputs[node.getnewaddress()] = send_value
37-
rawtx = node.createrawtransaction(inputs, outputs, 0, True)
38-
signedtx = node.signrawtransactionwithwallet(rawtx)
39-
txid = node.sendrawtransaction(signedtx['hex'])
40-
fulltx = node.getrawtransaction(txid, 1)
41-
assert len(fulltx['vout']) == num_outputs # make sure we didn't generate a change output
42-
return (txid, send_value)
43-
4431
def run_test(self):
4532
# Mine some blocks and have them mature.
4633
self.nodes[0].generate(COINBASE_MATURITY + 1)
@@ -53,32 +40,32 @@ def run_test(self):
5340
# MAX_ANCESTORS transactions off a confirmed tx should be fine
5441
chain = []
5542
for _ in range(4):
56-
(txid, sent_value) = self.chain_transaction(self.nodes[0], [txid], [vout], value, fee, 2)
43+
(txid, sent_value) = chain_transaction(self.nodes[0], [txid], [vout], value, fee, 2)
5744
vout = 0
5845
value = sent_value
5946
chain.append([txid, value])
6047
for _ in range(MAX_ANCESTORS - 4):
61-
(txid, sent_value) = self.chain_transaction(self.nodes[0], [txid], [0], value, fee, 1)
48+
(txid, sent_value) = chain_transaction(self.nodes[0], [txid], [0], value, fee, 1)
6249
value = sent_value
6350
chain.append([txid, value])
64-
(second_chain, second_chain_value) = self.chain_transaction(self.nodes[0], [utxo[1]['txid']], [utxo[1]['vout']], utxo[1]['amount'], fee, 1)
51+
(second_chain, second_chain_value) = chain_transaction(self.nodes[0], [utxo[1]['txid']], [utxo[1]['vout']], utxo[1]['amount'], fee, 1)
6552

6653
# Check mempool has MAX_ANCESTORS + 1 transactions in it
6754
assert_equal(len(self.nodes[0].getrawmempool(True)), MAX_ANCESTORS + 1)
6855

6956
# Adding one more transaction on to the chain should fail.
70-
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many unconfirmed ancestors [limit: 25]", self.chain_transaction, self.nodes[0], [txid], [0], value, fee, 1)
57+
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many unconfirmed ancestors [limit: 25]", chain_transaction, self.nodes[0], [txid], [0], value, fee, 1)
7158
# ...even if it chains on from some point in the middle of the chain.
72-
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", self.chain_transaction, self.nodes[0], [chain[2][0]], [1], chain[2][1], fee, 1)
73-
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", self.chain_transaction, self.nodes[0], [chain[1][0]], [1], chain[1][1], fee, 1)
59+
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", chain_transaction, self.nodes[0], [chain[2][0]], [1], chain[2][1], fee, 1)
60+
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", chain_transaction, self.nodes[0], [chain[1][0]], [1], chain[1][1], fee, 1)
7461
# ...even if it chains on to two parent transactions with one in the chain.
75-
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", self.chain_transaction, self.nodes[0], [chain[0][0], second_chain], [1, 0], chain[0][1] + second_chain_value, fee, 1)
62+
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", chain_transaction, self.nodes[0], [chain[0][0], second_chain], [1, 0], chain[0][1] + second_chain_value, fee, 1)
7663
# ...especially if its > 40k weight
77-
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", self.chain_transaction, self.nodes[0], [chain[0][0]], [1], chain[0][1], fee, 350)
64+
assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", chain_transaction, self.nodes[0], [chain[0][0]], [1], chain[0][1], fee, 350)
7865
# But not if it chains directly off the first transaction
79-
(replacable_txid, replacable_orig_value) = self.chain_transaction(self.nodes[0], [chain[0][0]], [1], chain[0][1], fee, 1)
66+
(replacable_txid, replacable_orig_value) = chain_transaction(self.nodes[0], [chain[0][0]], [1], chain[0][1], fee, 1)
8067
# and the second chain should work just fine
81-
self.chain_transaction(self.nodes[0], [second_chain], [0], second_chain_value, fee, 1)
68+
chain_transaction(self.nodes[0], [second_chain], [0], second_chain_value, fee, 1)
8269

8370
# Make sure we can RBF the chain which used our carve-out rule
8471
second_tx_outputs = {self.nodes[0].getrawtransaction(replacable_txid, True)["vout"][0]['scriptPubKey']['address']: replacable_orig_value - (Decimal(1) / Decimal(100))}

test/functional/mempool_packages.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from test_framework.util import (
1414
assert_equal,
1515
assert_raises_rpc_error,
16+
chain_transaction,
1617
satoshi_round,
1718
)
1819

@@ -42,21 +43,6 @@ def set_test_params(self):
4243
def skip_test_if_missing_module(self):
4344
self.skip_if_no_wallet()
4445

45-
# Build a transaction that spends parent_txid:vout
46-
# Return amount sent
47-
def chain_transaction(self, node, parent_txid, vout, value, fee, num_outputs):
48-
send_value = satoshi_round((value - fee)/num_outputs)
49-
inputs = [ {'txid' : parent_txid, 'vout' : vout} ]
50-
outputs = {}
51-
for _ in range(num_outputs):
52-
outputs[node.getnewaddress()] = send_value
53-
rawtx = node.createrawtransaction(inputs, outputs)
54-
signedtx = node.signrawtransactionwithwallet(rawtx)
55-
txid = node.sendrawtransaction(signedtx['hex'])
56-
fulltx = node.getrawtransaction(txid, 1)
57-
assert len(fulltx['vout']) == num_outputs # make sure we didn't generate a change output
58-
return (txid, send_value)
59-
6046
def run_test(self):
6147
# Mine some blocks and have them mature.
6248
peer_inv_store = self.nodes[0].add_p2p_connection(P2PTxInvStore()) # keep track of invs
@@ -71,7 +57,7 @@ def run_test(self):
7157
chain = []
7258
witness_chain = []
7359
for _ in range(MAX_ANCESTORS):
74-
(txid, sent_value) = self.chain_transaction(self.nodes[0], txid, 0, value, fee, 1)
60+
(txid, sent_value) = chain_transaction(self.nodes[0], [txid], [0], value, fee, 1)
7561
value = sent_value
7662
chain.append(txid)
7763
# We need the wtxids to check P2P announcements
@@ -189,7 +175,7 @@ def run_test(self):
189175
assert_equal(mempool[x]['descendantfees'], descendant_fees * COIN + 1000)
190176

191177
# Adding one more transaction on to the chain should fail.
192-
assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], txid, vout, value, fee, 1)
178+
assert_raises_rpc_error(-26, "too-long-mempool-chain", chain_transaction, self.nodes[0], [txid], [vout], value, fee, 1)
193179

194180
# Check that prioritising a tx before it's added to the mempool works
195181
# First clear the mempool by mining a block.
@@ -238,7 +224,7 @@ def run_test(self):
238224
transaction_package = []
239225
tx_children = []
240226
# First create one parent tx with 10 children
241-
(txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 10)
227+
(txid, sent_value) = chain_transaction(self.nodes[0], [txid], [vout], value, fee, 10)
242228
parent_transaction = txid
243229
for i in range(10):
244230
transaction_package.append({'txid': txid, 'vout': i, 'amount': sent_value})
@@ -247,7 +233,7 @@ def run_test(self):
247233
chain = [] # save sent txs for the purpose of checking node1's mempool later (see below)
248234
for _ in range(MAX_DESCENDANTS - 1):
249235
utxo = transaction_package.pop(0)
250-
(txid, sent_value) = self.chain_transaction(self.nodes[0], utxo['txid'], utxo['vout'], utxo['amount'], fee, 10)
236+
(txid, sent_value) = chain_transaction(self.nodes[0], [utxo['txid']], [utxo['vout']], utxo['amount'], fee, 10)
251237
chain.append(txid)
252238
if utxo['txid'] is parent_transaction:
253239
tx_children.append(txid)
@@ -263,7 +249,7 @@ def run_test(self):
263249

264250
# Sending one more chained transaction will fail
265251
utxo = transaction_package.pop(0)
266-
assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], utxo['txid'], utxo['vout'], utxo['amount'], fee, 10)
252+
assert_raises_rpc_error(-26, "too-long-mempool-chain", chain_transaction, self.nodes[0], [utxo['txid']], [utxo['vout']], utxo['amount'], fee, 10)
267253

268254
# Check that node1's mempool is as expected, containing:
269255
# - txs from previous ancestor test (-> custom ancestor limit)
@@ -321,13 +307,13 @@ def run_test(self):
321307
value = send_value
322308

323309
# Create tx1
324-
tx1_id, _ = self.chain_transaction(self.nodes[0], tx0_id, 0, value, fee, 1)
310+
tx1_id, _ = chain_transaction(self.nodes[0], [tx0_id], [0], value, fee, 1)
325311

326312
# Create tx2-7
327313
vout = 1
328314
txid = tx0_id
329315
for _ in range(6):
330-
(txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 1)
316+
(txid, sent_value) = chain_transaction(self.nodes[0], [txid], [vout], value, fee, 1)
331317
vout = 0
332318
value = sent_value
333319

test/functional/test_framework/util.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,24 @@ def create_confirmed_utxos(fee, node, count):
481481
return utxos
482482

483483

484+
# Build a transaction that spends parent_txid:vout
485+
# Return amount sent
486+
def chain_transaction(node, parent_txids, vouts, value, fee, num_outputs):
487+
send_value = satoshi_round((value - fee)/num_outputs)
488+
inputs = []
489+
for (txid, vout) in zip(parent_txids, vouts):
490+
inputs.append({'txid' : txid, 'vout' : vout})
491+
outputs = {}
492+
for _ in range(num_outputs):
493+
outputs[node.getnewaddress()] = send_value
494+
rawtx = node.createrawtransaction(inputs, outputs, 0, True)
495+
signedtx = node.signrawtransactionwithwallet(rawtx)
496+
txid = node.sendrawtransaction(signedtx['hex'])
497+
fulltx = node.getrawtransaction(txid, 1)
498+
assert len(fulltx['vout']) == num_outputs # make sure we didn't generate a change output
499+
return (txid, send_value)
500+
501+
484502
# Create large OP_RETURN txouts that can be appended to a transaction
485503
# to make it large (helper for constructing large transactions).
486504
def gen_return_txouts():

0 commit comments

Comments
 (0)