wallet, rpc: log instead of returning false #35966

Closed
vicjuma wants to merge 1 commit from fix-misleading-wallet-encryption-error into master AGit
vicjuma commented 2026-08-13 22:26:22 +00:00 (Migrated from github.com)

Summary

  if (!Unlock(strWalletPassphrase)) {
            return false;
    }

The above code src/wallet/wallet.cpp:890: runs after the wallet encryption process is already complete. Making the failed unlock operation to return false leads to a misleading RPC error message, just in case the encryption happens but the unlock operation fails. Probably it was added there because the function should always return a boolean.

Before change

This is the result of a failed unlock, i.e making the function to always return false to mimic the unlock failure

Mutated function

if (Unlock(plain_master_key)) {
        // Now that we've unlocked, upgrade the descriptor cache
        // UpgradeDescriptorCache();
         return false;
    }

Output

ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli createwallet testwallet
{
  "name": "testwallet"
}
ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass"
error code: -16
error message:
Error: Failed to encrypt the wallet.
ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass"
error code: -15
error message:
Error: running with an encrypted wallet, but encryptwallet was called.
ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ 

Solution

The failed unlock is inconsequential to the encryption process. It can even just be written as

Lock();
Unlock(strWalletPassphrase)

SetupWalletGeneration();

Lock();

Instead of returning false, which leads to a misleading error message to the user, probably just logging a message might be necessary

 if (!Unlock(strWalletPassphrase)) {
    WalletLogPrintf("Unlocking the encrypted wallet failed\n");
 }

After the change

ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli createwallet testwallet
{
 "name": "testwallet"
}
ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass"
wallet encrypted; The keypool has been flushed and a new HD seed was generated. You need to make a new backup with the backupwallet RPC.
ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass"
error code: -15
error message:
Error: running with an encrypted wallet, but encryptwallet was called.
ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ 

The nature of the log is the one to be probably determined or the essence of the conditional statement

### Summary ```cpp if (!Unlock(strWalletPassphrase)) { return false; } ``` The above code `src/wallet/wallet.cpp:890: ` runs after the wallet encryption process is already complete. Making the failed unlock operation to return false leads to a misleading RPC error message, just in case the encryption happens but the unlock operation fails. Probably it was added there because the function should always return a boolean. ### Before change This is the result of a failed unlock, i.e making the function to always return false to mimic the unlock failure **Mutated function** ```cpp if (Unlock(plain_master_key)) { // Now that we've unlocked, upgrade the descriptor cache // UpgradeDescriptorCache(); return false; } ``` **Output** ```console ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli createwallet testwallet { "name": "testwallet" } ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass" error code: -16 error message: Error: Failed to encrypt the wallet. ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass" error code: -15 error message: Error: running with an encrypted wallet, but encryptwallet was called. ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ``` ### Solution The failed unlock is inconsequential to the encryption process. It can even just be written as ```cpp Lock(); Unlock(strWalletPassphrase) SetupWalletGeneration(); Lock(); ``` Instead of returning false, which leads to a misleading error message to the user, probably just logging a message might be necessary ```cpp if (!Unlock(strWalletPassphrase)) { WalletLogPrintf("Unlocking the encrypted wallet failed\n"); } ``` **After the change** ```console ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli createwallet testwallet { "name": "testwallet" } ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass" wallet encrypted; The keypool has been flushed and a new HD seed was generated. You need to make a new backup with the backupwallet RPC. ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass" error code: -15 error message: Error: running with an encrypted wallet, but encryptwallet was called. ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ``` The nature of the log is the one to be probably determined or the essence of the conditional statement
DrahtBot commented 2026-08-13 22:26:29 +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 NACK achow101

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

<!--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/35966 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35966.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35966#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 NACK | [achow101](https://github.com/bitcoin/bitcoin/pull/35966#issuecomment-5961427714) | 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
katesalazar commented 2026-08-14 08:14:25 +00:00 (Migrated from github.com)

Is this consistent with PR 28333?
Can GetDatabase().Rewrite(); get called in a dangeruz way?

Is this consistent with PR 28333? Can `GetDatabase().Rewrite();` get called in a dangeruz way?
vicjuma commented 2026-08-14 08:37:52 +00:00 (Migrated from github.com)

Is this consistent with PR 28333? Can GetDatabase().Rewrite(); get called in a dangeruz way?

Thanks for the comment. Both GetDatabase().Rewrite(); and !Unlock(strWalletPassphrase) are called after the encryption is already successful. This PR does not look at the database rewrite in any way because its result does not also change anything. Whether the rewrite is a success or not, the encryption has already happened. Same as whether the unlock is successful or not. That's why returning false raises the wrong error message as I have demonstrated above.

> Is this consistent with PR 28333? Can `GetDatabase().Rewrite();` get called in a dangeruz way? Thanks for the comment. Both `GetDatabase().Rewrite();` and `!Unlock(strWalletPassphrase)` are called after the encryption is already successful. This PR does not look at the database rewrite in any way because its result does not also change anything. Whether the rewrite is a success or not, the encryption has already happened. Same as whether the unlock is successful or not. That's why returning false raises the wrong error message as I have demonstrated above.
vicjuma commented 2026-08-14 08:44:24 +00:00 (Migrated from github.com)

Is this consistent with PR 28333? Can GetDatabase().Rewrite(); get called in a dangeruz way?

This PR you are mentioning added the conditional statement, this PR tends to maintein it, only this time logging instead of returning. It is the simplest way if the function is to return a bool. Changing the function to return an enum class variant is also possible, like

enum class EncryptWalletResul {SUCCESS, FAILED, SUCCESS_UNLOCK_FAILED, SUCCESS_REWRITE_FAILED};

This is however complicated solution for the simple problem

> Is this consistent with PR 28333? Can `GetDatabase().Rewrite();` get called in a dangeruz way? This PR you are mentioning added the conditional statement, this PR tends to maintein it, only this time logging instead of returning. It is the simplest way if the function is to return a bool. Changing the function to return an enum class variant is also possible, like ```cpp enum class EncryptWalletResul {SUCCESS, FAILED, SUCCESS_UNLOCK_FAILED, SUCCESS_REWRITE_FAILED}; ``` This is however complicated solution for the simple problem
katesalazar commented 2026-08-14 11:38:08 +00:00 (Migrated from github.com)

I know I'm missing most of the thing here, but. If encryption of right-just-created wallets is currently broken, as I think your bitcoin-cli flow implies, isnt this change a lazy workaround fix instead of what would be a thorough elaborate fix? I can't see how this would be equivalent.

I know I'm missing most of the thing here, but. If encryption of right-just-created wallets is currently broken, as I think your bitcoin-cli flow implies, isnt this change a lazy workaround fix instead of what would be a thorough elaborate fix? I can't see how this would be equivalent.
vicjuma commented 2026-08-14 12:05:14 +00:00 (Migrated from github.com)

I know I'm missing most of the thing here, but. If encryption of right-just-created wallets is currently broken, as I think your bitcoin-cli flow implies, isnt this change a lazy workaround fix instead of what would be a thorough elaborate fix? I can't see how this would be equivalent.

It is not a break in encryption, its just the wrong error reporting. The process is Encryption -> Unlock Wallet -> Rewrite Database. Now returning false in the place of unlock is what results in

error code: -16
error message:
Error: Failed to encrypt the wallet.

which should not be the case because the encryption process has already happened. You can maybe try to reproduce it in regtest so that you see what is currently happening. The PR description shows the steps to reproduce it

> I know I'm missing most of the thing here, but. If encryption of right-just-created wallets is currently broken, as I think your bitcoin-cli flow implies, isnt this change a lazy workaround fix instead of what would be a thorough elaborate fix? I can't see how this would be equivalent. It is not a break in encryption, its just the wrong error reporting. The process is `Encryption -> Unlock Wallet -> Rewrite Database`. Now returning false in the place of unlock is what results in ```console error code: -16 error message: Error: Failed to encrypt the wallet. ``` which should not be the case because the encryption process has already happened. You can maybe try to reproduce it in regtest so that you see what is currently happening. The PR description shows the steps to reproduce it
katesalazar commented 2026-08-14 13:55:19 +00:00 (Migrated from github.com)

I'm not familiar with the wallet code, but I don't really feel like returning enum class is much of an overkill for wallet manipulation if you wanna edit this or as an idea for subsequent PRs if achow or whoever oversees wallet these days doesnt veto.

I'm not familiar with the wallet code, but I don't really feel like returning enum class is much of an overkill for wallet manipulation if you wanna edit this or as an idea for subsequent PRs if achow or whoever oversees wallet these days doesnt veto.
achow101 commented 2026-08-17 22:47:37 +00:00 (Migrated from github.com)

I don't think this is correct. If a wallet is successfully encrypted, and then we are unable to unlock it, that is a catastrophic failure. It means that the wallet is corrupted, and the funds are probably lost. This should really be an Assert or some failure mode which results in the wallet no longer being loaded and in use. Logging a warning is not the right thing to do. Returning false is reasonable, but not actually far enough in preventing usage of the wallet as we really should be if this were to occur.

I don't think this is correct. If a wallet is successfully encrypted, and then we are unable to unlock it, that is a catastrophic failure. It means that the wallet is corrupted, and the funds are probably lost. This should really be an `Assert` or some failure mode which results in the wallet no longer being loaded and in use. Logging a warning is not the right thing to do. Returning `false` is reasonable, but not actually far enough in preventing usage of the wallet as we really should be if this were to occur.
vicjuma commented 2026-08-17 22:57:27 +00:00 (Migrated from github.com)

Logging a warning is not the right thing to do. Returning false is reasonable, but not actually far enough in preventing usage of the wallet as we really should be if this were to occur.

Logging may not be right, this I did just to show that there might be an intermediate return value (instead of just a true or false), returning an internal RPC error may be appropriate here instead of telling the user that the encryption failed while it actually passed, just that maybe there an issue in the unlock process.

> Logging a warning is not the right thing to do. Returning `false` is reasonable, but not actually far enough in preventing usage of the wallet as we really should be if this were to occur. Logging may not be right, this I did just to show that there might be an intermediate return value (instead of just a true or false), returning an internal RPC error may be appropriate here instead of telling the user that the encryption failed while it actually passed, just that maybe there an issue in the unlock process.
vicjuma commented 2026-08-17 23:00:58 +00:00 (Migrated from github.com)
> ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass"
> error code: -16
> error message:
> Error: Failed to encrypt the wallet.
> ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass"
> error code: -15
> error message:
> Error: running with an encrypted wallet, but encryptwallet was called.
> ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ 
> 

As can be seen in this demonstration

```cpp > ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass" > error code: -16 > error message: > Error: Failed to encrypt the wallet. > ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ ./bitcoin-cli encryptwallet "testpass" > error code: -15 > error message: > Error: running with an encrypted wallet, but encryptwallet was called. > ratedg@0xratedg:~/Desktop/bitcoin/build/bin$ > ``` As can be seen in this demonstration
achow101 commented 2026-10-02 21:02:34 +00:00 (Migrated from github.com)

NACK

As I stated previously, I don't think this is the right approach.

this I did just to show that there might be an intermediate return value

If the point of this PR is to serve as a proof of concept, it should be closed, and an issue opened instead.

NACK As I stated previously, I don't think this is the right approach. > this I did just to show that there might be an intermediate return value If the point of this PR is to serve as a proof of concept, it should be closed, and an issue opened instead.
vicjuma commented 2026-10-02 21:09:50 +00:00 (Migrated from github.com)

Closing now

Closing now
First-time contributor

Base: 65896ac58ea63e29ad19d41b492e37f8722ac715
Head: d682e5fddfea4a88e12d47a38769db99d44b65fb

🟡 Minor

Failed unlock throws during wallet setup (src/wallet/wallet.cpp:891, head)

If Unlock returns false, EncryptWallet remains locked: it called Lock() immediately beforehand, and Unlock sets the master key only on success. For a nonblank, private-key-enabled wallet, SetupWalletGeneration() then reaches SetupDescriptorScriptPubKeyMan(), which throws when the wallet is locked. This branch therefore does not complete encryption setup or reach the successful return; it replaces the former false return with an exception. Handle the failure explicitly and report that encryption committed but follow-up setup did not complete, rather than logging and falling through. No current caller or ordinary input producing this false result in the fresh-encryption flow was identified, so production impact is unconfirmed.

Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review did not complete. Verification was partial.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `65896ac58ea63e29ad19d41b492e37f8722ac715` Head: `d682e5fddfea4a88e12d47a38769db99d44b65fb` ##### 🟡 Minor **Failed unlock throws during wallet setup** (src/wallet/wallet.cpp:891, head) If `Unlock` returns false, `EncryptWallet` remains locked: it called `Lock()` immediately beforehand, and `Unlock` sets the master key only on success. For a nonblank, private-key-enabled wallet, `SetupWalletGeneration()` then reaches `SetupDescriptorScriptPubKeyMan()`, which throws when the wallet is locked. This branch therefore does not complete encryption setup or reach the successful return; it replaces the former `false` return with an exception. Handle the failure explicitly and report that encryption committed but follow-up setup did not complete, rather than logging and falling through. No current caller or ordinary input producing this false result in the fresh-encryption flow was identified, so production impact is unconfirmed. Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review did not complete. Verification was partial. [Full review report](<https://review.fish.foo/traces/35966-d682e5fddfea4a88e12d47a38769db99d44b65fb-314-1.html>)
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!35966
No description provided.