Skip to content

wallet, rpc: log instead of returning false - #35966

Closed
vicjuma wants to merge 1 commit into
bitcoin:masterfrom
vicjuma:fix-misleading-wallet-encryption-error
Closed

vicjuma wants to merge 1 commit into
bitcoin:masterfrom
vicjuma:fix-misleading-wallet-encryption-error

Conversation

@vicjuma

@vicjuma vicjuma commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

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

replace the false return after encryption on !Unlock
@DrahtBot

DrahtBot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

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.

@katesalazar

Copy link
Copy Markdown
Contributor

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

@vicjuma

vicjuma commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor Author

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

vicjuma commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor Author

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

@katesalazar

Copy link
Copy Markdown
Contributor

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

vicjuma commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

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

@katesalazar

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Member

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

vicjuma commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

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

vicjuma commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor Author
> 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

@sedited
sedited requested a review from achow101 October 2, 2026 12:45
@achow101

achow101 commented Oct 2, 2026

Copy link
Copy Markdown
Member

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

vicjuma commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Closing now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants