[WIP] rpc: add clearmempool command for regtest mode #32418

pull cmdruid wants to merge 1 commits into bitcoin:master from cmdruid:rpc_add_clearmempool changing 3 files +88 −0
  1. cmdruid commented at 11:58 pm on May 4, 2025: none

    Description

    This commit adds a new RPC command called clearmempool. It is designed to remove all tranasctions from the mempool (including those from invalidated blocks), and return their TXIDs.

    The purpose of this command is to clear the mempool, then use the returned TXIDs to perform additional cleanup.

    When used in combination with invalidateblock, it allows me to roll-back the chain to a particular height, clear the mempool, then properly clean up wallets with abandontransaction.

    Note that this command does not prevent the transactions from being re-inserted back into the mempool, either through mempool.dat or user wallets. You have to perform additional cleanup to make sure your changes persist.

    I restricted this command to regtest mode as I only need it for running tests, but it could work in any mode.

    Rationale

    My rationale for adding this command is that I need the ability to cleanly rollback the chain and wallets during testing, and I kept running into the following roadblock:

    • invalidateblock will rollback the chain height, but all the transactions are dumped into the mempool, creating spending conflicts with the wallets.

    • You can prevent this dump from happening, most notably with -walletbroadcast=0, however that just hides the problem.

    • The real issue is that wallets are still tracking these transactions, and you need to pull their TXIDs from the mempool to properly purge them with abandontransaction.

    • But if the transactions are in the mempool, then abandontransaction will fail, which puts you in a catch-22 situation.

    clearmempool allows you to clear the mempool and capture a list of these transactions, so that you can perform the proper cleanup with abandontransaction.

  2. rpc: add `clearmempool` command for regtest
    This commit adds a new RPC command called `clearmempool`. It is designed to remove all tranasctions from the mempool (including those from invalidated blocks), and return their TXIDs.
    
    The purpose of this command is to clear the mempool, then iterate over the returned TXIDs to perform additional cleanup.
    
    When used in combination with `invalidateblock`, it allows me to roll-back the chain to a particular height, clear the mempool, then properly clean up wallets with `abandontransaction`.
    
    Note that this command does not prevent the transactions from being re-inserted back into the mempool, either through mempool.dat or user wallets. You have to perform additional cleanup to make sure your changes persist.
    
    I restricted this command to regtest mode as I only need it for running tests, but it could work in any mode.
    77d137b6d5
  3. DrahtBot commented at 11:58 pm on May 4, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32418.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

  4. DrahtBot added the label CI failed on May 5, 2025
  5. DrahtBot commented at 0:58 am on May 5, 2025: contributor

    🚧 At least one of the CI tasks failed. Task lint: https://github.com/bitcoin/bitcoin/runs/41620114643 LLM reason (✨ experimental): The CI failure is primarily caused by linting errors, specifically trailing whitespace in “rpc_clearmempool.py” and multiple spelling errors flagged by codespell.

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

  6. andrewtoth commented at 1:21 am on May 5, 2025: contributor

    I need the ability to cleanly rollback the chain and wallets during testing

    What about invalidating the block, recording all mempool txs via getrawmempool, stopping the node, deleting mempool.dat and restarting?

  7. ajtowns commented at 8:20 am on May 5, 2025: contributor

    Previously proposed in #13836

    Wouldn’t it be better to add an option to abandontransaction that tells it to first delete the transaction from the mempool? That could even be useful for mainnet, perhaps?


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-05-05 12:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me