Skip to content

Commit

Permalink
test: add functional test for new cluster mempool RPCs
Browse files Browse the repository at this point in the history
  • Loading branch information
sdaftuar committed Apr 27, 2024
1 parent 8ef26be commit c1875e5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test/functional/mempool_cluster.py
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
# Copyright (c) 2024 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test cluster mempool accessors and limits"""

from test_framework.test_framework import BitcoinTestFramework
from test_framework.wallet import (
MiniWallet,
)
from test_framework.util import (
assert_raises_rpc_error,
)

MAX_CLUSTER_COUNT = 100

class MempoolClusterTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1

def run_test(self):
node = self.nodes[0]
self.wallet = MiniWallet(node)

node = self.nodes[0]
parent_tx = self.wallet.send_self_transfer(from_node=node)
utxo_to_spend = parent_tx["new_utxo"]
ancestors = [parent_tx["txid"]]
while len(node.getrawmempool()) < MAX_CLUSTER_COUNT:
next_tx = self.wallet.send_self_transfer(from_node=node, utxo_to_spend=utxo_to_spend)
# Confirm that each transaction is in the same cluster as the first.
assert node.getmempoolentry(next_tx['txid'])['clusterid'] == node.getmempoolentry(parent_tx['txid'])['clusterid']
# Confirm that there is only one cluster in the mempool.
assert node.getmempoolinfo()['numberofclusters'] == 1

# Confirm that the ancestors are what we expect
mempool_ancestors = node.getmempoolancestors(next_tx['txid'])
assert sorted(mempool_ancestors) == sorted(ancestors)

# Confirm that each successive transaction is added as a descendant.
assert all([ next_tx["txid"] in node.getmempooldescendants(x) for x in ancestors ])

# Update for next iteration
ancestors.append(next_tx["txid"])
utxo_to_spend = next_tx["new_utxo"]

assert node.getmempoolinfo()['numberofclusters'] == 1
assert node.getmempoolinfo()['maxclustercount'] == MAX_CLUSTER_COUNT
assert node.getmempoolcluster(node.getmempoolentry(parent_tx['txid'])['clusterid'])['txcount'] == MAX_CLUSTER_COUNT
feeratediagram = node.getmempoolfeeratediagram()
last_val = [0, 0]
for x in feeratediagram:
assert x['size'] > 0
assert last_val[0]*x['fee'] >= last_val[1]*x['size']
last_val = [x['size'], x['fee']]

# Test that adding one more transaction to the cluster will fail.
bad_tx = self.wallet.create_self_transfer(utxo_to_spend=utxo_to_spend)
assert_raises_rpc_error(-26, "too-large-cluster", node.sendrawtransaction, bad_tx["hex"])

# TODO: verify that the size limits are also enforced.

if __name__ == '__main__':
MempoolClusterTest().main()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Expand Up @@ -377,6 +377,7 @@
'p2p_sendtxrcncl.py',
'rpc_scantxoutset.py',
'feature_unsupported_utxo_db.py',
'mempool_cluster.py',
'feature_logging.py',
'feature_anchors.py',
'mempool_datacarrier.py',
Expand Down

0 comments on commit c1875e5

Please sign in to comment.