Skip to content

Commit 9e077d9

Browse files
committed
salvage: Remove use of ReadKeyValue in salvage
To prepare to remove ReadKeyValue, change salvage to not use it
1 parent ad779e9 commit 9e077d9

File tree

3 files changed

+20
-40
lines changed

3 files changed

+20
-40
lines changed

src/wallet/salvage.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ static const char *HEADER_END = "HEADER=END";
1818
static const char *DATA_END = "DATA=END";
1919
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
2020

21-
static bool KeyFilter(const std::string& type)
22-
{
23-
return WalletBatch::IsKeyType(type) || type == DBKeys::HDCHAIN;
24-
}
25-
2621
class DummyCursor : public DatabaseCursor
2722
{
2823
Status Next(DataStream& key, DataStream& value) override { return Status::FAIL; }
@@ -186,17 +181,24 @@ bool RecoverDatabaseFile(const ArgsManager& args, const fs::path& file_path, bil
186181
{
187182
/* Filter for only private key type KV pairs to be added to the salvaged wallet */
188183
DataStream ssKey{row.first};
189-
CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
184+
DataStream ssValue(row.second);
190185
std::string strType, strErr;
191-
bool fReadOK;
192-
{
193-
// Required in LoadKeyMetadata():
194-
LOCK(dummyWallet.cs_wallet);
195-
fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue, strType, strErr, KeyFilter);
196-
}
197-
if (!KeyFilter(strType)) {
186+
187+
// We only care about KEY, MASTER_KEY, CRYPTED_KEY, and HDCHAIN types
188+
ssKey >> strType;
189+
bool fReadOK = false;
190+
if (strType == DBKeys::KEY) {
191+
fReadOK = LoadKey(&dummyWallet, ssKey, ssValue, strErr);
192+
} else if (strType == DBKeys::CRYPTED_KEY) {
193+
fReadOK = LoadCryptedKey(&dummyWallet, ssKey, ssValue, strErr);
194+
} else if (strType == DBKeys::MASTER_KEY) {
195+
fReadOK = LoadEncryptionKey(&dummyWallet, ssKey, ssValue, strErr);
196+
} else if (strType == DBKeys::HDCHAIN) {
197+
fReadOK = LoadHDChain(&dummyWallet, ssValue, strErr);
198+
} else {
198199
continue;
199200
}
201+
200202
if (!fReadOK)
201203
{
202204
warnings.push_back(strprintf(Untranslated("WARNING: WalletBatch::Recover skipping %s: %s"), strType, strErr));

src/wallet/walletdb.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,13 @@ bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr)
470470

471471
static bool
472472
ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
473-
CWalletScanState &wss, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
473+
CWalletScanState &wss, std::string& strType, std::string& strErr) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
474474
{
475475
try {
476476
// Unserialize
477477
// Taking advantage of the fact that pair serialization
478478
// is just the two items serialized one after the other
479479
ssKey >> strType;
480-
// If we have a filter, check if this matches the filter
481-
if (filter_fn && !filter_fn(strType)) {
482-
return true;
483-
}
484480
// Legacy entries in descriptor wallets are not allowed, abort immediately
485481
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS) && DBKeys::LEGACY_TYPES.count(strType) > 0) {
486482
wss.unexpected_legacy_entry = true;
@@ -834,19 +830,6 @@ ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
834830
return true;
835831
}
836832

837-
bool ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn)
838-
{
839-
CWalletScanState dummy_wss;
840-
LOCK(pwallet->cs_wallet);
841-
return ReadKeyValue(pwallet, ssKey, ssValue, dummy_wss, strType, strErr, filter_fn);
842-
}
843-
844-
bool WalletBatch::IsKeyType(const std::string& strType)
845-
{
846-
return (strType == DBKeys::KEY ||
847-
strType == DBKeys::MASTER_KEY || strType == DBKeys::CRYPTED_KEY);
848-
}
849-
850833
static DBErrors LoadMinVersion(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
851834
{
852835
AssertLockHeld(pwallet->cs_wallet);
@@ -934,7 +917,10 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
934917
}
935918
// losing keys is considered a catastrophic error, anything else
936919
// we assume the user can live with:
937-
if (IsKeyType(strType) || strType == DBKeys::DEFAULTKEY) {
920+
if (strType == DBKeys::KEY ||
921+
strType == DBKeys::MASTER_KEY ||
922+
strType == DBKeys::CRYPTED_KEY ||
923+
strType == DBKeys::DEFAULTKEY) {
938924
result = DBErrors::CORRUPT;
939925
} else if (wss.tx_corrupt) {
940926
pwallet->WalletLogPrintf("Error: Corrupt transaction found. This can be fixed by removing transactions from wallet and rescanning.\n");

src/wallet/walletdb.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,6 @@ class WalletBatch
276276
DBErrors LoadWallet(CWallet* pwallet);
277277
DBErrors FindWalletTxHashes(std::vector<uint256>& tx_hashes);
278278
DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
279-
/* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
280-
static bool IsKeyType(const std::string& strType);
281279

282280
//! write the hdchain model (external chain child index counter)
283281
bool WriteHDChain(const CHDChain& chain);
@@ -300,12 +298,6 @@ class WalletBatch
300298
//! Compacts BDB state so that wallet.dat is self-contained (if there are changes)
301299
void MaybeCompactWalletDB(WalletContext& context);
302300

303-
//! Callback for filtering key types to deserialize in ReadKeyValue
304-
using KeyFilterFn = std::function<bool(const std::string&)>;
305-
306-
//! Unserialize a given Key-Value pair and load it into the wallet
307-
bool ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
308-
309301
bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
310302
bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
311303
bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);

0 commit comments

Comments
 (0)