Skip to content

Commit ea337f2

Browse files
committed
Move RecoverKeysOnlyFilter into RecoverDataBaseFile
1 parent 9ea2d25 commit ea337f2

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

src/wallet/salvage.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static const char *HEADER_END = "HEADER=END";
1515
static const char *DATA_END = "DATA=END";
1616
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
1717

18-
bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& newFilename)
18+
bool RecoverDatabaseFile(const fs::path& file_path)
1919
{
2020
std::string filename;
2121
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
@@ -28,7 +28,7 @@ bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (
2828
// Set -rescan so any missing transactions will be
2929
// found.
3030
int64_t now = GetTime();
31-
newFilename = strprintf("%s.%d.bak", filename, now);
31+
std::string newFilename = strprintf("%s.%d.bak", filename, now);
3232

3333
int result = env->dbenv->dbrename(nullptr, filename.c_str(), nullptr,
3434
newFilename.c_str(), DB_AUTO_COMMIT);
@@ -116,14 +116,26 @@ bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (
116116
}
117117

118118
DbTxn* ptxn = env->TxnBegin();
119+
CWallet dummyWallet(nullptr, WalletLocation(), WalletDatabase::CreateDummy());
119120
for (KeyValPair& row : salvagedData)
120121
{
121-
if (recoverKVcallback)
122+
/* Filter for only private key type KV pairs to be added to the salvaged wallet */
123+
CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
124+
CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
125+
std::string strType, strErr;
126+
bool fReadOK;
122127
{
123-
CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
124-
CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
125-
if (!(*recoverKVcallback)(callbackDataIn, ssKey, ssValue))
126-
continue;
128+
// Required in LoadKeyMetadata():
129+
LOCK(dummyWallet.cs_wallet);
130+
fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue, strType, strErr);
131+
}
132+
if (!WalletBatch::IsKeyType(strType) && strType != DBKeys::HDCHAIN) {
133+
continue;
134+
}
135+
if (!fReadOK)
136+
{
137+
LogPrintf("WARNING: WalletBatch::Recover skipping %s: %s\n", strType, strErr);
138+
continue;
127139
}
128140
Dbt datKey(&row.first[0], row.first.size());
129141
Dbt datValue(&row.second[0], row.second.size());
@@ -136,25 +148,3 @@ bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (
136148

137149
return fSuccess;
138150
}
139-
140-
bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue)
141-
{
142-
CWallet *dummyWallet = reinterpret_cast<CWallet*>(callbackData);
143-
std::string strType, strErr;
144-
bool fReadOK;
145-
{
146-
// Required in LoadKeyMetadata():
147-
LOCK(dummyWallet->cs_wallet);
148-
fReadOK = ReadKeyValue(dummyWallet, ssKey, ssValue, strType, strErr);
149-
}
150-
if (!WalletBatch::IsKeyType(strType) && strType != DBKeys::HDCHAIN) {
151-
return false;
152-
}
153-
if (!fReadOK)
154-
{
155-
LogPrintf("WARNING: WalletBatch::Recover skipping %s: %s\n", strType, strErr);
156-
return false;
157-
}
158-
159-
return true;
160-
}

src/wallet/salvage.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#include <fs.h>
1010
#include <streams.h>
1111

12-
bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
13-
14-
/* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
15-
bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
12+
bool RecoverDatabaseFile(const fs::path& file_path);
1613

1714
#endif // BITCOIN_WALLET_SALVAGE_H

src/wallet/wallettool.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ static bool SalvageWallet(const fs::path& path)
122122
}
123123

124124
// Perform the recovery
125-
CWallet dummy_wallet(nullptr, WalletLocation(), WalletDatabase::CreateDummy());
126-
std::string backup_filename;
127-
return RecoverDatabaseFile(path, (void*)&dummy_wallet, RecoverKeysOnlyFilter, backup_filename);
125+
return RecoverDatabaseFile(path);
128126
}
129127

130128
bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)

0 commit comments

Comments
 (0)