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.
This commit is contained in:
Andrew Chow 2020-05-15 19:24:26 -04:00
parent 2741774214
commit b426c7764d
5 changed files with 9 additions and 26 deletions

View file

@ -323,7 +323,7 @@ static const char *HEADER_END = "HEADER=END";
static const char *DATA_END = "DATA=END";
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > 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<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);

View file

@ -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

View file

@ -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<CWallet*>(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)

View file

@ -263,12 +263,6 @@ public:
DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWalletTx>& vWtx);
DBErrors ZapWalletTx(std::list<CWalletTx>& vWtx);
DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& 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

View file

@ -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)