-
Notifications
You must be signed in to change notification settings - Fork 39.2k
refactor: Return util::Result from WalletLoader methods
#25616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,7 +88,7 @@ class Wallet | |
| virtual std::string getWalletName() = 0; | ||
|
|
||
| // 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. | ||
| virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0; | ||
|
|
@@ -320,31 +320,31 @@ class WalletLoader : public ChainClient | |
| { | ||
| public: | ||
| //! 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would probably be best to also wrap the warnings in the result? Are you working on this @ryanofsky ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re: #25616 (comment)
It needs rebase, but yes I did this in #25722
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I meant a minimal extract without the other C++ bloat (for example |
||
|
|
||
| //! Load existing wallet. | ||
| virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0; | ||
| //! Load existing wallet. | ||
| virtual util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) = 0; | ||
|
|
||
| //! Return default wallet directory. | ||
| virtual std::string getWalletDir() = 0; | ||
| //! Return default wallet directory. | ||
| virtual std::string getWalletDir() = 0; | ||
|
|
||
| //! 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; | ||
| //! 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; | ||
|
|
||
| //! Return available wallets in wallet directory. | ||
| virtual std::vector<std::string> listWalletDir() = 0; | ||
| //! Return available wallets in wallet directory. | ||
| virtual std::vector<std::string> listWalletDir() = 0; | ||
|
|
||
| //! Return interfaces for accessing wallets (if any). | ||
| virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0; | ||
| //! Return interfaces for accessing wallets (if any). | ||
| virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0; | ||
|
|
||
| //! Register handler for load wallet messages. This callback is triggered by | ||
| //! createWallet and loadWallet above, and also triggered when wallets are | ||
| //! loaded at startup or by RPC. | ||
| using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>; | ||
| virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0; | ||
| //! Register handler for load wallet messages. This callback is triggered by | ||
| //! createWallet and loadWallet above, and also triggered when wallets are | ||
| //! loaded at startup or by RPC. | ||
| using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>; | ||
| virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0; | ||
|
|
||
| //! Return pointer to internal context, useful for testing. | ||
| virtual wallet::WalletContext* context() { return nullptr; } | ||
| //! Return pointer to internal context, useful for testing. | ||
| virtual wallet::WalletContext* context() { return nullptr; } | ||
| }; | ||
|
|
||
| //! Information about one wallet address. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -262,9 +262,13 @@ void CreateWalletActivity::createWallet() | |
| } | ||
|
|
||
| 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); | ||
| }); | ||
|
|
@@ -343,9 +347,13 @@ void OpenWalletActivity::open(const std::string& path) | |
| tr("Opening Wallet <b>%1</b>…").arg(name.toHtmlEscaped())); | ||
|
|
||
| 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); | ||
| }); | ||
|
|
@@ -393,8 +401,11 @@ void RestoreWalletActivity::restore(const fs::path& backup_file, const std::stri | |
| QTimer::singleShot(0, worker(), [this, backup_file, wallet_name] { | ||
| auto wallet{node().walletLoader().restoreWallet(backup_file, wallet_name, m_warning_message)}; | ||
|
|
||
| m_error_message = util::ErrorString(wallet); | ||
| if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I preferred the previous code, as it is shorter and less ambiguous |
||
| if (wallet) { | ||
| m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet)); | ||
| } else { | ||
| m_error_message = util::ErrorString(wallet); | ||
| } | ||
|
|
||
| QTimer::singleShot(0, this, &RestoreWalletActivity::finish); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -919,10 +919,9 @@ BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup) | |
| // Add tx to wallet | ||
| const auto& op_dest = wallet.GetNewDestination(OutputType::BECH32M, ""); | ||
| BOOST_ASSERT(op_dest); | ||
| const CTxDestination& dest = *op_dest; | ||
|
|
||
| CMutableTransaction mtx; | ||
| mtx.vout.push_back({COIN, GetScriptForDestination(dest)}); | ||
| mtx.vout.push_back({COIN, GetScriptForDestination(*op_dest)}); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for picking up #25721 (comment) here. |
||
| mtx.vin.push_back(CTxIn(g_insecure_rand_ctx.rand256(), 0)); | ||
| const auto& tx_id_to_spend = wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInMempool{})->GetHash(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for picking up #25721 (comment) here and fixing up indentation in this file.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The wallet method still creates a copy?