Skip to content

Commit 2636844

Browse files
committed
walletdb: Remove loading code where the database is iterated
Instead of iterating the database to load the wallet, we now load particular kinds of records in an order that we want them to be loaded. So it is no longer necessary to iterate the entire database to load the wallet.
1 parent cd211b3 commit 2636844

File tree

1 file changed

+0
-101
lines changed

1 file changed

+0
-101
lines changed

src/wallet/walletdb.cpp

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,6 @@ bool WalletBatch::EraseLockedUTXO(const COutPoint& output)
298298
return EraseIC(std::make_pair(DBKeys::LOCKED_UTXO, std::make_pair(output.hash, output.n)));
299299
}
300300

301-
class CWalletScanState {
302-
public:
303-
unsigned int m_unknown_records{0};
304-
305-
CWalletScanState() = default;
306-
};
307-
308301
bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr)
309302
{
310303
LOCK(pwallet->cs_wallet);
@@ -453,61 +446,6 @@ bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr)
453446
return true;
454447
}
455448

456-
static bool
457-
ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
458-
CWalletScanState &wss, std::string& strType, std::string& strErr) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
459-
{
460-
try {
461-
// Unserialize
462-
// Taking advantage of the fact that pair serialization
463-
// is just the two items serialized one after the other
464-
ssKey >> strType;
465-
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS) && DBKeys::LEGACY_TYPES.count(strType) > 0) {
466-
return true;
467-
}
468-
if (strType == DBKeys::NAME) {
469-
} else if (strType == DBKeys::PURPOSE) {
470-
} else if (strType == DBKeys::TX) {
471-
} else if (strType == DBKeys::WATCHS) {
472-
} else if (strType == DBKeys::KEY) {
473-
} else if (strType == DBKeys::MASTER_KEY) {
474-
} else if (strType == DBKeys::CRYPTED_KEY) {
475-
} else if (strType == DBKeys::KEYMETA) {
476-
} else if (strType == DBKeys::WATCHMETA) {
477-
} else if (strType == DBKeys::DEFAULTKEY) {
478-
} else if (strType == DBKeys::POOL) {
479-
} else if (strType == DBKeys::CSCRIPT) {
480-
} else if (strType == DBKeys::ORDERPOSNEXT) {
481-
} else if (strType == DBKeys::DESTDATA) {
482-
} else if (strType == DBKeys::HDCHAIN) {
483-
} else if (strType == DBKeys::OLD_KEY) {
484-
} else if (strType == DBKeys::ACTIVEEXTERNALSPK || strType == DBKeys::ACTIVEINTERNALSPK) {
485-
} else if (strType == DBKeys::WALLETDESCRIPTOR) {
486-
} else if (strType == DBKeys::WALLETDESCRIPTORCACHE) {
487-
} else if (strType == DBKeys::WALLETDESCRIPTORLHCACHE) {
488-
} else if (strType == DBKeys::WALLETDESCRIPTORKEY) {
489-
} else if (strType == DBKeys::WALLETDESCRIPTORCKEY) {
490-
} else if (strType == DBKeys::LOCKED_UTXO) {
491-
} else if (strType != DBKeys::BESTBLOCK && strType != DBKeys::BESTBLOCK_NOMERKLE &&
492-
strType != DBKeys::MINVERSION && strType != DBKeys::ACENTRY &&
493-
strType != DBKeys::VERSION && strType != DBKeys::SETTINGS &&
494-
strType != DBKeys::FLAGS) {
495-
wss.m_unknown_records++;
496-
}
497-
} catch (const std::exception& e) {
498-
if (strErr.empty()) {
499-
strErr = e.what();
500-
}
501-
return false;
502-
} catch (...) {
503-
if (strErr.empty()) {
504-
strErr = "Caught unknown exception in ReadKeyValue";
505-
}
506-
return false;
507-
}
508-
return true;
509-
}
510-
511449
static DBErrors LoadMinVersion(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
512450
{
513451
AssertLockHeld(pwallet->cs_wallet);
@@ -1198,8 +1136,6 @@ static DBErrors LoadDecryptionKeys(CWallet* pwallet, DatabaseBatch& batch) EXCLU
11981136

11991137
DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
12001138
{
1201-
CWalletScanState wss;
1202-
bool fNoncriticalErrors = false;
12031139
DBErrors result = DBErrors::LOAD_OK;
12041140
bool any_unordered = false;
12051141
std::vector<uint256> upgraded_txs;
@@ -1246,49 +1182,12 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
12461182

12471183
// Load decryption keys
12481184
result = std::max(LoadDecryptionKeys(pwallet, *m_batch), result);
1249-
1250-
// Get cursor
1251-
std::unique_ptr<DatabaseCursor> cursor = m_batch->GetNewCursor();
1252-
if (!cursor)
1253-
{
1254-
pwallet->WalletLogPrintf("Error getting wallet database cursor\n");
1255-
return DBErrors::CORRUPT;
1256-
}
1257-
1258-
while (true)
1259-
{
1260-
// Read next record
1261-
DataStream ssKey{};
1262-
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
1263-
DatabaseCursor::Status status = cursor->Next(ssKey, ssValue);
1264-
if (status == DatabaseCursor::Status::DONE) {
1265-
break;
1266-
} else if (status == DatabaseCursor::Status::FAIL) {
1267-
cursor.reset();
1268-
pwallet->WalletLogPrintf("Error reading next record from wallet database\n");
1269-
return DBErrors::CORRUPT;
1270-
}
1271-
1272-
// Try to be tolerant of single corrupt records:
1273-
std::string strType, strErr;
1274-
if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
1275-
{
1276-
// Leave other errors alone, if we try to fix them we might make things worse.
1277-
fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
1278-
}
1279-
if (!strErr.empty())
1280-
pwallet->WalletLogPrintf("%s\n", strErr);
1281-
}
12821185
} catch (...) {
12831186
// Exceptions that can be ignored or treated as non-critical are handled by the individual loading functions.
12841187
// Any uncaught exceptions will be caught here and treated as critical.
12851188
result = DBErrors::CORRUPT;
12861189
}
12871190

1288-
if (fNoncriticalErrors && result == DBErrors::LOAD_OK) {
1289-
result = DBErrors::NONCRITICAL_ERROR;
1290-
}
1291-
12921191
// Any wallet corruption at all: skip any rewriting or
12931192
// upgrading, we don't want to make it worse.
12941193
if (result != DBErrors::LOAD_OK)

0 commit comments

Comments
 (0)