Skip to content

Commit e669833

Browse files
committed
test: dedup package limit checks via decorator in mempool_package_limits.py
1 parent 72f25e2 commit e669833

File tree

1 file changed

+40
-64
lines changed

1 file changed

+40
-64
lines changed

test/functional/mempool_package_limits.py

Lines changed: 40 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@
1313
)
1414
from test_framework.wallet import MiniWallet
1515

16+
# Decorator to
17+
# 1) check that mempool is empty at the start of a subtest
18+
# 2) run the subtest, which may submit some transaction(s) to the mempool and
19+
# create a list of hex transactions
20+
# 3) testmempoolaccept the package hex and check that it fails with the error
21+
# "package-mempool-limits" for each tx
22+
# 4) after mining a block, clearing the pre-submitted transactions from mempool,
23+
# check that submitting the created package succeeds
24+
def check_package_limits(func):
25+
def func_wrapper(self, *args, **kwargs):
26+
node = self.nodes[0]
27+
assert_equal(0, node.getmempoolinfo()["size"])
28+
package_hex = func(self, *args, **kwargs)
29+
testres_error_expected = node.testmempoolaccept(rawtxs=package_hex)
30+
assert_equal(len(testres_error_expected), len(package_hex))
31+
for txres in testres_error_expected:
32+
assert_equal(txres["package-error"], "package-mempool-limits")
33+
34+
# Clear mempool and check that the package passes now
35+
self.generate(node, 1)
36+
assert all([res["allowed"] for res in node.testmempoolaccept(rawtxs=package_hex)])
37+
38+
return func_wrapper
39+
1640

1741
class MempoolPackageLimitsTest(BitcoinTestFramework):
1842
def set_test_params(self):
@@ -37,9 +61,9 @@ def run_test(self):
3761
self.test_anc_size_limits()
3862
self.test_desc_size_limits()
3963

64+
@check_package_limits
4065
def test_chain_limits_helper(self, mempool_count, package_count):
4166
node = self.nodes[0]
42-
assert_equal(0, node.getmempoolinfo()["size"])
4367
chain_hex = []
4468

4569
chaintip_utxo = self.wallet.send_self_transfer_chain(from_node=node, chain_length=mempool_count)[-1]["new_utxo"]
@@ -48,13 +72,7 @@ def test_chain_limits_helper(self, mempool_count, package_count):
4872
tx = self.wallet.create_self_transfer(utxo_to_spend=chaintip_utxo)
4973
chaintip_utxo = tx["new_utxo"]
5074
chain_hex.append(tx["hex"])
51-
testres_too_long = node.testmempoolaccept(rawtxs=chain_hex)
52-
for txres in testres_too_long:
53-
assert_equal(txres["package-error"], "package-mempool-limits")
54-
55-
# Clear mempool and check that the package passes now
56-
self.generate(node, 1)
57-
assert all([res["allowed"] for res in node.testmempoolaccept(rawtxs=chain_hex)])
75+
return chain_hex
5876

5977
def test_chain_limits(self):
6078
"""Create chains from mempool and package transactions that are longer than 25,
@@ -73,6 +91,7 @@ def test_chain_limits(self):
7391
# 13 transactions in the mempool and 13 in the package.
7492
self.test_chain_limits_helper(13, 13)
7593

94+
@check_package_limits
7695
def test_desc_count_limits(self):
7796
"""Create an 'A' shaped package with 24 transactions in the mempool and 2 in the package:
7897
M1
@@ -90,7 +109,6 @@ def test_desc_count_limits(self):
90109
package transactions).
91110
"""
92111
node = self.nodes[0]
93-
assert_equal(0, node.getmempoolinfo()["size"])
94112
self.log.info("Check that in-mempool and in-package descendants are calculated properly in packages")
95113
# Top parent in mempool, M1
96114
m1_utxos = self.wallet.send_self_transfer_multi(from_node=node, num_outputs=2)['new_utxos']
@@ -110,14 +128,9 @@ def test_desc_count_limits(self):
110128

111129
assert_equal(24, node.getmempoolinfo()["size"])
112130
assert_equal(2, len(package_hex))
113-
testres_too_long = node.testmempoolaccept(rawtxs=package_hex)
114-
for txres in testres_too_long:
115-
assert_equal(txres["package-error"], "package-mempool-limits")
116-
117-
# Clear mempool and check that the package passes now
118-
self.generate(node, 1)
119-
assert all([res["allowed"] for res in node.testmempoolaccept(rawtxs=package_hex)])
131+
return package_hex
120132

133+
@check_package_limits
121134
def test_desc_count_limits_2(self):
122135
"""Create a Package with 24 transaction in mempool and 2 transaction in package:
123136
M1
@@ -154,15 +167,9 @@ def test_desc_count_limits_2(self):
154167

155168
assert_equal(24, node.getmempoolinfo()["size"])
156169
assert_equal(2, len(package_hex))
157-
testres = node.testmempoolaccept(rawtxs=package_hex)
158-
assert_equal(len(testres), len(package_hex))
159-
for txres in testres:
160-
assert_equal(txres["package-error"], "package-mempool-limits")
161-
162-
# Clear mempool and check that the package passes now
163-
self.generate(node, 1)
164-
assert all([res["allowed"] for res in node.testmempoolaccept(rawtxs=package_hex)])
170+
return package_hex
165171

172+
@check_package_limits
166173
def test_anc_count_limits(self):
167174
"""Create a 'V' shaped chain with 24 transactions in the mempool and 3 in the package:
168175
M1a M1b
@@ -180,7 +187,6 @@ def test_anc_count_limits(self):
180187
and in-package ancestors are all considered together.
181188
"""
182189
node = self.nodes[0]
183-
assert_equal(0, node.getmempoolinfo()["size"])
184190
package_hex = []
185191
pc_parent_utxos = []
186192

@@ -200,14 +206,9 @@ def test_anc_count_limits(self):
200206

201207
assert_equal(24, node.getmempoolinfo()["size"])
202208
assert_equal(3, len(package_hex))
203-
testres_too_long = node.testmempoolaccept(rawtxs=package_hex)
204-
for txres in testres_too_long:
205-
assert_equal(txres["package-error"], "package-mempool-limits")
206-
207-
# Clear mempool and check that the package passes now
208-
self.generate(node, 1)
209-
assert all([res["allowed"] for res in node.testmempoolaccept(rawtxs=package_hex)])
209+
return package_hex
210210

211+
@check_package_limits
211212
def test_anc_count_limits_2(self):
212213
"""Create a 'Y' shaped chain with 24 transactions in the mempool and 2 in the package:
213214
M1a M1b
@@ -225,7 +226,6 @@ def test_anc_count_limits_2(self):
225226
and in-package ancestors are all considered together.
226227
"""
227228
node = self.nodes[0]
228-
assert_equal(0, node.getmempoolinfo()["size"])
229229
pc_parent_utxos = []
230230

231231
self.log.info("Check that in-mempool and in-package ancestors are calculated properly in packages")
@@ -242,14 +242,9 @@ def test_anc_count_limits_2(self):
242242
pd_tx = self.wallet.create_self_transfer(utxo_to_spend=pc_tx["new_utxos"][0])
243243

244244
assert_equal(24, node.getmempoolinfo()["size"])
245-
testres_too_long = node.testmempoolaccept(rawtxs=[pc_tx["hex"], pd_tx["hex"]])
246-
for txres in testres_too_long:
247-
assert_equal(txres["package-error"], "package-mempool-limits")
248-
249-
# Clear mempool and check that the package passes now
250-
self.generate(node, 1)
251-
assert all([res["allowed"] for res in node.testmempoolaccept(rawtxs=[pc_tx["hex"], pd_tx["hex"]])])
245+
return [pc_tx["hex"], pd_tx["hex"]]
252246

247+
@check_package_limits
253248
def test_anc_count_limits_bushy(self):
254249
"""Create a tree with 20 transactions in the mempool and 6 in the package:
255250
M1...M4 M5...M8 M9...M12 M13...M16 M17...M20
@@ -262,7 +257,6 @@ def test_anc_count_limits_bushy(self):
262257
combined, PC has 25 in-mempool and in-package parents.
263258
"""
264259
node = self.nodes[0]
265-
assert_equal(0, node.getmempoolinfo()["size"])
266260
package_hex = []
267261
pc_parent_utxos = []
268262
for _ in range(5): # Make package transactions P0 ... P4
@@ -279,14 +273,9 @@ def test_anc_count_limits_bushy(self):
279273

280274
assert_equal(20, node.getmempoolinfo()["size"])
281275
assert_equal(6, len(package_hex))
282-
testres = node.testmempoolaccept(rawtxs=package_hex)
283-
for txres in testres:
284-
assert_equal(txres["package-error"], "package-mempool-limits")
285-
286-
# Clear mempool and check that the package passes now
287-
self.generate(node, 1)
288-
assert all([res["allowed"] for res in node.testmempoolaccept(rawtxs=package_hex)])
276+
return package_hex
289277

278+
@check_package_limits
290279
def test_anc_size_limits(self):
291280
"""Test Case with 2 independent transactions in the mempool and a parent + child in the
292281
package, where the package parent is the child of both mempool transactions (30KvB each):
@@ -299,7 +288,6 @@ def test_anc_size_limits(self):
299288
and in-package ancestors are all considered together.
300289
"""
301290
node = self.nodes[0]
302-
assert_equal(0, node.getmempoolinfo()["size"])
303291
parent_utxos = []
304292
target_vsize = 30_000
305293
high_fee = 10 * target_vsize # 10 sats/vB
@@ -318,14 +306,9 @@ def test_anc_size_limits(self):
318306
pd_tx = self.wallet.create_self_transfer(utxo_to_spend=pc_tx["new_utxos"][0], target_weight=target_weight)
319307

320308
assert_equal(2, node.getmempoolinfo()["size"])
321-
testres_too_heavy = node.testmempoolaccept(rawtxs=[pc_tx["hex"], pd_tx["hex"]])
322-
for txres in testres_too_heavy:
323-
assert_equal(txres["package-error"], "package-mempool-limits")
324-
325-
# Clear mempool and check that the package passes now
326-
self.generate(node, 1)
327-
assert all([res["allowed"] for res in node.testmempoolaccept(rawtxs=[pc_tx["hex"], pd_tx["hex"]])])
309+
return [pc_tx["hex"], pd_tx["hex"]]
328310

311+
@check_package_limits
329312
def test_desc_size_limits(self):
330313
"""Create 3 mempool transactions and 2 package transactions (21KvB each):
331314
Ma
@@ -337,7 +320,6 @@ def test_desc_size_limits(self):
337320
and in-package descendants are all considered together.
338321
"""
339322
node = self.nodes[0]
340-
assert_equal(0, node.getmempoolinfo()["size"])
341323
target_vsize = 21_000
342324
high_fee = 10 * target_vsize # 10 sats/vB
343325
target_weight = target_vsize * WITNESS_SCALE_FACTOR
@@ -358,13 +340,7 @@ def test_desc_size_limits(self):
358340

359341
assert_equal(3, node.getmempoolinfo()["size"])
360342
assert_equal(2, len(package_hex))
361-
testres_too_heavy = node.testmempoolaccept(rawtxs=package_hex)
362-
for txres in testres_too_heavy:
363-
assert_equal(txres["package-error"], "package-mempool-limits")
364-
365-
# Clear mempool and check that the package passes now
366-
self.generate(node, 1)
367-
assert all([res["allowed"] for res in node.testmempoolaccept(rawtxs=package_hex)])
343+
return package_hex
368344

369345

370346
if __name__ == "__main__":

0 commit comments

Comments
 (0)