Skip to content

Commit 81ea66c

Browse files
ryanofskypromag
authored andcommitted
Drop signal CClientUIInterface::LoadWallet
1 parent be50469 commit 81ea66c

10 files changed

+46
-21
lines changed

src/dummywallet.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ enum class WalletCreationStatus;
1111

1212
namespace interfaces {
1313
class Chain;
14+
class Handler;
15+
class Wallet;
1416
}
1517

1618
class DummyWalletInit : public WalletInitInterface {
@@ -80,9 +82,13 @@ WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString&
8082
throw std::logic_error("Wallet function called in non-wallet build.");
8183
}
8284

83-
namespace interfaces {
85+
using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wallet)>;
86+
std::unique_ptr<interfaces::Handler> HandleLoadWallet(LoadWalletFn load_wallet)
87+
{
88+
throw std::logic_error("Wallet function called in non-wallet build.");
89+
}
8490

85-
class Wallet;
91+
namespace interfaces {
8692

8793
std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet)
8894
{

src/interfaces/chain.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ class ChainImpl : public Chain
342342
void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }
343343
void initWarning(const std::string& message) override { InitWarning(message); }
344344
void initError(const std::string& message) override { InitError(message); }
345-
void loadWallet(std::unique_ptr<Wallet> wallet) override { ::uiInterface.LoadWallet(wallet); }
346345
void showProgress(const std::string& title, int progress, bool resume_possible) override
347346
{
348347
::uiInterface.ShowProgress(title, progress, resume_possible);

src/interfaces/chain.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Wallet;
4343
//! asynchronously
4444
//! (https://github.com/bitcoin/bitcoin/pull/10973#issuecomment-380101269).
4545
//!
46-
//! * The initMessages() and loadWallet() methods which the wallet uses to send
46+
//! * The initMessage() and showProgress() methods which the wallet uses to send
4747
//! notifications to the GUI should go away when GUI and wallet can directly
4848
//! communicate with each other without going through the node
4949
//! (https://github.com/bitcoin/bitcoin/pull/15288#discussion_r253321096).
@@ -213,9 +213,6 @@ class Chain
213213
//! Send init error.
214214
virtual void initError(const std::string& message) = 0;
215215

216-
//! Send wallet load notification to the GUI.
217-
virtual void loadWallet(std::unique_ptr<Wallet> wallet) = 0;
218-
219216
//! Send progress indicator.
220217
virtual void showProgress(const std::string& title, int progress, bool resume_possible) = 0;
221218

src/interfaces/handler.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,25 @@ class HandlerImpl : public Handler
2222
boost::signals2::scoped_connection m_connection;
2323
};
2424

25+
class CleanupHandler : public Handler
26+
{
27+
public:
28+
explicit CleanupHandler(std::function<void()> cleanup) : m_cleanup(std::move(cleanup)) {}
29+
~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
30+
void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
31+
std::function<void()> m_cleanup;
32+
};
33+
2534
} // namespace
2635

2736
std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection)
2837
{
2938
return MakeUnique<HandlerImpl>(std::move(connection));
3039
}
3140

41+
std::unique_ptr<Handler> MakeHandler(std::function<void()> cleanup)
42+
{
43+
return MakeUnique<CleanupHandler>(std::move(cleanup));
44+
}
45+
3246
} // namespace interfaces

src/interfaces/handler.h

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_INTERFACES_HANDLER_H
66
#define BITCOIN_INTERFACES_HANDLER_H
77

8+
#include <functional>
89
#include <memory>
910

1011
namespace boost {
@@ -30,6 +31,9 @@ class Handler
3031
//! Return handler wrapping a boost signal connection.
3132
std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection);
3233

34+
//! Return handler wrapping a cleanup function.
35+
std::unique_ptr<Handler> MakeHandler(std::function<void()> cleanup);
36+
3337
} // namespace interfaces
3438

3539
#endif // BITCOIN_INTERFACES_HANDLER_H

src/interfaces/node.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ std::vector<fs::path> ListWalletDir();
4242
std::vector<std::shared_ptr<CWallet>> GetWallets();
4343
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::vector<std::string>& warnings);
4444
WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::vector<std::string>& warnings, std::shared_ptr<CWallet>& result);
45+
std::unique_ptr<interfaces::Handler> HandleLoadWallet(interfaces::Node::LoadWalletFn load_wallet);
4546

4647
namespace interfaces {
4748

48-
class Wallet;
49-
5049
namespace {
5150

5251
class NodeImpl : public Node
@@ -282,7 +281,7 @@ class NodeImpl : public Node
282281
}
283282
std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override
284283
{
285-
return MakeHandler(::uiInterface.LoadWallet_connect([fn](std::unique_ptr<Wallet>& wallet) { fn(std::move(wallet)); }));
284+
return HandleLoadWallet(std::move(fn));
286285
}
287286
std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
288287
{

src/ui_interface.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ struct UISignals {
1616
boost::signals2::signal<CClientUIInterface::NotifyNumConnectionsChangedSig> NotifyNumConnectionsChanged;
1717
boost::signals2::signal<CClientUIInterface::NotifyNetworkActiveChangedSig> NotifyNetworkActiveChanged;
1818
boost::signals2::signal<CClientUIInterface::NotifyAlertChangedSig> NotifyAlertChanged;
19-
boost::signals2::signal<CClientUIInterface::LoadWalletSig> LoadWallet;
2019
boost::signals2::signal<CClientUIInterface::ShowProgressSig> ShowProgress;
2120
boost::signals2::signal<CClientUIInterface::NotifyBlockTipSig> NotifyBlockTip;
2221
boost::signals2::signal<CClientUIInterface::NotifyHeaderTipSig> NotifyHeaderTip;
@@ -36,7 +35,6 @@ ADD_SIGNALS_IMPL_WRAPPER(InitMessage);
3635
ADD_SIGNALS_IMPL_WRAPPER(NotifyNumConnectionsChanged);
3736
ADD_SIGNALS_IMPL_WRAPPER(NotifyNetworkActiveChanged);
3837
ADD_SIGNALS_IMPL_WRAPPER(NotifyAlertChanged);
39-
ADD_SIGNALS_IMPL_WRAPPER(LoadWallet);
4038
ADD_SIGNALS_IMPL_WRAPPER(ShowProgress);
4139
ADD_SIGNALS_IMPL_WRAPPER(NotifyBlockTip);
4240
ADD_SIGNALS_IMPL_WRAPPER(NotifyHeaderTip);
@@ -48,7 +46,6 @@ void CClientUIInterface::InitMessage(const std::string& message) { return g_ui_s
4846
void CClientUIInterface::NotifyNumConnectionsChanged(int newNumConnections) { return g_ui_signals.NotifyNumConnectionsChanged(newNumConnections); }
4947
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
5048
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
51-
void CClientUIInterface::LoadWallet(std::unique_ptr<interfaces::Wallet>& wallet) { return g_ui_signals.LoadWallet(wallet); }
5249
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
5350
void CClientUIInterface::NotifyBlockTip(bool b, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(b, i); }
5451
void CClientUIInterface::NotifyHeaderTip(bool b, const CBlockIndex* i) { return g_ui_signals.NotifyHeaderTip(b, i); }

src/ui_interface.h

-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ class connection;
1717
}
1818
} // namespace boost
1919

20-
namespace interfaces {
21-
class Wallet;
22-
} // namespace interfaces
23-
2420
/** General change type (added, updated, removed). */
2521
enum ChangeType
2622
{
@@ -105,9 +101,6 @@ class CClientUIInterface
105101
*/
106102
ADD_SIGNALS_DECL_WRAPPER(NotifyAlertChanged, void, );
107103

108-
/** A wallet has been loaded. */
109-
ADD_SIGNALS_DECL_WRAPPER(LoadWallet, void, std::unique_ptr<interfaces::Wallet>& wallet);
110-
111104
/**
112105
* Show progress e.g. for verifychain.
113106
* resume_possible indicates shutting down now will result in the current progress action resuming upon restart.

src/wallet/wallet.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static const size_t OUTPUT_GROUP_MAX_ENTRIES = 10;
4747

4848
static CCriticalSection cs_wallets;
4949
static std::vector<std::shared_ptr<CWallet>> vpwallets GUARDED_BY(cs_wallets);
50+
static std::list<LoadWalletFn> g_load_wallet_fns GUARDED_BY(cs_wallets);
5051

5152
bool AddWallet(const std::shared_ptr<CWallet>& wallet)
5253
{
@@ -89,6 +90,13 @@ std::shared_ptr<CWallet> GetWallet(const std::string& name)
8990
return nullptr;
9091
}
9192

93+
std::unique_ptr<interfaces::Handler> HandleLoadWallet(LoadWalletFn load_wallet)
94+
{
95+
LOCK(cs_wallets);
96+
auto it = g_load_wallet_fns.emplace(g_load_wallet_fns.end(), std::move(load_wallet));
97+
return interfaces::MakeHandler([it] { LOCK(cs_wallets); g_load_wallet_fns.erase(it); });
98+
}
99+
92100
static Mutex g_wallet_release_mutex;
93101
static std::condition_variable g_wallet_release_cv;
94102
static std::set<std::string> g_unloading_wallet_set;
@@ -4562,7 +4570,12 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
45624570
}
45634571
}
45644572

4565-
chain.loadWallet(interfaces::MakeWallet(walletInstance));
4573+
{
4574+
LOCK(cs_wallets);
4575+
for (auto& load_wallet : g_load_wallet_fns) {
4576+
load_wallet(interfaces::MakeWallet(walletInstance));
4577+
}
4578+
}
45664579

45674580
// Register with the validation interface. It's ok to do this after rescan since we're still holding locked_chain.
45684581
walletInstance->handleNotifications();

src/wallet/wallet.h

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
#include <boost/signals2/signal.hpp>
3737

38+
using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wallet)>;
39+
3840
//! Explicitly unload and delete the wallet.
3941
//! Blocks the current thread after signaling the unload intent so that all
4042
//! wallet clients release the wallet.
@@ -48,6 +50,7 @@ bool HasWallets();
4850
std::vector<std::shared_ptr<CWallet>> GetWallets();
4951
std::shared_ptr<CWallet> GetWallet(const std::string& name);
5052
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const WalletLocation& location, std::string& error, std::vector<std::string>& warnings);
53+
std::unique_ptr<interfaces::Handler> HandleLoadWallet(LoadWalletFn load_wallet);
5154

5255
enum class WalletCreationStatus {
5356
SUCCESS,

0 commit comments

Comments
 (0)