Skip to content

Commit

Permalink
scripted-diff: replace wallet DatabaseStatus with DatabaseError
Browse files Browse the repository at this point in the history
-BEGIN VERIFY SCRIPT-
git grep -l DatabaseStatus src | xargs sed -i s/DatabaseStatus/DatabaseError/g
sed -i '/^    SUCCESS,$/d' src/wallet/db.h
-END VERIFY SCRIPT-
  • Loading branch information
ryanofsky committed Nov 2, 2024
1 parent 4fb4690 commit 830570f
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 64 deletions.
8 changes: 4 additions & 4 deletions src/wallet/bdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,9 +943,9 @@ std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(bool flush_on_close)
return std::make_unique<BerkeleyBatch>(*this, false, flush_on_close);
}

util::ResultPtr<std::unique_ptr<BerkeleyDatabase>, DatabaseStatus> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options)
util::ResultPtr<std::unique_ptr<BerkeleyDatabase>, DatabaseError> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options)
{
util::ResultPtr<std::unique_ptr<BerkeleyDatabase>, DatabaseStatus> result;
util::ResultPtr<std::unique_ptr<BerkeleyDatabase>, DatabaseError> result;

fs::path data_file = BDBDataFile(path);
std::unique_ptr<BerkeleyDatabase> db;
Expand All @@ -955,14 +955,14 @@ util::ResultPtr<std::unique_ptr<BerkeleyDatabase>, DatabaseStatus> MakeBerkeleyD
std::shared_ptr<BerkeleyEnvironment> env = GetBerkeleyEnv(data_file.parent_path(), options.use_shared_memory);
if (env->m_databases.count(data_filename)) {
result.Update({util::Error{Untranslated(strprintf("Refusing to load database. Data file '%s' is already loaded.", fs::PathToString(env->Directory() / data_filename)))},
DatabaseStatus::FAILED_ALREADY_LOADED});
DatabaseError::FAILED_ALREADY_LOADED});
return result;
}
db = std::make_unique<BerkeleyDatabase>(std::move(env), std::move(data_filename), options);
}

if (options.verify && !(db->Verify() >> result)) {
result.Update({util::Error{}, DatabaseStatus::FAILED_VERIFY});
result.Update({util::Error{}, DatabaseError::FAILED_VERIFY});
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/bdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ std::string BerkeleyDatabaseVersion();
bool BerkeleyDatabaseSanityCheck();

//! Return object giving access to Berkeley database at specified path.
util::ResultPtr<std::unique_ptr<BerkeleyDatabase>, DatabaseStatus> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options);
util::ResultPtr<std::unique_ptr<BerkeleyDatabase>, DatabaseError> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options);
} // namespace wallet

#endif // BITCOIN_WALLET_BDB_H
5 changes: 2 additions & 3 deletions src/wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ struct DatabaseOptions {
int64_t max_log_mb = 100; //!< Max log size to allow before consolidating.
};

enum class DatabaseStatus {
SUCCESS,
enum class DatabaseError {
FAILED_BAD_PATH,
FAILED_BAD_FORMAT,
FAILED_ALREADY_LOADED,
Expand All @@ -221,7 +220,7 @@ enum class DatabaseStatus {
std::vector<std::pair<fs::path, std::string>> ListDatabases(const fs::path& path);

void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options);
util::ResultPtr<std::unique_ptr<WalletDatabase>, DatabaseStatus> MakeDatabase(const fs::path& path, const DatabaseOptions& options);
util::ResultPtr<std::unique_ptr<WalletDatabase>, DatabaseError> MakeDatabase(const fs::path& path, const DatabaseOptions& options);

fs::path BDBDataFile(const fs::path& path);
fs::path SQLiteDataFile(const fs::path& path);
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ bool VerifyWallets(WalletContext& context)
options.verify = true;
auto result{MakeWalletDatabase(wallet_file, options)};
if (!result) {
if (result.GetFailure() == DatabaseStatus::FAILED_NOT_FOUND) {
if (result.GetFailure() == DatabaseError::FAILED_NOT_FOUND) {
chain.initWarning(Untranslated(strprintf("Skipping -wallet path that doesn't exist. %s", util::ErrorString(result).original)));
} else {
chain.initError(util::ErrorString(result));
Expand Down Expand Up @@ -127,7 +127,7 @@ bool LoadWallets(WalletContext& context)
options.verify = false; // No need to verify, assuming verified earlier in VerifyWallets()
util::Result<void> result;
auto database{MakeWalletDatabase(name, options) >> result};
if (!database && database.GetFailure() == DatabaseStatus::FAILED_NOT_FOUND) {
if (!database && database.GetFailure() == DatabaseError::FAILED_NOT_FOUND) {
continue;
}
chain.initMessage(_("Loading wallet…").translated);
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/migrate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,13 +768,13 @@ std::unique_ptr<DatabaseCursor> BerkeleyROBatch::GetNewPrefixCursor(Span<const s
return std::make_unique<BerkeleyROCursor>(m_database, prefix);
}

util::ResultPtr<std::unique_ptr<BerkeleyRODatabase>, DatabaseStatus> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options)
util::ResultPtr<std::unique_ptr<BerkeleyRODatabase>, DatabaseError> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options)
{
fs::path data_file = BDBDataFile(path);
try {
return std::make_unique<BerkeleyRODatabase>(data_file);
} catch (const std::runtime_error& e) {
return {util::Error{Untranslated(e.what())}, DatabaseStatus::FAILED_LOAD};
return {util::Error{Untranslated(e.what())}, DatabaseError::FAILED_LOAD};
}
}
} // namespace wallet
2 changes: 1 addition & 1 deletion src/wallet/migrate.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class BerkeleyROBatch : public DatabaseBatch
};

//! Return object giving access to Berkeley Read Only database at specified path.
util::ResultPtr<std::unique_ptr<BerkeleyRODatabase>, DatabaseStatus> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options);
util::ResultPtr<std::unique_ptr<BerkeleyRODatabase>, DatabaseError> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options);
} // namespace wallet

#endif // BITCOIN_WALLET_MIGRATE_H
12 changes: 6 additions & 6 deletions src/wallet/rpc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,24 @@ void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey,
entry.pushKV("parent_descs", std::move(parent_descs));
}

void HandleWalletError(const util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus>& wallet)
void HandleWalletError(const util::ResultPtr<std::shared_ptr<CWallet>, DatabaseError>& wallet)
{
if (!wallet) {
// Map bad format to not found, since bad format is returned when the
// wallet directory exists, but doesn't contain a data file.
RPCErrorCode code = RPC_WALLET_ERROR;
switch (wallet.GetFailure()) {
case DatabaseStatus::FAILED_NOT_FOUND:
case DatabaseStatus::FAILED_BAD_FORMAT:
case DatabaseError::FAILED_NOT_FOUND:
case DatabaseError::FAILED_BAD_FORMAT:
code = RPC_WALLET_NOT_FOUND;
break;
case DatabaseStatus::FAILED_ALREADY_LOADED:
case DatabaseError::FAILED_ALREADY_LOADED:
code = RPC_WALLET_ALREADY_LOADED;
break;
case DatabaseStatus::FAILED_ALREADY_EXISTS:
case DatabaseError::FAILED_ALREADY_EXISTS:
code = RPC_WALLET_ALREADY_EXISTS;
break;
case DatabaseStatus::FAILED_INVALID_BACKUP_FILE:
case DatabaseError::FAILED_INVALID_BACKUP_FILE:
code = RPC_INVALID_PARAMETER;
break;
default: // RPC_WALLET_ERROR is returned for all other cases.
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/rpc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct bilingual_str;

namespace wallet {
class LegacyScriptPubKeyMan;
enum class DatabaseStatus;
enum class DatabaseError;
struct WalletContext;

extern const std::string HELP_REQUIRING_PASSPHRASE;
Expand Down Expand Up @@ -51,7 +51,7 @@ std::string LabelFromValue(const UniValue& value);
//! Fetch parent descriptors of this scriptPubKey.
void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, UniValue& entry);

void HandleWalletError(const util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus>& wallet);
void HandleWalletError(const util::ResultPtr<std::shared_ptr<CWallet>, DatabaseError>& wallet);
int64_t ParseISO8601DateTime(const std::string& str);
void AppendLastProcessedBlock(UniValue& entry, const CWallet& wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
} // namespace wallet
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpc/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ static RPCHelpMan createwallet()
std::optional<bool> load_on_start = request.params[6].isNull() ? std::nullopt : std::optional<bool>(request.params[6].get_bool());
auto wallet{CreateWallet(context, request.params[0].get_str(), load_on_start, options) >> result};
if (!wallet) {
RPCErrorCode code = wallet.GetFailure() == DatabaseStatus::FAILED_ENCRYPT ? RPC_WALLET_ENCRYPTION_FAILED : RPC_WALLET_ERROR;
RPCErrorCode code = wallet.GetFailure() == DatabaseError::FAILED_ENCRYPT ? RPC_WALLET_ENCRYPTION_FAILED : RPC_WALLET_ERROR;
throw JSONRPCError(code, util::ErrorString(result).original);
}

Expand Down
8 changes: 4 additions & 4 deletions src/wallet/sqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,19 +696,19 @@ bool SQLiteBatch::TxnAbort()
return res == SQLITE_OK;
}

util::ResultPtr<std::unique_ptr<SQLiteDatabase>, DatabaseStatus> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options)
util::ResultPtr<std::unique_ptr<SQLiteDatabase>, DatabaseError> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options)
{
util::ResultPtr<std::unique_ptr<SQLiteDatabase>, DatabaseStatus> result;
util::ResultPtr<std::unique_ptr<SQLiteDatabase>, DatabaseError> result;
try {
fs::path data_file = SQLiteDataFile(path);
auto db = std::make_unique<SQLiteDatabase>(data_file.parent_path(), data_file, options);
if (options.verify && !(db->Verify() >> result)) {
result.Update({util::Error{}, DatabaseStatus::FAILED_VERIFY});
result.Update({util::Error{}, DatabaseError::FAILED_VERIFY});
} else {
result.Update(std::move(db));
}
} catch (const std::runtime_error& e) {
result.Update({util::Error{Untranslated(e.what())}, DatabaseStatus::FAILED_LOAD});
result.Update({util::Error{Untranslated(e.what())}, DatabaseError::FAILED_LOAD});
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class SQLiteDatabase : public WalletDatabase
bool m_use_unsafe_sync;
};

util::ResultPtr<std::unique_ptr<SQLiteDatabase>, DatabaseStatus> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options);
util::ResultPtr<std::unique_ptr<SQLiteDatabase>, DatabaseError> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options);

std::string SQLiteDatabaseVersion();
} // namespace wallet
Expand Down
Loading

0 comments on commit 830570f

Please sign in to comment.