Skip to content

Commit

Permalink
wallet: bugfix, load wallet with an unknown descriptor cause fatal error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
furszy committed Sep 9, 2022
1 parent 5291933 commit d26c3cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/wallet/wallet.cpp
Expand Up @@ -2819,8 +2819,12 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
warnings.push_back(strprintf(_("Error reading %s! Transaction data may be missing or incorrect."
" Rescanning wallet."), walletFile));
rescan_required = true;
}
else {
} else if (nLoadWalletRet == DBErrors::UNKNOWN_DESCRIPTOR) {
error = strprintf(_("Unrecognized descriptor found. Loading wallet %s\n\n"
"The wallet might had been created on a newer version.\n"
"Please try running the latest software version.\n"), walletFile);
return nullptr;
} else {
error = strprintf(_("Error loading %s"), walletFile);
return nullptr;
}
Expand Down
27 changes: 21 additions & 6 deletions src/wallet/walletdb.cpp
Expand Up @@ -314,6 +314,7 @@ class CWalletScanState {
std::map<std::pair<uint256, CKeyID>, std::pair<CPubKey, std::vector<unsigned char>>> m_descriptor_crypt_keys;
std::map<uint160, CHDChain> m_hd_chains;
bool tx_corrupt{false};
bool descriptor_unknown{false};

CWalletScanState() = default;
};
Expand Down Expand Up @@ -627,7 +628,13 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
uint256 id;
ssKey >> id;
WalletDescriptor desc;
ssValue >> desc;
try {
ssValue >> desc;
} catch (const std::ios_base::failure& e) {
strErr = e.what();
wss.descriptor_unknown = true;
return false;
}
if (wss.m_descriptor_caches.count(id) == 0) {
wss.m_descriptor_caches[id] = DescriptorCache();
}
Expand Down Expand Up @@ -767,6 +774,12 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
DBErrors result = DBErrors::LOAD_OK;

LOCK(pwallet->cs_wallet);

// Last client version to open this wallet
int last_client = CLIENT_VERSION;
bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
pwallet->WalletLogPrintf("Wallet file version = %d, last client version = %d\n", pwallet->GetVersion(), last_client);

try {
int nMinVersion = 0;
if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
Expand Down Expand Up @@ -832,6 +845,13 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
// Set tx_corrupt back to false so that the error is only printed once (per corrupt tx)
wss.tx_corrupt = false;
result = DBErrors::CORRUPT;
} else if (wss.descriptor_unknown) {
strErr = strprintf("Error: Unrecognized descriptor found in wallet %s. ", pwallet->GetName());
strErr += (last_client > CLIENT_VERSION) ? "The wallet might had been created on a newer version. " :
"The database might be corrupted or the software version is not compatible with one of your wallet descriptors. ";
strErr += "Please try running the latest software version";
pwallet->WalletLogPrintf("%s\n", strErr);
return DBErrors::UNKNOWN_DESCRIPTOR;
} else {
// Leave other errors alone, if we try to fix them we might make things worse.
fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
Expand Down Expand Up @@ -884,11 +904,6 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
if (result != DBErrors::LOAD_OK)
return result;

// Last client version to open this wallet
int last_client = CLIENT_VERSION;
bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
pwallet->WalletLogPrintf("Wallet file version = %d, last client version = %d\n", pwallet->GetVersion(), last_client);

pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n",
wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records);

Expand Down
3 changes: 2 additions & 1 deletion src/wallet/walletdb.h
Expand Up @@ -51,7 +51,8 @@ enum class DBErrors
EXTERNAL_SIGNER_SUPPORT_REQUIRED,
LOAD_FAIL,
NEED_REWRITE,
NEED_RESCAN
NEED_RESCAN,
UNKNOWN_DESCRIPTOR
};

namespace DBKeys {
Expand Down

0 comments on commit d26c3cc

Please sign in to comment.