Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Current behaviour
When starting bitcoind with an old wallet (created July 2019, probably v0.17.x or v0.18.x) the log records 2 errors and one warning that are not self-explanatory.
Might be related to #19051
Expected behaviour
No warning/errors are shown or the errors/warnings are more detailed as to what's going on.
Steps to reproduce
Run v26.0 with a very old wallet.dat file. I wasn't able to reproduce this with a wallet.dat file created in v0.12.x, v0.16.3 or v0.18.x. I'm not sure how to reproduce this without uploading the specific wallet.dat file (I'm not able to, sorry).
Relevant log output
Log from v26.0.0
2023-12-18T13:24:00Z Bitcoin Core version v26.0.0 (release build)
2023-12-18T13:24:00Z Using the 'x86_shani(1way,2way)' SHA256 implementation
2023-12-18T13:24:00Z Using RdSeed as an additional entropy source
2023-12-18T13:24:00Z Using RdRand as an additional entropy source
[..]
2023-12-18T13:24:00Z Using at most 125 automatic connections (1024 file descriptors available)
2023-12-18T13:24:00Z Using 16 MiB out of 16 MiB requested for signature cache, able to store 524288 elements
2023-12-18T13:24:00Z Using 16 MiB out of 16 MiB requested for script execution cache, able to store 524288 elements
2023-12-18T13:24:00Z Script verification uses 7 additional threads
2023-12-18T13:24:00Z scheduler thread start
[..]
2023-12-18T13:24:00Z Using wallet directory /home/foobar/.bitcoin
2023-12-18T13:24:00Z init message: Verifying wallet(s)…
2023-12-18T13:24:00Z Using BerkeleyDB version Berkeley DB 4.8.30: (April 9, 2010)
2023-12-18T13:24:00Z Using wallet /home/foobar/.bitcoin/wallet.dat
2023-12-18T13:24:00Z BerkeleyEnvironment::Open: LogDir=/home/foobar/.bitcoin/database ErrorFile=/home/foobar/.bitcoin/db.log
[..]
2023-12-18T13:24:00Z init message: Loading block index…
2023-12-18T13:24:00Z Assuming ancestors of block 00000000000000000001a0a448d6cf2546b06801389cc030b2b18c6491266815 have valid signatures.
2023-12-18T13:24:00Z Setting nMinimumChainWork=000000000000000000000000000000000000000052b2559353df4117b7348b64
2023-12-18T13:24:00Z Opening LevelDB in /home/foobar/.bitcoin/blocks/index
2023-12-18T13:24:00Z Opened LevelDB successfully
2023-12-18T13:24:00Z Using obfuscation key for /home/foobar/.bitcoin/blocks/index: 0000000000000000
2023-12-18T13:24:03Z LoadBlockIndexDB: last block file = 3945
2023-12-18T13:24:03Z LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=51, size=84327429, heights=817322...817411, time=2023-11-18...2023-11-19)
2023-12-18T13:24:03Z Checking all blk files are present...
2023-12-18T13:24:04Z Initializing chainstate Chainstate [ibd] @ height -1 (null)
2023-12-18T13:24:04Z Opening LevelDB in /home/foobar/.bitcoin/chainstate
2023-12-18T13:24:04Z Opened LevelDB successfully
2023-12-18T13:24:04Z Using obfuscation key for /home/foobar/.bitcoin/chainstate: 9735fc6504867afd
2023-12-18T13:24:04Z Loaded best chain: hashBestChain=0000000000000000000290f359a90a5d57160fd27954703351c6f1bf838941e0 height=816997 date=2023-11-16T10:24:28Z progress=0.983619
2023-12-18T13:24:04Z Opening LevelDB in /home/foobar/.bitcoin/chainstate
2023-12-18T13:24:04Z Opened LevelDB successfully
2023-12-18T13:24:04Z Using obfuscation key for /home/foobar/.bitcoin/chainstate: 9735fc6504867afd
2023-12-18T13:24:04Z [Chainstate [ibd] @ height 816997 (0000000000000000000290f359a90a5d57160fd27954703351c6f1bf838941e0)] resized coinsdb cache to 8.0 MiB
2023-12-18T13:24:04Z [Chainstate [ibd] @ height 816997 (0000000000000000000290f359a90a5d57160fd27954703351c6f1bf838941e0)] resized coinstip cache to 440.0 MiB
2023-12-18T13:24:04Z init message: Verifying blocks…
2023-12-18T13:24:04Z Verifying last 6 blocks at level 3
2023-12-18T13:24:04Z Verification progress: 0%
2023-12-18T13:24:05Z Verification progress: 16%
2023-12-18T13:24:05Z Verification progress: 33%
2023-12-18T13:24:05Z Verification progress: 50%
2023-12-18T13:24:05Z Verification progress: 66%
2023-12-18T13:24:05Z Verification progress: 83%
2023-12-18T13:24:06Z Verification progress: 99%
2023-12-18T13:24:06Z Verification: No coin database inconsistencies in last 6 blocks (26158 transactions)
2023-12-18T13:24:06Z block index 5148ms
2023-12-18T13:24:06Z init message: Loading wallet…
2023-12-18T13:24:06Z BerkeleyEnvironment::Open: LogDir=/home/foobar/.bitcoin/database ErrorFile=/home/foobar/.bitcoin/db.log
2023-12-18T13:24:06Z [default wallet] Wallet file version = 10500, last client version = 220000
2023-12-18T13:24:06Z [default wallet] Error reading wallet database: keymeta found with unexpected path
2023-12-18T13:24:06Z [default wallet] Error reading wallet database: keymeta found with unexpected path
2023-12-18T13:24:06Z [default wallet] Legacy Wallet Keys: 657 plaintext, 0 encrypted, 657 w/ metadata, 657 total.
2023-12-18T13:24:06Z [default wallet] Descriptors: 0, Descriptor Keys: 0 plaintext, 0 encrypted, 0 total.
2023-12-18T13:24:06Z [default wallet] Wallet completed loading in 8ms
2023-12-18T13:24:06Z [default wallet] setKeyPool.size() = 600
2023-12-18T13:24:06Z [default wallet] mapWallet.size() = 51
2023-12-18T13:24:06Z [default wallet] m_address_book.size() = 100
2023-12-18T13:24:06Z Warning: Error reading /home/foobar/.bitcoin/wallet.dat! All keys read correctly, but transaction data or address metadata may be missing or incorrect.
2023-12-18T13:24:06Z Setting NODE_NETWORK on non-prune mode
2023-12-18T13:24:06Z block tree size = 821776
2023-12-18T13:24:06Z nBestHeight = 816997
2023-12-18T13:24:06Z initload thread start
2023-12-18T13:24:06Z torcontrol thread start
2023-12-18T13:24:06Z Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
2023-12-18T13:24:06Z initload thread exit
2023-12-18T13:24:06Z Bound to 127.0.0.1:8334
2023-12-18T13:24:06Z Bound to [::]:8333
2023-12-18T13:24:06Z Bound to 0.0.0.0:8333
2023-12-18T13:24:06Z Loaded 1 addresses from "anchors.dat"
2023-12-18T13:24:06Z 1 block-relay-only anchors will be tried for connections.
2023-12-18T13:24:06Z init message: Starting network threads…
[..]
Log from v25.1.0
The error/warning is slightly different.
Instead of
Warning: Error reading /home/foobar/.bitcoin/wallet.dat! All keys read correctly, but transaction data or address metadata may be missing or incorrect.
it logs
Warning: Error reading /home/foobar/.bitcoin/wallet.dat! All keys read correctly, but transaction data or address book entries might be missing or incorrect.
How did you obtain Bitcoin Core
Pre-built binaries
What version of Bitcoin Core are you using?
v26.0
Operating system and version
Ubuntu 22.04 LTS
Machine specifications
No response
Activity
achow101 commentedon Dec 18, 2023
The specific thing causing this error would be the lines:
This means that the stored metadata entries for a 2 keys has derivation paths of an unexpected length (
!= 3). This error should be benign, although I think it's also erroneous since I'm pretty sure it is possible to import keys with derivation path information that would not match the standard pattern the loading code is checking for.Edit: Actually, such imports would not reach this code. Rather the only way that it is possible to get these errors is if that wallet got corrupted.
c0deright commentedon Dec 19, 2023
Ok, thank you very much. This is a dedicated test machine and the error did not occur on production machines.
I'll try dumping the wallet.dat file and importing the keys into a new wallet.
c0deright commentedon Dec 19, 2023
I did
dumpwalletand the file created looks like this:The 6th key looks odd with it being one character short.
One key with has tag
inactivehdseed=1.Might this be the cause of the error messages?
The rest looks good, being
change=1,reserve=1and one beinghdseed=1achow101 commentedon Dec 19, 2023
Do you see any
hdkeypaththat doesn't follow the patternm/a'/b'/c'?c0deright commentedon Dec 19, 2023
Oh, now that you told me where to look ... there are indeed two strange looking key paths.
All but two are in the format
m/a'/b'/c', e.g.m/0'/1'/289'orm/0'/1'/4'. Most are distinct but eleven key paths are found twice in the dump. And then there are two with a very odd path:m/0'/1'/299'/0'/1'/300'andm/0'/0'/299'/0'/0'/300'.I'm sure our devs never tested with
derivation pathsbut it is possible that on that particular machine some keys from bitcoin forks were imported, e.g. BTG, BSV or BCH. I will talk to our devs after the holidays to get some feedback as to where these two keys originated so it's documented here in case someone has the same warnings pop up and ends up here.Thanks very much, @achow101.
achow101 commentedon Dec 19, 2023
Since there is an
inactivehdseed, that means that the seed has been rotated, probably by encrypting the wallet. So these duplicated paths are expected. One of the keys is derived from the inactive seed, and the other from the current active seed.This lines up with the errors in the debug.log.
These derivation paths look like the paths for a key got combined with the derivation path for the next key to be derived. I've seen this before, but I think only in PR branches, not after being merged. I suspect that these keys were automatically generated and the weird derivation path is the result of a bug in key metadata upgrading code.
Are there any keys with any of the derivation paths
m/0'/1'/299',m/0'/1'/300',m/0'/0'/299', orm/0'/0'/300'?If you are able to, try loading the wallet in 0.17 or earlier and then doing
dumpwallet. If you lookup these two addresses (with the weird derivation paths) in the dump, what derivation paths do they have?It would also be useful to see the actual keymeta records themselves. You can get a dump of raw data stored in the wallet file by using the
bitcoin-wallettool'sdumpcommand (e.g.bitcoin-wallet -dumpfile=dump.txt -wallet=mywallet dump). This will dump all of the records in the database as hex bytes to a text file. Within that file, search for the strings00000080000000802b01008000000080000000802c010080and00000080010000802b01008000000080010000802c010080. These are the derivation paths as they would be stored in the wallet file. Then you can copy the lines with those strings and paste them here. Those lines will be the entire metadata records for these two keys. These are safe to share as they will only contain the public key, a version number, the time the key was created, the seed's id (hash of the seed), and the derivation path.c0deright commentedon Dec 19, 2023
Not encrypted but a
dumpwalletdump from a different wallet.dat (might be from a fork) was imported withimportwalletinto the already existingwallet.datwhich most likely made the old hdseed inactive and the imported one active (or vice versa).Only these two:
I did as you described and
dumpwalletwith 0.17.0 giveshdkeypath=m/0'/1'/300'for the address withhdkeypath=m/0'/1'/299'/0'/1'/300'in v26.0hdkeypath=m/0'/0'/300'for the address withhdkeypath=m/0'/0'/299'/0'/0'/300'in v26.0Both addresses/keys were created on
2021-10-28according to thedumpwalletfile and are of typechange=1c0deright commentedon Dec 19, 2023
bitcoin-walletdoesn't create any file:achow101 commentedon Dec 19, 2023
Oh, that's a bug, Should be simple to fix. In any case, I don't think that dump will be needed since the
dumpwalletoutput should be enough to have an idea of what was stored.The
dumpwalletfile has a xprv at the top. Can you try deriving the 4 keys at the derivation paths I mentioned above and see which match the lines with the weird derivation paths? I'm pretty sure the wallet just stored the wrong derivation paths somehow.Any idea what version was running on that date?
c0deright commentedon Dec 19, 2023
Will try tomorrow using https://github.com/dan-da/hd-wallet-derive
c0deright commentedon Dec 20, 2023
derived keys
I used https://github.com/dan-da/hd-wallet-derive to derive
m/0'/0'/0'tom/0'/0'/300'm/0'/1'/0'tom/0'/1'/300'from the xprv like so:
Then I took all the private keys from the
walletdumpoutput and grepped for the keys in the 2.txtfiles and only found 11 matches for the following paths:m/0'/0'/0'tom/0'/0'/5'inxprv-0.txtm/0'/1'/0'tom/0'/1'/4'inxprv-1.txtThere are 623 keys in the
walletdumpoutput with ahdkeypathinfo but only 11 can be derived from thexprv? That's odd to me.Edit
Timeline according to company communication history
2021-10-28T09:48Z
We upgraded from
v0.19.1tov22.0on that dedicated test server.2021-10-28T11:42Z
getwalletinfo{ "walletname": "", "walletversion": 169900, "format": "bdb", "balance": 0.00032451, "unconfirmed_balance": 0.00000000, "immature_balance": 0.00000000, "txcount": 50, "keypoololdest": 1576677994, "keypoolsize": 300, "hdseedid": "8136................................49b9", "keypoolsize_hd_internal": 300, "paytxfee": 0.00000000, "private_keys_enabled": true, "avoid_reuse": false, "scanning": false, "descriptors": false }2021-10-28T13:33Z
One key is generated/imported:
2021-10-28T14:05Z
15 keys are generated/imported, among them two with the bogus keypath:
bitcoin-wallet
I compiled
v26.0with patch from #29117 and usedbitcoin-walletto dump thewallet.datintobitcoin-wallet.dumpc0deright commentedon Dec 20, 2023
Let me know if I can help with debugging this any further.
achow101 commentedon Dec 20, 2023
I was able to replicate the bug. I believe this is only an issue with 0.21.x and 22.x as the bug was fixed by #23304 which was first released in 23.0.
The issue occurs when inactive key chains are topped up. There was an off-by-one where loading would erroneously find the last index derived rather than the next index to be derived (last index + 1), and so it would end up re-deriving that last index, which I think is what causes the weird derivation path. Specifically,
DeriveNewChildKeytakes a metadata object and overwrites thehdKeypathstring but appends tokey_origin.path, so if it gets the same object twice, then it would overwritehdKeypathwith the path it just derived, but append that path tokey_origin.path. This results in the derivation path that looks like it's two derivation paths concatenated, because that's what it is.The exact mechanism is not entirely clear to me though since
DeriveNewChildKeyis always called byGenerateNewKeywhich always provides it with a new metadata object. However, empirically, I can replicate the issue on 22.x, but the same procedure does not have the issue on 23.x.It should be possible for the wallet to detect when this error had occurred previously and be able to fix it automatically. I'll see if I can put together a fix.
This is expected as the hd seed has been rotated (most commonly by encrypting the wallet). The one shown at the top will be for the current seed, while the wallet will still store and have metadata for all of the keys generated with the previous seed. We can further see that the keys with the odd derivation paths are generated from a seed whose id does not match the active one, which is consistent with the way that I replicated the issue.
c0deright commentedon Dec 20, 2023
Thanks for your thorough explanation of this bug!
achow101 commentedon Dec 20, 2023
#29124 should automatically fix the bad metadata. Can you try running that?
c0deright commentedon Dec 20, 2023
Looks good:
No warning/error regarding keymeta. The
WalletLogPrintfcall should end with\n, though.Did a diff between v26.0.0 RPC
dumpwalletand your version.Looks fixed for me.
Merge bitcoin/bitcoin#29117: wallettool: Always be able to dump a wal…
achow101 commentedon Apr 21, 2025
Apparently migrating the legacy wallet to descriptors is also a way to fix this issue.