Skip to content

Commit 6d53663

Browse files
committed
Merge #10762: [wallet] Remove Wallet dependencies from init.cpp
c7ec524 [wallet] Add dummy wallet init class (John Newbery) 49baa4a [wallet] Use global g_wallet_init_interface to init/destroy the wallet. (John Newbery) caaf972 [wallet] Create wallet init interface. (John Newbery) 5fb5421 [wallet] Move wallet init functions into WalletInit class. (John Newbery) Pull request description: This continues the work of #7965. This PR, along with several others, would remove the remaining dependencies from libbitcoin_server.a on libbitcoin_wallet.a. To create the interface, I've just translated all the old init.cpp wallet function calls into an interface class. I've not done any thinking about whether it makes sense to change that interface by combining/splitting those calls. This is a purely internal interface, so there's no problem in changing it later. Tree-SHA512: 32ea57615229c33fd1a7f2f29ebc11bf30337685f7211baffa899823ef74b65dcbf068289c557a161c5afffb51fdc38a2ee8180720371f64d433b12b0615cf3f
2 parents d3908e2 + c7ec524 commit 6d53663

File tree

8 files changed

+124
-64
lines changed

8 files changed

+124
-64
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ BITCOIN_CORE_H = \
162162
validation.h \
163163
validationinterface.h \
164164
versionbits.h \
165+
walletinitinterface.h \
165166
wallet/coincontrol.h \
166167
wallet/crypter.h \
167168
wallet/db.h \

src/bitcoind.cpp

Lines changed: 10 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+
#endif
24+
#include <walletinitinterface.h>
2125

2226
#include <boost/thread.hpp>
2327

@@ -59,6 +63,12 @@ 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+
#else
69+
g_wallet_init_interface.reset(new DummyWalletInit);
70+
#endif
71+
6272
//
6373
// Parameters
6474
//

src/init.cpp

Lines changed: 13 additions & 35 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,7 @@ void Shutdown()
189188
StopREST();
190189
StopRPC();
191190
StopHTTPServer();
192-
#ifdef ENABLE_WALLET
193-
FlushWallets();
194-
#endif
191+
g_wallet_init_interface->Flush();
195192
StopMapPort();
196193

197194
// Because these depend on each-other, we make sure that neither can be
@@ -249,9 +246,7 @@ void Shutdown()
249246
pcoinsdbview.reset();
250247
pblocktree.reset();
251248
}
252-
#ifdef ENABLE_WALLET
253-
StopWallets();
254-
#endif
249+
g_wallet_init_interface->Stop();
255250

256251
#if ENABLE_ZMQ
257252
if (pzmqNotificationInterface) {
@@ -271,9 +266,8 @@ void Shutdown()
271266
UnregisterAllValidationInterfaces();
272267
GetMainSignals().UnregisterBackgroundSignalScheduler();
273268
GetMainSignals().UnregisterWithMempoolSignals(mempool);
274-
#ifdef ENABLE_WALLET
275-
CloseWallets();
276-
#endif
269+
g_wallet_init_interface->Close();
270+
g_wallet_init_interface.reset();
277271
globalVerifyHandle.reset();
278272
ECC_Stop();
279273
LogPrintf("%s: done\n", __func__);
@@ -416,9 +410,7 @@ std::string HelpMessage(HelpMessageMode mode)
416410
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.") +
417411
" " + _("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"));
418412

419-
#ifdef ENABLE_WALLET
420-
strUsage += GetWalletHelpString(showDebug);
421-
#endif
413+
strUsage += g_wallet_init_interface->GetHelpString(showDebug);
422414

423415
#if ENABLE_ZMQ
424416
strUsage += HelpMessageGroup(_("ZeroMQ notification options:"));
@@ -1087,10 +1079,7 @@ bool AppInitParameterInteraction()
10871079
return InitError(strprintf("acceptnonstdtxn is not currently supported for %s chain", chainparams.NetworkIDString()));
10881080
nBytesPerSigOp = gArgs.GetArg("-bytespersigop", nBytesPerSigOp);
10891081

1090-
#ifdef ENABLE_WALLET
1091-
if (!WalletParameterInteraction())
1092-
return false;
1093-
#endif
1082+
if (!g_wallet_init_interface->ParameterInteraction()) return false;
10941083

10951084
fIsBareMultisigStd = gArgs.GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG);
10961085
fAcceptDatacarrier = gArgs.GetBoolArg("-datacarrier", DEFAULT_ACCEPT_DATACARRIER);
@@ -1256,9 +1245,7 @@ bool AppInitMain()
12561245
* available in the GUI RPC console even if external calls are disabled.
12571246
*/
12581247
RegisterAllCoreRPCCommands(tableRPC);
1259-
#ifdef ENABLE_WALLET
1260-
RegisterWalletRPC(tableRPC);
1261-
#endif
1248+
g_wallet_init_interface->RegisterRPC(tableRPC);
12621249

12631250
/* Start the RPC server already. It will be started in "warmup" mode
12641251
* and not really process calls already (but it will signify connections
@@ -1275,10 +1262,8 @@ bool AppInitMain()
12751262
int64_t nStart;
12761263

12771264
// ********************************************************* Step 5: verify wallet database integrity
1278-
#ifdef ENABLE_WALLET
1279-
if (!VerifyWallets())
1280-
return false;
1281-
#endif
1265+
if (!g_wallet_init_interface->Verify()) return false;
1266+
12821267
// ********************************************************* Step 6: network initialization
12831268
// Note that we absolutely cannot open any actual connections
12841269
// until the very end ("start node") as the UTXO/block state
@@ -1596,12 +1581,7 @@ bool AppInitMain()
15961581
fFeeEstimatesInitialized = true;
15971582

15981583
// ********************************************************* Step 8: load wallet
1599-
#ifdef ENABLE_WALLET
1600-
if (!OpenWallets())
1601-
return false;
1602-
#else
1603-
LogPrintf("No wallet support compiled in!\n");
1604-
#endif
1584+
if (!g_wallet_init_interface->Open()) return false;
16051585

16061586
// ********************************************************* Step 9: data directory maintenance
16071587

@@ -1747,9 +1727,7 @@ bool AppInitMain()
17471727
SetRPCWarmupFinished();
17481728
uiInterface.InitMessage(_("Done loading"));
17491729

1750-
#ifdef ENABLE_WALLET
1751-
StartWallets(scheduler);
1752-
#endif
1730+
g_wallet_init_interface->Start(scheduler);
17531731

17541732
return true;
17551733
}

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
#include <warnings.h>
3535

3636
#ifdef ENABLE_WALLET
37+
#include <wallet/init.h>
3738
#include <wallet/wallet.h>
3839
#endif
40+
#include <walletinitinterface.h>
3941

4042
#include <stdint.h>
4143

@@ -677,6 +679,11 @@ int main(int argc, char *argv[])
677679
// Start up the payment server early, too, so impatient users that click on
678680
// bitcoin: links repeatedly have their payment requests routed to this process:
679681
app.createPaymentServer();
682+
683+
// Hook up the wallet init interface
684+
g_wallet_init_interface.reset(new WalletInit);
685+
#else
686+
g_wallet_init_interface.reset(new DummyWalletInit);
680687
#endif
681688

682689
/// 9. Main GUI initialization

src/wallet/init.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <wallet/wallet.h>
1515
#include <wallet/walletutil.h>
1616

17-
std::string GetWalletHelpString(bool showDebug)
17+
std::string WalletInit::GetHelpString(bool showDebug)
1818
{
1919
std::string strUsage = HelpMessageGroup(_("Wallet options:"));
2020
strUsage += HelpMessageOpt("-addresstype", strprintf("What type of addresses to use (\"legacy\", \"p2sh-segwit\", or \"bech32\", default: \"%s\")", FormatOutputType(DEFAULT_ADDRESS_TYPE)));
@@ -56,7 +56,7 @@ std::string GetWalletHelpString(bool showDebug)
5656
return strUsage;
5757
}
5858

59-
bool WalletParameterInteraction()
59+
bool WalletInit::ParameterInteraction()
6060
{
6161
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
6262
for (const std::string& wallet : gArgs.GetArgs("-wallet")) {
@@ -184,7 +184,7 @@ bool WalletParameterInteraction()
184184
return true;
185185
}
186186

187-
void RegisterWalletRPC(CRPCTable &t)
187+
void WalletInit::RegisterRPC(CRPCTable &t)
188188
{
189189
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
190190
return;
@@ -193,7 +193,7 @@ void RegisterWalletRPC(CRPCTable &t)
193193
RegisterWalletRPCCommands(t);
194194
}
195195

196-
bool VerifyWallets()
196+
bool WalletInit::Verify()
197197
{
198198
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
199199
return true;
@@ -268,7 +268,7 @@ bool VerifyWallets()
268268
return true;
269269
}
270270

271-
bool OpenWallets()
271+
bool WalletInit::Open()
272272
{
273273
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
274274
LogPrintf("Wallet disabled!\n");
@@ -286,25 +286,29 @@ bool OpenWallets()
286286
return true;
287287
}
288288

289-
void StartWallets(CScheduler& scheduler) {
289+
void WalletInit::Start(CScheduler& scheduler)
290+
{
290291
for (CWalletRef pwallet : vpwallets) {
291292
pwallet->postInitProcess(scheduler);
292293
}
293294
}
294295

295-
void FlushWallets() {
296+
void WalletInit::Flush()
297+
{
296298
for (CWalletRef pwallet : vpwallets) {
297299
pwallet->Flush(false);
298300
}
299301
}
300302

301-
void StopWallets() {
303+
void WalletInit::Stop()
304+
{
302305
for (CWalletRef pwallet : vpwallets) {
303306
pwallet->Flush(true);
304307
}
305308
}
306309

307-
void CloseWallets() {
310+
void WalletInit::Close()
311+
{
308312
for (CWalletRef pwallet : vpwallets) {
309313
delete pwallet;
310314
}

src/wallet/init.h

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +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-
//! Return the wallets help message.
15-
std::string GetWalletHelpString(bool showDebug);
15+
class WalletInit : public WalletInitInterface {
16+
public:
1617

17-
//! Wallets parameter interaction
18-
bool WalletParameterInteraction();
18+
//! Return the wallets help message.
19+
std::string GetHelpString(bool showDebug) override;
1920

20-
//! Register wallet RPCs.
21-
void RegisterWalletRPC(CRPCTable &tableRPC);
21+
//! Wallets parameter interaction
22+
bool ParameterInteraction() override;
2223

23-
//! Responsible for reading and validating the -wallet arguments and verifying the wallet database.
24-
// This function will perform salvage on the wallet if requested, as long as only one wallet is
25-
// being loaded (WalletParameterInteraction forbids -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet).
26-
bool VerifyWallets();
24+
//! Register wallet RPCs.
25+
void RegisterRPC(CRPCTable &tableRPC) override;
2726

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

31-
//! Complete startup of wallets.
32-
void StartWallets(CScheduler& scheduler);
32+
//! Load wallet databases.
33+
bool Open() override;
3334

34-
//! Flush all wallets in preparation for shutdown.
35-
void FlushWallets();
35+
//! Complete startup of wallets.
36+
void Start(CScheduler& scheduler) override;
3637

37-
//! Stop all wallets. Wallets will be flushed first.
38-
void StopWallets();
38+
//! Flush all wallets in preparation for shutdown.
39+
void Flush() override;
3940

40-
//! Close all wallets.
41-
void CloseWallets();
41+
//! Stop all wallets. Wallets will be flushed first.
42+
void Stop() override;
43+
44+
//! Close all wallets.
45+
void Close() override;
46+
};
4247

4348
#endif // BITCOIN_WALLET_INIT_H

src/walletinitinterface.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2017 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef WALLETINITINTERFACE_H
6+
#define WALLETINITINTERFACE_H
7+
8+
#include <string>
9+
10+
class CScheduler;
11+
class CRPCTable;
12+
13+
class WalletInitInterface {
14+
public:
15+
/** Get wallet help string */
16+
virtual std::string GetHelpString(bool showDebug) = 0;
17+
/** Check wallet parameter interaction */
18+
virtual bool ParameterInteraction() = 0;
19+
/** Register wallet RPC*/
20+
virtual void RegisterRPC(CRPCTable &) = 0;
21+
/** Verify wallets */
22+
virtual bool Verify() = 0;
23+
/** Open wallets*/
24+
virtual bool Open() = 0;
25+
/** Start wallets*/
26+
virtual void Start(CScheduler& scheduler) = 0;
27+
/** Flush Wallets*/
28+
virtual void Flush() = 0;
29+
/** Stop Wallets*/
30+
virtual void Stop() = 0;
31+
/** Close wallets */
32+
virtual void Close() = 0;
33+
34+
virtual ~WalletInitInterface() {}
35+
};
36+
37+
class DummyWalletInit : public WalletInitInterface {
38+
public:
39+
40+
std::string GetHelpString(bool showDebug) override {return std::string{};}
41+
bool ParameterInteraction() override {return true;}
42+
void RegisterRPC(CRPCTable &) override {}
43+
bool Verify() override {return true;}
44+
bool Open() override {return true;}
45+
void Start(CScheduler& scheduler) override {}
46+
void Flush() override {}
47+
void Stop() override {}
48+
void Close() override {}
49+
};
50+
51+
#endif // WALLETINITINTERFACE_H

0 commit comments

Comments
 (0)