diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -198,6 +198,7 @@ validation.h \ validationinterface.h \ versionbits.h \ + walletinitinterface.h \ wallet/coincontrol.h \ wallet/crypter.h \ wallet/db.h \ diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -20,6 +20,10 @@ #include "scheduler.h" #include "util.h" #include "utilstrencodings.h" +#if ENABLE_WALLET +#include "wallet/init.h" +#endif +#include "walletinitinterface.h" #include @@ -73,6 +77,12 @@ bool fRet = false; +#if ENABLE_WALLET + g_wallet_init_interface.reset(new WalletInit); +#else + g_wallet_init_interface.reset(new DummyWalletInit); +#endif + // // Parameters // diff --git a/src/init.h b/src/init.h --- a/src/init.h +++ b/src/init.h @@ -7,6 +7,7 @@ #ifndef BITCOIN_INIT_H #define BITCOIN_INIT_H +#include #include class Config; @@ -15,6 +16,9 @@ class HTTPRPCRequestProcessor; class RPCServer; +class WalletInitInterface; +extern std::unique_ptr g_wallet_init_interface; + namespace boost { class thread_group; } // namespace boost diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -44,9 +44,9 @@ #include "validation.h" #include "validationinterface.h" #ifdef ENABLE_WALLET -#include "wallet/init.h" #include "wallet/rpcdump.h" #endif +#include "walletinitinterface.h" #include "warnings.h" #include @@ -75,6 +75,7 @@ std::unique_ptr g_connman; std::unique_ptr peerLogic; +std::unique_ptr g_wallet_init_interface; #if ENABLE_ZMQ static CZMQNotificationInterface *pzmqNotificationInterface = nullptr; @@ -189,9 +190,7 @@ StopREST(); StopRPC(); StopHTTPServer(); -#ifdef ENABLE_WALLET - FlushWallets(); -#endif + g_wallet_init_interface->Flush(); StopMapPort(); // Because these depend on each-other, we make sure that neither can be @@ -246,9 +245,7 @@ pcoinsdbview.reset(); pblocktree.reset(); } -#ifdef ENABLE_WALLET - StopWallets(); -#endif + g_wallet_init_interface->Stop(); #if ENABLE_ZMQ if (pzmqNotificationInterface) { @@ -267,9 +264,8 @@ #endif UnregisterAllValidationInterfaces(); GetMainSignals().UnregisterBackgroundSignalScheduler(); -#ifdef ENABLE_WALLET - CloseWallets(); -#endif + g_wallet_init_interface->Close(); + g_wallet_init_interface.reset(); globalVerifyHandle.reset(); ECC_Stop(); LogPrintf("%s: done\n", __func__); @@ -592,9 +588,7 @@ "MiB per 24h), 0 = no limit (default: %d)"), DEFAULT_MAX_UPLOAD_TARGET)); -#ifdef ENABLE_WALLET - strUsage += GetWalletHelpString(showDebug); -#endif + strUsage += g_wallet_init_interface->GetHelpString(showDebug); #if ENABLE_ZMQ strUsage += HelpMessageGroup(_("ZeroMQ notification options:")); @@ -1571,8 +1565,8 @@ } RegisterAllRPCCommands(config, rpcServer, tableRPC); + g_wallet_init_interface->RegisterRPC(tableRPC); #ifdef ENABLE_WALLET - RegisterWalletRPC(tableRPC); RegisterDumpRPCCommands(tableRPC); #endif @@ -1643,11 +1637,9 @@ } nBytesPerSigOp = gArgs.GetArg("-bytespersigop", nBytesPerSigOp); -#ifdef ENABLE_WALLET - if (!WalletParameterInteraction()) { + if (!g_wallet_init_interface->ParameterInteraction()) { return false; } -#endif fIsBareMultisigStd = gArgs.GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG); @@ -1807,12 +1799,11 @@ } } -// Step 5: verify wallet database integrity -#ifdef ENABLE_WALLET - if (!VerifyWallets(chainparams)) { + // Step 5: verify wallet database integrity + if (!g_wallet_init_interface->Verify(chainparams)) { return false; } -#endif + // Step 6: network initialization // Note that we absolutely cannot open any actual connections @@ -2195,14 +2186,10 @@ config.SetCashAddrEncoding( gArgs.GetBoolArg("-usecashaddr", GetAdjustedTime() > 1515900000)); -// Step 8: load wallet -#ifdef ENABLE_WALLET - if (!OpenWallets(chainparams)) { + // Step 8: load wallet + if (!g_wallet_init_interface->Open(chainparams)) { return false; } -#else - LogPrintf("No wallet support compiled in!\n"); -#endif // Step 9: data directory maintenance @@ -2339,9 +2326,7 @@ SetRPCWarmupFinished(); uiInterface.InitMessage(_("Done loading")); -#ifdef ENABLE_WALLET - StartWallets(scheduler); -#endif + g_wallet_init_interface->Start(scheduler); return !fRequestShutdown; } diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -35,8 +35,10 @@ #include "warnings.h" #ifdef ENABLE_WALLET +#include "wallet/init.h" #include "wallet/wallet.h" #endif +#include "walletinitinterface.h" #include @@ -736,6 +738,11 @@ // bitcoincash: links repeatedly have their payment requests routed to this // process: app.createPaymentServer(); + + // Hook up the wallet init interface + g_wallet_init_interface.reset(new WalletInit); +#else + g_wallet_init_interface.reset(new DummyWalletInit); #endif /// 9. Main GUI initialization diff --git a/src/wallet/init.h b/src/wallet/init.h --- a/src/wallet/init.h +++ b/src/wallet/init.h @@ -8,53 +8,63 @@ #define BITCOIN_WALLET_INIT_H #include "chainparams.h" +#include "walletinitinterface.h" #include class CRPCTable; class CScheduler; -/** - * Return the wallets help message. - */ -std::string GetWalletHelpString(bool showDebug); +class WalletInit : public WalletInitInterface { +public: + /** + * Return the wallets help message. + */ + std::string GetHelpString(bool showDebug) override; -/** - * Wallets parameter interaction - */ -bool WalletParameterInteraction(); + /** + * Wallets parameter interaction + */ + bool ParameterInteraction() override; -/** - * Register wallet RPCs. - */ -void RegisterWalletRPC(CRPCTable &tableRPC); + /** + * Register wallet RPCs. + */ + void RegisterRPC(CRPCTable &tableRPC) override; -/** - * Responsible for reading and validating the -wallet arguments and verifying - * the wallet database. - * This function will perform salvage on the wallet if requested, as long as - * only one wallet is - * being loaded (WalletParameterInteraction forbids -salvagewallet, - * -zapwallettxes or -upgradewallet with multiwallet). - */ + /** + * Responsible for reading and validating the -wallet arguments and + * verifying the wallet database. + * This function will perform salvage on the wallet if requested, as long as + * only one wallet is being loaded (WalletParameterInteraction forbids + * -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet). + */ + bool Verify(const CChainParams &chainParams) override; -bool VerifyWallets(const CChainParams &chainParams); + /** + * Load wallet databases. + */ + bool Open(const CChainParams &chainParams) override; -/** - * Load wallet databases. - */ -bool OpenWallets(const CChainParams &chainParams); + /** + * Complete startup of wallets. + */ + void Start(CScheduler &scheduler) override; -//! Complete startup of wallets. -void StartWallets(CScheduler &scheduler); + /** + * Flush all wallets in preparation for shutdown. + */ + void Flush() override; -//! Flush all wallets in preparation for shutdown. -void FlushWallets(); + /** + * Stop all wallets. Wallets will be flushed first. + */ + void Stop() override; -//! Stop all wallets. Wallets will be flushed first. -void StopWallets(); - -//! Close all wallets. -void CloseWallets(); + /** + * Close all wallets. + */ + void Close() override; +}; #endif // BITCOIN_WALLET_INIT_H diff --git a/src/wallet/init.cpp b/src/wallet/init.cpp --- a/src/wallet/init.cpp +++ b/src/wallet/init.cpp @@ -14,7 +14,7 @@ #include "wallet/rpcwallet.h" #include "wallet/wallet.h" -std::string GetWalletHelpString(bool showDebug) { +std::string WalletInit::GetHelpString(bool showDebug) { std::string strUsage = HelpMessageGroup(_("Wallet options:")); strUsage += HelpMessageOpt( "-disablewallet", @@ -101,7 +101,7 @@ return strUsage; } -bool WalletParameterInteraction() { +bool WalletInit::ParameterInteraction() { CFeeRate minRelayTxFee = GetConfig().GetMinFeePerKB(); gArgs.SoftSetArg("-wallet", DEFAULT_WALLET_DAT); @@ -250,7 +250,7 @@ return true; } -void RegisterWalletRPC(CRPCTable &t) { +void WalletInit::RegisterRPC(CRPCTable &t) { if (gArgs.GetBoolArg("-disablewallet", false)) { return; } @@ -258,7 +258,7 @@ RegisterWalletRPCCommands(t); } -bool VerifyWallets(const CChainParams &chainParams) { +bool WalletInit::Verify(const CChainParams &chainParams) { if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) { return true; } @@ -329,7 +329,7 @@ return true; } -bool OpenWallets(const CChainParams &chainParams) { +bool WalletInit::Open(const CChainParams &chainParams) { if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) { LogPrintf("Wallet disabled!\n"); return true; @@ -347,25 +347,25 @@ return true; } -void StartWallets(CScheduler &scheduler) { +void WalletInit::Start(CScheduler &scheduler) { for (CWalletRef pwallet : vpwallets) { pwallet->postInitProcess(scheduler); } } -void FlushWallets() { +void WalletInit::Flush() { for (CWalletRef pwallet : vpwallets) { pwallet->Flush(false); } } -void StopWallets() { +void WalletInit::Stop() { for (CWalletRef pwallet : vpwallets) { pwallet->Flush(true); } } -void CloseWallets() { +void WalletInit::Close() { for (CWalletRef pwallet : vpwallets) { delete pwallet; } diff --git a/src/walletinitinterface.h b/src/walletinitinterface.h new file mode 100644 --- /dev/null +++ b/src/walletinitinterface.h @@ -0,0 +1,52 @@ +// Copyright (c) 2017 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef WALLETINITINTERFACE_H +#define WALLETINITINTERFACE_H + +#include "chainparams.h" + +#include + +class CScheduler; +class CRPCTable; + +class WalletInitInterface { +public: + /** Get wallet help string */ + virtual std::string GetHelpString(bool showDebug) = 0; + /** Check wallet parameter interaction */ + virtual bool ParameterInteraction() = 0; + /** Register wallet RPC*/ + virtual void RegisterRPC(CRPCTable &) = 0; + /** Verify wallets */ + virtual bool Verify(const CChainParams &chainParams) = 0; + /** Open wallets*/ + virtual bool Open(const CChainParams &chainParams) = 0; + /** Start wallets*/ + virtual void Start(CScheduler &scheduler) = 0; + /** Flush Wallets*/ + virtual void Flush() = 0; + /** Stop Wallets*/ + virtual void Stop() = 0; + /** Close wallets */ + virtual void Close() = 0; + + virtual ~WalletInitInterface() {} +}; + +class DummyWalletInit : public WalletInitInterface { +public: + std::string GetHelpString(bool showDebug) override { return std::string{}; } + bool ParameterInteraction() override { return true; } + void RegisterRPC(CRPCTable &) override {} + bool Verify(const CChainParams &chainParams) override { return true; } + bool Open(const CChainParams &chainParams) override { return true; } + void Start(CScheduler &scheduler) override {} + void Flush() override {} + void Stop() override {} + void Close() override {} +}; + +#endif // WALLETINITINTERFACE_H