Skip to content

Commit 4f34714

Browse files
committed
test: refactor: move fill_mempool to new module mempool_util
This is needed to avoid circular dependencies in later commits. Can be reviewed via `--color-moved=dimmed-zebra`.
1 parent eb0bdbd commit 4f34714

File tree

7 files changed

+89
-64
lines changed

7 files changed

+89
-64
lines changed

test/functional/mempool_limit.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66

77
from decimal import Decimal
88

9+
from test_framework.mempool_util import (
10+
fill_mempool,
11+
)
912
from test_framework.p2p import P2PTxInvStore
1013
from test_framework.test_framework import BitcoinTestFramework
1114
from test_framework.util import (
1215
assert_equal,
1316
assert_fee_amount,
1417
assert_greater_than,
1518
assert_raises_rpc_error,
16-
fill_mempool,
1719
)
1820
from test_framework.wallet import (
1921
COIN,

test/functional/p2p_1p1c_network.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from decimal import Decimal
1313
from math import ceil
1414

15+
from test_framework.mempool_util import (
16+
fill_mempool,
17+
)
1518
from test_framework.messages import (
1619
msg_tx,
1720
)
@@ -22,7 +25,6 @@
2225
from test_framework.util import (
2326
assert_equal,
2427
assert_greater_than,
25-
fill_mempool,
2628
)
2729
from test_framework.wallet import (
2830
MiniWallet,

test/functional/p2p_opportunistic_1p1c.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
from decimal import Decimal
1010
import time
11+
from test_framework.mempool_util import (
12+
fill_mempool,
13+
)
1114
from test_framework.messages import (
1215
CInv,
1316
CTxInWitness,
@@ -24,7 +27,6 @@
2427
from test_framework.util import (
2528
assert_equal,
2629
assert_greater_than,
27-
fill_mempool,
2830
)
2931
from test_framework.wallet import (
3032
MiniWallet,

test/functional/p2p_tx_download.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from decimal import Decimal
99
import time
1010

11+
from test_framework.mempool_util import (
12+
fill_mempool,
13+
)
1114
from test_framework.messages import (
1215
CInv,
1316
MSG_TX,
@@ -24,7 +27,6 @@
2427
from test_framework.test_framework import BitcoinTestFramework
2528
from test_framework.util import (
2629
assert_equal,
27-
fill_mempool,
2830
)
2931
from test_framework.wallet import MiniWallet
3032

test/functional/rpc_packages.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import random
99

1010
from test_framework.blocktools import COINBASE_MATURITY
11+
from test_framework.mempool_util import (
12+
fill_mempool,
13+
)
1114
from test_framework.messages import (
1215
MAX_BIP125_RBF_SEQUENCE,
1316
tx_from_hex,
@@ -18,7 +21,6 @@
1821
assert_equal,
1922
assert_fee_amount,
2023
assert_raises_rpc_error,
21-
fill_mempool,
2224
)
2325
from test_framework.wallet import (
2426
DEFAULT_FEE,
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2024 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Helpful routines for mempool testing."""
6+
from decimal import Decimal
7+
8+
from .util import (
9+
assert_equal,
10+
assert_greater_than,
11+
create_lots_of_big_transactions,
12+
gen_return_txouts,
13+
)
14+
15+
16+
def fill_mempool(test_framework, node, miniwallet):
17+
"""Fill mempool until eviction.
18+
19+
Allows for simpler testing of scenarios with floating mempoolminfee > minrelay
20+
Requires -datacarriersize=100000 and
21+
-maxmempool=5.
22+
It will not ensure mempools become synced as it
23+
is based on a single node and assumes -minrelaytxfee
24+
is 1 sat/vbyte.
25+
To avoid unintentional tx dependencies, it is recommended to use separate miniwallets for
26+
mempool filling vs transactions in tests.
27+
"""
28+
test_framework.log.info("Fill the mempool until eviction is triggered and the mempoolminfee rises")
29+
txouts = gen_return_txouts()
30+
relayfee = node.getnetworkinfo()['relayfee']
31+
32+
assert_equal(relayfee, Decimal('0.00001000'))
33+
34+
tx_batch_size = 1
35+
num_of_batches = 75
36+
# Generate UTXOs to flood the mempool
37+
# 1 to create a tx initially that will be evicted from the mempool later
38+
# 75 transactions each with a fee rate higher than the previous one
39+
test_framework.generate(miniwallet, 1 + (num_of_batches * tx_batch_size))
40+
41+
# Mine COINBASE_MATURITY - 1 blocks so that the UTXOs are allowed to be spent
42+
test_framework.generate(node, 100 - 1)
43+
44+
# Get all UTXOs up front to ensure none of the transactions spend from each other, as that may
45+
# change their effective feerate and thus the order in which they are selected for eviction.
46+
confirmed_utxos = [miniwallet.get_utxo(confirmed_only=True) for _ in range(num_of_batches * tx_batch_size + 1)]
47+
assert_equal(len(confirmed_utxos), num_of_batches * tx_batch_size + 1)
48+
49+
test_framework.log.debug("Create a mempool tx that will be evicted")
50+
tx_to_be_evicted_id = miniwallet.send_self_transfer(from_node=node, utxo_to_spend=confirmed_utxos[0], fee_rate=relayfee)["txid"]
51+
del confirmed_utxos[0]
52+
53+
# Increase the tx fee rate to give the subsequent transactions a higher priority in the mempool
54+
# The tx has an approx. vsize of 65k, i.e. multiplying the previous fee rate (in sats/kvB)
55+
# by 130 should result in a fee that corresponds to 2x of that fee rate
56+
base_fee = relayfee * 130
57+
58+
test_framework.log.debug("Fill up the mempool with txs with higher fee rate")
59+
with node.assert_debug_log(["rolling minimum fee bumped"]):
60+
for batch_of_txid in range(num_of_batches):
61+
fee = (batch_of_txid + 1) * base_fee
62+
utxos = confirmed_utxos[:tx_batch_size]
63+
create_lots_of_big_transactions(miniwallet, node, fee, tx_batch_size, txouts, utxos)
64+
del confirmed_utxos[:tx_batch_size]
65+
66+
test_framework.log.debug("The tx should be evicted by now")
67+
# The number of transactions created should be greater than the ones present in the mempool
68+
assert_greater_than(tx_batch_size * num_of_batches, len(node.getrawmempool()))
69+
# Initial tx created should not be present in the mempool anymore as it had a lower fee rate
70+
assert tx_to_be_evicted_id not in node.getrawmempool()
71+
72+
test_framework.log.debug("Check that mempoolminfee is larger than minrelaytxfee")
73+
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00001000'))
74+
assert_greater_than(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00001000'))

test/functional/test_framework/util.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -496,65 +496,6 @@ def check_node_connections(*, node, num_in, num_out):
496496
assert_equal(info["connections_in"], num_in)
497497
assert_equal(info["connections_out"], num_out)
498498

499-
def fill_mempool(test_framework, node, miniwallet):
500-
"""Fill mempool until eviction.
501-
502-
Allows for simpler testing of scenarios with floating mempoolminfee > minrelay
503-
Requires -datacarriersize=100000 and
504-
-maxmempool=5.
505-
It will not ensure mempools become synced as it
506-
is based on a single node and assumes -minrelaytxfee
507-
is 1 sat/vbyte.
508-
To avoid unintentional tx dependencies, it is recommended to use separate miniwallets for
509-
mempool filling vs transactions in tests.
510-
"""
511-
test_framework.log.info("Fill the mempool until eviction is triggered and the mempoolminfee rises")
512-
txouts = gen_return_txouts()
513-
relayfee = node.getnetworkinfo()['relayfee']
514-
515-
assert_equal(relayfee, Decimal('0.00001000'))
516-
517-
tx_batch_size = 1
518-
num_of_batches = 75
519-
# Generate UTXOs to flood the mempool
520-
# 1 to create a tx initially that will be evicted from the mempool later
521-
# 75 transactions each with a fee rate higher than the previous one
522-
test_framework.generate(miniwallet, 1 + (num_of_batches * tx_batch_size))
523-
524-
# Mine COINBASE_MATURITY - 1 blocks so that the UTXOs are allowed to be spent
525-
test_framework.generate(node, 100 - 1)
526-
527-
# Get all UTXOs up front to ensure none of the transactions spend from each other, as that may
528-
# change their effective feerate and thus the order in which they are selected for eviction.
529-
confirmed_utxos = [miniwallet.get_utxo(confirmed_only=True) for _ in range(num_of_batches * tx_batch_size + 1)]
530-
assert_equal(len(confirmed_utxos), num_of_batches * tx_batch_size + 1)
531-
532-
test_framework.log.debug("Create a mempool tx that will be evicted")
533-
tx_to_be_evicted_id = miniwallet.send_self_transfer(from_node=node, utxo_to_spend=confirmed_utxos[0], fee_rate=relayfee)["txid"]
534-
del confirmed_utxos[0]
535-
536-
# Increase the tx fee rate to give the subsequent transactions a higher priority in the mempool
537-
# The tx has an approx. vsize of 65k, i.e. multiplying the previous fee rate (in sats/kvB)
538-
# by 130 should result in a fee that corresponds to 2x of that fee rate
539-
base_fee = relayfee * 130
540-
541-
test_framework.log.debug("Fill up the mempool with txs with higher fee rate")
542-
with node.assert_debug_log(["rolling minimum fee bumped"]):
543-
for batch_of_txid in range(num_of_batches):
544-
fee = (batch_of_txid + 1) * base_fee
545-
utxos = confirmed_utxos[:tx_batch_size]
546-
create_lots_of_big_transactions(miniwallet, node, fee, tx_batch_size, txouts, utxos)
547-
del confirmed_utxos[:tx_batch_size]
548-
549-
test_framework.log.debug("The tx should be evicted by now")
550-
# The number of transactions created should be greater than the ones present in the mempool
551-
assert_greater_than(tx_batch_size * num_of_batches, len(node.getrawmempool()))
552-
# Initial tx created should not be present in the mempool anymore as it had a lower fee rate
553-
assert tx_to_be_evicted_id not in node.getrawmempool()
554-
555-
test_framework.log.debug("Check that mempoolminfee is larger than minrelaytxfee")
556-
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00001000'))
557-
assert_greater_than(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00001000'))
558499

559500
# Transaction/Block functions
560501
#############################

0 commit comments

Comments
 (0)