Skip to content

Commit 27933a7

Browse files
committed
merge bitcoin#25594: Return BResult from restoreWallet
1 parent b75f1ad commit 27933a7

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/interfaces/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <script/standard.h> // For CTxDestination
1313
#include <support/allocators/secure.h> // For SecureString
1414
#include <util/message.h>
15+
#include <util/result.h>
1516
#include <util/ui_change_type.h>
1617

1718
#include <cstdint>
@@ -357,7 +358,7 @@ class WalletLoader : public ChainClient
357358
virtual std::string getWalletDir() = 0;
358359

359360
//! Restore backup wallet
360-
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;
361+
virtual BResult<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) = 0;
361362

362363
//! Return available wallets in wallet directory.
363364
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
@@ -373,9 +373,10 @@ void RestoreWalletActivity::restore(const fs::path& backup_file, const std::stri
373373
tr("Restoring Wallet <b>%1</b>…").arg(name.toHtmlEscaped()));
374374

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

378-
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
378+
m_error_message = wallet ? bilingual_str{} : wallet.GetError();
379+
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(wallet.ReleaseObj());
379380

380381
QTimer::singleShot(0, this, &RestoreWalletActivity::finish);
381382
});

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,13 @@ class WalletLoaderImpl : public WalletLoader
628628
options.require_existing = true;
629629
return MakeWallet(m_context, LoadWallet(m_context, name, true /* load_on_start */, options, status, error, warnings));
630630
}
631-
std::unique_ptr<Wallet> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
631+
BResult<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) override
632632
{
633633
DatabaseStatus status;
634-
return MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings));
634+
bilingual_str error;
635+
BResult<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings))};
636+
if (!wallet) return error;
637+
return wallet;
635638
}
636639
std::string getWalletDir() override
637640
{

0 commit comments

Comments
 (0)