Skip to content

Commit 544e12a

Browse files
committed
walletdb: Add KeyFilterFn to ReadKeyValue
Add a KeyFilterFn callback to ReadKeyValue which allows the caller to specify which types to actually deserialize. A KeyFilterFn takes the type as the parameter and returns a bool indicating whether deserialization should continue.
1 parent f8462a6 commit 544e12a

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/wallet/walletdb.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,17 @@ class CWalletScanState {
263263

264264
static bool
265265
ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
266-
CWalletScanState &wss, std::string& strType, std::string& strErr) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
266+
CWalletScanState &wss, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
267267
{
268268
try {
269269
// Unserialize
270270
// Taking advantage of the fact that pair serialization
271271
// is just the two items serialized one after the other
272272
ssKey >> strType;
273+
// If we have a filter, check if this matches the filter
274+
if (filter_fn && !filter_fn(strType)) {
275+
return true;
276+
}
273277
if (strType == DBKeys::NAME) {
274278
std::string strAddress;
275279
ssKey >> strAddress;
@@ -668,11 +672,11 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
668672
return true;
669673
}
670674

671-
bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr)
675+
bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn)
672676
{
673677
CWalletScanState dummy_wss;
674678
LOCK(pwallet->cs_wallet);
675-
return ReadKeyValue(pwallet, ssKey, ssValue, dummy_wss, strType, strErr);
679+
return ReadKeyValue(pwallet, ssKey, ssValue, dummy_wss, strType, strErr, filter_fn);
676680
}
677681

678682
bool WalletBatch::IsKeyType(const std::string& strType)

src/wallet/walletdb.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,11 @@ class WalletBatch
280280
//! Compacts BDB state so that wallet.dat is self-contained (if there are changes)
281281
void MaybeCompactWalletDB();
282282

283+
//! Callback for filtering key types to deserialize in ReadKeyValue
284+
using KeyFilterFn = std::function<bool(const std::string&)>;
285+
283286
//! Unserialize a given Key-Value pair and load it into the wallet
284-
bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr);
287+
bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
285288

286289
/** Return whether a wallet database is currently loaded. */
287290
bool IsWalletLoaded(const fs::path& wallet_path);

0 commit comments

Comments
 (0)