Skip to content

Commit 062b9db

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25594: refactor: Return BResult from restoreWallet
fa475e9 refactor: Return BResult from restoreWallet (MacroFake) fa8de09 Prepare BResult for non-copyable types (MacroFake) Pull request description: This avoids the `error` in-out param (and if `warnings` is added to `BResult`, it will avoid passing that in-out param as well). Also, as it is needed for this change, prepare `BResult` for non-copyable types. ACKs for top commit: w0xlt: reACK bitcoin/bitcoin@fa475e9 ryanofsky: Code review ACK fa475e9. Changes since last review were replacing auto with explicit type and splitting commits Tree-SHA512: 46350883572f13721ddd198f5dfb88d2fa58ebcbda416f74da3563ea15c920fb1e6ff30558526a4ac91c36c21e6afe27751a4e51b7b8bcbcbe805209f4e9014b
2 parents 8efa73e + fa475e9 commit 062b9db

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

src/interfaces/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ class WalletLoader : public ChainClient
329329
virtual std::string getWalletDir() = 0;
330330

331331
//! Restore backup wallet
332-
virtual std::unique_ptr<Wallet> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
332+
virtual BResult<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) = 0;
333333

334334
//! Return available wallets in wallet directory.
335335
virtual std::vector<std::string> listWalletDir() = 0;

src/qt/walletcontroller.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,10 @@ void RestoreWalletActivity::restore(const fs::path& backup_file, const std::stri
391391
tr("Restoring Wallet <b>%1</b>…").arg(name.toHtmlEscaped()));
392392

393393
QTimer::singleShot(0, worker(), [this, backup_file, wallet_name] {
394-
std::unique_ptr<interfaces::Wallet> wallet = node().walletLoader().restoreWallet(backup_file, wallet_name, m_error_message, m_warning_message);
394+
auto wallet{node().walletLoader().restoreWallet(backup_file, wallet_name, m_warning_message)};
395395

396-
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
396+
m_error_message = wallet ? bilingual_str{} : wallet.GetError();
397+
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(wallet.ReleaseObj());
397398

398399
QTimer::singleShot(0, this, &RestoreWalletActivity::finish);
399400
});

src/util/result.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_UTIL_RESULT_H
77

88
#include <util/translation.h>
9+
910
#include <variant>
1011

1112
/*
@@ -18,9 +19,9 @@ class BResult {
1819
std::variant<bilingual_str, T> m_variant;
1920

2021
public:
21-
BResult() : m_variant(Untranslated("")) {}
22-
BResult(const T& _obj) : m_variant(_obj) {}
23-
BResult(const bilingual_str& error) : m_variant(error) {}
22+
BResult() : m_variant{Untranslated("")} {}
23+
BResult(T obj) : m_variant{std::move(obj)} {}
24+
BResult(bilingual_str error) : m_variant{std::move(error)} {}
2425

2526
/* Whether the function succeeded or not */
2627
bool HasRes() const { return std::holds_alternative<T>(m_variant); }
@@ -30,6 +31,11 @@ class BResult {
3031
assert(HasRes());
3132
return std::get<T>(m_variant);
3233
}
34+
T ReleaseObj()
35+
{
36+
assert(HasRes());
37+
return std::move(std::get<T>(m_variant));
38+
}
3339

3440
/* In case of failure, the error cause */
3541
const bilingual_str& GetError() const {

src/wallet/interfaces.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,13 @@ class WalletLoaderImpl : public WalletLoader
569569
options.require_existing = true;
570570
return MakeWallet(m_context, LoadWallet(m_context, name, true /* load_on_start */, options, status, error, warnings));
571571
}
572-
std::unique_ptr<Wallet> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
572+
BResult<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) override
573573
{
574574
DatabaseStatus status;
575-
576-
return MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings));
575+
bilingual_str error;
576+
BResult<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings))};
577+
if (!wallet) return error;
578+
return wallet;
577579
}
578580
std::string getWalletDir() override
579581
{

0 commit comments

Comments
 (0)