Skip to content

Commit 07df6cd

Browse files
committed
wallet: Return util::Result from WalletLoader methods
1 parent a6fc293 commit 07df6cd

File tree

4 files changed

+47
-35
lines changed

4 files changed

+47
-35
lines changed

src/interfaces/wallet.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Wallet
8888
virtual std::string getWalletName() = 0;
8989

9090
// Get a new address.
91-
virtual util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string label) = 0;
91+
virtual util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string& label) = 0;
9292

9393
//! Get public key.
9494
virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;
@@ -320,31 +320,31 @@ class WalletLoader : public ChainClient
320320
{
321321
public:
322322
//! Create new wallet.
323-
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;
323+
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;
324324

325-
//! Load existing wallet.
326-
virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
325+
//! Load existing wallet.
326+
virtual util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) = 0;
327327

328-
//! Return default wallet directory.
329-
virtual std::string getWalletDir() = 0;
328+
//! Return default wallet directory.
329+
virtual std::string getWalletDir() = 0;
330330

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

334-
//! Return available wallets in wallet directory.
335-
virtual std::vector<std::string> listWalletDir() = 0;
334+
//! Return available wallets in wallet directory.
335+
virtual std::vector<std::string> listWalletDir() = 0;
336336

337-
//! Return interfaces for accessing wallets (if any).
338-
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
337+
//! Return interfaces for accessing wallets (if any).
338+
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
339339

340-
//! Register handler for load wallet messages. This callback is triggered by
341-
//! createWallet and loadWallet above, and also triggered when wallets are
342-
//! loaded at startup or by RPC.
343-
using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
344-
virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
340+
//! Register handler for load wallet messages. This callback is triggered by
341+
//! createWallet and loadWallet above, and also triggered when wallets are
342+
//! loaded at startup or by RPC.
343+
using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
344+
virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
345345

346-
//! Return pointer to internal context, useful for testing.
347-
virtual wallet::WalletContext* context() { return nullptr; }
346+
//! Return pointer to internal context, useful for testing.
347+
virtual wallet::WalletContext* context() { return nullptr; }
348348
};
349349

350350
//! Information about one wallet address.

src/qt/walletcontroller.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,13 @@ void CreateWalletActivity::createWallet()
262262
}
263263

264264
QTimer::singleShot(500ms, worker(), [this, name, flags] {
265-
std::unique_ptr<interfaces::Wallet> wallet = node().walletLoader().createWallet(name, m_passphrase, flags, m_error_message, m_warning_message);
265+
auto wallet{node().walletLoader().createWallet(name, m_passphrase, flags, m_warning_message)};
266266

267-
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
267+
if (wallet) {
268+
m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet));
269+
} else {
270+
m_error_message = util::ErrorString(wallet);
271+
}
268272

269273
QTimer::singleShot(500ms, this, &CreateWalletActivity::finish);
270274
});
@@ -343,9 +347,13 @@ void OpenWalletActivity::open(const std::string& path)
343347
tr("Opening Wallet <b>%1</b>…").arg(name.toHtmlEscaped()));
344348

345349
QTimer::singleShot(0, worker(), [this, path] {
346-
std::unique_ptr<interfaces::Wallet> wallet = node().walletLoader().loadWallet(path, m_error_message, m_warning_message);
350+
auto wallet{node().walletLoader().loadWallet(path, m_warning_message)};
347351

348-
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
352+
if (wallet) {
353+
m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet));
354+
} else {
355+
m_error_message = util::ErrorString(wallet);
356+
}
349357

350358
QTimer::singleShot(0, this, &OpenWalletActivity::finish);
351359
});
@@ -393,8 +401,11 @@ void RestoreWalletActivity::restore(const fs::path& backup_file, const std::stri
393401
QTimer::singleShot(0, worker(), [this, backup_file, wallet_name] {
394402
auto wallet{node().walletLoader().restoreWallet(backup_file, wallet_name, m_warning_message)};
395403

396-
m_error_message = util::ErrorString(wallet);
397-
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet));
404+
if (wallet) {
405+
m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(*wallet));
406+
} else {
407+
m_error_message = util::ErrorString(wallet);
408+
}
398409

399410
QTimer::singleShot(0, this, &RestoreWalletActivity::finish);
400411
});

src/wallet/interfaces.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class WalletImpl : public Wallet
148148
void abortRescan() override { m_wallet->AbortRescan(); }
149149
bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); }
150150
std::string getWalletName() override { return m_wallet->GetName(); }
151-
util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string label) override
151+
util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string& label) override
152152
{
153153
LOCK(m_wallet->cs_wallet);
154154
return m_wallet->GetNewDestination(type, label);
@@ -551,32 +551,34 @@ class WalletLoaderImpl : public WalletLoader
551551
void setMockTime(int64_t time) override { return SetMockTime(time); }
552552

553553
//! WalletLoader methods
554-
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
554+
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
555555
{
556-
std::shared_ptr<CWallet> wallet;
557556
DatabaseOptions options;
558557
DatabaseStatus status;
559558
ReadDatabaseArgs(*m_context.args, options);
560559
options.require_create = true;
561560
options.create_flags = wallet_creation_flags;
562561
options.create_passphrase = passphrase;
563-
return MakeWallet(m_context, CreateWallet(m_context, name, true /* load_on_start */, options, status, error, warnings));
562+
bilingual_str error;
563+
util::Result<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, CreateWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))};
564+
return wallet ? std::move(wallet) : util::Error{error};
564565
}
565-
std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
566+
util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) override
566567
{
567568
DatabaseOptions options;
568569
DatabaseStatus status;
569570
ReadDatabaseArgs(*m_context.args, options);
570571
options.require_existing = true;
571-
return MakeWallet(m_context, LoadWallet(m_context, name, true /* load_on_start */, options, status, error, warnings));
572+
bilingual_str error;
573+
util::Result<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, LoadWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))};
574+
return wallet ? std::move(wallet) : util::Error{error};
572575
}
573576
util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) override
574577
{
575578
DatabaseStatus status;
576579
bilingual_str error;
577580
util::Result<std::unique_ptr<Wallet>> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings))};
578-
if (!wallet) return util::Error{error};
579-
return wallet;
581+
return wallet ? std::move(wallet) : util::Error{error};
580582
}
581583
std::string getWalletDir() override
582584
{

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,10 +919,9 @@ BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup)
919919
// Add tx to wallet
920920
const auto& op_dest = wallet.GetNewDestination(OutputType::BECH32M, "");
921921
BOOST_ASSERT(op_dest);
922-
const CTxDestination& dest = *op_dest;
923922

924923
CMutableTransaction mtx;
925-
mtx.vout.push_back({COIN, GetScriptForDestination(dest)});
924+
mtx.vout.push_back({COIN, GetScriptForDestination(*op_dest)});
926925
mtx.vin.push_back(CTxIn(g_insecure_rand_ctx.rand256(), 0));
927926
const auto& tx_id_to_spend = wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInMempool{})->GetHash();
928927

0 commit comments

Comments
 (0)