From 138f35cba6bdc5c1c1c064e5237e3a3bb1f70699 Mon Sep 17 00:00:00 2001 From: Amine Khaldi Date: Fri, 19 Sep 2025 10:59:16 +0100 Subject: [PATCH] Simplify can_replace. --- chia/_tests/core/mempool/test_mempool_manager.py | 3 +-- chia/full_node/mempool_manager.py | 13 ++++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/chia/_tests/core/mempool/test_mempool_manager.py b/chia/_tests/core/mempool/test_mempool_manager.py index 24d92fe0efcb..461009c57140 100644 --- a/chia/_tests/core/mempool/test_mempool_manager.py +++ b/chia/_tests/core/mempool/test_mempool_manager.py @@ -1119,8 +1119,7 @@ def make_test_coins() -> list[Coin]: ], ) def test_can_replace(existing_items: list[MempoolItem], new_item: MempoolItem, expected: bool) -> None: - removals = {c.name() for c in new_item.spend_bundle.removals()} - assert can_replace(existing_items, removals, new_item) == expected + assert can_replace(existing_items, new_item) == expected @pytest.mark.anyio diff --git a/chia/full_node/mempool_manager.py b/chia/full_node/mempool_manager.py index 034afe4b0a13..480214d77457 100644 --- a/chia/full_node/mempool_manager.py +++ b/chia/full_node/mempool_manager.py @@ -764,7 +764,7 @@ async def validate_spend_bundle( if fail_reason is Err.MEMPOOL_CONFLICT: log.debug(f"Replace attempted. number of MempoolItems: {len(conflicts)}") - if not can_replace(conflicts, removal_names, potential): + if not can_replace(conflicts, potential): return Err.MEMPOOL_CONFLICT, potential, [] duration = time.monotonic() - start_time @@ -1024,17 +1024,12 @@ def optional_max(a: Optional[T], b: Optional[T]) -> Optional[T]: return max((v for v in [a, b] if v is not None), default=None) -def can_replace( - conflicting_items: list[MempoolItem], - removal_names: set[bytes32], - new_item: MempoolItem, -) -> bool: +def can_replace(conflicting_items: list[MempoolItem], new_item: MempoolItem) -> bool: """ This function implements the mempool replacement rules. Given a Mempool item we're attempting to insert into the mempool (new_item) and the set of existing mempool items that conflict with it, this function answers the question whether - the existing items can be replaced by the new one. The removals parameter are - the coin IDs the new mempool item is spending. + the existing items can be replaced by the new one. """ conflicting_fees = 0 @@ -1059,7 +1054,7 @@ def can_replace( # fee than AB therefore kicking out A altogether. The better way to solve this would be to keep a cache # of booted transactions like A, and retry them after they get removed from mempool due to a conflict. for coin_id, bcs in item.bundle_coin_spends.items(): - if coin_id not in removal_names: + if coin_id not in new_item.bundle_coin_spends: log.debug("Rejecting conflicting tx as it does not spend conflicting coin %s", coin_id) return False if bcs.eligible_for_fast_forward: