0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-05 14:06:27 -05:00

wallet: Return util::Result from WalletLoader methods

This commit is contained in:
w0xlt 2022-08-07 00:56:25 -03:00
parent a6fc293c0a
commit 07df6cda14
4 changed files with 47 additions and 35 deletions

View file

@ -88,7 +88,7 @@ public:
virtual std::string getWalletName() = 0; virtual std::string getWalletName() = 0;
// Get a new address. // Get a new address.
virtual util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string label) = 0; virtual util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string& label) = 0;
//! Get public key. //! Get public key.
virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0; virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;
@ -320,31 +320,31 @@ class WalletLoader : public ChainClient
{ {
public: public:
//! Create new wallet. //! Create new wallet.
virtual std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0; virtual util::Result<std::unique_ptr<Wallet>> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, std::vector<bilingual_str>& warnings) = 0;
//! Load existing wallet. //! Load existing wallet.
virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0; virtual util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) = 0;
//! Return default wallet directory. //! Return default wallet directory.
virtual std::string getWalletDir() = 0; virtual std::string getWalletDir() = 0;
//! Restore backup wallet //! Restore backup wallet
virtual util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) = 0; virtual util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) = 0;
//! Return available wallets in wallet directory. //! Return available wallets in wallet directory.
virtual std::vector<std::string> listWalletDir() = 0; virtual std::vector<std::string> listWalletDir() = 0;
//! Return interfaces for accessing wallets (if any). //! Return interfaces for accessing wallets (if any).
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0; virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
//! Register handler for load wallet messages. This callback is triggered by //! Register handler for load wallet messages. This callback is triggered by
//! createWallet and loadWallet above, and also triggered when wallets are //! createWallet and loadWallet above, and also triggered when wallets are
//! loaded at startup or by RPC. //! loaded at startup or by RPC.
using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>; using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0; virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
//! Return pointer to internal context, useful for testing. //! Return pointer to internal context, useful for testing.
virtual wallet::WalletContext* context() { return nullptr; } virtual wallet::WalletContext* context() { return nullptr; }
}; };
//! Information about one wallet address. //! Information about one wallet address.

View file

@ -262,9 +262,13 @@ void CreateWalletActivity::createWallet()
} }
QTimer::singleShot(500ms, worker(), [this, name, flags] { QTimer::singleShot(500ms, worker(), [this, name, flags] {
std::unique_ptr<interfaces::Wallet> wallet = node().walletLoader().createWallet(name, m_passphrase, flags, m_error_message, m_warning_message); auto wallet{node().walletLoader().createWallet(name, m_passphrase, flags, m_warning_message)};
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet)); if (wallet) {
m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet));
} else {
m_error_message = util::ErrorString(wallet);
}
QTimer::singleShot(500ms, this, &CreateWalletActivity::finish); QTimer::singleShot(500ms, this, &CreateWalletActivity::finish);
}); });
@ -343,9 +347,13 @@ void OpenWalletActivity::open(const std::string& path)
tr("Opening Wallet <b>%1</b>…").arg(name.toHtmlEscaped())); tr("Opening Wallet <b>%1</b>…").arg(name.toHtmlEscaped()));
QTimer::singleShot(0, worker(), [this, path] { QTimer::singleShot(0, worker(), [this, path] {
std::unique_ptr<interfaces::Wallet> wallet = node().walletLoader().loadWallet(path, m_error_message, m_warning_message); auto wallet{node().walletLoader().loadWallet(path, m_warning_message)};
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet)); if (wallet) {
m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet));
} else {
m_error_message = util::ErrorString(wallet);
}
QTimer::singleShot(0, this, &OpenWalletActivity::finish); QTimer::singleShot(0, this, &OpenWalletActivity::finish);
}); });
@ -393,8 +401,11 @@ void RestoreWalletActivity::restore(const fs::path& backup_file, const std::stri
QTimer::singleShot(0, worker(), [this, backup_file, wallet_name] { QTimer::singleShot(0, worker(), [this, backup_file, wallet_name] {
auto wallet{node().walletLoader().restoreWallet(backup_file, wallet_name, m_warning_message)}; auto wallet{node().walletLoader().restoreWallet(backup_file, wallet_name, m_warning_message)};
m_error_message = util::ErrorString(wallet); if (wallet) {
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet)); m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet));
} else {
m_error_message = util::ErrorString(wallet);
}
QTimer::singleShot(0, this, &RestoreWalletActivity::finish); QTimer::singleShot(0, this, &RestoreWalletActivity::finish);
}); });

View file

@ -148,7 +148,7 @@ public:
void abortRescan() override { m_wallet->AbortRescan(); } void abortRescan() override { m_wallet->AbortRescan(); }
bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); } bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); }
std::string getWalletName() override { return m_wallet->GetName(); } std::string getWalletName() override { return m_wallet->GetName(); }
util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string label) override util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string& label) override
{ {
LOCK(m_wallet->cs_wallet); LOCK(m_wallet->cs_wallet);
return m_wallet->GetNewDestination(type, label); return m_wallet->GetNewDestination(type, label);
@ -551,32 +551,34 @@ public:
void setMockTime(int64_t time) override { return SetMockTime(time); } void setMockTime(int64_t time) override { return SetMockTime(time); }
//! WalletLoader methods //! WalletLoader methods
std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings) override util::Result<std::unique_ptr<Wallet>> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, std::vector<bilingual_str>& warnings) override
{ {
std::shared_ptr<CWallet> wallet;
DatabaseOptions options; DatabaseOptions options;
DatabaseStatus status; DatabaseStatus status;
ReadDatabaseArgs(*m_context.args, options); ReadDatabaseArgs(*m_context.args, options);
options.require_create = true; options.require_create = true;
options.create_flags = wallet_creation_flags; options.create_flags = wallet_creation_flags;
options.create_passphrase = passphrase; options.create_passphrase = passphrase;
return MakeWallet(m_context, CreateWallet(m_context, name, true /* load_on_start */, options, status, error, warnings)); bilingual_str error;
util::Result<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, CreateWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))};
return wallet ? std::move(wallet) : util::Error{error};
} }
std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) override util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) override
{ {
DatabaseOptions options; DatabaseOptions options;
DatabaseStatus status; DatabaseStatus status;
ReadDatabaseArgs(*m_context.args, options); ReadDatabaseArgs(*m_context.args, options);
options.require_existing = true; options.require_existing = true;
return MakeWallet(m_context, LoadWallet(m_context, name, true /* load_on_start */, options, status, error, warnings)); bilingual_str error;
util::Result<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, LoadWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))};
return wallet ? std::move(wallet) : util::Error{error};
} }
util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) override util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) override
{ {
DatabaseStatus status; DatabaseStatus status;
bilingual_str error; bilingual_str error;
util::Result<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings))}; util::Result<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings))};
if (!wallet) return util::Error{error}; return wallet ? std::move(wallet) : util::Error{error};
return wallet;
} }
std::string getWalletDir() override std::string getWalletDir() override
{ {

View file

@ -919,10 +919,9 @@ BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup)
// Add tx to wallet // Add tx to wallet
const auto& op_dest = wallet.GetNewDestination(OutputType::BECH32M, ""); const auto& op_dest = wallet.GetNewDestination(OutputType::BECH32M, "");
BOOST_ASSERT(op_dest); BOOST_ASSERT(op_dest);
const CTxDestination& dest = *op_dest;
CMutableTransaction mtx; CMutableTransaction mtx;
mtx.vout.push_back({COIN, GetScriptForDestination(dest)}); mtx.vout.push_back({COIN, GetScriptForDestination(*op_dest)});
mtx.vin.push_back(CTxIn(g_insecure_rand_ctx.rand256(), 0)); mtx.vin.push_back(CTxIn(g_insecure_rand_ctx.rand256(), 0));
const auto& tx_id_to_spend = wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInMempool{})->GetHash(); const auto& tx_id_to_spend = wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInMempool{})->GetHash();