Skip to content

Commit 30dd562

Browse files
author
MarcoFalke
committed
Merge #19457: wallet: Cleanup wallettool salvage and walletdb extraneous declarations
0e279fe walletdb: Remove unused static functions from walletdb.h (Andrew Chow) 9f536d4 wallettool: Have RecoverDatabaseFile return errors and warnings (Andrew Chow) 06e263a Call RecoverDatabaseFile directly from wallettool (Andrew Chow) Pull request description: Followup to #19324 addressing some comments. Removes the `SalvageWallet` function in wallettool and instead directly calls `RecoverDatabaseFile` as suggested in bitcoin/bitcoin#19324 (comment) Removes the `LogPrintf`s and `tfm::format`s in `RecoverDatabaseFile` as noted in bitcoin/bitcoin#19324 (comment) Removes the declarations of `VerifyEnvironment` and `VerifyDatabaseFile` that were forgotten in `walletdb.h` as noted in bitcoin/bitcoin#19324 (comment) ACKs for top commit: meshcollider: Code review ACK 0e279fe ryanofsky: Code review ACK 0e279fe, just dropped last commit Tree-SHA512: ffd01f30536c2eab4bf40ba363c3ea916ecef3c8f0c5262040b40498776ffb00f95240204a40e38415d6931800851d0a3fa63ee91efc1d329b60ac317da0363d
2 parents 1a43cd3 + 0e279fe commit 30dd562

File tree

4 files changed

+26
-43
lines changed

4 files changed

+26
-43
lines changed

src/wallet/salvage.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ static const char *HEADER_END = "HEADER=END";
1616
static const char *DATA_END = "DATA=END";
1717
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
1818

19-
bool RecoverDatabaseFile(const fs::path& file_path)
19+
bool RecoverDatabaseFile(const fs::path& file_path, bilingual_str& error, std::vector<bilingual_str>& warnings)
2020
{
2121
std::string filename;
2222
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
2323

24-
bilingual_str open_err;
25-
if (!env->Open(open_err)) {
26-
tfm::format(std::cerr, "%s\n", open_err.original);
24+
if (!env->Open(error)) {
2725
return false;
2826
}
2927

@@ -39,11 +37,9 @@ bool RecoverDatabaseFile(const fs::path& file_path)
3937

4038
int result = env->dbenv->dbrename(nullptr, filename.c_str(), nullptr,
4139
newFilename.c_str(), DB_AUTO_COMMIT);
42-
if (result == 0)
43-
LogPrintf("Renamed %s to %s\n", filename, newFilename);
44-
else
40+
if (result != 0)
4541
{
46-
LogPrintf("Failed to rename %s to %s\n", filename, newFilename);
42+
error = strprintf(Untranslated("Failed to rename %s to %s"), filename, newFilename);
4743
return false;
4844
}
4945

@@ -60,10 +56,10 @@ bool RecoverDatabaseFile(const fs::path& file_path)
6056
Db db(env->dbenv.get(), 0);
6157
result = db.verify(newFilename.c_str(), nullptr, &strDump, DB_SALVAGE | DB_AGGRESSIVE);
6258
if (result == DB_VERIFY_BAD) {
63-
LogPrintf("Salvage: Database salvage found errors, all data may not be recoverable.\n");
59+
warnings.push_back(Untranslated("Salvage: Database salvage found errors, all data may not be recoverable."));
6460
}
6561
if (result != 0 && result != DB_VERIFY_BAD) {
66-
LogPrintf("Salvage: Database salvage failed with result %d.\n", result);
62+
error = strprintf(Untranslated("Salvage: Database salvage failed with result %d."), result);
6763
return false;
6864
}
6965

@@ -87,7 +83,7 @@ bool RecoverDatabaseFile(const fs::path& file_path)
8783
break;
8884
getline(strDump, valueHex);
8985
if (valueHex == DATA_END) {
90-
LogPrintf("Salvage: WARNING: Number of keys in data does not match number of values.\n");
86+
warnings.push_back(Untranslated("Salvage: WARNING: Number of keys in data does not match number of values."));
9187
break;
9288
}
9389
salvagedData.push_back(make_pair(ParseHex(keyHex), ParseHex(valueHex)));
@@ -96,18 +92,17 @@ bool RecoverDatabaseFile(const fs::path& file_path)
9692

9793
bool fSuccess;
9894
if (keyHex != DATA_END) {
99-
LogPrintf("Salvage: WARNING: Unexpected end of file while reading salvage output.\n");
95+
warnings.push_back(Untranslated("Salvage: WARNING: Unexpected end of file while reading salvage output."));
10096
fSuccess = false;
10197
} else {
10298
fSuccess = (result == 0);
10399
}
104100

105101
if (salvagedData.empty())
106102
{
107-
LogPrintf("Salvage(aggressive) found no records in %s.\n", newFilename);
103+
error = strprintf(Untranslated("Salvage(aggressive) found no records in %s."), newFilename);
108104
return false;
109105
}
110-
LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size());
111106

112107
std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0);
113108
int ret = pdbCopy->open(nullptr, // Txn pointer
@@ -117,7 +112,7 @@ bool RecoverDatabaseFile(const fs::path& file_path)
117112
DB_CREATE, // Flags
118113
0);
119114
if (ret > 0) {
120-
LogPrintf("Cannot create database file %s\n", filename);
115+
error = strprintf(Untranslated("Cannot create database file %s"), filename);
121116
pdbCopy->close(0);
122117
return false;
123118
}
@@ -141,7 +136,7 @@ bool RecoverDatabaseFile(const fs::path& file_path)
141136
}
142137
if (!fReadOK)
143138
{
144-
LogPrintf("WARNING: WalletBatch::Recover skipping %s: %s\n", strType, strErr);
139+
warnings.push_back(strprintf(Untranslated("WARNING: WalletBatch::Recover skipping %s: %s"), strType, strErr));
145140
continue;
146141
}
147142
Dbt datKey(&row.first[0], row.first.size());

src/wallet/salvage.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <fs.h>
1010
#include <streams.h>
1111

12-
bool RecoverDatabaseFile(const fs::path& file_path);
12+
struct bilingual_str;
13+
14+
bool RecoverDatabaseFile(const fs::path& file_path, bilingual_str& error, std::vector<bilingual_str>& warnings);
1315

1416
#endif // BITCOIN_WALLET_SALVAGE_H

src/wallet/walletdb.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,6 @@ class WalletBatch
261261
DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
262262
/* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
263263
static bool IsKeyType(const std::string& strType);
264-
/* verifies the database environment */
265-
static bool VerifyEnvironment(const fs::path& wallet_path, bilingual_str& errorStr);
266-
/* verifies the database file */
267-
static bool VerifyDatabaseFile(const fs::path& wallet_path, bilingual_str& errorStr);
268264

269265
//! write the hdchain model (external chain child index counter)
270266
bool WriteHDChain(const CHDChain& chain);

src/wallet/wallettool.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,6 @@ static void WalletShowInfo(CWallet* wallet_instance)
104104
tfm::format(std::cout, "Address Book: %zu\n", wallet_instance->m_address_book.size());
105105
}
106106

107-
static bool SalvageWallet(const fs::path& path)
108-
{
109-
// Create a Database handle to allow for the db to be initialized before recovery
110-
std::unique_ptr<WalletDatabase> database = CreateWalletDatabase(path);
111-
112-
// Initialize the environment before recovery
113-
bilingual_str error_string;
114-
try {
115-
database->Verify(error_string);
116-
} catch (const fs::filesystem_error& e) {
117-
error_string = Untranslated(strprintf("Error loading wallet. %s", fsbridge::get_filesystem_error_message(e)));
118-
}
119-
if (!error_string.original.empty()) {
120-
tfm::format(std::cerr, "Failed to open wallet for salvage :%s\n", error_string.original);
121-
return false;
122-
}
123-
124-
// Perform the recovery
125-
return RecoverDatabaseFile(path);
126-
}
127-
128107
bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
129108
{
130109
fs::path path = fs::absolute(name, GetWalletDir());
@@ -147,7 +126,18 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
147126
WalletShowInfo(wallet_instance.get());
148127
wallet_instance->Close();
149128
} else if (command == "salvage") {
150-
return SalvageWallet(path);
129+
bilingual_str error;
130+
std::vector<bilingual_str> warnings;
131+
bool ret = RecoverDatabaseFile(path, error, warnings);
132+
if (!ret) {
133+
for (const auto warning : warnings) {
134+
tfm::format(std::cerr, "%s\n", warning.original);
135+
}
136+
if (!error.empty()) {
137+
tfm::format(std::cerr, "%s\n", error.original);
138+
}
139+
}
140+
return ret;
151141
}
152142
} else {
153143
tfm::format(std::cerr, "Invalid command: %s\n", command);

0 commit comments

Comments
 (0)