Skip to content

Commit d26c3cc

Browse files
committed
wallet: bugfix, load wallet with an unknown descriptor cause fatal error
If the descriptor entry is unrecognized/corrupt, the unserialization fails and `LoadWallet` instead of stop there and return the error, continues reading all the db records. As other records tied to the unrecognized/corrupted descriptor are scanned, a fatal error is thrown.
1 parent 5291933 commit d26c3cc

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/wallet/wallet.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -2819,8 +2819,12 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
28192819
warnings.push_back(strprintf(_("Error reading %s! Transaction data may be missing or incorrect."
28202820
" Rescanning wallet."), walletFile));
28212821
rescan_required = true;
2822-
}
2823-
else {
2822+
} else if (nLoadWalletRet == DBErrors::UNKNOWN_DESCRIPTOR) {
2823+
error = strprintf(_("Unrecognized descriptor found. Loading wallet %s\n\n"
2824+
"The wallet might had been created on a newer version.\n"
2825+
"Please try running the latest software version.\n"), walletFile);
2826+
return nullptr;
2827+
} else {
28242828
error = strprintf(_("Error loading %s"), walletFile);
28252829
return nullptr;
28262830
}

src/wallet/walletdb.cpp

+21-6
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ class CWalletScanState {
314314
std::map<std::pair<uint256, CKeyID>, std::pair<CPubKey, std::vector<unsigned char>>> m_descriptor_crypt_keys;
315315
std::map<uint160, CHDChain> m_hd_chains;
316316
bool tx_corrupt{false};
317+
bool descriptor_unknown{false};
317318

318319
CWalletScanState() = default;
319320
};
@@ -627,7 +628,13 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
627628
uint256 id;
628629
ssKey >> id;
629630
WalletDescriptor desc;
630-
ssValue >> desc;
631+
try {
632+
ssValue >> desc;
633+
} catch (const std::ios_base::failure& e) {
634+
strErr = e.what();
635+
wss.descriptor_unknown = true;
636+
return false;
637+
}
631638
if (wss.m_descriptor_caches.count(id) == 0) {
632639
wss.m_descriptor_caches[id] = DescriptorCache();
633640
}
@@ -767,6 +774,12 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
767774
DBErrors result = DBErrors::LOAD_OK;
768775

769776
LOCK(pwallet->cs_wallet);
777+
778+
// Last client version to open this wallet
779+
int last_client = CLIENT_VERSION;
780+
bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
781+
pwallet->WalletLogPrintf("Wallet file version = %d, last client version = %d\n", pwallet->GetVersion(), last_client);
782+
770783
try {
771784
int nMinVersion = 0;
772785
if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
@@ -832,6 +845,13 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
832845
// Set tx_corrupt back to false so that the error is only printed once (per corrupt tx)
833846
wss.tx_corrupt = false;
834847
result = DBErrors::CORRUPT;
848+
} else if (wss.descriptor_unknown) {
849+
strErr = strprintf("Error: Unrecognized descriptor found in wallet %s. ", pwallet->GetName());
850+
strErr += (last_client > CLIENT_VERSION) ? "The wallet might had been created on a newer version. " :
851+
"The database might be corrupted or the software version is not compatible with one of your wallet descriptors. ";
852+
strErr += "Please try running the latest software version";
853+
pwallet->WalletLogPrintf("%s\n", strErr);
854+
return DBErrors::UNKNOWN_DESCRIPTOR;
835855
} else {
836856
// Leave other errors alone, if we try to fix them we might make things worse.
837857
fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
@@ -884,11 +904,6 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
884904
if (result != DBErrors::LOAD_OK)
885905
return result;
886906

887-
// Last client version to open this wallet
888-
int last_client = CLIENT_VERSION;
889-
bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
890-
pwallet->WalletLogPrintf("Wallet file version = %d, last client version = %d\n", pwallet->GetVersion(), last_client);
891-
892907
pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n",
893908
wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records);
894909

src/wallet/walletdb.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ enum class DBErrors
5151
EXTERNAL_SIGNER_SUPPORT_REQUIRED,
5252
LOAD_FAIL,
5353
NEED_REWRITE,
54-
NEED_RESCAN
54+
NEED_RESCAN,
55+
UNKNOWN_DESCRIPTOR
5556
};
5657

5758
namespace DBKeys {

0 commit comments

Comments
 (0)