wallet, sqlite: Enable secure_delete by default #36796

Open
achow101 wants to merge 6 commits from raii-sqlite-secure-delete into master AGit
achow101 commented 2026-11-07 02:13:02 +00:00 (Migrated from github.com)

Currently, when we make deletions or updates to the database that require records to be fully deleted and to not persist in free pages afterwards, we need to explicitly call WalletDatabase::Rewrite. This behavior was introduced in #635 to ensure that unencrypted private keys are erased from the database after encrypting a wallet, and initial encryption is the only consumer of this function.

However, initial encryption is not the only functionality that requires secure deletion. l0rinc's LLMs recently found changing the passphrase can leave the original that encryption key record in free pages. We should want to ensure that those records are also deleted, although I do not consider that the presence of such records in free space to actually be a problem. Note that this behavior appears to be present since wallet encryption was initially introduced in #352.

Fortunately, SQLite offers a secure_delete flag that can be enabled on each connection to a database. If set, all deletes or updates will ensure that any data that was deleted or overwritten actually are overwritten with 0's. It is one of the 2 methods in SQLite's documentation for ensuring that records do not persist in free pages, the other method being to call VACUUM after deletions or updates, which is what Rewrite does.

The downside is that secure_delete will increase CPU and disk I/O, which is why this was not set previously. However, it seems like several distros ship with secure_delete enabled by default: Debian, Ubuntu, Fedora, Arch, and NixOS all set SQLite's compile time option to default secure_delete to enabled, and these are just the ones I've looked at. This suggests to me that the performance penalty is not actually that bad. As I run Arch and often do not use depends builds, I had not noticed that secure_delete was always on and did not experience any particularly poor wallet performance that could be attributed to that behavior. In the grand scheme of things, erases and updates are fairly infrequent in the wallet.

This PR enables secure_delete when the database is opened, and also enables it by default in the sqlite depends package. There are also a couple tests to check that all records we want deleted actually are being deleted from the database. WalletDatabase::Rewrite is removed as it it is no longer used.

Currently, when we make deletions or updates to the database that require records to be fully deleted and to not persist in free pages afterwards, we need to explicitly call `WalletDatabase::Rewrite`. This behavior was introduced in #635 to ensure that unencrypted private keys are erased from the database after encrypting a wallet, and initial encryption is the only consumer of this function. However, initial encryption is not the only functionality that requires secure deletion. l0rinc's LLMs recently found changing the passphrase can leave the original that encryption key record in free pages. We should want to ensure that those records are also deleted, although I do not consider that the presence of such records in free space to actually be a problem. Note that this behavior appears to be present since wallet encryption was initially introduced in #352. Fortunately, SQLite offers a secure_delete flag that can be enabled on each connection to a database. If set, all deletes or updates will ensure that any data that was deleted or overwritten actually are overwritten with 0's. It is one of the 2 methods in SQLite's documentation for ensuring that records do not persist in free pages, the other method being to call `VACUUM` after deletions or updates, which is what `Rewrite` does. The downside is that secure_delete will increase CPU and disk I/O, which is why this was not set previously. However, it seems like several distros ship with secure_delete enabled by default: [Debian](https://sources.debian.org/src/sqlite3/3.54.1-2/debian/rules#L48#:~:text=DSQLITE_SECURE_DELETE), [Ubuntu](https://git.launchpad.net/ubuntu/+source/sqlite3/plain/debian/rules#:~:text=DSQLITE_SECURE_DELETE), [Fedora](https://src.fedoraproject.org/rpms/sqlite/raw/rawhide/f/sqlite.spec#:~:text=DSQLITE_SECURE_DELETE=1), [Arch](https://gitlab.archlinux.org/archlinux/packaging/packages/sqlite/-/raw/main/PKGBUILD#:~:text=DSQLITE_SECURE_DELETE), and [NixOS](https://github.com/NixOS/nixpkgs/blob/nixos-26.05/pkgs/development/libraries/sqlite/default.nix#L184) all set SQLite's compile time option to default secure_delete to enabled, and these are just the ones I've looked at. This suggests to me that the performance penalty is not actually that bad. As I run Arch and often do not use depends builds, I had not noticed that secure_delete was always on and did not experience any particularly poor wallet performance that could be attributed to that behavior. In the grand scheme of things, erases and updates are fairly infrequent in the wallet. This PR enables secure_delete when the database is opened, and also enables it by default in the sqlite depends package. There are also a couple tests to check that all records we want deleted actually are being deleted from the database. `WalletDatabase::Rewrite` is removed as it it is no longer used.
DrahtBot commented 2026-11-07 02:13:10 +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
Concept ACK Zippo76, rkrux
Stale ACK jpk68, fametrano

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:

  • #36257 (qa: assert_equal -> assert_true/assert_false by hodlinator)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/wallet/sqlite.cpp: Enbling → Enabling [misspelling]

2026-12-01 03:31:20

<!--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/36796 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36796.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36796#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 | | ---- | --------- | | Concept ACK | [Zippo76](https://github.com/bitcoin/bitcoin/pull/36796#pullrequestreview-5903903738), [rkrux](https://github.com/bitcoin/bitcoin/pull/36796#issuecomment-7163595542) | | Stale ACK | [jpk68](https://github.com/bitcoin/bitcoin/pull/36796#issuecomment-6714872383), [fametrano](https://github.com/bitcoin/bitcoin/pull/36796#pullrequestreview-5819356619) | 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: * [#36257](https://github.com/bitcoin/bitcoin/pull/36257) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36257.svg"></sub> (qa: assert_equal -> assert_true/assert_false by hodlinator) 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--> ### LLM Linter (✨ experimental) Possible typos and grammar issues: - `src/wallet/sqlite.cpp`: `Enbling` → `Enabling` [misspelling] <sup>2026-12-01 03:31:20</sup>
DrahtBot commented 2026-11-07 02:23:52 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/42328113449/job/128268068670
LLM reason (✨ experimental): CI failed because Python lint found six unnecessary semicolons in test/functional/wallet_encryption.py.

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 `lint`: https://github.com/bitcoin/bitcoin/actions/runs/42328113449/job/128268068670</sub> <sub>LLM reason (✨ experimental): CI failed because Python lint found six unnecessary semicolons in `test/functional/wallet_encryption.py`.</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>
First-time contributor

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

🟠 Minor · A close-time incremental_vacuum failure terminates the node

If reclaiming free pages fails during wallet unload or shutdown, the new close-time incremental_vacuum throws through Cleanup() noexcept and terminates the node.

Location: src/wallet/sqlite.cpp:403 (head).

🟠 Minor · Migration VACUUM runs before wallet verification

Existing wallets with auto_vacuum != 2 are rewritten before schema and integrity verification, and a migration failure now prevents loading.

Location: src/wallet/sqlite.cpp:326 (head).

🔵 Suggestion · wallet_encryption.py now skips all checks without Python sqlite3

The unconditional skip_if_no_py_sqlite3() removes existing encryption RPC coverage in environments without the optional module, although only the old mkey comparison needs database access.

Location: test/functional/wallet_encryption.py:35 (head).

🔵 Suggestion · Rewrite() disappears before replacement migration cleanup lands

Removing Rewrite() one commit before adding migration VACUUM makes the cleanup transition harder to validate for wallets containing stale records from older releases.

Location: src/wallet/wallet.cpp:851 (commit).

Review coverage was incomplete.


Reviewed: d0868f381ae4 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:312ddb2095238aa2ca395a9cc39b05ab5a38a7177afbdb0d69098df9c3e0bef9:9a7f127b15c6116c267953d38288a9f17ec7c7bcd3a975fb71a2db8bfd974dbf --> <!-- ralph-base:1ea904d983f8841c28cb8240c576e46ac2efe529 --> <!-- ralph-head:d0868f381ae4f4a2d5cc0aac93f440706c7a1601 --> <!-- ralph-case:bd978170f6c390f7812dd3178f5747f18c6bd1203c6ad9cf9b155e69525756da --> Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments. **🟠 Minor · A close-time `incremental_vacuum` failure terminates the node** If reclaiming free pages fails during wallet unload or shutdown, the new close-time `incremental_vacuum` throws through `Cleanup() noexcept` and terminates the node. Location: [`src/wallet/sqlite.cpp:403`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d0868f381ae4f4a2d5cc0aac93f440706c7a1601/src/wallet/sqlite.cpp#L403>) (head). **🟠 Minor · Migration `VACUUM` runs before wallet verification** Existing wallets with `auto_vacuum != 2` are rewritten before schema and integrity verification, and a migration failure now prevents loading. Location: [`src/wallet/sqlite.cpp:326`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d0868f381ae4f4a2d5cc0aac93f440706c7a1601/src/wallet/sqlite.cpp#L326>) (head). **🔵 Suggestion · `wallet_encryption.py` now skips all checks without Python `sqlite3`** The unconditional `skip_if_no_py_sqlite3()` removes existing encryption RPC coverage in environments without the optional module, although only the old `mkey` comparison needs database access. Location: [`test/functional/wallet_encryption.py:35`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d0868f381ae4f4a2d5cc0aac93f440706c7a1601/test/functional/wallet_encryption.py#L35>) (head). **🔵 Suggestion · `Rewrite()` disappears before replacement migration cleanup lands** Removing `Rewrite()` one commit before adding migration `VACUUM` makes the cleanup transition harder to validate for wallets containing stale records from older releases. Location: src/wallet/wallet.cpp:851 (commit). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`d0868f381ae4`](<https://git.fish.foo/bitcoin/bitcoin/commit/d0868f381ae4f4a2d5cc0aac93f440706c7a1601>) · [Review report](<https://review.fish.foo/report/36796-d0868f381ae4f4a2d5cc0aac93f440706c7a1601-3369-7.html>) · [Previous reports](<https://review.fish.foo/traces/36796/>) [permalink](<https://review.fish.foo/report/36796-d0868f381ae4f4a2d5cc0aac93f440706c7a1601-3369-7.html>)
fametrano (Migrated from github.com) reviewed 2026-11-07 08:03:37 +00:00
fametrano (Migrated from github.com) left a comment

Concept ACK

For a wallet created at this head, no copy of the plaintext key stays in wallet.dat-journal after encryptwallet, while the wallet is loaded or after a kill -9.

I ran encryptwallet on a fresh descriptor wallet at 96352f5200 and counted copies of its 32-byte xprv private key in its files. I linked bitcoind against Homebrew SQLite and against depends, both leaving secure_delete off by default, so only the pragma this PR adds can zero the pages. wallet.dat and the journal held 0 copies while loaded, after a kill -9, and after a restart and reload. The same run on the head of #36793, which still relies on VACUUM, left 6 copies in the journal, 1 after the restart.

The script, builds and output are in https://gist.github.com/fametrano/245e7d7aa2a4762d2ce7c102e097b92f/7cf068b0300062264dce40a1838c98d4958504b0.

Concept ACK For a wallet created at this head, no copy of the plaintext key stays in `wallet.dat-journal` after `encryptwallet`, while the wallet is loaded or after a `kill -9`. I ran `encryptwallet` on a fresh descriptor wallet at 96352f5200 and counted copies of its 32-byte xprv private key in its files. I linked bitcoind against Homebrew SQLite and against depends, both leaving `secure_delete` off by default, so only the pragma this PR adds can zero the pages. `wallet.dat` and the journal held 0 copies while loaded, after a `kill -9`, and after a restart and reload. The same run on the head of #36793, which still relies on VACUUM, left 6 copies in the journal, 1 after the restart. The script, builds and output are in https://gist.github.com/fametrano/245e7d7aa2a4762d2ce7c102e097b92f/7cf068b0300062264dce40a1838c98d4958504b0.
ValeraFinebits (Migrated from github.com) reviewed 2026-11-07 08:53:05 +00:00
ValeraFinebits (Migrated from github.com) commented 2026-11-07 08:53:05 +00:00
$(package)_cppflags += -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_SECURE_DELETE

Looks like there's a typo here.

```suggestion $(package)_cppflags += -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_SECURE_DELETE ``` Looks like there's a typo here.
achow101 (Migrated from github.com) reviewed 2026-11-07 09:01:59 +00:00
achow101 (Migrated from github.com) commented 2026-11-07 09:01:59 +00:00

Oops fixed

Oops fixed
jpk68 commented 2026-11-08 00:10:41 +00:00 (Migrated from github.com)
ACK 38c28393cb481b844a63e4c965875a45aac3e819
fametrano (Migrated from github.com) reviewed 2026-11-08 19:36:47 +00:00
fametrano (Migrated from github.com) left a comment

tACK 38c28393cb

With the secure_delete pragma removed from SQLiteDatabase::Open, the depends default alone now keeps the key out of wallet.dat and its journal. Linked against the depends SQLite built at 38c28393cb, wallet.dat and its journal held no copy of the plaintext key after encryptwallet, after a kill -9, or after the restart. Linked against the one built at 96352f5200, wallet.dat held 6 copies, and the journal 14 after encryption, 14 after the kill and 5 after the restart.

The PR as pushed left no copy with Homebrew SQLite or with either depends build. wallet_encryption.py and the wallet unit tests pass at this head. As before, this covers a freshly created wallet.

The builds, script and output are in https://gist.github.com/fametrano/ef1fb32f05c8163272c8be27aeb32cf2/c4fe15cebece1cc35956f636de8648a74ce8940c.

tACK 38c28393cb With the `secure_delete` pragma removed from `SQLiteDatabase::Open`, the depends default alone now keeps the key out of `wallet.dat` and its journal. Linked against the depends SQLite built at 38c28393cb, `wallet.dat` and its journal held no copy of the plaintext key after `encryptwallet`, after a `kill -9`, or after the restart. Linked against the one built at 96352f5200, `wallet.dat` held 6 copies, and the journal 14 after encryption, 14 after the kill and 5 after the restart. The PR as pushed left no copy with Homebrew SQLite or with either depends build. `wallet_encryption.py` and the wallet unit tests pass at this head. As before, this covers a freshly created wallet. The builds, script and output are in https://gist.github.com/fametrano/ef1fb32f05c8163272c8be27aeb32cf2/c4fe15cebece1cc35956f636de8648a74ce8940c.
Zippo76 (Migrated from github.com) reviewed 2026-11-14 10:43:46 +00:00
Zippo76 (Migrated from github.com) left a comment

Concept ACK

Tested 38c28393cb against master a527a0077a on macOS 27.2.1 arm64.

With macOS system SQLite (secure_delete=2, FAST), master and the PR produced the same results, and the PR's wallet_encryption.py passed on both. With Homebrew SQLite 3.54.1 (secure_delete=0 by default), the test failed on master at line 118 because the old mkey was still present, and passed with the PR.

The Homebrew build confirmed both that the PR prevents new residues and that pre-upgrade residues can persist, as noted in the Ralph review:

Scenario Result
Fresh wallet: old mkey after walletpassphrasechange 1 copy on master, 0 with the PR
Wallet created on master, then encrypted with the PR 5 plaintext private-key copies remain in wallet.dat; an equivalent wallet encrypted on master has 0
Old mkey residue left by a passphrase change on master Both copies remain after opening with the PR, after another passphrase change with the PR, and in a backup made with the PR

Enabling secure_delete prevents new residues in these tests, but does not remove existing ones. Removing the encryption-time VACUUM therefore leaves plaintext private-key copies in an existing wallet that master would have removed when encrypting it.

Concept ACK Tested `38c28393cb` against master `a527a0077a` on macOS 27.2.1 arm64. With macOS system SQLite (`secure_delete=2`, FAST), master and the PR produced the same results, and the PR's `wallet_encryption.py` passed on both. With Homebrew SQLite 3.54.1 (`secure_delete=0` by default), the test failed on master at line 118 because the old `mkey` was still present, and passed with the PR. The Homebrew build confirmed both that the PR prevents new residues and that pre-upgrade residues can persist, as noted in the [Ralph review](https://git.fish.foo/bitcoin/bitcoin/pulls/36796#author-ralph): | Scenario | Result | | --- | --- | | Fresh wallet: old `mkey` after `walletpassphrasechange` | 1 copy on master, 0 with the PR | | Wallet created on master, then encrypted with the PR | 5 plaintext private-key copies remain in `wallet.dat`; an equivalent wallet encrypted on master has 0 | | Old `mkey` residue left by a passphrase change on master | Both copies remain after opening with the PR, after another passphrase change with the PR, and in a backup made with the PR | Enabling `secure_delete` prevents new residues in these tests, but does not remove existing ones. Removing the encryption-time `VACUUM` therefore leaves plaintext private-key copies in an existing wallet that master would have removed when encrypting it.
furszy (Migrated from github.com) reviewed 2026-11-14 17:55:03 +00:00
Lines 312-313
furszy (Migrated from github.com) commented 2026-11-14 17:54:48 +00:00

As encryption only happens on specific paths, what about enabling secure deletion only for the DB connection that will erase the keys? Otherwise all wallet workflows pay the option overhead for no reason. As we never delete other records during any common path.

We currently keep a single DB connection per wallet for its entire lifetime, but as we lock cs_wallet during the entire operation, we can create a new connection only for this use case, with the secure deletion option enabled, then close it and resume/recreate the lifetime one.

As encryption only happens on specific paths, what about enabling secure deletion only for the DB connection that will erase the keys? Otherwise all wallet workflows pay the option overhead for no reason. As we never delete other records during any common path. We currently keep a single DB connection per wallet for its entire lifetime, but as we lock `cs_wallet` during the entire operation, we can create a new connection only for this use case, with the secure deletion option enabled, then close it and resume/recreate the lifetime one.
achow101 (Migrated from github.com) reviewed 2026-11-14 21:01:37 +00:00
Lines 312-313
achow101 (Migrated from github.com) commented 2026-11-14 21:01:37 +00:00

My original plan was to do something like that, to enable and disable secure delete as needed. But as I was testing that, I realized that multiple distros ship with sqlite compiled with secure delete enabled by default, so I figured the performance cost of enabling secure delete always probably isn't actually that bad. Enabling it always makes this much easier to reason about as well.

My original plan was to do something like that, to enable and disable secure delete as needed. But as I was testing that, I realized that multiple distros ship with sqlite compiled with secure delete enabled by default, so I figured the performance cost of enabling secure delete always probably isn't actually that bad. Enabling it always makes this much easier to reason about as well.
DrahtBot commented 2026-11-20 22:36:31 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
DrahtBot commented 2026-11-24 16:49:15 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
rkrux commented 2026-11-25 13:18:48 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
furszy (Migrated from github.com) reviewed 2026-11-30 20:42:23 +00:00
furszy (Migrated from github.com) left a comment

While this solves the encryption key leak during passphrase change for new wallets, it introduces something worse, as the removal of Rewrite leaves private keys dangling in existing wallets after encryption.

Can cherry-pick the following test to reproduce the behavior here (see test_encrypt_wallet): https://github.com/furszy/bitcoin-core/blob/2026_wallet_where_is_my_key/test/functional/wallet_encryption_compatibility.py

Essentially, previous releases don't have secure_delete enabled, so when a page fills up, SQLite moves its records to a new page without erasing them from the old one. Creating a wallet always fills its first page, so that page keeps old copies of the first records. These leftovers are no longer part of any record, so SQLite doesn't track them, and secure_delete never sees them (it only zeroes data being touched at the moment it's deleted). So when such a prev-release wallet is encrypted with this PR, the key records get replaced and zeroed, but the old raw copies stay.

So, while I also agree on having secure_delete enabled by default, it doesn't let us drop the VACUUM cleanup. Just pushed a branch with everything, including a few more tests too: 2026_wallet_where_is_my_key. All yours if you like it.

While this solves the encryption key leak during passphrase change for new wallets, it introduces something worse, as the removal of `Rewrite` leaves private keys dangling in existing wallets after encryption. Can cherry-pick the following test to reproduce the behavior here (see `test_encrypt_wallet`): https://github.com/furszy/bitcoin-core/blob/2026_wallet_where_is_my_key/test/functional/wallet_encryption_compatibility.py Essentially, previous releases don't have `secure_delete` enabled, so when a page fills up, SQLite moves its records to a new page without erasing them from the old one. Creating a wallet always fills its first page, so that page keeps old copies of the first records. These leftovers are no longer part of any record, so SQLite doesn't track them, and `secure_delete` never sees them (it only zeroes data being touched at the moment it's deleted). So when such a prev-release wallet is encrypted with this PR, the key records get replaced and zeroed, but the old raw copies stay. So, while I also agree on having `secure_delete` enabled by default, it doesn't let us drop the `VACUUM` cleanup. Just pushed a branch with everything, including a few more tests too: [2026_wallet_where_is_my_key](https://github.com/furszy/bitcoin-core/tree/2026_wallet_where_is_my_key). All yours if you like it.
achow101 commented 2026-12-01 03:33:28 +00:00 (Migrated from github.com)

Cherry picked the test.

As long as we vacuum old databases prior to encrypting them, we can still drop Rewrite. I was looking at enabling auto_vacuum for disk space saving, and if we were to enable it, we would need to perform a VACUUM, so I've added a commit that does that. This both enables additional disk space saving, and automatic vacuum for old databases (without running vacuum every time) which allows us to drop the vacuum after encrypting.

Cherry picked the test. As long as we vacuum old databases prior to encrypting them, we can still drop `Rewrite`. I was looking at enabling `auto_vacuum` for disk space saving, and if we were to enable it, we would need to perform a `VACUUM`, so I've added a commit that does that. This both enables additional disk space saving, and automatic vacuum for old databases (without running vacuum every time) which allows us to drop the vacuum after encrypting.
Sign in to join this conversation.
No reviewers
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!36796
No description provided.