Skip to content

Commit cd211b3

Browse files
committed
walletdb: refactor decryption key loading
Instead of loading decryption keys as we iterate the database, load them explicitly.
1 parent 31c033e commit cd211b3

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/wallet/walletdb.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
471471
} else if (strType == DBKeys::WATCHS) {
472472
} else if (strType == DBKeys::KEY) {
473473
} else if (strType == DBKeys::MASTER_KEY) {
474-
if (!LoadEncryptionKey(pwallet, ssKey, ssValue, strErr)) return false;
475474
} else if (strType == DBKeys::CRYPTED_KEY) {
476475
} else if (strType == DBKeys::KEYMETA) {
477476
} else if (strType == DBKeys::WATCHMETA) {
@@ -1182,6 +1181,21 @@ static DBErrors LoadActiveSPKMs(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIV
11821181
return result;
11831182
}
11841183

1184+
static DBErrors LoadDecryptionKeys(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1185+
{
1186+
AssertLockHeld(pwallet->cs_wallet);
1187+
1188+
// Load decryption key (mkey) records
1189+
LoadResult mkey_res = LoadRecords(pwallet, batch, DBKeys::MASTER_KEY,
1190+
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
1191+
if (!LoadEncryptionKey(pwallet, key, value, err)) {
1192+
return DBErrors::CORRUPT;
1193+
}
1194+
return DBErrors::LOAD_OK;
1195+
});
1196+
return mkey_res.m_result;
1197+
}
1198+
11851199
DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
11861200
{
11871201
CWalletScanState wss;
@@ -1230,6 +1244,9 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
12301244
// Load SPKMs
12311245
result = std::max(LoadActiveSPKMs(pwallet, *m_batch), result);
12321246

1247+
// Load decryption keys
1248+
result = std::max(LoadDecryptionKeys(pwallet, *m_batch), result);
1249+
12331250
// Get cursor
12341251
std::unique_ptr<DatabaseCursor> cursor = m_batch->GetNewCursor();
12351252
if (!cursor)
@@ -1256,14 +1273,8 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
12561273
std::string strType, strErr;
12571274
if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
12581275
{
1259-
// losing keys is considered a catastrophic error, anything else
1260-
// we assume the user can live with:
1261-
if (strType == DBKeys::MASTER_KEY) {
1262-
result = DBErrors::CORRUPT;
1263-
} else {
1264-
// Leave other errors alone, if we try to fix them we might make things worse.
1265-
fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
1266-
}
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.
12671278
}
12681279
if (!strErr.empty())
12691280
pwallet->WalletLogPrintf("%s\n", strErr);

0 commit comments

Comments
 (0)