Skip to content

Commit 49baa4a

Browse files
committed
[wallet] Use global g_wallet_init_interface to init/destroy the wallet.
This commit creates a global g_wallet_init_interface, which is created in bitcoind and bitcoin-qt. g_wallet_init_interface is used to init and destroy the wallet. This removes the dependency from init.cpp on the wallet library.
1 parent caaf972 commit 49baa4a

File tree

5 files changed

+53
-42
lines changed

5 files changed

+53
-42
lines changed

src/bitcoind.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include <httpserver.h>
1919
#include <httprpc.h>
2020
#include <utilstrencodings.h>
21+
#if ENABLE_WALLET
22+
#include <wallet/init.h>
23+
#include <walletinitinterface.h>
24+
#endif
2125

2226
#include <boost/thread.hpp>
2327

@@ -59,6 +63,10 @@ bool AppInit(int argc, char* argv[])
5963
{
6064
bool fRet = false;
6165

66+
#if ENABLE_WALLET
67+
g_wallet_init_interface.reset(new WalletInit);
68+
#endif
69+
6270
//
6371
// Parameters
6472
//

src/init.cpp

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@
4343
#include <util.h>
4444
#include <utilmoneystr.h>
4545
#include <validationinterface.h>
46-
#ifdef ENABLE_WALLET
47-
#include <wallet/init.h>
48-
#endif
4946
#include <warnings.h>
47+
#include <walletinitinterface.h>
5048
#include <stdint.h>
5149
#include <stdio.h>
5250
#include <memory>
@@ -74,6 +72,7 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
7472

7573
std::unique_ptr<CConnman> g_connman;
7674
std::unique_ptr<PeerLogicValidation> peerLogic;
75+
std::unique_ptr<WalletInitInterface> g_wallet_init_interface;
7776

7877
#if ENABLE_ZMQ
7978
static CZMQNotificationInterface* pzmqNotificationInterface = nullptr;
@@ -189,9 +188,9 @@ void Shutdown()
189188
StopREST();
190189
StopRPC();
191190
StopHTTPServer();
192-
#ifdef ENABLE_WALLET
193-
WalletInit::Flush();
194-
#endif
191+
if (g_wallet_init_interface) {
192+
g_wallet_init_interface->Flush();
193+
}
195194
StopMapPort();
196195

197196
// Because these depend on each-other, we make sure that neither can be
@@ -249,9 +248,9 @@ void Shutdown()
249248
pcoinsdbview.reset();
250249
pblocktree.reset();
251250
}
252-
#ifdef ENABLE_WALLET
253-
WalletInit::Stop();
254-
#endif
251+
if (g_wallet_init_interface) {
252+
g_wallet_init_interface->Stop();
253+
}
255254

256255
#if ENABLE_ZMQ
257256
if (pzmqNotificationInterface) {
@@ -271,9 +270,10 @@ void Shutdown()
271270
UnregisterAllValidationInterfaces();
272271
GetMainSignals().UnregisterBackgroundSignalScheduler();
273272
GetMainSignals().UnregisterWithMempoolSignals(mempool);
274-
#ifdef ENABLE_WALLET
275-
WalletInit::Close();
276-
#endif
273+
if (g_wallet_init_interface) {
274+
g_wallet_init_interface->Close();
275+
}
276+
g_wallet_init_interface.reset();
277277
globalVerifyHandle.reset();
278278
ECC_Stop();
279279
LogPrintf("%s: done\n", __func__);
@@ -415,9 +415,9 @@ std::string HelpMessage(HelpMessageMode mode)
415415
strUsage += HelpMessageOpt("-whitelist=<IP address or network>", _("Whitelist peers connecting from the given IP address (e.g. 1.2.3.4) or CIDR notated network (e.g. 1.2.3.0/24). Can be specified multiple times.") +
416416
" " + _("Whitelisted peers cannot be DoS banned and their transactions are always relayed, even if they are already in the mempool, useful e.g. for a gateway"));
417417

418-
#ifdef ENABLE_WALLET
419-
strUsage += WalletInit::GetHelpString(showDebug);
420-
#endif
418+
if (g_wallet_init_interface) {
419+
strUsage += g_wallet_init_interface->GetHelpString(showDebug);
420+
}
421421

422422
#if ENABLE_ZMQ
423423
strUsage += HelpMessageGroup(_("ZeroMQ notification options:"));
@@ -1091,9 +1091,7 @@ bool AppInitParameterInteraction()
10911091
return InitError(strprintf("acceptnonstdtxn is not currently supported for %s chain", chainparams.NetworkIDString()));
10921092
nBytesPerSigOp = gArgs.GetArg("-bytespersigop", nBytesPerSigOp);
10931093

1094-
#ifdef ENABLE_WALLET
1095-
if (!WalletInit::ParameterInteraction()) return false;
1096-
#endif
1094+
if (g_wallet_init_interface && !g_wallet_init_interface->ParameterInteraction()) return false;
10971095

10981096
fIsBareMultisigStd = gArgs.GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG);
10991097
fAcceptDatacarrier = gArgs.GetBoolArg("-datacarrier", DEFAULT_ACCEPT_DATACARRIER);
@@ -1256,9 +1254,9 @@ bool AppInitMain()
12561254
* available in the GUI RPC console even if external calls are disabled.
12571255
*/
12581256
RegisterAllCoreRPCCommands(tableRPC);
1259-
#ifdef ENABLE_WALLET
1260-
WalletInit::RegisterRPC(tableRPC);
1261-
#endif
1257+
if (g_wallet_init_interface) {
1258+
g_wallet_init_interface->RegisterRPC(tableRPC);
1259+
}
12621260

12631261
/* Start the RPC server already. It will be started in "warmup" mode
12641262
* and not really process calls already (but it will signify connections
@@ -1275,9 +1273,8 @@ bool AppInitMain()
12751273
int64_t nStart;
12761274

12771275
// ********************************************************* Step 5: verify wallet database integrity
1278-
#ifdef ENABLE_WALLET
1279-
if (!WalletInit::Verify()) return false;
1280-
#endif
1276+
if (g_wallet_init_interface && !g_wallet_init_interface->Verify()) return false;
1277+
12811278
// ********************************************************* Step 6: network initialization
12821279
// Note that we absolutely cannot open any actual connections
12831280
// until the very end ("start node") as the UTXO/block state
@@ -1595,11 +1592,7 @@ bool AppInitMain()
15951592
fFeeEstimatesInitialized = true;
15961593

15971594
// ********************************************************* Step 8: load wallet
1598-
#ifdef ENABLE_WALLET
1599-
if (!WalletInit::Open()) return false;
1600-
#else
1601-
LogPrintf("No wallet support compiled in!\n");
1602-
#endif
1595+
if (g_wallet_init_interface && !g_wallet_init_interface->Open()) return false;
16031596

16041597
// ********************************************************* Step 9: data directory maintenance
16051598

@@ -1745,9 +1738,9 @@ bool AppInitMain()
17451738
SetRPCWarmupFinished();
17461739
uiInterface.InitMessage(_("Done loading"));
17471740

1748-
#ifdef ENABLE_WALLET
1749-
WalletInit::Start(scheduler);
1750-
#endif
1741+
if (g_wallet_init_interface) {
1742+
g_wallet_init_interface->Start(scheduler);
1743+
}
17511744

17521745
return true;
17531746
}

src/init.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
#ifndef BITCOIN_INIT_H
77
#define BITCOIN_INIT_H
88

9+
#include <memory>
910
#include <string>
1011

1112
class CScheduler;
1213
class CWallet;
1314

15+
class WalletInitInterface;
16+
extern std::unique_ptr<WalletInitInterface> g_wallet_init_interface;
17+
1418
namespace boost
1519
{
1620
class thread_group;

src/qt/bitcoin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#include <warnings.h>
3434

3535
#ifdef ENABLE_WALLET
36+
#include <wallet/init.h>
3637
#include <wallet/wallet.h>
38+
#include <walletinitinterface.h>
3739
#endif
3840

3941
#include <stdint.h>
@@ -669,6 +671,9 @@ int main(int argc, char *argv[])
669671
// Start up the payment server early, too, so impatient users that click on
670672
// bitcoin: links repeatedly have their payment requests routed to this process:
671673
app.createPaymentServer();
674+
675+
// Hook up the wallet init interface
676+
g_wallet_init_interface.reset(new WalletInit);
672677
#endif
673678

674679
/// 9. Main GUI initialization

src/wallet/init.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,43 @@
66
#ifndef BITCOIN_WALLET_INIT_H
77
#define BITCOIN_WALLET_INIT_H
88

9+
#include <walletinitinterface.h>
910
#include <string>
1011

1112
class CRPCTable;
1213
class CScheduler;
1314

14-
class WalletInit {
15+
class WalletInit : public WalletInitInterface {
1516
public:
1617

1718
//! Return the wallets help message.
18-
static std::string GetHelpString(bool showDebug);
19+
std::string GetHelpString(bool showDebug) override;
1920

2021
//! Wallets parameter interaction
21-
static bool ParameterInteraction();
22+
bool ParameterInteraction() override;
2223

2324
//! Register wallet RPCs.
24-
static void RegisterRPC(CRPCTable &tableRPC);
25+
void RegisterRPC(CRPCTable &tableRPC) override;
2526

2627
//! Responsible for reading and validating the -wallet arguments and verifying the wallet database.
2728
// This function will perform salvage on the wallet if requested, as long as only one wallet is
2829
// being loaded (WalletParameterInteraction forbids -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet).
29-
static bool Verify();
30+
bool Verify() override;
3031

3132
//! Load wallet databases.
32-
static bool Open();
33+
bool Open() override;
3334

3435
//! Complete startup of wallets.
35-
static void Start(CScheduler& scheduler);
36+
void Start(CScheduler& scheduler) override;
3637

3738
//! Flush all wallets in preparation for shutdown.
38-
static void Flush();
39+
void Flush() override;
3940

4041
//! Stop all wallets. Wallets will be flushed first.
41-
static void Stop();
42+
void Stop() override;
4243

4344
//! Close all wallets.
44-
static void Close();
45+
void Close() override;
4546
};
4647

4748
#endif // BITCOIN_WALLET_INIT_H

0 commit comments

Comments
 (0)