Move RecoverKeysOnlyFilter into RecoverDataBaseFile

pull/764/head
Andrew Chow 5 years ago
parent 9ea2d258b4
commit ea337f2d03

@ -15,7 +15,7 @@ static const char *HEADER_END = "HEADER=END";
static const char *DATA_END = "DATA=END"; static const char *DATA_END = "DATA=END";
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair; typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
bool RecoverDatabaseFile(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)
{ {
std::string filename; std::string filename;
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename); std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
@ -28,7 +28,7 @@ bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (
// Set -rescan so any missing transactions will be // Set -rescan so any missing transactions will be
// found. // found.
int64_t now = GetTime(); int64_t now = GetTime();
newFilename = strprintf("%s.%d.bak", filename, now); std::string newFilename = strprintf("%s.%d.bak", filename, now);
int result = env->dbenv->dbrename(nullptr, filename.c_str(), nullptr, int result = env->dbenv->dbrename(nullptr, filename.c_str(), nullptr,
newFilename.c_str(), DB_AUTO_COMMIT); newFilename.c_str(), DB_AUTO_COMMIT);
@ -116,45 +116,35 @@ bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (
} }
DbTxn* ptxn = env->TxnBegin(); DbTxn* ptxn = env->TxnBegin();
CWallet dummyWallet(nullptr, WalletLocation(), WalletDatabase::CreateDummy());
for (KeyValPair& row : salvagedData) for (KeyValPair& row : salvagedData)
{ {
if (recoverKVcallback) /* Filter for only private key type KV pairs to be added to the salvaged wallet */
{
CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION); CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION); CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
if (!(*recoverKVcallback)(callbackDataIn, ssKey, ssValue))
continue;
}
Dbt datKey(&row.first[0], row.first.size());
Dbt datValue(&row.second[0], row.second.size());
int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
if (ret2 > 0)
fSuccess = false;
}
ptxn->commit(0);
pdbCopy->close(0);
return fSuccess;
}
bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue)
{
CWallet *dummyWallet = reinterpret_cast<CWallet*>(callbackData);
std::string strType, strErr; std::string strType, strErr;
bool fReadOK; bool fReadOK;
{ {
// Required in LoadKeyMetadata(): // Required in LoadKeyMetadata():
LOCK(dummyWallet->cs_wallet); LOCK(dummyWallet.cs_wallet);
fReadOK = ReadKeyValue(dummyWallet, ssKey, ssValue, strType, strErr); fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue, strType, strErr);
} }
if (!WalletBatch::IsKeyType(strType) && strType != DBKeys::HDCHAIN) { if (!WalletBatch::IsKeyType(strType) && strType != DBKeys::HDCHAIN) {
return false; continue;
} }
if (!fReadOK) if (!fReadOK)
{ {
LogPrintf("WARNING: WalletBatch::Recover skipping %s: %s\n", strType, strErr); LogPrintf("WARNING: WalletBatch::Recover skipping %s: %s\n", strType, strErr);
return false; continue;
}
Dbt datKey(&row.first[0], row.first.size());
Dbt datValue(&row.second[0], row.second.size());
int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
if (ret2 > 0)
fSuccess = false;
} }
ptxn->commit(0);
pdbCopy->close(0);
return true; return fSuccess;
} }

@ -9,9 +9,6 @@
#include <fs.h> #include <fs.h>
#include <streams.h> #include <streams.h>
bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename); bool RecoverDatabaseFile(const fs::path& file_path);
/* 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_SALVAGE_H #endif // BITCOIN_WALLET_SALVAGE_H

@ -122,9 +122,7 @@ static bool SalvageWallet(const fs::path& path)
} }
// Perform the recovery // Perform the recovery
CWallet dummy_wallet(nullptr, WalletLocation(), WalletDatabase::CreateDummy()); return RecoverDatabaseFile(path);
std::string backup_filename;
return RecoverDatabaseFile(path, (void*)&dummy_wallet, RecoverKeysOnlyFilter, backup_filename);
} }
bool ExecuteWalletToolFunc(const std::string& command, const std::string& name) bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)

Loading…
Cancel
Save