From cd211b3b9965b5070d68adc1a03043d82d904d5b Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Mon, 18 Apr 2022 15:08:51 -0400 Subject: [PATCH] walletdb: refactor decryption key loading Instead of loading decryption keys as we iterate the database, load them explicitly. --- src/wallet/walletdb.cpp | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index d48a5c01dbf..fb1e4f8fb57 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -471,7 +471,6 @@ ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue, } else if (strType == DBKeys::WATCHS) { } else if (strType == DBKeys::KEY) { } else if (strType == DBKeys::MASTER_KEY) { - if (!LoadEncryptionKey(pwallet, ssKey, ssValue, strErr)) return false; } else if (strType == DBKeys::CRYPTED_KEY) { } else if (strType == DBKeys::KEYMETA) { } else if (strType == DBKeys::WATCHMETA) { @@ -1182,6 +1181,21 @@ static DBErrors LoadActiveSPKMs(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIV return result; } +static DBErrors LoadDecryptionKeys(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) +{ + AssertLockHeld(pwallet->cs_wallet); + + // Load decryption key (mkey) records + LoadResult mkey_res = LoadRecords(pwallet, batch, DBKeys::MASTER_KEY, + [] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) { + if (!LoadEncryptionKey(pwallet, key, value, err)) { + return DBErrors::CORRUPT; + } + return DBErrors::LOAD_OK; + }); + return mkey_res.m_result; +} + DBErrors WalletBatch::LoadWallet(CWallet* pwallet) { CWalletScanState wss; @@ -1230,6 +1244,9 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) // Load SPKMs result = std::max(LoadActiveSPKMs(pwallet, *m_batch), result); + // Load decryption keys + result = std::max(LoadDecryptionKeys(pwallet, *m_batch), result); + // Get cursor std::unique_ptr cursor = m_batch->GetNewCursor(); if (!cursor) @@ -1256,14 +1273,8 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) std::string strType, strErr; if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr)) { - // losing keys is considered a catastrophic error, anything else - // we assume the user can live with: - if (strType == DBKeys::MASTER_KEY) { - result = DBErrors::CORRUPT; - } 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. - } + // 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. } if (!strErr.empty()) pwallet->WalletLogPrintf("%s\n", strErr);