From b426c7764d26e280e1f814cf36e050743c45cd12 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Fri, 15 May 2020 19:24:26 -0400 Subject: Make BerkeleyBatch::Recover and WalletBatch::RecoverKeysOnlyFilter standalone Instead of having these be class static functions, just make them be standalone. Also removes WalletBatch::Recover which just passed through to BerkeleyBatch::Recover. --- src/wallet/db.cpp | 2 +- src/wallet/db.h | 3 ++- src/wallet/walletdb.cpp | 19 ++----------------- src/wallet/walletdb.h | 9 +++------ src/wallet/wallettool.cpp | 2 +- 5 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index c115786b7f..cff83e3d01 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -323,7 +323,7 @@ static const char *HEADER_END = "HEADER=END"; static const char *DATA_END = "DATA=END"; typedef std::pair, std::vector > KeyValPair; -bool BerkeleyBatch::Recover(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& newFilename) +bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& newFilename) { std::string filename; std::shared_ptr env = GetWalletEnv(file_path, filename); diff --git a/src/wallet/db.h b/src/wallet/db.h index 4acb414a5b..c802470045 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -226,7 +226,6 @@ public: void Flush(); void Close(); - static bool Recover(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename); /* flush the wallet passively (TRY_LOCK) ideal to be called periodically */ @@ -390,6 +389,8 @@ public: bool static Rewrite(BerkeleyDatabase& database, const char* pszSkip = nullptr); }; +bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename); + std::string BerkeleyDatabaseVersion(); #endif // BITCOIN_WALLET_DB_H diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index c04637934d..2a3daf59b4 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -885,22 +885,7 @@ void MaybeCompactWalletDB() fOneThread = false; } -// -// Try to (very carefully!) recover wallet file if there is a problem. -// -bool WalletBatch::Recover(const fs::path& wallet_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename) -{ - return BerkeleyBatch::Recover(wallet_path, callbackDataIn, recoverKVcallback, out_backup_filename); -} - -bool WalletBatch::Recover(const fs::path& wallet_path, std::string& out_backup_filename) -{ - // recover without a key filter callback - // results in recovering all record types - return WalletBatch::Recover(wallet_path, nullptr, nullptr, out_backup_filename); -} - -bool WalletBatch::RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue) +bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue) { CWallet *dummyWallet = reinterpret_cast(callbackData); std::string strType, strErr; @@ -910,7 +895,7 @@ bool WalletBatch::RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, C LOCK(dummyWallet->cs_wallet); fReadOK = ReadKeyValue(dummyWallet, ssKey, ssValue, strType, strErr); } - if (!IsKeyType(strType) && strType != DBKeys::HDCHAIN) { + if (!WalletBatch::IsKeyType(strType) && strType != DBKeys::HDCHAIN) { return false; } if (!fReadOK) diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 155046dfba..389b96189f 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -263,12 +263,6 @@ public: DBErrors FindWalletTx(std::vector& vTxHash, std::list& vWtx); DBErrors ZapWalletTx(std::list& vWtx); DBErrors ZapSelectTx(std::vector& vHashIn, std::vector& vHashOut); - /* Try to (very carefully!) recover wallet database (with a possible key type filter) */ - static bool Recover(const fs::path& wallet_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename); - /* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */ - static bool Recover(const fs::path& wallet_path, std::string& out_backup_filename); - /* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */ - static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue); /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */ static bool IsKeyType(const std::string& strType); /* verifies the database environment */ @@ -297,4 +291,7 @@ void MaybeCompactWalletDB(); //! Unserialize a given Key-Value pair and load it into the wallet bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr); +/* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */ +bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue); + #endif // BITCOIN_WALLET_WALLETDB_H diff --git a/src/wallet/wallettool.cpp b/src/wallet/wallettool.cpp index 89645fb5cd..2b46eef0e0 100644 --- a/src/wallet/wallettool.cpp +++ b/src/wallet/wallettool.cpp @@ -123,7 +123,7 @@ static bool SalvageWallet(const fs::path& path) // Perform the recovery CWallet dummy_wallet(nullptr, WalletLocation(), WalletDatabase::CreateDummy()); std::string backup_filename; - return WalletBatch::Recover(path, (void*)&dummy_wallet, WalletBatch::RecoverKeysOnlyFilter, backup_filename); + return RecoverDatabaseFile(path, (void*)&dummy_wallet, RecoverKeysOnlyFilter, backup_filename); } bool ExecuteWalletToolFunc(const std::string& command, const std::string& name) -- cgit v1.2.3