3 suggestions
Optional improvements
[suggestion] · Encryption callers do not receive the cleanup warning
When cleanup fails, the encryptwallet RPC and GUI encryption dialog still show their normal success messages without the move-funds advice.
Location: src/wallet/wallet.cpp:901 (head).
Details and suggested fix
A failed Rewrite() only calls LogWarning; EncryptWallet() then returns true. The RPC returns its fixed success string, while interfaces::Wallet::encryptWallet and WalletModel::setWalletEncrypted pass only the bool to the GUI success dialog. Foreground bitcoind also prints the warning to stdout by default, but neither requesting workflow receives a distinguishable cleanup result. Return a separate nonfatal warning and propagate it into the RPC response and GUI confirmation, retaining the log line. This requires changing the encryption interface and its callers, but preserves success because the encrypted records have already committed. This is a limitation of the new notification, not a regression in encryption behavior. The residual-plaintext concern applies to encryption of existing plaintext keys, not creation of a wallet with a passphrase.
[suggestion] · Cleanup advice unnecessarily alarms born-encrypted wallets
A rewrite failure during passphrase-protected wallet creation triggers unnecessary move-funds advice, and the advice does not specify creating the replacement with a passphrase.
Location: src/wallet/wallet.cpp:901 (head).
Details and suggested fix
For createwallet with a nonempty passphrase, CreateNew defers key generation until EncryptWallet has installed the master key. Descriptor generation then writes encrypted private-key records through AddDescriptorKeyWithDB; no plaintext private keys were written during creation. If the final rewrite fails, the new warning nevertheless suggests that plaintext may remain and advises moving funds from this newly created wallet. Distinguish this born-encrypted case before generating keys and use a cleanup-only message for it. For wallets that did contain plaintext keys, change the remediation to “move funds to a new wallet created with a passphrase.” Creating an unencrypted replacement and encrypting it later writes plaintext keys again and depends on another successful rewrite. Neither change needs to alter encryption success semantics.
[suggestion] · Rewrite-failure test omits committed key-record assertions
The new test checks cached wallet state after rewrite failure but does not directly check the committed key records that justify returning success.
Location: src/wallet/test/wallet_tests.cpp:203 (head).
Details and suggested fix
HaveCryptedKeys(), IsLocked() and Unlock("passphrase") do not reload the encrypted key records from the database. Add the neighboring tests' HasRecordType assertions inside this loop: require MASTER_KEY and WALLETDESCRIPTORCKEY, and require the absence of WALLETDESCRIPTORKEY. Current code commits before Rewrite(), and the injected failure short-circuits before SQLite, so this is a regression-test improvement rather than a current persistence defect. It documents the success guarantee for the rewrite-failure branch and would catch branch-specific changes that damage database records while leaving cached keys usable. These assertions check logical records, not physical removal of plaintext from slack space.