Skip to content

Commit f2f6068

Browse files
committed
test: MiniWallet: add send_self_transfer_chain to create chain of txns
With this new method, a chain of transactions can be created. This method is introduced to further simplify the mempool_package_limits.py tests.
1 parent 1d6b438 commit f2f6068

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

test/functional/mempool_package_limits.py

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

48-
chaintip_utxo = None
49-
for i in range(mempool_count + package_count):
48+
chaintip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=mempool_count)
49+
# in-package transactions
50+
for _ in range(package_count):
5051
tx = self.wallet.create_self_transfer(utxo_to_spend=chaintip_utxo)
5152
chaintip_utxo = tx["new_utxo"]
52-
if i < mempool_count:
53-
# in-mempool transactions
54-
txid = self.wallet.sendrawtransaction(from_node=node, tx_hex=tx["hex"])
55-
assert_equal(node.getmempoolentry(txid)["ancestorcount"], i + 1)
56-
else:
57-
# in-package transactions
58-
chain_hex.append(tx["hex"])
53+
chain_hex.append(tx["hex"])
5954
testres_too_long = node.testmempoolaccept(rawtxs=chain_hex)
6055
for txres in testres_too_long:
6156
assert_equal(txres["package-error"], "package-mempool-limits")
@@ -104,25 +99,17 @@ def test_desc_count_limits(self):
10499
m1_utxos = self.wallet.send_self_transfer_multi(from_node=node, num_outputs=2)['new_utxos']
105100

106101
package_hex = []
107-
# Chain A
108-
chain_a_utxo = m1_utxos[0]
109-
for i in range(12):
110-
tx = self.wallet.create_self_transfer(utxo_to_spend=chain_a_utxo)
111-
chain_a_utxo = tx["new_utxo"]
112-
if i < 11: # M2a... M12a
113-
self.wallet.sendrawtransaction(from_node=node, tx_hex=tx["hex"])
114-
else: # Pa
115-
package_hex.append(tx["hex"])
116-
117-
# Chain B
118-
chain_b_utxo = m1_utxos[1]
119-
for i in range(13):
120-
tx = self.wallet.create_self_transfer(utxo_to_spend=chain_b_utxo)
121-
chain_b_utxo = tx["new_utxo"]
122-
if i < 12: # M3b... M13b
123-
self.wallet.sendrawtransaction(from_node=node, tx_hex=tx["hex"])
124-
else: # Pb
125-
package_hex.append(tx["hex"])
102+
# 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])
104+
# Pa
105+
pa_hex = self.wallet.create_self_transfer(utxo_to_spend=chain_a_tip_utxo)["hex"]
106+
package_hex.append(pa_hex)
107+
108+
# 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])
110+
# Pb
111+
pb_hex = self.wallet.create_self_transfer(utxo_to_spend=chain_b_tip_utxo)["hex"]
112+
package_hex.append(pb_hex)
126113

127114
assert_equal(24, node.getmempoolinfo()["size"])
128115
assert_equal(2, len(package_hex))
@@ -158,9 +145,7 @@ def test_desc_count_limits_2(self):
158145
m1_utxos = self.wallet.send_self_transfer_multi(from_node=node, num_outputs=2)['new_utxos']
159146

160147
# Chain M2...M24
161-
chain_utxo = m1_utxos[0]
162-
for _ in range(23): # M2...M24
163-
chain_utxo = self.wallet.send_self_transfer(from_node=node, utxo_to_spend=chain_utxo)["new_utxo"]
148+
self.wallet.send_self_transfer_chain(from_node=node, chain_length=23, utxo_to_spend=m1_utxos[0])
164149

165150
# P1
166151
p1_tx = self.wallet.create_self_transfer(utxo_to_spend=m1_utxos[1])
@@ -206,15 +191,11 @@ def test_anc_count_limits(self):
206191

207192
# Two chains of 13 transactions each
208193
for _ in range(2):
209-
chaintip_utxo = None
210-
for i in range(13):
211-
tx = self.wallet.create_self_transfer(utxo_to_spend=chaintip_utxo)
212-
chaintip_utxo = tx["new_utxo"]
213-
if i < 12:
214-
self.wallet.sendrawtransaction(from_node=node, tx_hex=tx["hex"])
215-
else: # Save the 13th transaction for the package
216-
package_hex.append(tx["hex"])
217-
pc_parent_utxos.append(chaintip_utxo)
194+
chain_tip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=12)
195+
# Save the 13th transaction for the package
196+
tx = self.wallet.create_self_transfer(utxo_to_spend=chain_tip_utxo)
197+
package_hex.append(tx["hex"])
198+
pc_parent_utxos.append(tx["new_utxo"])
218199

219200
# Child Pc
220201
pc_hex = self.wallet.create_self_transfer_multi(utxos_to_spend=pc_parent_utxos)["hex"]
@@ -253,12 +234,9 @@ def test_anc_count_limits_2(self):
253234
self.log.info("Check that in-mempool and in-package ancestors are calculated properly in packages")
254235
# Two chains of 12 transactions each
255236
for _ in range(2):
256-
chaintip_utxo = None
257-
for i in range(12):
258-
chaintip_utxo = self.wallet.send_self_transfer(from_node=node, utxo_to_spend=chaintip_utxo)["new_utxo"]
259-
if i == 11:
260-
# last 2 transactions will be the parents of Pc
261-
pc_parent_utxos.append(chaintip_utxo)
237+
chaintip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=12)
238+
# last 2 transactions will be the parents of Pc
239+
pc_parent_utxos.append(chaintip_utxo)
262240

263241
# Child Pc
264242
pc_tx = self.wallet.create_self_transfer_multi(utxos_to_spend=pc_parent_utxos)

test/functional/test_framework/wallet.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,18 @@ def sendrawtransaction(self, *, from_node, tx_hex, maxfeerate=0, **kwargs):
335335
self.scan_tx(from_node.decoderawtransaction(tx_hex))
336336
return txid
337337

338+
def send_self_transfer_chain(self, *, from_node, chain_length, utxo_to_spend=None):
339+
"""Create and send a "chain" of chain_length transactions. The nth transaction in
340+
the chain is a child of the n-1th transaction and parent of the n+1th transaction.
341+
342+
Returns the chaintip (nth) utxo
343+
"""
344+
chaintip_utxo = utxo_to_spend or self.get_utxo()
345+
for _ in range(chain_length):
346+
chaintip_utxo = self.send_self_transfer(utxo_to_spend=chaintip_utxo, from_node=from_node)["new_utxo"]
347+
return chaintip_utxo
348+
349+
338350
def getnewdestination(address_type='bech32m'):
339351
"""Generate a random destination of the specified type and return the
340352
corresponding public key, scriptPubKey and address. Supported types are

0 commit comments

Comments
 (0)