rpc: Manual prune lock management (Take 2) #34534

Open
fjahr wants to merge 6 commits from 2026-02-prunelocks-rpc-v2 into master AGit
fjahr commented 2026-02-08 14:01:05 +00:00 (Migrated from github.com)

This is a refreshed version of #19463, which was closed due to inactivity.

Motivation: On pruned nodes, if a wallet is not loaded while the node is running, blocks necessary for rescanning when the wallet is loaded may be pruned. This PR allows users and external wallet software to manually manage prune locks via RPC, so they can protect blocks needed for future rescans and also clean up locks that are no longer relevant (e.g. after permanently unloading a wallet from a node). This is useful both for Bitcoin Core's own wallet and for external wallets that use Bitcoin Core as a backend. Usage of temporary prune locking may also be helpful in other functional test scenarios but I didn't spend time to check where might apply.

This PR leverages the existing prune locks system we already have. The interface of the RPCs of the old PR has been slightly updated, it now matches the pattern used by setban/listbanned exactly. The locks are prefixed to distinguish them from the systems locks and prevent naming colissions. They are not persisted across restarts.

This is a refreshed version of #19463, which was closed due to inactivity. Motivation: On pruned nodes, if a wallet is not loaded while the node is running, blocks necessary for rescanning when the wallet is loaded may be pruned. This PR allows users and external wallet software to manually manage prune locks via RPC, so they can protect blocks needed for future rescans and also clean up locks that are no longer relevant (e.g. after permanently unloading a wallet from a node). This is useful both for Bitcoin Core's own wallet and for external wallets that use Bitcoin Core as a backend. Usage of temporary prune locking may also be helpful in other functional test scenarios but I didn't spend time to check where might apply. This PR leverages the existing prune locks system we already have. The interface of the RPCs of the old PR has been slightly updated, it now matches the pattern used by `setban`/`listbanned` exactly. The locks are prefixed to distinguish them from the systems locks and prevent naming colissions. They are not persisted across restarts.
DrahtBot commented 2026-02-08 14:01:50 +00:00 (Migrated from github.com)

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK optout21, andrewtoth
Concept ACK stickies-v
Stale ACK sedited, GEREMIAA

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36904 (log: Remove trailing and embedded newlines from log messages by maflcko)
  • #36858 (refactor: break the blockstorage/validation circular dependency by pzafonte)
  • #36856 (kernel: add manual pruning by pzafonte)
  • #36778 (AssumeUTXO: replace loadtxoutset RPC with a startup arg by l0rinc)
  • #36738 (refactor: move block writes and pruning into BlockManager by pzafonte)
  • #36266 (rpc: Let dumptxoutset fail early on stale targets and concurrent runs by fjahr)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/34534 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/34534.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/34534#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [optout21](https://github.com/bitcoin/bitcoin/pull/34534#issuecomment-7272019991), [andrewtoth](https://github.com/bitcoin/bitcoin/pull/34534#pullrequestreview-6179145610) | | Concept ACK | [stickies-v](https://github.com/bitcoin/bitcoin/pull/34534#issuecomment-4096999828) | | Stale ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/34534#pullrequestreview-5441066705), [GEREMIAA](https://github.com/bitcoin/bitcoin/pull/34534#issuecomment-7172773594) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36904](https://github.com/bitcoin/bitcoin/pull/36904) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36904.svg"></sub> (log: Remove trailing and embedded newlines from log messages by maflcko) * [#36858](https://github.com/bitcoin/bitcoin/pull/36858) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36858.svg"></sub> (refactor: break the blockstorage/validation circular dependency by pzafonte) * [#36856](https://github.com/bitcoin/bitcoin/pull/36856) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36856.svg"></sub> (kernel: add manual pruning by pzafonte) * [#36778](https://github.com/bitcoin/bitcoin/pull/36778) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36778.svg"></sub> (AssumeUTXO: replace `loadtxoutset` RPC with a startup arg by l0rinc) * [#36738](https://github.com/bitcoin/bitcoin/pull/36738) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36738.svg"></sub> (refactor: move block writes and pruning into BlockManager by pzafonte) * [#36266](https://github.com/bitcoin/bitcoin/pull/36266) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36266.svg"></sub> (rpc: Let dumptxoutset fail early on stale targets and concurrent runs by fjahr) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
sedited commented 2026-02-08 14:15:01 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
DrahtBot commented 2026-02-08 15:19:01 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task Windows native, fuzz, VS 2022: https://github.com/bitcoin/bitcoin/actions/runs/21799390662/job/62892243983
LLM reason (✨ experimental): Fuzz test failed because the RPC command "listprunelocks" is not registered as safe or unsafe for fuzzing in rpc.cpp.

Hints

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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `Windows native, fuzz, VS 2022`: https://github.com/bitcoin/bitcoin/actions/runs/21799390662/job/62892243983</sub> <sub>LLM reason (✨ experimental): Fuzz test failed because the RPC command "listprunelocks" is not registered as safe or unsafe for fuzzing in rpc.cpp.</sub> <details><summary>Hints</summary> 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. </details>
fjahr commented 2026-02-08 15:29:23 +00:00 (Migrated from github.com)

Fixing CI failure: Forgot to set RPC_COMMANDS_[NOT_]SAFE_FOR_FUZZING for the new RPCs.

Fixing CI failure: Forgot to set `RPC_COMMANDS_[NOT_]SAFE_FOR_FUZZING` for the new RPCs.
luke-jr (Migrated from github.com) requested changes 2026-02-10 01:53:34 +00:00
luke-jr (Migrated from github.com) left a comment

Incomplete review. Basically, the original PR interface was way better.

Incomplete review. Basically, the original PR interface was way better.
luke-jr (Migrated from github.com) commented 2026-02-10 01:51:23 +00:00

This was an Object for future extensibility. No reason to lose that.

This was an Object for future extensibility. No reason to lose that.
luke-jr (Migrated from github.com) commented 2026-02-10 01:51:33 +00:00
                    {RPCResult::Type::STR, "id", "The identifier of the prune lock"},
```suggestion {RPCResult::Type::STR, "id", "The identifier of the prune lock"}, ```
bvbfan (Migrated from github.com) reviewed 2026-02-10 06:56:23 +00:00
bvbfan (Migrated from github.com) commented 2026-02-10 06:56:23 +00:00

You could get it as copy and don't keep lock while iterating, performance wise.

const auto prune_locks = WITH_LOCK(::cs_main, return chainman.m_blockman.GetPruneLocks());
You could get it as copy and don't keep lock while iterating, performance wise. ``` const auto prune_locks = WITH_LOCK(::cs_main, return chainman.m_blockman.GetPruneLocks()); ```
fjahr (Migrated from github.com) reviewed 2026-02-18 13:54:21 +00:00
fjahr (Migrated from github.com) commented 2026-02-18 13:54:21 +00:00

Done

Done
fjahr (Migrated from github.com) reviewed 2026-02-18 13:54:26 +00:00
fjahr (Migrated from github.com) commented 2026-02-18 13:54:26 +00:00

Done

Done
fjahr (Migrated from github.com) reviewed 2026-02-18 13:54:51 +00:00
fjahr (Migrated from github.com) commented 2026-02-18 13:54:51 +00:00

The RPC is called listprunelocks, I don't see how it could be extended to return anything else aside from a list of locks without that other thing feeling out of place. Can you give some specific ideas that you have? At least I would want to give the RPC a different name then (getpruneinfo or so) but potentially these ideas should rather be in a separate RPC. I prefer consistency and so I really like that we could be consistenty with the banning RPC.

The RPC is called `listprunelocks`, I don't see how it could be extended to return anything else aside from a list of locks without that other thing feeling out of place. Can you give some specific ideas that you have? At least I would want to give the RPC a different name then (`getpruneinfo` or so) but potentially these ideas should rather be in a separate RPC. I prefer consistency and so I really like that we could be consistenty with the banning RPC.
fjahr commented 2026-02-18 13:55:02 +00:00 (Migrated from github.com)

@luke-jr

Basically, the original PR interface was way better.

Is this just about https://github.com/bitcoin/bitcoin/pull/34534#discussion_r2785381112 or did you mean something else?

@luke-jr > Basically, the original PR interface was way better. Is this just about https://github.com/bitcoin/bitcoin/pull/34534#discussion_r2785381112 or did you mean something else?
fjahr commented 2026-02-18 13:55:23 +00:00 (Migrated from github.com)

Addressed feedback from @luke-jr and @bvbfan , thanks for the review!

Addressed feedback from @luke-jr and @bvbfan , thanks for the review!
maflcko (Migrated from github.com) reviewed 2026-02-18 14:28:32 +00:00
maflcko (Migrated from github.com) commented 2026-02-18 14:28:32 +00:00

listprunelocks could return a "blockman epoch" in the future, if there was need to further distinguish rpc results. (Yes, this is made up, but with an array, this is impossible, with an object, this is trivial and harmless)

`listprunelocks` could return a "blockman epoch" in the future, if there was need to further distinguish rpc results. (Yes, this is made up, but with an array, this is impossible, with an object, this is trivial and harmless)
fjahr (Migrated from github.com) reviewed 2026-02-18 14:35:51 +00:00
fjahr (Migrated from github.com) commented 2026-02-18 14:35:51 +00:00

What is a "blockman epoch"? I don't think I have heard of that before.

What is a "blockman epoch"? I don't think I have heard of that before.
fjahr (Migrated from github.com) reviewed 2026-02-19 15:40:00 +00:00
fjahr (Migrated from github.com) commented 2026-02-19 15:40:00 +00:00

I guess TIL per this comment that the JSON object thing is in the dev notes, fair enough, I have changed it to return an object.

I guess TIL per [this comment](https://github.com/bitcoin/bitcoin/pull/33920#discussion_r2826613479) that the JSON object thing is in the dev notes, fair enough, I have changed it to return an object.
sedited commented 2026-03-18 15:57:59 +00:00 (Migrated from github.com)

I can not recall who was mentioned as a potential user but I would appreciate if someone could ping them to take a look if this is still relevant to them.

Do you remember at least who mentioned these users/use cases?

> I can not recall who was mentioned as a potential user but I would appreciate if someone could ping them to take a look if this is still relevant to them. Do you remember at least who mentioned these users/use cases?
stickies-v commented 2026-03-19 17:30:15 +00:00 (Migrated from github.com)

Unfortunately, I can not recall who was mentioned as a potential user but I would appreciate if someone could ping them to take a look if this is still relevant to them.

This looks potentially interesting, but I don't think we should be implementing RPCs when we don't know anyone that would actually be using them? Since the previous PR has been open since 2020 already without users chiming in, it seems like there's no real demand for this?

> Unfortunately, I can not recall who was mentioned as a potential user but I would appreciate if someone could ping them to take a look if this is still relevant to them. This looks potentially interesting, but I don't think we should be implementing RPCs when we don't know anyone that would actually be using them? Since the previous PR has been open since 2020 already without users chiming in, it seems like there's no real demand for this?
achow101 commented 2026-03-19 17:47:16 +00:00 (Migrated from github.com)

This is generally useful in the wallet and for any wallets that leverage Bitcoin Core. The fundamental issue is that if the wallet is not loaded while the node is running, blocks necessary for rescanning when the wallet is loaded may be pruned away. The wallet definitely should be using prune locks, but it's also necessary to have a way to for users to remove prune locks that are no longer relevant, e.g. if they unload a wallet that they don't plan to load on that node again. External wallets that use Bitcoin Core should also find this useful as they can then set prune locks that match the scan state of the external wallet.

This is generally useful in the wallet and for any wallets that leverage Bitcoin Core. The fundamental issue is that if the wallet is not loaded while the node is running, blocks necessary for rescanning when the wallet is loaded may be pruned away. The wallet definitely should be using prune locks, but it's also necessary to have a way to for users to remove prune locks that are no longer relevant, e.g. if they unload a wallet that they don't plan to load on that node again. External wallets that use Bitcoin Core should also find this useful as they can then set prune locks that match the scan state of the external wallet.
stickies-v commented 2026-03-20 10:10:18 +00:00 (Migrated from github.com)

Thanks for the explanation @achow101 . Concept ACK. Enabling wallets to build on top of a pruned node via RPC seems enough of a use case to move this forward. @fjahr I think this might be useful to include in PR description to give it more motivation?

Thanks for the explanation @achow101 . Concept ACK. Enabling wallets to build on top of a pruned node via RPC seems enough of a use case to move this forward. @fjahr I think this might be useful to include in PR description to give it more motivation?
fjahr commented 2026-03-20 11:54:19 +00:00 (Migrated from github.com)

Thanks for the explanation @achow101 . Concept ACK. Enabling wallets to build on top of a pruned node via RPC seems enough of a use case to move this forward. @fjahr I think this might be useful to include in PR description to give it more motivation?

Done, replace that vague part of the description with a better motivation paragraph.

> Thanks for the explanation @achow101 . Concept ACK. Enabling wallets to build on top of a pruned node via RPC seems enough of a use case to move this forward. @fjahr I think this might be useful to include in PR description to give it more motivation? Done, replace that vague part of the description with a better motivation paragraph.
sedited (Migrated from github.com) reviewed 2026-03-21 12:17:09 +00:00
sedited (Migrated from github.com) left a comment

One thing that is still not clear to me is how this is actually useful to external applications if the locks are not persisted. I could see how this might be interesting for our own wallets, by setting prune locks for wallets even though they are not loaded yet. But for outside users this seems like a limitation that is difficult to deal with. If you want to add a wallet on startup, wouldn't that require racing to set a lock again if you restart? What is the exact scenario here?

One thing that is still not clear to me is how this is actually useful to external applications if the locks are not persisted. I could see how this might be interesting for our own wallets, by setting prune locks for wallets even though they are not loaded yet. But for outside users this seems like a limitation that is difficult to deal with. If you want to add a wallet on startup, wouldn't that require racing to set a lock again if you restart? What is the exact scenario here?
sedited (Migrated from github.com) commented 2026-03-21 12:02:03 +00:00

Is there a reason for introducing this helper? Existing validation code that iterates through the locks directly.

Is there a reason for introducing this helper? Existing validation code that iterates through the locks directly.
sedited (Migrated from github.com) commented 2026-03-21 11:41:59 +00:00

Don't we either have to hold this lock while iterating through the prune locks, or copy them? I think my preference would be to just take a copy here.

Don't we either have to hold this lock while iterating through the prune locks, or copy them? I think my preference would be to just take a copy here.
fjahr (Migrated from github.com) reviewed 2026-03-22 13:41:03 +00:00
fjahr (Migrated from github.com) commented 2026-03-22 13:41:03 +00:00

Hm, it certainly seems like I addressed this comment without thinking about it enough because the surrounding parentesis were unnecessary as well. I am now using a explicit unordered_map constructor now to ensure the copy happens before the lock scope ends and I have added a comment and removed the parenthesis.

Hm, it certainly seems like I addressed [this comment](https://github.com/bitcoin/bitcoin/pull/34534#discussion_r2786137682) without thinking about it enough because the surrounding parentesis were unnecessary as well. I am now using a explicit `unordered_map` constructor now to ensure the copy happens before the lock scope ends and I have added a comment and removed the parenthesis.
fjahr commented 2026-03-22 13:41:25 +00:00 (Migrated from github.com)

Addressed latest @sedited feedback, thank you!

One thing that is still not clear to me is how this is actually useful to external applications if the locks are not persisted. I could see how this might be interesting for our own wallets, by setting prune locks for wallets even though they are not loaded yet. But for outside users this seems like a limitation that is difficult to deal with. If you want to add a wallet on startup, wouldn't that require racing to set a lock again if you restart? What is the exact scenario here?

First, I think we can agree that it's useful for long-running nodes and the internal wallet, right? So for the following I am still unsure if it should be included here or part of follow-up PR.

For a node that is relaunched only every few weeks for usage of (cold-ish) wallet the persisted lock sounds useful. But I am not sure if just making the locks persistent is actually the best UI for this use-case. The pattern would probably be that the user, before shutdown, sets a prune lock to the last scan height (potentially the tip) and then shuts down. Then on restart the prune lock is respected. But then maybe it would actually be more convenient if there was a startup option -prunelocked or so where the node can be started with the current tip locked. It could get a height parameter too if an earlier height should be locked. I haven't spent much time thinking if this covers all the use-cases but so far I tend to think the startup option covers what I have in mind and comes with a few upsides: If the node crashes and the user couldn't set the lock before, with persistence they might be still screwed but with the startup option they are safe. And since we could avoid persistence this should be much easier to implement and have less code to maintain.

EDIT: Also, for certain wallets that really need a lot of control, there is always the option to manually prune as well.

Addressed latest @sedited feedback, thank you! > One thing that is still not clear to me is how this is actually useful to external applications if the locks are not persisted. I could see how this might be interesting for our own wallets, by setting prune locks for wallets even though they are not loaded yet. But for outside users this seems like a limitation that is difficult to deal with. If you want to add a wallet on startup, wouldn't that require racing to set a lock again if you restart? What is the exact scenario here? First, I think we can agree that it's useful for long-running nodes and the internal wallet, right? So for the following I am still unsure if it should be included here or part of follow-up PR. For a node that is relaunched only every few weeks for usage of (cold-ish) wallet the persisted lock sounds useful. But I am not sure if just making the locks persistent is actually the best UI for this use-case. The pattern would probably be that the user, before shutdown, sets a prune lock to the last scan height (potentially the tip) and then shuts down. Then on restart the prune lock is respected. But then maybe it would actually be more convenient if there was a startup option `-prunelocked` or so where the node can be started with the current tip locked. It could get a height parameter too if an earlier height should be locked. I haven't spent much time thinking if this covers all the use-cases but so far I tend to think the startup option covers what I have in mind and comes with a few upsides: If the node crashes and the user couldn't set the lock before, with persistence they might be still screwed but with the startup option they are safe. And since we could avoid persistence this should be much easier to implement and have less code to maintain. EDIT: Also, for certain wallets that really need a lot of control, there is always the option to manually prune as well.
fjahr (Migrated from github.com) reviewed 2026-03-22 13:41:34 +00:00
fjahr (Migrated from github.com) commented 2026-03-22 13:41:34 +00:00

m_prune_locks is private and the direct access to it in validation.cpp is only possible because ChainState is a friend class of BlockManager. Of course we could also just make it public but a getter method seemed nicer to me.

`m_prune_locks` is private and the direct access to it in `validation.cpp` is only possible because `ChainState` is a friend class of `BlockManager`. Of course we could also just make it public but a getter method seemed nicer to me.
sedited commented 2026-03-25 10:28:47 +00:00 (Migrated from github.com)

First, I think we can agree that it's useful for long-running nodes and the internal wallet, right? So for the following I am still unsure if it should be included here or part of follow-up PR.

The case is still not quite clear to me to be honest. Would the wallet start tracking prune locks for unloaded wallets? Afaict it doesn't currently hold a lock during rescan - that does indeed seem like something useful. I can also see how deleting / cleaning up existing locks could be useful, though it also feels like bit of a footgun. I'm having a hard time thinking of a scenario where you can robustly race a node pruning and important an existing wallet / descriptor. The startup option does seem better to me too.

> First, I think we can agree that it's useful for long-running nodes and the internal wallet, right? So for the following I am still unsure if it should be included here or part of follow-up PR. The case is still not quite clear to me to be honest. Would the wallet start tracking prune locks for unloaded wallets? Afaict it doesn't currently hold a lock during rescan - that does indeed seem like something useful. I can also see how deleting / cleaning up existing locks could be useful, though it also feels like bit of a footgun. I'm having a hard time thinking of a scenario where you can robustly race a node pruning and important an existing wallet / descriptor. The startup option does seem better to me too.
fjahr commented 2026-03-25 13:34:03 +00:00 (Migrated from github.com)

The startup option does seem better to me too.

Thanks, I have added a minimal implementation of the startup option because so far seems like a clearly useful feature to me if outside wallets want to use prune locks in any capacity. But conceptual feedback still very welcome!

Would the wallet start tracking prune locks for unloaded wallets?

I think so? I could see something like this become more common in the future when more users switch on pruning to avoid having to get a larger disk. I would expect there are wallet options that market themselves being compatible with (possibly aggressive) pruning on the node and that this option will become more common in the future. But of course it would be a stronger statement to hear from an implementer directly that they would use such a feature. I just looked at the sparrow docs as a start and they are rather "hands-off" so far and just warn about potential issues: https://sparrowwallet.com/docs/connect-node.html#pruned-nodes

> The startup option does seem better to me too. Thanks, I have added a minimal implementation of the startup option because so far seems like a clearly useful feature to me if outside wallets want to use prune locks in any capacity. But conceptual feedback still very welcome! > Would the wallet start tracking prune locks for unloaded wallets? I think so? I could see something like this become more common in the future when more users switch on pruning to avoid having to get a larger disk. I would expect there are wallet options that market themselves being compatible with (possibly aggressive) pruning on the node and that this option will become more common in the future. But of course it would be a stronger statement to hear from an implementer directly that they would use such a feature. I just looked at the sparrow docs as a start and they are rather "hands-off" so far and just warn about potential issues: https://sparrowwallet.com/docs/connect-node.html#pruned-nodes
fjahr commented 2026-03-31 21:55:53 +00:00 (Migrated from github.com)

Rebased

Rebased
fjahr commented 2026-04-19 15:48:28 +00:00 (Migrated from github.com)

Rebased

Rebased
sedited (Migrated from github.com) approved these changes 2026-04-23 13:37:39 +00:00
sedited (Migrated from github.com) left a comment

ACK 9465be6fd01138246337ec91745d08a65f7bea66

ACK 9465be6fd01138246337ec91745d08a65f7bea66
sedited (Migrated from github.com) commented 2026-04-23 13:03:25 +00:00

Nit: Why not duplicate the description from setprunelock to describe their mechanics? The description here feels a bit incomplete, since it does not just prevent pruning at a single height.

Nit: Why not duplicate the description from setprunelock to describe their mechanics? The description here feels a bit incomplete, since it does not just prevent pruning at a single height.
fjahr (Migrated from github.com) reviewed 2026-04-23 14:36:02 +00:00
fjahr (Migrated from github.com) commented 2026-04-23 14:36:02 +00:00

Fair, I will address it the next time I touch this.

Fair, I will address it the next time I touch this.
rkrux (Migrated from github.com) reviewed 2026-05-25 14:59:14 +00:00
rkrux (Migrated from github.com) left a comment

Thanks for the review ping, I was not aware of this PR.

I started looking and it looks interesting - also helpful if it can be leveraged by the wallet (or by external ones).

Thanks for the review ping, I was not aware of this PR. I started looking and it looks interesting - also helpful if it can be leveraged by the wallet (or by external ones).
rkrux (Migrated from github.com) commented 2026-05-25 14:56:22 +00:00

Can't there be a case wherein this new argument causes the -prune= option to not be respected? If this has already been discussed before, please point me to that conversation.

Can't there be a case wherein this new argument causes the -prune=<n> option to not be respected? If this has already been discussed before, please point me to that conversation.
fjahr (Migrated from github.com) reviewed 2026-05-25 19:53:09 +00:00
fjahr (Migrated from github.com) commented 2026-05-25 19:53:09 +00:00

Yes, unless I am misunderstanding your question that is the whole idea behind prune locks, to temporarily protect certain block ranges from pruning if there is some work left to do. I added that functionality here a few years ago with the initial goal to let indexes catch up without the necessary blocks disappearing suddenly if they are running on a pruned node. The basic discussion can be found there. This PR only allows to configure these prune locks via RPC which may be interesting for external wallets for example. Does that answer your question?

Yes, unless I am misunderstanding your question that is the whole idea behind prune locks, to temporarily protect certain block ranges from pruning if there is some work left to do. I added that functionality [here](https://github.com/bitcoin/bitcoin/pull/21726) a few years ago with the initial goal to let indexes catch up without the necessary blocks disappearing suddenly if they are running on a pruned node. The basic discussion can be found there. This PR only allows to configure these prune locks via RPC which may be interesting for external wallets for example. Does that answer your question?
rkrux (Migrated from github.com) reviewed 2026-06-08 13:55:32 +00:00
rkrux (Migrated from github.com) commented 2026-06-08 13:55:32 +00:00

Sorry for the delay in getting back here, I should be able to get to it this week.

Sorry for the delay in getting back here, I should be able to get to it this week.
rkrux (Migrated from github.com) reviewed 2026-06-12 11:48:50 +00:00
rkrux (Migrated from github.com) left a comment

Overall, I feel this PR needs to market itself a bit. Either in the release notes or maybe a new section in the docs/ directory. I worry that this feature would be underutilised or, worse, unused because of lack of awareness. The motivation in the PR description and the use cases discussed in the PR comments previously can be listed there.

From https://github.com/bitcoin/bitcoin/pull/34534#issuecomment-4092098083:

The fundamental issue is that if the wallet is not loaded while the node is running, blocks necessary for rescanning when the wallet is loaded may be pruned away. The wallet definitely should be using prune locks, but it's also necessary to have a way to for users to remove prune locks that are no longer relevant, e.g. if they unload a wallet that they don't plan to load on that node again.

Also, a functional test for wallet exercising prune locks that solves the above scenario would be nice. Using prune locks temporarily instead of manually importing via importprunedfunds RPC (which requires the the proof via another RPC - gettxoutproof) seems like a more natural choice to me (can be a part of the motivation/marketing).

Overall, I feel this PR needs to market itself a bit. Either in the release notes or maybe a new section in the docs/ directory. I worry that this feature would be underutilised or, worse, unused because of lack of awareness. The motivation in the PR description and the use cases discussed in the PR comments previously can be listed there. From https://github.com/bitcoin/bitcoin/pull/34534#issuecomment-4092098083: > The fundamental issue is that if the wallet is not loaded while the node is running, blocks necessary for rescanning when the wallet is loaded may be pruned away. The wallet definitely should be using prune locks, but it's also necessary to have a way to for users to remove prune locks that are no longer relevant, e.g. if they unload a wallet that they don't plan to load on that node again. Also, a functional test for wallet exercising prune locks that solves the above scenario would be nice. Using prune locks temporarily instead of manually importing via importprunedfunds RPC (which requires the the proof via another RPC - gettxoutproof) seems like a more natural choice to me (can be a part of the motivation/marketing).
rkrux (Migrated from github.com) commented 2026-06-12 11:41:31 +00:00

Ok, can add a sentence here that this option will cause the -prune option to not be respected - just to be explicit.

Ok, can add a sentence here that this option will cause the -prune option to not be respected - just to be explicit.
rkrux (Migrated from github.com) commented 2026-06-12 11:37:41 +00:00

The changes in this file require a release note because of new RPCs being added.
Along with the new startup option.

The changes in this file require a release note because of new RPCs being added. Along with the new startup option.
rkrux (Migrated from github.com) commented 2026-06-12 11:40:24 +00:00

There isn't a check that forbids locking blocks that'd be generated in the future. Is it intentional?
The following test change passes:

-        node.setprunelock("testlock", "add", 100)
+        node.setprunelock("testlock", "add", 1600)
         assert_array_result(node.listprunelocks()["prune_locks"],
                             {"id": "rpc:testlock"},
-                            {"height": 100})
+                            {"height": 1600})
There isn't a check that forbids locking blocks that'd be generated in the future. Is it intentional? The following test change passes: ```diff - node.setprunelock("testlock", "add", 100) + node.setprunelock("testlock", "add", 1600) assert_array_result(node.listprunelocks()["prune_locks"], {"id": "rpc:testlock"}, - {"height": 100}) + {"height": 1600}) ```
rkrux (Migrated from github.com) commented 2026-06-12 11:31:42 +00:00

Is this correct? The following diff passes where this value is halved along with the corresponding heights in the pruneblockchain RPCs.

diff --git a/test/functional/rpc_prunelock.py b/test/functional/rpc_prunelock.py
index a122a6164a..9a2dd5b6c2 100755
--- a/test/functional/rpc_prunelock.py
+++ b/test/functional/rpc_prunelock.py
@@ -17,7 +17,7 @@ from test_framework.util import (
 
 # Blocks needed to generate at minimum to allow pruning to work on
 # regtest (with fastprune).
-INITIAL_BLOCKS = 1500
+INITIAL_BLOCKS = 750
 
 
 class PruneLockTest(BitcoinTestFramework):
@@ -92,7 +92,7 @@ class PruneLockTest(BitcoinTestFramework):
                             {"id": "rpc:protect_early"},
                             {"height": 2})
 
-        prune_height_locked = node.pruneblockchain(500)
+        prune_height_locked = node.pruneblockchain(250)
         assert self.has_block_file(node, 0), "blk00000.dat should still exist with prune lock active"
 
         self.log.info("Test that removing the prune lock allows further pruning")
@@ -101,7 +101,7 @@ class PruneLockTest(BitcoinTestFramework):
                             {"id": "rpc:protect_early"}, {},
                             should_not_find=True)
 
-        prune_height_unlocked = node.pruneblockchain(500)
+        prune_height_unlocked = node.pruneblockchain(250)
         assert_greater_than(prune_height_unlocked, prune_height_locked)
         assert not self.has_block_file(node, 0), "blk00000.dat should be pruned after lock removal"
 
@@ -142,15 +142,15 @@ class PruneLockTest(BitcoinTestFramework):
         node = self.nodes[0]
 
         self.log.info("Test -prunelockheight with explicit height")
-        self.restart_node(0, extra_args=["-prune=1", "-prunelockheight=500"])
+        self.restart_node(0, extra_args=["-prune=1", "-prunelockheight=250"])
         assert_array_result(node.listprunelocks()["prune_locks"],
                             {"id": "init"},
-                            {"height": 500})
+                            {"height": 250})
 
         self.log.info("Test -prunelockheight can coexist with RPC locks")
         node.setprunelock("mylock", "add", 100)
         locks = node.listprunelocks()["prune_locks"]
-        assert_array_result(locks, {"id": "init"}, {"height": 500})
+        assert_array_result(locks, {"id": "init"}, {"height": 250})
         assert_array_result(locks, {"id": "rpc:mylock"}, {"height": 100})
         node.setprunelock("mylock", "remove")
 

Based it off of this conditional:
github.com/bitcoin/bitcoin@142e86a65c/src/kernel/chainparams.cpp (L603)

Is this correct? The following diff passes where this value is halved along with the corresponding heights in the pruneblockchain RPCs. ```diff diff --git a/test/functional/rpc_prunelock.py b/test/functional/rpc_prunelock.py index a122a6164a..9a2dd5b6c2 100755 --- a/test/functional/rpc_prunelock.py +++ b/test/functional/rpc_prunelock.py @@ -17,7 +17,7 @@ from test_framework.util import ( # Blocks needed to generate at minimum to allow pruning to work on # regtest (with fastprune). -INITIAL_BLOCKS = 1500 +INITIAL_BLOCKS = 750 class PruneLockTest(BitcoinTestFramework): @@ -92,7 +92,7 @@ class PruneLockTest(BitcoinTestFramework): {"id": "rpc:protect_early"}, {"height": 2}) - prune_height_locked = node.pruneblockchain(500) + prune_height_locked = node.pruneblockchain(250) assert self.has_block_file(node, 0), "blk00000.dat should still exist with prune lock active" self.log.info("Test that removing the prune lock allows further pruning") @@ -101,7 +101,7 @@ class PruneLockTest(BitcoinTestFramework): {"id": "rpc:protect_early"}, {}, should_not_find=True) - prune_height_unlocked = node.pruneblockchain(500) + prune_height_unlocked = node.pruneblockchain(250) assert_greater_than(prune_height_unlocked, prune_height_locked) assert not self.has_block_file(node, 0), "blk00000.dat should be pruned after lock removal" @@ -142,15 +142,15 @@ class PruneLockTest(BitcoinTestFramework): node = self.nodes[0] self.log.info("Test -prunelockheight with explicit height") - self.restart_node(0, extra_args=["-prune=1", "-prunelockheight=500"]) + self.restart_node(0, extra_args=["-prune=1", "-prunelockheight=250"]) assert_array_result(node.listprunelocks()["prune_locks"], {"id": "init"}, - {"height": 500}) + {"height": 250}) self.log.info("Test -prunelockheight can coexist with RPC locks") node.setprunelock("mylock", "add", 100) locks = node.listprunelocks()["prune_locks"] - assert_array_result(locks, {"id": "init"}, {"height": 500}) + assert_array_result(locks, {"id": "init"}, {"height": 250}) assert_array_result(locks, {"id": "rpc:mylock"}, {"height": 100}) node.setprunelock("mylock", "remove") ``` Based it off of this conditional: https://github.com/bitcoin/bitcoin/blob/142e86a65c8a809911e37ead5ece63ad7854077e/src/kernel/chainparams.cpp#L603
rkrux (Migrated from github.com) commented 2026-06-12 11:36:08 +00:00

prune_height_locked is -1 because the lock prevents the pruning from happening.
I don't see much point in asserting that prune_height_unlocked is greater than prune_height_locked.

prune_height_locked is -1 because the lock prevents the pruning from happening. I don't see much point in asserting that prune_height_unlocked is greater than prune_height_locked.
fjahr (Migrated from github.com) reviewed 2026-07-15 21:48:31 +00:00
fjahr (Migrated from github.com) commented 2026-07-15 21:48:31 +00:00

Good idea, done!

Good idea, done!
fjahr (Migrated from github.com) reviewed 2026-07-15 21:49:08 +00:00
fjahr (Migrated from github.com) commented 2026-07-15 21:49:08 +00:00

Hm, for me the proposed diff fails because the blocks are not enough to get actual pruning. I am not sure why it would pass for you and not for me, it's possible that there is some dependency on filesystems but I usually try to tweak these values to the minimum that works reliably locally.

Hm, for me the proposed diff fails because the blocks are not enough to get actual pruning. I am not sure why it would pass for you and not for me, it's possible that there is some dependency on filesystems but I usually try to tweak these values to the minimum that works reliably locally.
fjahr (Migrated from github.com) reviewed 2026-07-15 21:49:13 +00:00
fjahr (Migrated from github.com) commented 2026-07-15 21:49:13 +00:00

Changed this to check for -1 and >0 more explicitly now.

Changed this to check for -1 and >0 more explicitly now.
fjahr (Migrated from github.com) reviewed 2026-07-15 21:49:22 +00:00
fjahr (Migrated from github.com) commented 2026-07-15 21:49:22 +00:00

Right, added the necessary release notes!

Right, added the necessary release notes!
fjahr (Migrated from github.com) reviewed 2026-07-15 21:49:28 +00:00
fjahr (Migrated from github.com) commented 2026-07-15 21:49:28 +00:00

Right, that's a good observation, I didn't think about this explicitly so far but I don't see harm in allowing this and it may even be useful for some use-cases. So I tend to say I would keep allowing it and I have added a note in the docs on it. But if you (or other reviewers) strongly prefer these values to be rejected I can be convinced to do that as well.

Right, that's a good observation, I didn't think about this explicitly so far but I don't see harm in allowing this and it may even be useful for some use-cases. So I tend to say I would keep allowing it and I have added a note in the docs on it. But if you (or other reviewers) strongly prefer these values to be rejected I can be convinced to do that as well.
fjahr commented 2026-07-15 21:52:26 +00:00 (Migrated from github.com)

Addressed feedback by @rkrux , thanks a lot!

I feel this PR needs to market itself a bit

I added release notes per your inline comment

a functional test for wallet exercising prune locks that solves the above scenario would be nice

I added this in a separate commit. I folded it into the existing test file since generating all those blocks is pretty expensive and I didn't see this justifying a new functional test file. Let me know what you think.

Addressed feedback by @rkrux , thanks a lot! > I feel this PR needs to market itself a bit I added release notes per your inline comment > a functional test for wallet exercising prune locks that solves the above scenario would be nice I added this in a separate commit. I folded it into the existing test file since generating all those blocks is pretty expensive and I didn't see this justifying a new functional test file. Let me know what you think.
sedited (Migrated from github.com) approved these changes 2026-07-23 21:31:03 +00:00
sedited (Migrated from github.com) left a comment

Re-ACK 9fa4953f01cab3ab8669b316224b4eeb8433a8d8

Re-ACK 9fa4953f01cab3ab8669b316224b4eeb8433a8d8
sedited (Migrated from github.com) commented 2026-07-23 21:30:12 +00:00

Maybe mention here that the lock may be viewed by listprunelockand removed by setprunelock.

Maybe mention here that the lock may be viewed by `listprunelock`and removed by `setprunelock`.
fjahr (Migrated from github.com) reviewed 2026-07-28 22:04:21 +00:00
fjahr (Migrated from github.com) commented 2026-07-28 22:04:21 +00:00

Well, now that you say it.... the setprunelock removal part wasn't actually possible because of the forced rpc: prefix which the init option didn't use. I changed this and renamed the prefix to user: since it's not strictly used by RPCs anymore. Otherwise, removing the lock would have required a restart.

Well, now that you say it.... the `setprunelock` removal part wasn't actually possible because of the forced `rpc:` prefix which the init option didn't use. I changed this and renamed the prefix to `user:` since it's not strictly used by RPCs anymore. Otherwise, removing the lock would have required a restart.
andrewtoth commented 2026-09-09 17:02:26 +00:00 (Migrated from github.com)

I'm not sure I find the motivation compelling. Would the same functionality be achieved via the pruneblockchain RPC and a -prune=1 setting? This way a user can have all their wallets sync, and then call pruneblockchain. Keeping the node running would not prune on its own.

I'm not sure I find the motivation compelling. Would the same functionality be achieved via the `pruneblockchain` RPC and a `-prune=1` setting? This way a user can have all their wallets sync, and then call `pruneblockchain`. Keeping the node running would not prune on its own.
fjahr commented 2026-09-13 11:30:54 +00:00 (Migrated from github.com)

Would the same functionality be achieved via the pruneblockchain RPC and a -prune=1 setting?

Partly yes, but I think it would be much more complex and difficult to implement managing that behavior on the side of an external wallet. With -prune=1 nothing is pruned unless pruneblockchain is called and to achieve some kind of constant disk usage threshold it needs to be called regularly. In order to decide when to prune the wallet would need to monitor disk usage constantly and then decide with which height to call pruneblockchain. It basically means the wallet has the burden to reimplement our automatic pruning logic, just to get that extra bit of control that the pull here provides. It gets even trickier if there are multiple consumers of RPC that need that pruning control, like a wallet and an external index, with management RPCs they both can set their locks as they need and it should be simple to implement that.

> Would the same functionality be achieved via the `pruneblockchain` RPC and a `-prune=1` setting? Partly yes, but I think it would be much more complex and difficult to implement managing that behavior on the side of an external wallet. With `-prune=1` nothing is pruned unless `pruneblockchain` is called and to achieve some kind of constant disk usage threshold it needs to be called regularly. In order to decide when to prune the wallet would need to monitor disk usage constantly and then decide with which height to call `pruneblockchain`. It basically means the wallet has the burden to reimplement our automatic pruning logic, just to get that extra bit of control that the pull here provides. It gets even trickier if there are multiple consumers of RPC that need that pruning control, like a wallet and an external index, with management RPCs they both can set their locks as they need and it should be simple to implement that.
First-time contributor

3 additional verified findings in the Review report.

Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.

🟠 Minor · -prunelock is installed after IPC submissions can trigger pruning

With -ipcbind and automatic pruning, an early trusted mining submission can prune protected data before startup locks are installed; no current client exercising this window or resulting incident was identified.

Location: src/init.cpp:2076 (head).

🟠 Minor · Future-height user locks collapse after an unrelated tip disconnect

Although the help documents rollback, a future-height reservation becomes a fixed near-tip lock after one disconnect and can make automatic pruning exceed its target until the lock is updated or removed.

Location: src/rpc/blockchain.cpp:1003 (head).

🔵 Suggestion · setprunelock help omits how to retain protection across restarts

RPC locks disappear before startup pruning, so restoring them after reconnecting can be too late; the RPC help does not direct users to configure -prunelock beforehand.

Location: src/rpc/blockchain.cpp:1001 (head).

🔵 Suggestion · The startup-lock test checks log order without exercising destructive pruning

The startup check cannot distinguish effective protection from correctly logged registration, because its -prune=1 restart cannot delete files.

Location: test/functional/rpc_prunelock.py:235 (head).

🔵 Suggestion · User-lock reorg rollback has no regression assertion

The help promises that user locks move down to the fork height and stay there, but the tests never disconnect a block while a user lock is present.

Location: test/functional/rpc_prunelock.py:138 (head).

🔵 Suggestion · -debug=prune messages omit the lock namespace

When internal and user locks share an id, pruning diagnostics identify only the name, obscuring which lock limited pruning or moved during a reorg.

Location: src/validation.cpp:2804 (head).

Review coverage was incomplete.


Reviewed: dd6c67b0e5f6 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:312ddb2095238aa2ca395a9cc39b05ab5a38a7177afbdb0d69098df9c3e0bef9:9a7f127b15c6116c267953d38288a9f17ec7c7bcd3a975fb71a2db8bfd974dbf --> <!-- ralph-base:afedb19cca4c3b40d89514fb0c66b0c20e4f5d62 --> <!-- ralph-head:dd6c67b0e5f6ffe184d101281eb107fcf32a26f6 --> <!-- ralph-case:4f343924277574a5d3829063b71242707016cfc18c39db6faf67902896dc1ba7 --> [3 additional verified findings in the Review report](<https://review.fish.foo/report/34534-dd6c67b0e5f6ffe184d101281eb107fcf32a26f6-3311-10.html>). Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments. **🟠 Minor · `-prunelock` is installed after IPC submissions can trigger pruning** With `-ipcbind` and automatic pruning, an early trusted mining submission can prune protected data before startup locks are installed; no current client exercising this window or resulting incident was identified. Location: [`src/init.cpp:2076`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/dd6c67b0e5f6ffe184d101281eb107fcf32a26f6/src/init.cpp#L2076>) (head). **🟠 Minor · Future-height user locks collapse after an unrelated tip disconnect** Although the help documents rollback, a future-height reservation becomes a fixed near-tip lock after one disconnect and can make automatic pruning exceed its target until the lock is updated or removed. Location: [`src/rpc/blockchain.cpp:1003`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/dd6c67b0e5f6ffe184d101281eb107fcf32a26f6/src/rpc/blockchain.cpp#L1003>) (head). **🔵 Suggestion · `setprunelock` help omits how to retain protection across restarts** RPC locks disappear before startup pruning, so restoring them after reconnecting can be too late; the RPC help does not direct users to configure `-prunelock` beforehand. Location: [`src/rpc/blockchain.cpp:1001`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/dd6c67b0e5f6ffe184d101281eb107fcf32a26f6/src/rpc/blockchain.cpp#L1001>) (head). **🔵 Suggestion · The startup-lock test checks log order without exercising destructive pruning** The startup check cannot distinguish effective protection from correctly logged registration, because its `-prune=1` restart cannot delete files. Location: [`test/functional/rpc_prunelock.py:235`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/dd6c67b0e5f6ffe184d101281eb107fcf32a26f6/test/functional/rpc_prunelock.py#L235>) (head). **🔵 Suggestion · User-lock reorg rollback has no regression assertion** The help promises that user locks move down to the fork height and stay there, but the tests never disconnect a block while a user lock is present. Location: [`test/functional/rpc_prunelock.py:138`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/dd6c67b0e5f6ffe184d101281eb107fcf32a26f6/test/functional/rpc_prunelock.py#L138>) (head). **🔵 Suggestion · `-debug=prune` messages omit the lock namespace** When internal and user locks share an id, pruning diagnostics identify only the name, obscuring which lock limited pruning or moved during a reorg. Location: [`src/validation.cpp:2804`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/dd6c67b0e5f6ffe184d101281eb107fcf32a26f6/src/validation.cpp#L2804>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`dd6c67b0e5f6`](<https://git.fish.foo/bitcoin/bitcoin/commit/dd6c67b0e5f6ffe184d101281eb107fcf32a26f6>) · [Review report](<https://review.fish.foo/report/34534-dd6c67b0e5f6ffe184d101281eb107fcf32a26f6-3311-10.html>) · [Previous reports](<https://review.fish.foo/traces/34534/>) [permalink](<https://review.fish.foo/report/34534-dd6c67b0e5f6ffe184d101281eb107fcf32a26f6-3311-10.html>)
sedited (Migrated from github.com) approved these changes 2026-10-07 10:37:29 +00:00
sedited (Migrated from github.com) left a comment

Re-ACK 67960317cd

Re-ACK 67960317cda01b102c5cbec7efb8ab56611b21a7
shuv-amp commented 2026-10-30 14:09:38 +00:00 (Migrated from github.com)

The lock is registered here, but the startup prune at L1962–1972 already ran in Step 10. So restarting with -prune=550 -prunelockheight=H while block-file usage is above the target can delete the blocks the lock should keep, then sets the lock. Index prune locks don't have this problem because they're registered in Step 8, before the prune.

The test diff below shows it on 67960317cd: it runs with -prune=550 -fastprune -prunelockheight=<tip+1> until usage is over the target, then restarts with the same args. The getblock on the locked height fails with Block not available (pruned data), and debug.log shows removed 63 blk/rev pairs before Initial prune lock set at height 2101. Moving the registration before Step 10 (after chainstate load so the tip is available for =0) makes the test pass for me.

The PR's own rpc_prunelock.py doesn't catch this because it uses -prune=1, which never prunes automatically.

diff
diff --git a/test/functional/rpc_prunelock.py b/test/functional/rpc_prunelock.py
index a6e8b7f..fc23fec 100755
--- a/test/functional/rpc_prunelock.py
+++ b/test/functional/rpc_prunelock.py
@@ -6,6 +6,8 @@
 
 import os
 
+from test_framework.blocktools import create_block, create_coinbase
+from test_framework.script import CScript, OP_NOP, OP_RETURN
 from test_framework.test_framework import BitcoinTestFramework
 from test_framework.util import (
     assert_array_result,
@@ -213,6 +215,20 @@ class PruneLockTest(BitcoinTestFramework):
                             {"id": "user:init"}, {},
                             should_not_find=True)
 
+        self.log.info("Test the startup prune respects -prunelockheight")
+        lock_args = ["-prune=550", "-fastprune", f"-prunelockheight={tip_height + 1}"]
+        self.restart_node(0, extra_args=lock_args)
+        big_script = CScript([OP_RETURN] + [OP_NOP] * 950000)
+        best = node.getblockheader(node.getbestblockhash())
+        prev = int(best["hash"], 16)
+        for i in range(1, 651):
+            block = create_block(prev, create_coinbase(tip_height + i, script_pubkey=big_script), ntime=best["time"] + i)
+            block.solve()
+            node.submitblock(block.serialize().hex())
+            prev = block.hash_int
+        self.restart_node(0, extra_args=lock_args)
+        node.getblock(node.getblockhash(tip_height + 1))
+
         self.log.info("Test -prunelockheight without -prune fails")
         self.stop_node(0)
         node.assert_start_raises_init_error(
The lock is registered here, but the startup prune at [L1962–1972](https://github.com/bitcoin/bitcoin/blob/67960317cda01b102c5cbec7efb8ab56611b21a7/src/init.cpp#L1962-L1972) already ran in Step 10. So restarting with `-prune=550 -prunelockheight=H` while block-file usage is above the target can delete the blocks the lock should keep, then sets the lock. Index prune locks don't have this problem because they're registered in Step 8, before the prune. The test diff below shows it on 67960317cd: it runs with `-prune=550 -fastprune -prunelockheight=<tip+1>` until usage is over the target, then restarts with the same args. The `getblock` on the locked height fails with `Block not available (pruned data)`, and debug.log shows `removed 63 blk/rev pairs` before `Initial prune lock set at height 2101`. Moving the registration before Step 10 (after chainstate load so the tip is available for `=0`) makes the test pass for me. The PR's own `rpc_prunelock.py` doesn't catch this because it uses `-prune=1`, which never prunes automatically. <details><summary>diff</summary> ```diff diff --git a/test/functional/rpc_prunelock.py b/test/functional/rpc_prunelock.py index a6e8b7f..fc23fec 100755 --- a/test/functional/rpc_prunelock.py +++ b/test/functional/rpc_prunelock.py @@ -6,6 +6,8 @@ import os +from test_framework.blocktools import create_block, create_coinbase +from test_framework.script import CScript, OP_NOP, OP_RETURN from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_array_result, @@ -213,6 +215,20 @@ class PruneLockTest(BitcoinTestFramework): {"id": "user:init"}, {}, should_not_find=True) + self.log.info("Test the startup prune respects -prunelockheight") + lock_args = ["-prune=550", "-fastprune", f"-prunelockheight={tip_height + 1}"] + self.restart_node(0, extra_args=lock_args) + big_script = CScript([OP_RETURN] + [OP_NOP] * 950000) + best = node.getblockheader(node.getbestblockhash()) + prev = int(best["hash"], 16) + for i in range(1, 651): + block = create_block(prev, create_coinbase(tip_height + i, script_pubkey=big_script), ntime=best["time"] + i) + block.solve() + node.submitblock(block.serialize().hex()) + prev = block.hash_int + self.restart_node(0, extra_args=lock_args) + node.getblock(node.getblockhash(tip_height + 1)) + self.log.info("Test -prunelockheight without -prune fails") self.stop_node(0) node.assert_start_raises_init_error( ``` </details>
DrahtBot commented 2026-10-31 22:52:19 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/41248829209/job/124677834304
LLM reason (✨ experimental): CI failed because IWYU reordered/added includes in src/rpc/blockchain.cpp, leaving a diff that the lint check rejects.

Hints

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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `iwyu`: https://github.com/bitcoin/bitcoin/actions/runs/41248829209/job/124677834304</sub> <sub>LLM reason (✨ experimental): CI failed because IWYU reordered/added includes in `src/rpc/blockchain.cpp`, leaving a diff that the lint check rejects.</sub> <details><summary>Hints</summary> 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. </details>
fjahr commented 2026-10-31 23:11:43 +00:00 (Migrated from github.com)

The lock is registered here, but the startup prune at L1962–1972 already ran in Step 10.

Good catch, thanks. Moved the lock add before the initial prune and added a minimal test that checks the debug log for it. Your suggested test seemed a bit too heavy for my taste.

Also rebased and fixed a silent merge conflict.

> The lock is registered here, but the startup prune at [L1962–1972](https://github.com/bitcoin/bitcoin/blob/67960317cda01b102c5cbec7efb8ab56611b21a7/src/init.cpp#L1962-L1972) already ran in Step 10. Good catch, thanks. Moved the lock add before the initial prune and added a minimal test that checks the debug log for it. Your suggested test seemed a bit too heavy for my taste. Also rebased and fixed a silent merge conflict.
andrewtoth (Migrated from github.com) reviewed 2026-11-01 21:16:21 +00:00
andrewtoth (Migrated from github.com) left a comment

Concept ACK

Concept ACK
andrewtoth (Migrated from github.com) commented 2026-11-01 21:15:02 +00:00

The release notes also don't mention the 0 case meaning tip here.

The release notes also don't mention the 0 case meaning tip here.
andrewtoth (Migrated from github.com) commented 2026-11-01 21:13:35 +00:00

Hmm I think setting an explicit 0 value should set to genesis block, same as the RPC. What is the rationale for making 0 use tip? Using no-value to mean tip could make sense though.

Hmm I think setting an explicit 0 value should set to genesis block, same as the RPC. What is the rationale for making 0 use tip? Using no-value to mean tip could make sense though.
andrewtoth (Migrated from github.com) commented 2026-11-01 21:07:10 +00:00

This comment doesn't seem to add any value here IMO.

This comment doesn't seem to add any value here IMO.
andrewtoth (Migrated from github.com) commented 2026-11-01 21:07:49 +00:00

Same with this comment. It doesn't seem necessary.

Same with this comment. It doesn't seem necessary.
andrewtoth (Migrated from github.com) commented 2026-11-01 20:52:51 +00:00

nit

        "Prune locks prevent block data at or above specific heights from being pruned.\n"
nit ```suggestion "Prune locks prevent block data at or above specific heights from being pruned.\n" ```
andrewtoth (Migrated from github.com) commented 2026-11-01 20:55:59 +00:00

I think this second note can be struck. It describes an implementation detail that should have no effect on usage. To me it seems it doesn't give the user any useful information.

I think this second note can be struck. It describes an implementation detail that should have no effect on usage. To me it seems it doesn't give the user any useful information.
andrewtoth (Migrated from github.com) commented 2026-11-01 21:05:00 +00:00

The test framework has has_blockfile which could be used instead of this.

The test framework has `has_blockfile` which could be used instead of this.
optout21 commented 2026-11-03 23:57:05 +00:00 (Migrated from github.com)

Concept ACK.

A concrete use case for this: an Electrum server (electrs) backed by a pruned node. External indexers have only come up in passing here, so here is a data point.

I prototyped a patched electrs on top of this PR and completed indexing on a pruned signet node (-prune=550). It uses the prune lock like this:

  • on connect, set a lock (user:electrs) at the first block not indexed yet, then remove the init lock
  • advance the lock after each index sync
  • across bitcoind restarts, -prunelockheight=<n> (at or below the indexed height) keeps the needed blocks until electrs reconnects and sets its lock again

For this use case the startup option is enough, and locks don't need to be persisted. It does rely on the lock being in place before the startup prune (thanks for the fix in response to https://github.com/bitcoin/bitcoin/pull/34534#issuecomment-6524831809). The setup is resilient for either of the processes being stopped for a period.

Out of scope here, but for context: there is a constraint in how the two processes are started (electrs has to be started before bitcoind starts to prune); relaxing this may require some new mechanism. Also, serving transactions from blocks that were pruned before indexing will need more work (an indexer-side concern).

I'll review the code later.

Concept ACK. A concrete use case for this: an Electrum server (`electrs`) backed by a pruned node. External indexers have only come up in passing here, so here is a data point. I prototyped a patched `electrs` on top of this PR and completed indexing on a pruned signet node (`-prune=550`). It uses the prune lock like this: - on connect, set a lock (`user:electrs`) at the first block not indexed yet, then remove the `init` lock - advance the lock after each index sync - across bitcoind restarts, `-prunelockheight=<n>` (at or below the indexed height) keeps the needed blocks until electrs reconnects and sets its lock again For this use case the startup option is enough, and locks don't need to be persisted. It does rely on the lock being in place before the startup prune (thanks for the fix in response to https://github.com/bitcoin/bitcoin/pull/34534#issuecomment-6524831809). The setup is resilient for either of the processes being stopped for a period. Out of scope here, but for context: there is a constraint in how the two processes are started (electrs has to be started before bitcoind starts to prune); relaxing this may require some new mechanism. Also, serving transactions from blocks that were pruned before indexing will need more work (an indexer-side concern). I'll review the code later.
optout21 commented 2026-11-08 05:18:55 +00:00 (Migrated from github.com)

Concept ACK. Manual prune locks are needed to support external indexers on a pruned node. Some comments with that use case in mind:

  1. Initial lock. Its purpose is to keep the blocks an external app needs from being pruned while the app is not connected (not started yet, restarting, etc.). Rather than a general init lock, it could be the initial placement of the app's own lock, which the app then takes over. A height isn't needed either: the app controls the height, and until it connects the safest default is "don't prune anything".
    Suggestion: replace -prunelockheight=<n> with -prunelock=<id>, where <id> is the same id the app uses with setprunelock. When the app connects, its setprunelock call simply updates this lock, with no separate init lock to clean up. This way the app can safely remove or update "its" lock (with the same id).
    The option goes in the node's config file and stays there as long as the app is in use. It could be extended later with [:<height>], though I don't see a strict need for that now. The trade-off is that no pruning happens until the app connects, so disk usage can go above the -prune target for that time. (I briefly discussed this idea with the author offline.)

  2. Persistence. I agree that locks don't need to be persisted. If bitcoind starts with -prunelock=<id> and the app soon connects and calls setprunelock, the setup is robust. The app knows its own persisted state, and the node only has to make sure nothing is pruned while the app is away. Persisting locks would allow more fine-tuned pruning in some cases. For example: the indexer has reached height 800, blocks are pruned up to 600, and the node restarts before the indexer does. Without a persisted lock, the node cannot prune until the indexer reconnects, so disk usage grows. With one, it could prune roughly 600–799. But persistence adds complexity, such as having to remove stale persisted locks by hand.

  3. 0 meaning "tip" in -prunelockheight. In this PR, -prunelockheight=0 locks at the startup tip, while setprunelock <id> add 0 (also the default when height is omitted) locks at height 0, which means nothing is pruned. The same value has opposite meanings, and code that treats the lowest height as "keep everything" would use it the wrong way.
    The natural values are 0 for "keep everything" and "infinity" for "no lock" (height_first already defaults to max int). I'd use a separate value for "lock at the current tip", such as -1 or a named value like "tip". -1 has its own pitfalls (a negative number where heights are otherwise non-negative, and the current check that rejects negative heights). So I'd suggest named value "tip" in the RPC commands and max int in the internal representation for tip.

Concept ACK. Manual prune locks are needed to support external indexers on a pruned node. Some comments with that use case in mind: 1. Initial lock. Its purpose is to keep the blocks an external app needs from being pruned while the app is not connected (not started yet, restarting, etc.). Rather than a general init lock, it could be the initial placement of the app's own lock, which the app then takes over. A height isn't needed either: the app controls the height, and until it connects the safest default is "don't prune anything". Suggestion: replace `-prunelockheight=<n>` with `-prunelock=<id>`, where `<id>` is the same id the app uses with `setprunelock`. When the app connects, its `setprunelock` call simply updates this lock, with no separate init lock to clean up. This way the app can safely remove or update "its" lock (with the same id). The option goes in the node's config file and stays there as long as the app is in use. It could be extended later with `[:<height>]`, though I don't see a strict need for that now. The trade-off is that no pruning happens until the app connects, so disk usage can go above the -prune target for that time. (I briefly discussed this idea with the author offline.) 2. Persistence. I agree that locks don't need to be persisted. If bitcoind starts with `-prunelock=<id`> and the app soon connects and calls `setprunelock`, the setup is robust. The app knows its own persisted state, and the node only has to make sure nothing is pruned while the app is away. Persisting locks would allow more fine-tuned pruning in some cases. For example: the indexer has reached height 800, blocks are pruned up to 600, and the node restarts before the indexer does. Without a persisted lock, the node cannot prune until the indexer reconnects, so disk usage grows. With one, it could prune roughly 600–799. But persistence adds complexity, such as having to remove stale persisted locks by hand. 3. 0 meaning "tip" in `-prunelockheight`. In this PR, `-prunelockheight=0` locks at the startup tip, while `setprunelock <id> add 0` (also the default when height is omitted) locks at height 0, which means nothing is pruned. The same value has opposite meanings, and code that treats the lowest height as "keep everything" would use it the wrong way. The natural values are 0 for "keep everything" and "infinity" for "no lock" (`height_first already` defaults to max int). I'd use a separate value for "lock at the current tip", such as -1 or a named value like "tip". -1 has its own pitfalls (a negative number where heights are otherwise non-negative, and the current check that rejects negative heights). So I'd suggest named value "tip" in the RPC commands and max int in the internal representation for tip.
fjahr (Migrated from github.com) reviewed 2026-11-14 14:16:17 +00:00
fjahr (Migrated from github.com) commented 2026-11-14 14:16:17 +00:00

Addressed now in the latest push since @andrewtoth had a similar comment

Addressed now in the latest push since @andrewtoth had a similar comment
fjahr (Migrated from github.com) reviewed 2026-11-14 14:17:06 +00:00
fjahr (Migrated from github.com) commented 2026-11-14 14:17:06 +00:00

Done

Done
fjahr (Migrated from github.com) reviewed 2026-11-14 14:17:17 +00:00
fjahr (Migrated from github.com) commented 2026-11-14 14:17:17 +00:00

Removed

Removed
fjahr (Migrated from github.com) reviewed 2026-11-14 14:18:03 +00:00
fjahr (Migrated from github.com) commented 2026-11-14 14:18:03 +00:00

Ah, nice, replaced the helper with has_blockfile.

Ah, nice, replaced the helper with `has_blockfile`.
fjahr (Migrated from github.com) reviewed 2026-11-14 14:18:25 +00:00
fjahr (Migrated from github.com) commented 2026-11-14 14:18:25 +00:00

Removed

Removed
fjahr (Migrated from github.com) reviewed 2026-11-14 14:18:34 +00:00
fjahr (Migrated from github.com) commented 2026-11-14 14:18:34 +00:00

Removed

Removed
fjahr (Migrated from github.com) reviewed 2026-11-14 14:19:36 +00:00
fjahr (Migrated from github.com) commented 2026-11-14 14:19:36 +00:00

With the latest push we now have -prunelock=<id> which doesn't take a height and always locks at height 0, so this comment should be obsolete.

With the latest push we now have `-prunelock=<id>` which doesn't take a height and always locks at height 0, so this comment should be obsolete.
fjahr (Migrated from github.com) reviewed 2026-11-14 14:21:02 +00:00
fjahr (Migrated from github.com) commented 2026-11-14 14:21:02 +00:00

Same reason as above, since there is no height parameter with the latest version this comment is obsolete.

Same reason as above, since there is no height parameter with the latest version this comment is obsolete.
fjahr commented 2026-11-14 14:28:28 +00:00 (Migrated from github.com)

Addressed the feedback from @andrewtoth and @optout21, thanks for the reviews!

The main change is that -prunelockheight=<n> is replaced by -prunelock=<id> in response to @optout21 's explicit use case description. The option creates a lock with the given id at height 0 before the startup prune, so nothing is pruned until the lock is updated or removed with setprunelock using the same id. It can be specified multiple times. This removes the "handover" of the init lock to the lock of the specific app, and it also works if more than one app needs a lock.

Since it doesn't seem strictly necessary for the discussed use cases to have the explicit height or tip functionality, this is left out for now to keep things simple.

Addressed the feedback from @andrewtoth and @optout21, thanks for the reviews! The main change is that `-prunelockheight=<n>` is replaced by `-prunelock=<id>` in response to @optout21 's explicit use case description. The option creates a lock with the given id at height 0 before the startup prune, so nothing is pruned until the lock is updated or removed with `setprunelock` using the same id. It can be specified multiple times. This removes the "handover" of the `init` lock to the lock of the specific app, and it also works if more than one app needs a lock. Since it doesn't seem strictly necessary for the discussed use cases to have the explicit height or tip functionality, this is left out for now to keep things simple.
andrewtoth (Migrated from github.com) reviewed 2026-11-21 17:48:44 +00:00
andrewtoth (Migrated from github.com) commented 2026-11-21 17:48:44 +00:00

So here we are rejecting lock names with : in the config args. However, we don't reject them via RPC.

Is this to support a future change to lock by height?

If so we should also enforce this restriction via RPC, so users who choose names with : aren't blocked from using them as a startup config arg after the fact.
We should also have a more useful error message, describing why it's invalid.

Otherwise we should remove this restriction.

So here we are rejecting lock names with `:` in the config args. However, we don't reject them via RPC. Is this to support a future change to lock by height? If so we should also enforce this restriction via RPC, so users who choose names with `:` aren't blocked from using them as a startup config arg after the fact. We should also have a more useful error message, describing why it's invalid. Otherwise we should remove this restriction.
DrahtBot commented 2026-11-22 22:19:28 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/45074726945/job/137369313684
LLM reason (✨ experimental): CI failed during linking because libbitcoinkernel.so has an undefined SanitizeString(std::string_view, int) symbol.

Hints

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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `test ancestor commits`: https://github.com/bitcoin/bitcoin/actions/runs/45074726945/job/137369313684</sub> <sub>LLM reason (✨ experimental): CI failed during linking because `libbitcoinkernel.so` has an undefined `SanitizeString(std::string_view, int)` symbol.</sub> <details><summary>Hints</summary> 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. </details>
fjahr (Migrated from github.com) reviewed 2026-11-22 22:26:57 +00:00
fjahr (Migrated from github.com) commented 2026-11-22 22:26:57 +00:00

Is this to support a future change to lock by height?

Yes, that was the idea. But the inconsistency between init option and RPC is indeed problematic. The best solution to me seemed to remove the ambiguity by just adding the option to add height to the init option now so that both ways to set the lock and work the same way. As a default to both now the height can be omitted and then genesis is the height default, which means that the init option can still be used as discussed based on the feedback from @optout21 . If there are duplicate init options (multiple in config file or config file and command line) then the lowest height is used. I also made parsing and error messages much more explicit.

> Is this to support a future change to lock by height? Yes, that was the idea. But the inconsistency between init option and RPC is indeed problematic. The best solution to me seemed to remove the ambiguity by just adding the option to add height to the init option now so that both ways to set the lock and work the same way. As a default to both now the height can be omitted and then genesis is the height default, which means that the init option can still be used as discussed based on the feedback from @optout21 . If there are duplicate init options (multiple in config file or config file and command line) then the lowest height is used. I also made parsing and error messages much more explicit.
GEREMIAA commented 2026-11-24 20:03:42 +00:00 (Migrated from github.com)

tACK d025cf1dc7

Tested on Ubuntu 22.04.5 (x86_64, GCC 11.4), signet node with pruning, wallet loaded.

  • rpc_prunelock.py passes, and the unit tests pass (378/378).
  • With a lock at 330000, pruneblockchain 331500 did not advance pruneheight (329114) and block 330000 stayed available. After setprunelock <id> remove, the same call pruned to 330933 and getblock 330000 returned "Block not available (pruned data)". Note that the lock applies per blk file, so blocks a bit below the lock height can also be kept. That looks expected.
  • listprunelocks shows the user: prefix. Manual locks are gone after a restart, as documented.
  • -prunelock=a:331500 -prunelock=a:331200 -prunelock=b gives a at 331200 (the lowest height) and b at 0.
  • Error cases give clear messages: invalid id, unknown id on remove, unknown command, negative height, -prunelock without -prune, and an invalid -prunelock value.

Two small points on the setprunelock help:

  1. The height argument says default=0 and also "(only required for "add")". add without a height is accepted and sets the lock at 0, so the height is not really required. Maybe "(only used for "add")"?
  2. setprunelock <id> remove 123 is accepted and the height is silently ignored. That's not a problem, but it could be rejected, or the help could mention it.
tACK d025cf1dc707 Tested on Ubuntu 22.04.5 (x86_64, GCC 11.4), signet node with pruning, wallet loaded. - `rpc_prunelock.py` passes, and the unit tests pass (378/378). - With a lock at 330000, `pruneblockchain 331500` did not advance `pruneheight` (329114) and block 330000 stayed available. After `setprunelock <id> remove`, the same call pruned to 330933 and `getblock 330000` returned "Block not available (pruned data)". Note that the lock applies per blk file, so blocks a bit below the lock height can also be kept. That looks expected. - `listprunelocks` shows the `user:` prefix. Manual locks are gone after a restart, as documented. - `-prunelock=a:331500 -prunelock=a:331200 -prunelock=b` gives `a` at 331200 (the lowest height) and `b` at 0. - Error cases give clear messages: invalid id, unknown id on `remove`, unknown command, negative height, `-prunelock` without `-prune`, and an invalid `-prunelock` value. Two small points on the `setprunelock` help: 1. The `height` argument says `default=0` and also "(only required for "add")". `add` without a height is accepted and sets the lock at 0, so the height is not really required. Maybe "(only used for "add")"? 2. `setprunelock <id> remove 123` is accepted and the height is silently ignored. That's not a problem, but it could be rejected, or the help could mention it.
optout21 (Migrated from github.com) reviewed 2026-11-24 22:13:11 +00:00
optout21 (Migrated from github.com) left a comment

Reviewed (d025cf1). Comments:

  • A side effect worth exploring: during any reorg, all prune locks above tip-1 are set to tip-1. Maybe this should not apply on manual blocks. The lowering is done in Chainstate::DisconnectTip().

  • No unit tests for the parsing helpers IsValidPruneLockId and ParsePruneLockArg.

  • listprunelocks returns user:testlock, but setprunelock only accepts testlock. Passing user:testlock fails because : isn't allowed. Should be more consistent.

Reviewed (d025cf1). Comments: - A side effect worth exploring: during any reorg, all prune locks above `tip-1` are set to `tip-1`. Maybe this should not apply on manual blocks. The lowering is done in `Chainstate::DisconnectTip()`. - No unit tests for the parsing helpers `IsValidPruneLockId` and `ParsePruneLockArg`. - `listprunelocks` returns `user:testlock`, but `setprunelock` only accepts `testlock`. Passing `user:testlock` fails because `:` isn't allowed. Should be more consistent.
optout21 (Migrated from github.com) commented 2026-11-24 21:58:02 +00:00

af51414 rpc: Add setprunelock and listprunelocks:

The order of the result is undefined. Maybe it could be sorted deterministically after it has been obtained.

> [af51414](https://github.com/bitcoin/bitcoin/pull/34534/changes/af514149a5414766268dcf8b48b727f7dca42b83) _rpc: Add setprunelock and listprunelocks_: The order of the result is undefined. Maybe it could be sorted deterministically after it has been obtained.
optout21 (Migrated from github.com) commented 2026-11-24 22:08:10 +00:00

af51414 rpc: Add setprunelock and listprunelocks:

Maybe a check for pruned node should be here, as in e.g. pruneblockchain.

> [af51414](https://github.com/bitcoin/bitcoin/pull/34534/changes/af514149a5414766268dcf8b48b727f7dca42b83) _rpc: Add setprunelock and listprunelocks_: Maybe a check for pruned node should be here, as in e.g. `pruneblockchain`.
fjahr (Migrated from github.com) reviewed 2026-11-25 15:46:16 +00:00
fjahr (Migrated from github.com) commented 2026-11-25 15:46:16 +00:00

Not sure if it's that important but it was easy to switch from std::unordered_map to std::map so I did that.

Not sure if it's that important but it was easy to switch from `std::unordered_map` to `std::map` so I did that.
fjahr (Migrated from github.com) reviewed 2026-11-25 15:46:24 +00:00
fjahr (Migrated from github.com) commented 2026-11-25 15:46:24 +00:00

Yeah, setprunelock now fails if the node is not in prune mode.

Yeah, `setprunelock` now fails if the node is not in prune mode.
fjahr commented 2026-11-25 15:46:42 +00:00 (Migrated from github.com)

Thanks for the review @optout21!

A side effect worth exploring: during any reorg, all prune locks above tip-1 are set to tip-1. Maybe this should not apply on manual blocks.

I think this should still apply to manual locks too. An external indexer could run behind the chain just like the internal indexes and needs the blocks from the fork point on to handle the reorg. But you are right that this could be surprising behavior, so I add some mention of this in the setprunelock help.

No unit tests for the parsing helpers IsValidPruneLockId and ParsePruneLockArg.

The functional test covers a lot of invalid ids and values through both setprunelock and -prunelock which should provide good coverage of these helpers, so I left this as is.

listprunelocks returns user:testlock, but setprunelock only accepts testlock.

Right, the inconsistency could be confusing to some users. I opted for changing listprunelocks so it returns the id without the prefix and has a new type field (user or internal), so a listed id could be passed to setprunelock.

Thanks for the review @optout21! > A side effect worth exploring: during any reorg, all prune locks above `tip-1` are set to `tip-1`. Maybe this should not apply on manual blocks. I think this should still apply to manual locks too. An external indexer could run behind the chain just like the internal indexes and needs the blocks from the fork point on to handle the reorg. But you are right that this could be surprising behavior, so I add some mention of this in the `setprunelock` help. > No unit tests for the parsing helpers `IsValidPruneLockId` and `ParsePruneLockArg`. The functional test covers a lot of invalid ids and values through both `setprunelock` and `-prunelock` which should provide good coverage of these helpers, so I left this as is. > `listprunelocks` returns `user:testlock`, but `setprunelock` only accepts `testlock`. Right, the inconsistency could be confusing to some users. I opted for changing `listprunelocks` so it returns the id without the prefix and has a new `type` field (`user` or `internal`), so a listed id could be passed to `setprunelock`.
GEREMIAA commented 2026-11-25 19:47:39 +00:00 (Migrated from github.com)

re-ACK 20cb220b55

Re-tested on Ubuntu 22.04.5, on a pruned signet node:

  • rpc_prunelock.py and the unit tests pass (378/378).
  • On a non-pruned node (regtest), setprunelock now fails with "Cannot set a prune lock because node is not in prune mode."
  • listprunelocks returns ids without the user: prefix, with the new type field, sorted by id. An id from the list can be passed directly to setprunelock <id> remove.
  • Locks still prevent pruning: with a lock at 331900, pruneblockchain 332150 kept block 331900.
  • The help now says "only used for "add"" and documents the reorg behavior. Thanks for taking that into account.
re-ACK 20cb220b55f7 Re-tested on Ubuntu 22.04.5, on a pruned signet node: - `rpc_prunelock.py` and the unit tests pass (378/378). - On a non-pruned node (regtest), `setprunelock` now fails with "Cannot set a prune lock because node is not in prune mode." - `listprunelocks` returns ids without the `user:` prefix, with the new `type` field, sorted by id. An id from the list can be passed directly to `setprunelock <id> remove`. - Locks still prevent pruning: with a lock at 331900, `pruneblockchain 332150` kept block 331900. - The help now says "only used for "add"" and documents the reorg behavior. Thanks for taking that into account.
andrewtoth commented 2026-11-26 04:03:55 +00:00 (Migrated from github.com)
ACK 20cb220b55f746a8b4538f640aee374604863c85
optout21 commented 2026-11-26 15:55:21 +00:00 (Migrated from github.com)

@fjahr , thanks for considering my points.
On the reorg issue: I agree. If there is a reorg, the external indexer will likely reindex the reorged height again as well.

@fjahr , thanks for considering my points. On the reorg issue: I agree. If there is a reorg, the external indexer will likely reindex the reorged height again as well.
optout21 commented 2026-11-26 16:34:42 +00:00 (Migrated from github.com)

ACK 20cb220b55

Concluding my previous reviews with an ack, looks good!

A new comment/suggestion: Internally, the "user:" prefix is used to mark external locks. This is internal now -- added automatically by add, removed automatically when listed. The magic string prefix could be replaced by a bool flag or enum (but has to be part of the key, a composite key). This scheme would be less error prone, but also less flexible. Or, if the "user:" prefix is indeed internal, at least replace it with the more visible "USER:" or "EXT:" form.

ACK 20cb220b55f746a8b4538f640aee374604863c85 Concluding my previous reviews with an ack, looks good! A new comment/suggestion: Internally, the "user:" prefix is used to mark external locks. This is internal now -- added automatically by add, removed automatically when listed. The magic string prefix could be replaced by a bool flag or enum (but has to be part of the key, a composite key). This scheme would be less error prone, but also less flexible. Or, if the "user:" prefix is indeed internal, at least replace it with the more visible "USER:" or "EXT:" form.
fjahr commented 2026-11-29 23:30:17 +00:00 (Migrated from github.com)

Internally, the "user:" prefix is used to mark external locks. This is internal now -- added automatically by add, removed automatically when listed. The magic string prefix could be replaced by a bool flag or enum (but has to be part of the key, a composite key). This scheme would be less error prone, but also less flexible. Or, if the "user:" prefix is indeed internal, at least replace it with the more visible "USER:" or "EXT:" form.

I agree that this would be cleaner and clearer and decided to implement this in another push. Sorry for invalidating the ACKs one more time but I hope the rest of the reviewers agree that this is better. I went with an enum to allow for new types like the wallet to be added later. Or if indexes are moved outside of the node at some point they may get a different category.

The change is roughly neutral in terms of LOC added/removed so I think the change overall get easier to review but those that need to re-review see a bit of churn, again sorry about that: github.com/bitcoin/[email protected]

> Internally, the "user:" prefix is used to mark external locks. This is internal now -- added automatically by add, removed automatically when listed. The magic string prefix could be replaced by a bool flag or enum (but has to be part of the key, a composite key). This scheme would be less error prone, but also less flexible. Or, if the "user:" prefix is indeed internal, at least replace it with the more visible "USER:" or "EXT:" form. I agree that this would be cleaner and clearer and decided to implement this in another push. Sorry for invalidating the ACKs one more time but I hope the rest of the reviewers agree that this is better. I went with an enum to allow for new types like the wallet to be added later. Or if indexes are moved outside of the node at some point they may get a different category. The change is roughly neutral in terms of LOC added/removed so I think the change overall get easier to review but those that need to re-review see a bit of churn, again sorry about that: https://github.com/bitcoin/bitcoin/compare/20cb220b55f746a8b4538f640aee374604863c85..dd6c67b0e5f6ffe184d101281eb107fcf32a26f6
optout21 commented 2026-11-30 07:41:49 +00:00 (Migrated from github.com)

reACK dd6c67b0e5

Sorry for triggering another ack-cycle, I'm keen on this PR to land :)
The enum is more pleasing than the string prefix operations (comparison, removal, etc.). Thanks for taking it!

git range-diff 20cb220...dd6c67b
git range-diff 1b2c63d..20cb220 9601d8c..dd6c67b

reACK dd6c67b0e5f6ffe184d101281eb107fcf32a26f6 Sorry for triggering another ack-cycle, I'm keen on this PR to land :) The enum is more pleasing than the string prefix operations (comparison, removal, etc.). Thanks for taking it! `git range-diff 20cb220...dd6c67b` `git range-diff 1b2c63d..20cb220 9601d8c..dd6c67b`
andrewtoth (Migrated from github.com) approved these changes 2026-12-03 21:44:58 +00:00
andrewtoth (Migrated from github.com) left a comment

reACK dd6c67b0e5

reACK dd6c67b0e5f6ffe184d101281eb107fcf32a26f6
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!34534
No description provided.