Skip to content

Commit 269a7cc

Browse files
author
MarcoFalke
committed
Merge #19099: refactor: Move wallet methods out of chain.h and node.h
24bf176 gui refactor: Inline SplashScreen::ConnectWallet (Russell Yanofsky) e4f4350 refactor: Move wallet methods out of chain.h and node.h (Russell Yanofsky) b266b3e refactor: Create interfaces earlier during initialization (Russell Yanofsky) Pull request description: Add WalletClient interface so node interface is cleaner and don't need wallet-specific methods. The new NodeContext::wallet_client pointer will also be needed to eliminate global wallet variables like ::vpwallets in #19101, because createWallet(), loadWallet(), getWallets(), etc methods called by the GUI need a way to get a reference to the list of open wallets if it is no longer a global variable. ACKs for top commit: promag: Code review ACK 24bf176. MarcoFalke: ACK 24bf176 🐚 Tree-SHA512: a70d3776cd6723093db8912028c50075ec5fa0a48b961cb1a945f922658f5363754f8380dbb8378ed128c8c858913024f8264740905b8121a35c0d63bfaed7cf
2 parents afffbb1 + 24bf176 commit 269a7cc

23 files changed

+131
-158
lines changed

src/bitcoind.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ static void WaitForShutdown(NodeContext& node)
4444
static bool AppInit(int argc, char* argv[])
4545
{
4646
NodeContext node;
47-
node.chain = interfaces::MakeChain(node);
4847

4948
bool fRet = false;
5049

@@ -144,7 +143,7 @@ static bool AppInit(int argc, char* argv[])
144143
// If locking the data directory failed, exit immediately
145144
return false;
146145
}
147-
fRet = AppInitMain(context, node);
146+
fRet = AppInitInterfaces(node) && AppInitMain(context, node);
148147
}
149148
catch (const std::exception& e) {
150149
PrintExceptionContinue(&e, "AppInit()");

src/dummywallet.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
#include <util/system.h>
66
#include <walletinitinterface.h>
7-
#include <support/allocators/secure.h>
87

98
class CWallet;
10-
enum class WalletCreationStatus;
11-
struct bilingual_str;
129

1310
namespace interfaces {
1411
class Chain;
@@ -59,37 +56,6 @@ void DummyWalletInit::AddWalletOptions(ArgsManager& argsman) const
5956

6057
const WalletInitInterface& g_wallet_init_interface = DummyWalletInit();
6158

62-
fs::path GetWalletDir()
63-
{
64-
throw std::logic_error("Wallet function called in non-wallet build.");
65-
}
66-
67-
std::vector<fs::path> ListWalletDir()
68-
{
69-
throw std::logic_error("Wallet function called in non-wallet build.");
70-
}
71-
72-
std::vector<std::shared_ptr<CWallet>> GetWallets()
73-
{
74-
throw std::logic_error("Wallet function called in non-wallet build.");
75-
}
76-
77-
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings)
78-
{
79-
throw std::logic_error("Wallet function called in non-wallet build.");
80-
}
81-
82-
WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, std::shared_ptr<CWallet>& result)
83-
{
84-
throw std::logic_error("Wallet function called in non-wallet build.");
85-
}
86-
87-
using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wallet)>;
88-
std::unique_ptr<interfaces::Handler> HandleLoadWallet(LoadWalletFn load_wallet)
89-
{
90-
throw std::logic_error("Wallet function called in non-wallet build.");
91-
}
92-
9359
namespace interfaces {
9460

9561
std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet)

src/init.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,17 @@ bool AppInitLockDataDirectory()
12291229
return true;
12301230
}
12311231

1232+
bool AppInitInterfaces(NodeContext& node)
1233+
{
1234+
node.chain = interfaces::MakeChain(node);
1235+
// Create client interfaces for wallets that are supposed to be loaded
1236+
// according to -wallet and -disablewallet options. This only constructs
1237+
// the interfaces, it doesn't load wallet data. Wallets actually get loaded
1238+
// when load() and start() interface methods are called below.
1239+
g_wallet_init_interface.Construct(node);
1240+
return true;
1241+
}
1242+
12321243
bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12331244
{
12341245
const ArgsManager& args = *Assert(node.args);
@@ -1318,12 +1329,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
13181329

13191330
GetMainSignals().RegisterBackgroundSignalScheduler(*node.scheduler);
13201331

1321-
// Create client interfaces for wallets that are supposed to be loaded
1322-
// according to -wallet and -disablewallet options. This only constructs
1323-
// the interfaces, it doesn't load wallet data. Wallets actually get loaded
1324-
// when load() and start() interface methods are called below.
1325-
g_wallet_init_interface.Construct(node);
1326-
13271332
/* Register RPC commands regardless of -server setting so they will be
13281333
* available in the GUI RPC console even if external calls are disabled.
13291334
*/

src/init.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ bool AppInitSanityChecks();
5252
* @pre Parameters should be parsed and config file should be read, AppInitSanityChecks should have been called.
5353
*/
5454
bool AppInitLockDataDirectory();
55+
/**
56+
* Initialize node and wallet interface pointers. Has no prerequisites or side effects besides allocating memory.
57+
*/
58+
bool AppInitInterfaces(NodeContext& node);
5559
/**
5660
* Bitcoin core main initialization.
5761
* @note This should only be done after daemonization. Call Shutdown() if this function fails.

src/interfaces/chain.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,24 +314,11 @@ class ChainClient
314314

315315
//! Set mock time.
316316
virtual void setMockTime(int64_t time) = 0;
317-
318-
//! Return interfaces for accessing wallets (if any).
319-
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
320317
};
321318

322319
//! Return implementation of Chain interface.
323320
std::unique_ptr<Chain> MakeChain(NodeContext& node);
324321

325-
//! Return implementation of ChainClient interface for a wallet client. This
326-
//! function will be undefined in builds where ENABLE_WALLET is false.
327-
//!
328-
//! Currently, wallets are the only chain clients. But in the future, other
329-
//! types of chain clients could be added, such as tools for monitoring,
330-
//! analysis, or fee estimation. These clients need to expose their own
331-
//! MakeXXXClient functions returning their implementations of the ChainClient
332-
//! interface.
333-
std::unique_ptr<ChainClient> MakeWalletClient(Chain& chain, ArgsManager& args, std::vector<std::string> wallet_filenames);
334-
335322
} // namespace interfaces
336323

337324
#endif // BITCOIN_INTERFACES_CHAIN_H

src/interfaces/node.cpp

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <support/allocators/secure.h>
2828
#include <sync.h>
2929
#include <txmempool.h>
30+
#include <util/check.h>
3031
#include <util/ref.h>
3132
#include <util/system.h>
3233
#include <util/translation.h>
@@ -41,16 +42,7 @@
4142

4243
#include <boost/signals2/signal.hpp>
4344

44-
class CWallet;
45-
fs::path GetWalletDir();
46-
std::vector<fs::path> ListWalletDir();
47-
std::vector<std::shared_ptr<CWallet>> GetWallets();
48-
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings);
49-
WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, std::shared_ptr<CWallet>& result);
50-
std::unique_ptr<interfaces::Handler> HandleLoadWallet(interfaces::Node::LoadWalletFn load_wallet);
51-
5245
namespace interfaces {
53-
5446
namespace {
5547

5648
class NodeImpl : public Node
@@ -64,11 +56,10 @@ class NodeImpl : public Node
6456
bool baseInitialize() override
6557
{
6658
return AppInitBasicSetup(gArgs) && AppInitParameterInteraction(gArgs) && AppInitSanityChecks() &&
67-
AppInitLockDataDirectory();
59+
AppInitLockDataDirectory() && AppInitInterfaces(*m_context);
6860
}
6961
bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override
7062
{
71-
m_context->chain = MakeChain(*m_context);
7263
return AppInitMain(m_context_ref, *m_context, tip_info);
7364
}
7465
void appShutdown() override
@@ -240,36 +231,9 @@ class NodeImpl : public Node
240231
LOCK(::cs_main);
241232
return ::ChainstateActive().CoinsTip().GetCoin(output, coin);
242233
}
243-
std::string getWalletDir() override
244-
{
245-
return GetWalletDir().string();
246-
}
247-
std::vector<std::string> listWalletDir() override
248-
{
249-
std::vector<std::string> paths;
250-
for (auto& path : ListWalletDir()) {
251-
paths.push_back(path.string());
252-
}
253-
return paths;
254-
}
255-
std::vector<std::unique_ptr<Wallet>> getWallets() override
234+
WalletClient& walletClient() override
256235
{
257-
std::vector<std::unique_ptr<Wallet>> wallets;
258-
for (auto& client : m_context->chain_clients) {
259-
auto client_wallets = client->getWallets();
260-
std::move(client_wallets.begin(), client_wallets.end(), std::back_inserter(wallets));
261-
}
262-
return wallets;
263-
}
264-
std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
265-
{
266-
return MakeWallet(LoadWallet(*m_context->chain, name, error, warnings));
267-
}
268-
std::unique_ptr<Wallet> createWallet(const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, WalletCreationStatus& status) override
269-
{
270-
std::shared_ptr<CWallet> wallet;
271-
status = CreateWallet(*m_context->chain, passphrase, wallet_creation_flags, name, error, warnings, wallet);
272-
return MakeWallet(wallet);
236+
return *Assert(m_context->wallet_client);
273237
}
274238
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
275239
{
@@ -287,10 +251,6 @@ class NodeImpl : public Node
287251
{
288252
return MakeHandler(::uiInterface.ShowProgress_connect(fn));
289253
}
290-
std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override
291-
{
292-
return HandleLoadWallet(std::move(fn));
293-
}
294254
std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
295255
{
296256
return MakeHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn));

src/interfaces/node.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ class RPCTimerInterface;
2929
class UniValue;
3030
class proxyType;
3131
enum class SynchronizationState;
32-
enum class WalletCreationStatus;
3332
struct CNodeStateStats;
3433
struct NodeContext;
3534
struct bilingual_str;
3635

3736
namespace interfaces {
3837
class Handler;
39-
class Wallet;
38+
class WalletClient;
4039
struct BlockTip;
4140

4241
//! Block and header tip information
@@ -173,22 +172,8 @@ class Node
173172
//! Get unspent outputs associated with a transaction.
174173
virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
175174

176-
//! Return default wallet directory.
177-
virtual std::string getWalletDir() = 0;
178-
179-
//! Return available wallets in wallet directory.
180-
virtual std::vector<std::string> listWalletDir() = 0;
181-
182-
//! Return interfaces for accessing wallets (if any).
183-
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
184-
185-
//! Attempts to load a wallet from file or directory.
186-
//! The loaded wallet is also notified to handlers previously registered
187-
//! with handleLoadWallet.
188-
virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
189-
190-
//! Create a wallet from file
191-
virtual std::unique_ptr<Wallet> createWallet(const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, WalletCreationStatus& status) = 0;
175+
//! Get wallet client.
176+
virtual WalletClient& walletClient() = 0;
192177

193178
//! Register handler for init messages.
194179
using InitMessageFn = std::function<void(const std::string& message)>;
@@ -210,10 +195,6 @@ class Node
210195
using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
211196
virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
212197

213-
//! Register handler for load wallet messages.
214-
using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
215-
virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
216-
217198
//! Register handler for number of connections changed messages.
218199
using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
219200
virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;

src/interfaces/wallet.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ class WalletImpl : public Wallet
485485
std::shared_ptr<CWallet> m_wallet;
486486
};
487487

488-
class WalletClientImpl : public ChainClient
488+
class WalletClientImpl : public WalletClient
489489
{
490490
public:
491491
WalletClientImpl(Chain& chain, ArgsManager& args, std::vector<std::string> wallet_filenames)
@@ -494,6 +494,9 @@ class WalletClientImpl : public ChainClient
494494
m_context.chain = &chain;
495495
m_context.args = &args;
496496
}
497+
~WalletClientImpl() override { UnloadWallets(); }
498+
499+
//! ChainClient methods
497500
void registerRpcs() override
498501
{
499502
for (const CRPCCommand& command : GetWalletRPCCommands()) {
@@ -509,6 +512,30 @@ class WalletClientImpl : public ChainClient
509512
void flush() override { return FlushWallets(); }
510513
void stop() override { return StopWallets(); }
511514
void setMockTime(int64_t time) override { return SetMockTime(time); }
515+
516+
//! WalletClient methods
517+
std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, WalletCreationStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings) override
518+
{
519+
std::shared_ptr<CWallet> wallet;
520+
status = CreateWallet(*m_context.chain, passphrase, wallet_creation_flags, name, error, warnings, wallet);
521+
return MakeWallet(std::move(wallet));
522+
}
523+
std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
524+
{
525+
return MakeWallet(LoadWallet(*m_context.chain, WalletLocation(name), error, warnings));
526+
}
527+
std::string getWalletDir() override
528+
{
529+
return GetWalletDir().string();
530+
}
531+
std::vector<std::string> listWalletDir() override
532+
{
533+
std::vector<std::string> paths;
534+
for (auto& path : ListWalletDir()) {
535+
paths.push_back(path.string());
536+
}
537+
return paths;
538+
}
512539
std::vector<std::unique_ptr<Wallet>> getWallets() override
513540
{
514541
std::vector<std::unique_ptr<Wallet>> wallets;
@@ -517,7 +544,10 @@ class WalletClientImpl : public ChainClient
517544
}
518545
return wallets;
519546
}
520-
~WalletClientImpl() override { UnloadWallets(); }
547+
std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override
548+
{
549+
return HandleLoadWallet(std::move(fn));
550+
}
521551

522552
WalletContext m_context;
523553
const std::vector<std::string> m_wallet_filenames;
@@ -529,7 +559,7 @@ class WalletClientImpl : public ChainClient
529559

530560
std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet) { return wallet ? MakeUnique<WalletImpl>(wallet) : nullptr; }
531561

532-
std::unique_ptr<ChainClient> MakeWalletClient(Chain& chain, ArgsManager& args, std::vector<std::string> wallet_filenames)
562+
std::unique_ptr<WalletClient> MakeWalletClient(Chain& chain, ArgsManager& args, std::vector<std::string> wallet_filenames)
533563
{
534564
return MakeUnique<WalletClientImpl>(chain, args, std::move(wallet_filenames));
535565
}

0 commit comments

Comments
 (0)