Skip to content

Conversation

@achow101
Copy link
Member

@achow101 achow101 commented Jul 29, 2017

Removes vchDefaultKey which was only used for first run detection. Improves wallet first run detection by checking to see if any keys were read from the database.

This also fixes a (rather contrived) case where an encrypted non-HD wallet has corruption such that the default key is no longer valid and is loaded into a Core version that supports HD wallets. This causes a runtime exception since a new hd master key is generated as the software believes the wallet file is newly created but cannot add the generated key to the wallet since it is encrypted. I was only able to replicate this error by creating a non-hd wallet, encrypting it, then editing the wallet using db_dump and db_load before loading the wallet with hd enabled. This problem has been reported by two users so it is something that can happen, although that raises the question of "what corrupted the default key".

P.S. I don't know what's up with the whitespace changes. I think my text editor is doing something stupid but I don't think those are important enough to attempt undoing them. Undid those

@gmaxwell
Copy link
Contributor

So you still need to write a default key unless we're going to bump the wallet version again for this, because otherwise you'll make it backwards incompatible with software without this patch.

Otherwise, Concept ACK!

@achow101
Copy link
Member Author

How about bump the wallet version again? If there are any other wallet changes that need a version bump, we could do them all at the same time.

@gmaxwell
Copy link
Contributor

I would be fine with that but I think it might be easier to get this bugfix merged without it.

@TheBlueMatt
Copy link
Contributor

Sounds like maybe something that we should look into for 15, then, though I'd really like to look into the source of the corruption here, rather than patching over it.

@achow101 achow101 force-pushed the remove-defaultkey branch from b156448 to 94df789 Compare July 31, 2017 18:17
@achow101
Copy link
Member Author

@gmaxwell I added WriteDefaultKey back in so that a default key will be written to maintain backwards compatibility. The key will not be read in.

@achow101 achow101 force-pushed the remove-defaultkey branch 2 times, most recently from 06bd881 to 1a22b28 Compare August 2, 2017 00:25
Copy link
Contributor

@gmaxwell gmaxwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

@TheBlueMatt
Copy link
Contributor

I really dont think this is the right way to fix this issue. Can we instead ensure that, if the wallet has keys but vchDefaultKey is invalid, an error is printed like "your wallet is corrupted, seems you disk is silently corrupting things", and then make sure -salvagewallet is able to properly recover from this state?

@achow101 achow101 force-pushed the remove-defaultkey branch from 1a22b28 to 29824af Compare August 3, 2017 23:41
@achow101
Copy link
Member Author

achow101 commented Aug 3, 2017

I have updated this with a few changes.

I removed the defaultkey writing again. New wallets will no longer have a default key. If a defaultkey is found in the wallet, it must be valid, otherwise it will exit with a wallet corruption error.

It turns out that -salvagewallet may have been the cause of all of these issues. -salvagewallet apparently does not write a default key to the wallet, which with an encrypted wallet and with HD enabled, will cause the aforementioned runtime exception and crash. If a user does have a corrupted default key, then -salvagewallet will remove it, as it did in the past.

@achow101 achow101 force-pushed the remove-defaultkey branch from 29824af to 8f3ea17 Compare August 3, 2017 23:50
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you call walletInstance->TopUpKeyPool(); here instead of this entire section? No need to waste a key and an address book entry for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"defaultkey" is not part of IsKeyType, so this failure will just be a non-critical warning upon loading. I assume that's intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a critical warning and exit with an error. I have fixed this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style nit: { on the same like as if.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@achow101 achow101 force-pushed the remove-defaultkey branch 2 times, most recently from 27598a0 to f1e4eff Compare August 4, 2017 03:26
@maflcko
Copy link
Member

maflcko commented Aug 4, 2017

AssertionError in wallet-hd:

AssertionError: not(m/0'/0'/0' == m/0'/0'/1')

@achow101 achow101 force-pushed the remove-defaultkey branch from f1e4eff to 3157760 Compare August 4, 2017 06:43
@achow101
Copy link
Member Author

achow101 commented Aug 4, 2017

Can this be tagged for 0.15.0? It's a bug fix for a problem with -salvagewallet which would result in further corruption and unusability of people's wallets.

@sipa sipa added this to the 0.15.0 milestone Aug 5, 2017
@sipa
Copy link
Member

sipa commented Aug 5, 2017

Tagged for 0.15, as it fixes a bug when using -savagewallet with encrypted wallets.

@TheBlueMatt
Copy link
Contributor

TheBlueMatt commented Aug 6, 2017 via email

@achow101
Copy link
Member Author

achow101 commented Aug 7, 2017

@TheBlueMatt What for?

@TheBlueMatt
Copy link
Contributor

@achow101 because its a trivial check and its a type of corruption we've seen in wallets on the wild...it costs nothing to add a check for it.

@sipa
Copy link
Member

sipa commented Aug 7, 2017

@TheBlueMatt The current code requires that if a default key is present, it must be valid. Do you think an extra check is needed to verify that it hasn't been accidentally deleted?

@TheBlueMatt
Copy link
Contributor

TheBlueMatt commented Aug 7, 2017 via email

@achow101
Copy link
Member Author

achow101 commented Aug 7, 2017

@TheBlueMatt I meant what do you want it to check for and error on and why do that check? That below a certain version number you must have a valid default key? If that is the case, that isn't possible since this PR does not change the version number, thus all wallets will have to have a valid default key.

Furthermore, I believe the corruption that we are seeing in the wild is a result of -savagewallet not passing the default key through to the new wallet file, not because of hardware corruption or the like.

@achow101
Copy link
Member Author

achow101 commented Aug 8, 2017

I changed the detection back to CWallet::LoadWallet() since I think it is more logical to be there. That was my original plan, I just couldn't figure out how since I forgot that it has access to mapKeys, etc.

@jnewbery
Copy link
Contributor

Looks good. Tested ACK b8aa9729a5dbeb883857b14918aa6c6d2b71eea4 (although needs rebase)

@jonasschnelli
Copy link
Contributor

Needs rebase.

@achow101
Copy link
Member Author

rebased

@jnewbery
Copy link
Contributor

utACK 1a7bc5aac60a37b6f9f0632546467d716a3fd5e4

@sipa
Copy link
Member

sipa commented Aug 15, 2017

There seems to be a problem in keypool_topup.py.

Removes vchDefaultKey which was only used for first run detection.
Improves wallet first run detection by checking to see if any keys
were read from the database.

This will now also check for a valid defaultkey for backwards
compatibility reasons and to check for any corruption.

Keys will stil be generated on the first one, but there won't be
any shown in the address book as was previously done.
@achow101
Copy link
Member Author

Fixed that I think. It's an off-by-one since default key is no longer created and consuming a key from the keypool.

@laanwj
Copy link
Member

laanwj commented Aug 16, 2017

since default key is no longer created and consuming a key from the keypool.

Finally :-)

@jonasschnelli
Copy link
Contributor

Great to see the default key vanish...
utACK e53615b

@laanwj laanwj merged commit e53615b into bitcoin:master Aug 18, 2017
laanwj added a commit that referenced this pull request Aug 18, 2017
… detection

e53615b Remove vchDefaultKey and have better first run detection (Andrew Chow)

Pull request description:

  Removes vchDefaultKey which was only used for first run detection. Improves wallet first run detection by checking to see if any keys were read from the database.

  This also fixes a (rather contrived) case where an encrypted non-HD wallet has corruption such that the default key is no longer valid and is loaded into a Core version that supports HD wallets. This causes a runtime exception since a new hd master key is generated as the software believes the wallet file is newly created but cannot add the generated key to the wallet since it is encrypted. I was only able to replicate this error by creating a non-hd wallet, encrypting it, then editing the wallet using `db_dump` and `db_load` before loading the wallet with hd enabled. This problem has been reported by [two](https://bitcointalk.org/index.php?topic=1993244.0) [users](https://bitcointalk.org/index.php?topic=1746976.msg17511261#msg17511261) so it is something that can happen, although that raises the question of "what corrupted the default key".

  ~P.S. I don't know what's up with the whitespace changes. I think my text editor is doing something stupid but I don't think those are important enough to attempt undoing them.~ Undid those

Tree-SHA512: 63b485f356566e8ffa033ad9b7101f7f6b56372b29ec2a43b947b0eeb1ada4c2cfe24740515d013aedd5f51aa1890dfbe499d2c5c062fc1b5d272324728a7d55
@achow101 achow101 deleted the remove-defaultkey branch August 29, 2017 15:28
@laanwj laanwj removed this from the 0.15.1 milestone Sep 5, 2017
@laanwj
Copy link
Member

laanwj commented Sep 5, 2017

Removing 0.15.1 milestone, as it can create problems at the moment when opening a new wallet with older versions.

furszy added a commit to furszy/bitcoin-core that referenced this pull request Sep 30, 2019
…emove vchDefaultKey and have better first run detection".

[Wallet][Startup][DB][Backport] Don't create any default address
random-zebra added a commit to PIVX-Project/PIVX that referenced this pull request Oct 11, 2019
…m wallet.h

83d8d00 [Trivial] Init.cpp, instead of use LogPrint, use error. (furszy)
6637fd1 [Wallet][Startup][DB][Backport] bitcoin#10952 BTC back port. Named "Remove vchDefaultKey and have better first run detection". (furszy)

Pull request description:

  Back port from bitcoin#10952.

  This PR is on top of #1029 because of both PRs modifying `init.cpp`.
  Check only the last commit.

ACKs for top commit:
  random-zebra:
    ACK 83d8d00
  Warrows:
    ACK 83d8d00
  Fuzzbawls:
    ACK 83d8d00

Tree-SHA512: 0e05f7f80897a421c541580abbd10af37db083a43c6edf49f61e8d1977c58c6e486b1f17e0eac4fb4e8257200eea3e04b5510321cb4484e110f466f5a5132151
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 31, 2020
…rst run detection

e53615b Remove vchDefaultKey and have better first run detection (Andrew Chow)

Pull request description:

  Removes vchDefaultKey which was only used for first run detection. Improves wallet first run detection by checking to see if any keys were read from the database.

  This also fixes a (rather contrived) case where an encrypted non-HD wallet has corruption such that the default key is no longer valid and is loaded into a Core version that supports HD wallets. This causes a runtime exception since a new hd master key is generated as the software believes the wallet file is newly created but cannot add the generated key to the wallet since it is encrypted. I was only able to replicate this error by creating a non-hd wallet, encrypting it, then editing the wallet using `db_dump` and `db_load` before loading the wallet with hd enabled. This problem has been reported by [two](https://bitcointalk.org/index.php?topic=1993244.0) [users](https://bitcointalk.org/index.php?topic=1746976.msg17511261#msg17511261) so it is something that can happen, although that raises the question of "what corrupted the default key".

  ~P.S. I don't know what's up with the whitespace changes. I think my text editor is doing something stupid but I don't think those are important enough to attempt undoing them.~ Undid those

Tree-SHA512: 63b485f356566e8ffa033ad9b7101f7f6b56372b29ec2a43b947b0eeb1ada4c2cfe24740515d013aedd5f51aa1890dfbe499d2c5c062fc1b5d272324728a7d55
UdjinM6 added a commit to dashpay/dash that referenced this pull request Feb 4, 2020
…rst run… (#3319)

* Merge bitcoin#10952: [wallet] Remove vchDefaultKey and have better first run detection

e53615b Remove vchDefaultKey and have better first run detection (Andrew Chow)

Pull request description:

  Removes vchDefaultKey which was only used for first run detection. Improves wallet first run detection by checking to see if any keys were read from the database.

  This also fixes a (rather contrived) case where an encrypted non-HD wallet has corruption such that the default key is no longer valid and is loaded into a Core version that supports HD wallets. This causes a runtime exception since a new hd master key is generated as the software believes the wallet file is newly created but cannot add the generated key to the wallet since it is encrypted. I was only able to replicate this error by creating a non-hd wallet, encrypting it, then editing the wallet using `db_dump` and `db_load` before loading the wallet with hd enabled. This problem has been reported by [two](https://bitcointalk.org/index.php?topic=1993244.0) [users](https://bitcointalk.org/index.php?topic=1746976.msg17511261#msg17511261) so it is something that can happen, although that raises the question of "what corrupted the default key".

  ~P.S. I don't know what's up with the whitespace changes. I think my text editor is doing something stupid but I don't think those are important enough to attempt undoing them.~ Undid those

Tree-SHA512: 63b485f356566e8ffa033ad9b7101f7f6b56372b29ec2a43b947b0eeb1ada4c2cfe24740515d013aedd5f51aa1890dfbe499d2c5c062fc1b5d272324728a7d55

* Update src/wallet/wallet.cpp

Co-Authored-By: UdjinM6 <[email protected]>

Co-authored-by: Wladimir J. van der Laan <[email protected]>
Co-authored-by: UdjinM6 <[email protected]>
KolbyML pushed a commit to KolbyML/bitcoin that referenced this pull request Nov 21, 2020
…emove vchDefaultKey and have better first run detection".

[Wallet][Startup][DB][Backport] Don't create any default address
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants