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
This commit is contained in:
Wladimir J. van der Laan 2018-03-29 16:39:50 +02:00
commit 6d53663a43
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
8 changed files with 124 additions and 64 deletions

View File

@ -162,6 +162,7 @@ BITCOIN_CORE_H = \
validation.h \ validation.h \
validationinterface.h \ validationinterface.h \
versionbits.h \ versionbits.h \
walletinitinterface.h \
wallet/coincontrol.h \ wallet/coincontrol.h \
wallet/crypter.h \ wallet/crypter.h \
wallet/db.h \ wallet/db.h \

View File

@ -18,6 +18,10 @@
#include <httpserver.h> #include <httpserver.h>
#include <httprpc.h> #include <httprpc.h>
#include <utilstrencodings.h> #include <utilstrencodings.h>
#if ENABLE_WALLET
#include <wallet/init.h>
#endif
#include <walletinitinterface.h>
#include <boost/thread.hpp> #include <boost/thread.hpp>
@ -59,6 +63,12 @@ bool AppInit(int argc, char* argv[])
{ {
bool fRet = false; bool fRet = false;
#if ENABLE_WALLET
g_wallet_init_interface.reset(new WalletInit);
#else
g_wallet_init_interface.reset(new DummyWalletInit);
#endif
// //
// Parameters // Parameters
// //

View File

@ -43,10 +43,8 @@
#include <util.h> #include <util.h>
#include <utilmoneystr.h> #include <utilmoneystr.h>
#include <validationinterface.h> #include <validationinterface.h>
#ifdef ENABLE_WALLET
#include <wallet/init.h>
#endif
#include <warnings.h> #include <warnings.h>
#include <walletinitinterface.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <memory> #include <memory>
@ -74,6 +72,7 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
std::unique_ptr<CConnman> g_connman; std::unique_ptr<CConnman> g_connman;
std::unique_ptr<PeerLogicValidation> peerLogic; std::unique_ptr<PeerLogicValidation> peerLogic;
std::unique_ptr<WalletInitInterface> g_wallet_init_interface;
#if ENABLE_ZMQ #if ENABLE_ZMQ
static CZMQNotificationInterface* pzmqNotificationInterface = nullptr; static CZMQNotificationInterface* pzmqNotificationInterface = nullptr;
@ -189,9 +188,7 @@ void Shutdown()
StopREST(); StopREST();
StopRPC(); StopRPC();
StopHTTPServer(); StopHTTPServer();
#ifdef ENABLE_WALLET g_wallet_init_interface->Flush();
FlushWallets();
#endif
StopMapPort(); StopMapPort();
// Because these depend on each-other, we make sure that neither can be // Because these depend on each-other, we make sure that neither can be
@ -249,9 +246,7 @@ void Shutdown()
pcoinsdbview.reset(); pcoinsdbview.reset();
pblocktree.reset(); pblocktree.reset();
} }
#ifdef ENABLE_WALLET g_wallet_init_interface->Stop();
StopWallets();
#endif
#if ENABLE_ZMQ #if ENABLE_ZMQ
if (pzmqNotificationInterface) { if (pzmqNotificationInterface) {
@ -271,9 +266,8 @@ void Shutdown()
UnregisterAllValidationInterfaces(); UnregisterAllValidationInterfaces();
GetMainSignals().UnregisterBackgroundSignalScheduler(); GetMainSignals().UnregisterBackgroundSignalScheduler();
GetMainSignals().UnregisterWithMempoolSignals(mempool); GetMainSignals().UnregisterWithMempoolSignals(mempool);
#ifdef ENABLE_WALLET g_wallet_init_interface->Close();
CloseWallets(); g_wallet_init_interface.reset();
#endif
globalVerifyHandle.reset(); globalVerifyHandle.reset();
ECC_Stop(); ECC_Stop();
LogPrintf("%s: done\n", __func__); LogPrintf("%s: done\n", __func__);
@ -416,9 +410,7 @@ std::string HelpMessage(HelpMessageMode mode)
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.") + 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.") +
" " + _("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")); " " + _("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"));
#ifdef ENABLE_WALLET strUsage += g_wallet_init_interface->GetHelpString(showDebug);
strUsage += GetWalletHelpString(showDebug);
#endif
#if ENABLE_ZMQ #if ENABLE_ZMQ
strUsage += HelpMessageGroup(_("ZeroMQ notification options:")); strUsage += HelpMessageGroup(_("ZeroMQ notification options:"));
@ -1087,10 +1079,7 @@ bool AppInitParameterInteraction()
return InitError(strprintf("acceptnonstdtxn is not currently supported for %s chain", chainparams.NetworkIDString())); return InitError(strprintf("acceptnonstdtxn is not currently supported for %s chain", chainparams.NetworkIDString()));
nBytesPerSigOp = gArgs.GetArg("-bytespersigop", nBytesPerSigOp); nBytesPerSigOp = gArgs.GetArg("-bytespersigop", nBytesPerSigOp);
#ifdef ENABLE_WALLET if (!g_wallet_init_interface->ParameterInteraction()) return false;
if (!WalletParameterInteraction())
return false;
#endif
fIsBareMultisigStd = gArgs.GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG); fIsBareMultisigStd = gArgs.GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG);
fAcceptDatacarrier = gArgs.GetBoolArg("-datacarrier", DEFAULT_ACCEPT_DATACARRIER); fAcceptDatacarrier = gArgs.GetBoolArg("-datacarrier", DEFAULT_ACCEPT_DATACARRIER);
@ -1256,9 +1245,7 @@ bool AppInitMain()
* available in the GUI RPC console even if external calls are disabled. * available in the GUI RPC console even if external calls are disabled.
*/ */
RegisterAllCoreRPCCommands(tableRPC); RegisterAllCoreRPCCommands(tableRPC);
#ifdef ENABLE_WALLET g_wallet_init_interface->RegisterRPC(tableRPC);
RegisterWalletRPC(tableRPC);
#endif
/* Start the RPC server already. It will be started in "warmup" mode /* Start the RPC server already. It will be started in "warmup" mode
* and not really process calls already (but it will signify connections * and not really process calls already (but it will signify connections
@ -1275,10 +1262,8 @@ bool AppInitMain()
int64_t nStart; int64_t nStart;
// ********************************************************* Step 5: verify wallet database integrity // ********************************************************* Step 5: verify wallet database integrity
#ifdef ENABLE_WALLET if (!g_wallet_init_interface->Verify()) return false;
if (!VerifyWallets())
return false;
#endif
// ********************************************************* Step 6: network initialization // ********************************************************* Step 6: network initialization
// Note that we absolutely cannot open any actual connections // Note that we absolutely cannot open any actual connections
// until the very end ("start node") as the UTXO/block state // until the very end ("start node") as the UTXO/block state
@ -1596,12 +1581,7 @@ bool AppInitMain()
fFeeEstimatesInitialized = true; fFeeEstimatesInitialized = true;
// ********************************************************* Step 8: load wallet // ********************************************************* Step 8: load wallet
#ifdef ENABLE_WALLET if (!g_wallet_init_interface->Open()) return false;
if (!OpenWallets())
return false;
#else
LogPrintf("No wallet support compiled in!\n");
#endif
// ********************************************************* Step 9: data directory maintenance // ********************************************************* Step 9: data directory maintenance
@ -1747,9 +1727,7 @@ bool AppInitMain()
SetRPCWarmupFinished(); SetRPCWarmupFinished();
uiInterface.InitMessage(_("Done loading")); uiInterface.InitMessage(_("Done loading"));
#ifdef ENABLE_WALLET g_wallet_init_interface->Start(scheduler);
StartWallets(scheduler);
#endif
return true; return true;
} }

View File

@ -6,11 +6,15 @@
#ifndef BITCOIN_INIT_H #ifndef BITCOIN_INIT_H
#define BITCOIN_INIT_H #define BITCOIN_INIT_H
#include <memory>
#include <string> #include <string>
class CScheduler; class CScheduler;
class CWallet; class CWallet;
class WalletInitInterface;
extern std::unique_ptr<WalletInitInterface> g_wallet_init_interface;
namespace boost namespace boost
{ {
class thread_group; class thread_group;

View File

@ -34,8 +34,10 @@
#include <warnings.h> #include <warnings.h>
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
#include <wallet/init.h>
#include <wallet/wallet.h> #include <wallet/wallet.h>
#endif #endif
#include <walletinitinterface.h>
#include <stdint.h> #include <stdint.h>
@ -677,6 +679,11 @@ int main(int argc, char *argv[])
// Start up the payment server early, too, so impatient users that click on // Start up the payment server early, too, so impatient users that click on
// bitcoin: links repeatedly have their payment requests routed to this process: // bitcoin: links repeatedly have their payment requests routed to this process:
app.createPaymentServer(); app.createPaymentServer();
// Hook up the wallet init interface
g_wallet_init_interface.reset(new WalletInit);
#else
g_wallet_init_interface.reset(new DummyWalletInit);
#endif #endif
/// 9. Main GUI initialization /// 9. Main GUI initialization

View File

@ -14,7 +14,7 @@
#include <wallet/wallet.h> #include <wallet/wallet.h>
#include <wallet/walletutil.h> #include <wallet/walletutil.h>
std::string GetWalletHelpString(bool showDebug) std::string WalletInit::GetHelpString(bool showDebug)
{ {
std::string strUsage = HelpMessageGroup(_("Wallet options:")); std::string strUsage = HelpMessageGroup(_("Wallet options:"));
strUsage += HelpMessageOpt("-addresstype", strprintf("What type of addresses to use (\"legacy\", \"p2sh-segwit\", or \"bech32\", default: \"%s\")", FormatOutputType(DEFAULT_ADDRESS_TYPE))); 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)
return strUsage; return strUsage;
} }
bool WalletParameterInteraction() bool WalletInit::ParameterInteraction()
{ {
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) { if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
for (const std::string& wallet : gArgs.GetArgs("-wallet")) { for (const std::string& wallet : gArgs.GetArgs("-wallet")) {
@ -184,7 +184,7 @@ bool WalletParameterInteraction()
return true; return true;
} }
void RegisterWalletRPC(CRPCTable &t) void WalletInit::RegisterRPC(CRPCTable &t)
{ {
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) { if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
return; return;
@ -193,7 +193,7 @@ void RegisterWalletRPC(CRPCTable &t)
RegisterWalletRPCCommands(t); RegisterWalletRPCCommands(t);
} }
bool VerifyWallets() bool WalletInit::Verify()
{ {
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) { if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
return true; return true;
@ -268,7 +268,7 @@ bool VerifyWallets()
return true; return true;
} }
bool OpenWallets() bool WalletInit::Open()
{ {
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) { if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
LogPrintf("Wallet disabled!\n"); LogPrintf("Wallet disabled!\n");
@ -286,25 +286,29 @@ bool OpenWallets()
return true; return true;
} }
void StartWallets(CScheduler& scheduler) { void WalletInit::Start(CScheduler& scheduler)
{
for (CWalletRef pwallet : vpwallets) { for (CWalletRef pwallet : vpwallets) {
pwallet->postInitProcess(scheduler); pwallet->postInitProcess(scheduler);
} }
} }
void FlushWallets() { void WalletInit::Flush()
{
for (CWalletRef pwallet : vpwallets) { for (CWalletRef pwallet : vpwallets) {
pwallet->Flush(false); pwallet->Flush(false);
} }
} }
void StopWallets() { void WalletInit::Stop()
{
for (CWalletRef pwallet : vpwallets) { for (CWalletRef pwallet : vpwallets) {
pwallet->Flush(true); pwallet->Flush(true);
} }
} }
void CloseWallets() { void WalletInit::Close()
{
for (CWalletRef pwallet : vpwallets) { for (CWalletRef pwallet : vpwallets) {
delete pwallet; delete pwallet;
} }

View File

@ -6,38 +6,43 @@
#ifndef BITCOIN_WALLET_INIT_H #ifndef BITCOIN_WALLET_INIT_H
#define BITCOIN_WALLET_INIT_H #define BITCOIN_WALLET_INIT_H
#include <walletinitinterface.h>
#include <string> #include <string>
class CRPCTable; class CRPCTable;
class CScheduler; class CScheduler;
//! Return the wallets help message. class WalletInit : public WalletInitInterface {
std::string GetWalletHelpString(bool showDebug); public:
//! Wallets parameter interaction //! Return the wallets help message.
bool WalletParameterInteraction(); std::string GetHelpString(bool showDebug) override;
//! Register wallet RPCs. //! Wallets parameter interaction
void RegisterWalletRPC(CRPCTable &tableRPC); bool ParameterInteraction() override;
//! Responsible for reading and validating the -wallet arguments and verifying the wallet database. //! Register wallet RPCs.
// This function will perform salvage on the wallet if requested, as long as only one wallet is void RegisterRPC(CRPCTable &tableRPC) override;
// being loaded (WalletParameterInteraction forbids -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet).
bool VerifyWallets();
//! Load wallet databases. //! Responsible for reading and validating the -wallet arguments and verifying the wallet database.
bool OpenWallets(); // 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() override;
//! Complete startup of wallets. //! Load wallet databases.
void StartWallets(CScheduler& scheduler); bool Open() override;
//! Flush all wallets in preparation for shutdown. //! Complete startup of wallets.
void FlushWallets(); void Start(CScheduler& scheduler) override;
//! Stop all wallets. Wallets will be flushed first. //! Flush all wallets in preparation for shutdown.
void StopWallets(); void Flush() override;
//! Close all wallets. //! Stop all wallets. Wallets will be flushed first.
void CloseWallets(); void Stop() override;
//! Close all wallets.
void Close() override;
};
#endif // BITCOIN_WALLET_INIT_H #endif // BITCOIN_WALLET_INIT_H

51
src/walletinitinterface.h Normal file
View File

@ -0,0 +1,51 @@
// 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 <string>
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() = 0;
/** Open wallets*/
virtual bool Open() = 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() override {return true;}
bool Open() override {return true;}
void Start(CScheduler& scheduler) override {}
void Flush() override {}
void Stop() override {}
void Close() override {}
};
#endif // WALLETINITINTERFACE_H