wallet: repair a truncated hdchain record instead of refusing the wallet
A build predating VERSION_HD_TRANSPARENT rewrites the hdchain record with only its four base fields while leaving nVersion at whatever it read. Our version-gated reads then run off the end of the stream, ReadKeyValue turns the throw into fHDChainRead=false, and LoadWallet escalates that to DB_CORRUPT. Verified against the real v1.0.2-2b011d6ee release binary: a dev wallet, opened once by v1.0.2 and given a single new sapling address, came back to Error reading wallet database: hdchain record is corrupt Error loading wallet.dat: HD seed present but the hdchain record is missing or corrupt Error loading wallet.dat: Wallet corrupted Nothing is actually lost there -- the same test showed the seed phrase restoring the full balance, the autoshield destination, and even the address v1.0.2 had generated -- but the user is shown "Wallet corrupted" with no hint of that. Recover instead, for a v1 or v2 record. Everything derivation depends on is either in the four-field prefix or in the separate hdseed record, and the trailing counters are self-healing: DeriveNewChildKey and GenerateNewSaplingZKey both skip indices whose key the wallet already holds, so restarting a counter at 0 re-walks past existing keys rather than reissuing them. Read the prefix from an untouched copy of the stream, default the missing tail, log it, and rewrite the record in full form so the next load is clean. A record claiming nVersion >= VERSION_HD_MNEMONIC still fails loud: that flag selects the derivation input, so guessing it wrong yields a different key tree in silence. No wallet this code has written can be in that state -- every InstallHDSeed call site passes fMnemonic=false -- so the branch is defensive only. Also say what to do about it. Both the log line and the init error now name the remedy (move wallet.dat aside, restart with -mnemonic and -rescan) rather than stopping at "Wallet corrupted". Verified on regtest against the real v1.0.2 binary, 14/14: the round-trip that previously ended in "Wallet corrupted" now loads, logs the repair and the rewrite, keeps the balance, the autoshield destination and v1.0.2's own address, still issues distinct fresh t-addresses after the counter reset, needs no repair on the second load, and remains fully recoverable from the seed phrase. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+4
-1
@@ -2271,7 +2271,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
if (nLoadWalletRet != DB_LOAD_OK)
|
||||
{
|
||||
if (nLoadWalletRet == DB_CORRUPT)
|
||||
strErrors << _("Error loading wallet.dat: Wallet corrupted") << "\n";
|
||||
strErrors << _("Error loading wallet.dat: Wallet corrupted. If this wallet was last opened "
|
||||
"by an older version, move wallet.dat aside and restore from your seed "
|
||||
"phrase with -mnemonic=\"<your seed phrase>\" -rescan (see debug.log for "
|
||||
"the specific record at fault).") << "\n";
|
||||
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
|
||||
{
|
||||
string msg(_("Warning: error reading wallet.dat! All keys read correctly, but transaction data"
|
||||
|
||||
Reference in New Issue
Block a user