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 May 1, 2024
1 parent 07b2a78 commit ae0b9b8
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 60 deletions.
8 changes: 4 additions & 4 deletions src/wallet/bdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,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 @@ -941,14 +941,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 @@ -216,7 +216,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 @@ -194,8 +194,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 @@ -212,7 +211,7 @@ enum class DatabaseStatus {
std::vector<fs::path> 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 @@ -87,7 +87,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 @@ -115,7 +115,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
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", 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 @@ -428,7 +428,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 @@ -698,19 +698,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 @@ -178,7 +178,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
46 changes: 23 additions & 23 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ void UnloadWallet(std::shared_ptr<CWallet>&& wallet)
}

namespace {
util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> LoadWalletInternal(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options)
util::ResultPtr<std::shared_ptr<CWallet>, DatabaseError> LoadWalletInternal(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options)
{
util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> result;
util::ResultPtr<std::shared_ptr<CWallet>, DatabaseError> result;
try {
auto database{MakeWalletDatabase(name, options) >> result};
if (!database) {
Expand All @@ -277,7 +277,7 @@ util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> LoadWalletInternal(Wal
if (!wallet) {
auto& errors{result.EnsureMessages().errors};
errors.insert(errors.begin(), Untranslated("Wallet loading failed."));
result.Update({util::Error{}, DatabaseStatus::FAILED_LOAD});
result.Update({util::Error{}, DatabaseError::FAILED_LOAD});
return result;
}

Expand All @@ -295,7 +295,7 @@ util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> LoadWalletInternal(Wal

result.Update(std::move(wallet.value()));
} 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 Expand Up @@ -359,20 +359,20 @@ class FastWalletRescanFilter
};
} // namespace

util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> LoadWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options)
util::ResultPtr<std::shared_ptr<CWallet>, DatabaseError> LoadWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options)
{
auto result = WITH_LOCK(g_loading_wallet_mutex, return g_loading_wallet_set.insert(name));
if (!result.second) {
return {util::Error{Untranslated("Wallet already loading.")}, DatabaseStatus::FAILED_LOAD};
return {util::Error{Untranslated("Wallet already loading.")}, DatabaseError::FAILED_LOAD};
}
auto wallet{LoadWalletInternal(context, name, load_on_start, options)};
WITH_LOCK(g_loading_wallet_mutex, g_loading_wallet_set.erase(result.first));
return wallet;
}

util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> CreateWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, DatabaseOptions& options)
util::ResultPtr<std::shared_ptr<CWallet>, DatabaseError> CreateWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, DatabaseOptions& options)
{
util::Result<std::shared_ptr<CWallet>, DatabaseStatus> result;
util::Result<std::shared_ptr<CWallet>, DatabaseError> result;
uint64_t wallet_creation_flags = options.create_flags;
const SecureString& passphrase = options.create_passphrase;

Expand All @@ -388,19 +388,19 @@ util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> CreateWallet(WalletCon

// Private keys must be disabled for an external signer wallet
if ((wallet_creation_flags & WALLET_FLAG_EXTERNAL_SIGNER) && !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
result.Update({util::Error{Untranslated("Private keys must be disabled when using an external signer")}, DatabaseStatus::FAILED_CREATE});
result.Update({util::Error{Untranslated("Private keys must be disabled when using an external signer")}, DatabaseError::FAILED_CREATE});
return result;
}

// Descriptor support must be enabled for an external signer wallet
if ((wallet_creation_flags & WALLET_FLAG_EXTERNAL_SIGNER) && !(wallet_creation_flags & WALLET_FLAG_DESCRIPTORS)) {
result.Update({util::Error{Untranslated("Descriptor support must be enabled when using an external signer")}, DatabaseStatus::FAILED_CREATE});
result.Update({util::Error{Untranslated("Descriptor support must be enabled when using an external signer")}, DatabaseError::FAILED_CREATE});
return result;
}

// Do not allow a passphrase when private keys are disabled
if (!passphrase.empty() && (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
result.Update({util::Error{Untranslated("Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled.")}, DatabaseStatus::FAILED_CREATE});
result.Update({util::Error{Untranslated("Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled.")}, DatabaseError::FAILED_CREATE});
return result;
}

Expand All @@ -409,29 +409,29 @@ util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> CreateWallet(WalletCon
if (!database) {
auto& errors{result.EnsureMessages().errors};
errors.insert(errors.begin(), Untranslated("Wallet file verification failed."));
result.Update({util::Error{}, DatabaseStatus::FAILED_VERIFY});
result.Update({util::Error{}, DatabaseError::FAILED_VERIFY});
return result;
}

// Make the wallet
context.chain->initMessage(_("Loading wallet…").translated);
auto create{CWallet::Create(context, name, std::move(database.value()), wallet_creation_flags) >> result};
if (!create) {
result.Update({util::Error{Untranslated("Wallet creation failed.")}, DatabaseStatus::FAILED_CREATE});
result.Update({util::Error{Untranslated("Wallet creation failed.")}, DatabaseError::FAILED_CREATE});
return result;
}
std::shared_ptr<CWallet> wallet = create.value();

// Encrypt the wallet
if (!passphrase.empty() && !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
if (!wallet->EncryptWallet(passphrase)) {
result.Update({util::Error{Untranslated("Error: Wallet created but failed to encrypt.")}, DatabaseStatus::FAILED_ENCRYPT});
result.Update({util::Error{Untranslated("Error: Wallet created but failed to encrypt.")}, DatabaseError::FAILED_ENCRYPT});
return result;
}
if (!create_blank) {
// Unlock the wallet
if (!wallet->Unlock(passphrase)) {
result.Update({util::Error{Untranslated("Error: Wallet was encrypted but could not be unlocked")}, DatabaseStatus::FAILED_ENCRYPT});
result.Update({util::Error{Untranslated("Error: Wallet was encrypted but could not be unlocked")}, DatabaseError::FAILED_ENCRYPT});
return result;
}

Expand All @@ -443,7 +443,7 @@ util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> CreateWallet(WalletCon
} else {
for (auto spk_man : wallet->GetActiveScriptPubKeyMans()) {
if (!spk_man->SetupGeneration()) {
result.Update({util::Error{Untranslated("Unable to generate initial keys")}, DatabaseStatus::FAILED_CREATE});
result.Update({util::Error{Untranslated("Unable to generate initial keys")}, DatabaseError::FAILED_CREATE});
return result;
}
}
Expand Down Expand Up @@ -471,9 +471,9 @@ util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> CreateWallet(WalletCon
return result;
}

util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start)
util::ResultPtr<std::shared_ptr<CWallet>, DatabaseError> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start)
{
util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> result;
util::ResultPtr<std::shared_ptr<CWallet>, DatabaseError> result;
DatabaseOptions options;
ReadDatabaseArgs(*context.args, options);
options.require_existing = true;
Expand All @@ -483,12 +483,12 @@ util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> RestoreWallet(WalletCo

try {
if (!fs::exists(backup_file)) {
result.Update({util::Error{Untranslated("Backup file does not exist")}, DatabaseStatus::FAILED_INVALID_BACKUP_FILE});
result.Update({util::Error{Untranslated("Backup file does not exist")}, DatabaseError::FAILED_INVALID_BACKUP_FILE});
return result;
}

if (fs::exists(wallet_path) || !TryCreateDirectories(wallet_path)) {
result.Update({util::Error{Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path)))}, DatabaseStatus::FAILED_ALREADY_EXISTS});
result.Update({util::Error{Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path)))}, DatabaseError::FAILED_ALREADY_EXISTS});
return result;
}

Expand All @@ -497,7 +497,7 @@ util::ResultPtr<std::shared_ptr<CWallet>, DatabaseStatus> RestoreWallet(WalletCo
result.Update(LoadWallet(context, wallet_name, load_on_start, options));
} catch (const std::exception& e) {
assert(!result.value());
result.Update({util::Error{strprintf(Untranslated("Unexpected exception: %s"), e.what())}, DatabaseStatus::FAILED_LOAD});
result.Update({util::Error{strprintf(Untranslated("Unexpected exception: %s"), e.what())}, DatabaseError::FAILED_LOAD});
return result;
}
if (!result) {
Expand Down Expand Up @@ -2914,7 +2914,7 @@ bool CWallet::EraseAddressReceiveRequest(WalletBatch& batch, const CTxDestinatio
return true;
}

util::ResultPtr<std::unique_ptr<WalletDatabase>, DatabaseStatus> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options)
util::ResultPtr<std::unique_ptr<WalletDatabase>, DatabaseError> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options)
{
// Do some checking on wallet path. It should be either a:
//
Expand All @@ -2932,7 +2932,7 @@ util::ResultPtr<std::unique_ptr<WalletDatabase>, DatabaseStatus> MakeWalletDatab
"database/log.?????????? files can be stored, a location where such a directory could be created, "
"or (for backwards compatibility) the name of an existing data file in -walletdir (%s)",
name, fs::quoted(fs::PathToString(GetWalletDir()))))},
DatabaseStatus::FAILED_BAD_PATH};
DatabaseError::FAILED_BAD_PATH};
}
return MakeDatabase(wallet_path, options);
}
Expand Down

0 comments on commit ae0b9b8

Please sign in to comment.