aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/walletdb.cpp
diff options
context:
space:
mode:
authorAndrew Chow <achow101-github@achow101.com>2022-04-18 15:08:51 -0400
committerAndrew Chow <github@achow101.com>2023-06-27 11:08:00 -0400
commitcd211b3b9965b5070d68adc1a03043d82d904d5b (patch)
tree6f834c7ac3cbb4094990470426025548ed081848 /src/wallet/walletdb.cpp
parent31c033e5ca3b65f4f5345d5aa17aafedd637ef4f (diff)
downloadbitcoin-cd211b3b9965b5070d68adc1a03043d82d904d5b.tar.xz
walletdb: refactor decryption key loading
Instead of loading decryption keys as we iterate the database, load them explicitly.
Diffstat (limited to 'src/wallet/walletdb.cpp')
-rw-r--r--src/wallet/walletdb.cpp29
1 files changed, 20 insertions, 9 deletions
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index d48a5c01db..fb1e4f8fb5 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<DatabaseCursor> 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);