Skip to content

Commit

Permalink
Remove uses of wallet functions in init.cpp
Browse files Browse the repository at this point in the history
This commit mostly does not change behavior. The only change is that "No wallet
support compiled in!" and "Wallet disabled!" messages are now logged a little
earlier.
  • Loading branch information
ryanofsky committed Jul 21, 2017
1 parent 5269b56 commit 7bfb409
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Expand Up @@ -7,6 +7,7 @@ DIST_SUBDIRS = secp256k1 univalue
AM_LDFLAGS = $(PTHREAD_CFLAGS) $(LIBTOOL_LDFLAGS) $(HARDENED_LDFLAGS)
AM_CXXFLAGS = $(HARDENED_CXXFLAGS) $(ERROR_CXXFLAGS)
AM_CPPFLAGS = $(HARDENED_CPPFLAGS)
AM_LIBTOOLFLAGS = --preserve-dup-deps
EXTRA_LIBRARIES =

if EMBEDDED_UNIVALUE
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.bench.include
Expand Up @@ -50,7 +50,7 @@ endif

if ENABLE_WALLET
bench_bench_bitcoin_SOURCES += bench/coin_selection.cpp
bench_bench_bitcoin_LDADD += $(LIBBITCOIN_WALLET) $(LIBBITCOIN_CRYPTO)
bench_bench_bitcoin_LDADD += $(LIBBITCOIN_WALLET) $(LIBBITCOIN_CRYPTO) $(LIBBITCOIN_UTIL)
endif

bench_bench_bitcoin_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS)
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.test.include
Expand Up @@ -98,7 +98,7 @@ test_test_bitcoin_SOURCES = $(BITCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES
test_test_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -I$(builddir)/test/ $(TESTDEFS) $(EVENT_CFLAGS)
test_test_bitcoin_LDADD =
if ENABLE_WALLET
test_test_bitcoin_LDADD += $(LIBBITCOIN_WALLET)
test_test_bitcoin_LDADD += $(LIBBITCOIN_SERVER) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_WALLET)
endif
test_test_bitcoin_LDADD += $(LIBBITCOIN_SERVER) $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CONSENSUS) $(LIBBITCOIN_CRYPTO) $(LIBUNIVALUE) \
$(LIBLEVELDB) $(LIBLEVELDB_SSE42) $(LIBMEMENV) $(BOOST_LIBS) $(BOOST_UNIT_TEST_FRAMEWORK_LIB) $(LIBSECP256K1) $(EVENT_LIBS) $(EVENT_PTHREADS_LIBS)
Expand Down
46 changes: 20 additions & 26 deletions src/init.cpp
Expand Up @@ -189,11 +189,9 @@ void Shutdown(ipc::Chain::Clients& ipc_clients)
StopREST();
StopRPC();
StopHTTPServer();
#ifdef ENABLE_WALLET
for (CWalletRef pwallet : vpwallets) {
pwallet->Flush(false);
for (const auto& client : ipc_clients) {
client->stop();
}
#endif
MapPort(false);
UnregisterValidationInterface(peerLogic.get());
peerLogic.reset();
Expand Down Expand Up @@ -246,11 +244,9 @@ void Shutdown(ipc::Chain::Clients& ipc_clients)
delete pblocktree;
pblocktree = nullptr;
}
#ifdef ENABLE_WALLET
for (CWalletRef pwallet : vpwallets) {
pwallet->Flush(true);
for (const auto& client : ipc_clients) {
client->shutdown();
}
#endif

#if ENABLE_ZMQ
if (pzmqNotificationInterface) {
Expand All @@ -270,12 +266,7 @@ void Shutdown(ipc::Chain::Clients& ipc_clients)
UnregisterAllValidationInterfaces();
GetMainSignals().UnregisterBackgroundSignalScheduler();
GetMainSignals().UnregisterWithMempoolSignals(mempool);
#ifdef ENABLE_WALLET
for (CWalletRef pwallet : vpwallets) {
delete pwallet;
}
vpwallets.clear();
#endif
ipc_clients.clear();
globalVerifyHandle.reset();
ECC_Stop();
LogPrintf("%s: done\n", __func__);
Expand Down Expand Up @@ -1024,11 +1015,17 @@ bool AppInitParameterInteraction(ipc::Chain& ipc_chain, ipc::Chain::Clients& ipc
fPruneMode = true;
}

RegisterAllCoreRPCCommands(tableRPC);
#ifdef ENABLE_WALLET
RegisterWalletRPCCommands(tableRPC);
MakeWalletClients(ipc_chain, ipc_clients);
#else
LogPrintf("No wallet support compiled in!\n");
#endif

RegisterAllCoreRPCCommands(tableRPC);
for (const auto& client : ipc_clients) {
client->registerRpcs();
}

nConnectTimeout = gArgs.GetArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
if (nConnectTimeout <= 0)
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
Expand Down Expand Up @@ -1569,12 +1566,11 @@ bool AppInitMain(ipc::Chain& ipc_chain, const ipc::Chain::Clients& ipc_clients,
fFeeEstimatesInitialized = true;

// ********************************************************* Step 8: load wallet
#ifdef ENABLE_WALLET
if (!InitLoadWallet(ipc_chain))
return false;
#else
LogPrintf("No wallet support compiled in!\n");
#endif
for (const auto& client : ipc_clients) {
if (!client->prepare()) {
return false;
}
}

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

Expand Down Expand Up @@ -1700,11 +1696,9 @@ bool AppInitMain(ipc::Chain& ipc_chain, const ipc::Chain::Clients& ipc_clients,
SetRPCWarmupFinished();
uiInterface.InitMessage(_("Done loading"));

#ifdef ENABLE_WALLET
for (CWalletRef pwallet : vpwallets) {
pwallet->postInitProcess(scheduler);
for (const auto& client : ipc_clients) {
client->start(scheduler);
}
#endif

return !fRequestShutdown;
}
30 changes: 30 additions & 0 deletions src/ipc/interfaces.h
Expand Up @@ -4,6 +4,8 @@
#include <memory>
#include <vector>

class CScheduler;

namespace ipc {

//! Interface for giving wallet processes access to blockchain state.
Expand All @@ -18,6 +20,22 @@ class Chain
{
public:
virtual ~Client() {}

//! Register rpcs.
virtual void registerRpcs() = 0;

//! Prepare for execution, loading any needed state.
virtual bool prepare() = 0;

//! Start client execution and provide a scheduler. (Scheduler is
//! ignored if client is out-of-process).
virtual void start(CScheduler& scheduler) = 0;

//! Stop client execution and prepare for shutdown.
virtual void stop() = 0;

//! Shut down client.
virtual void shutdown() = 0;
};

//! List of clients.
Expand All @@ -42,13 +60,25 @@ struct ChainClientOptions
//! (tools for monitoring, analysis, fee estimation, etc).
enum Type { WALLET = 0 };
Type type;

//! For WALLET client, wallet filenames to load.
std::vector<std::string> wallet_filenames;
};

//! Create chain client interface, communicating with requested protocol.
//! Returns null if protocol or client type aren't implemented or available in
//! the current build configuration.
std::unique_ptr<Chain::Client> MakeChainClient(Protocol protocol, Chain& chain, ChainClientOptions options);

//! Convenience function to return options object for wallet clients.
inline ChainClientOptions WalletOptions(std::vector<std::string> wallet_filenames = {})
{
ChainClientOptions options;
options.type = ChainClientOptions::WALLET;
options.wallet_filenames = std::move(wallet_filenames);
return options;
}

} // namespace ipc

#endif // BITCOIN_IPC_INTERFACES_H
38 changes: 36 additions & 2 deletions src/ipc/local/bitcoin-walletd.cpp
@@ -1,6 +1,8 @@
#include <ipc/interfaces.h>

#include <ipc/util.h>
#include <rpc/server.h>
#include <wallet/init.h>
#include <wallet/wallet.h>

namespace ipc {
Expand All @@ -10,15 +12,47 @@ namespace {
class WalletClientImpl : public Chain::Client
{
public:
WalletClientImpl(Chain& chain) : m_chain(chain) {}
WalletClientImpl(Chain& chain, std::vector<std::string> wallet_filenames)
: m_chain(chain), m_wallet_filenames(std::move(wallet_filenames))
{
}
void registerRpcs() override { RegisterWalletRPCCommands(::tableRPC); }
bool prepare() override { return InitLoadWallet(m_chain, *this, m_wallet_filenames); }
void start(CScheduler& scheduler) override
{
for (CWalletRef wallet : ::vpwallets) {
wallet->postInitProcess(scheduler);
}
}
void stop() override
{
for (CWalletRef wallet : ::vpwallets) {
wallet->Flush(false /* shutdown */);
}
}
void shutdown() override
{
for (CWalletRef wallet : ::vpwallets) {
wallet->Flush(true /* shutdown */);
}
}
~WalletClientImpl()
{
for (CWalletRef wallet : ::vpwallets) {
delete wallet;
}
::vpwallets.clear();
}

Chain& m_chain;
std::vector<std::string> m_wallet_filenames;
};

} // namespace

std::unique_ptr<Chain::Client> MakeWalletClient(Chain& chain, ChainClientOptions options)
{
return MakeUnique<WalletClientImpl>(chain);
return MakeUnique<WalletClientImpl>(chain, std::move(options.wallet_filenames));
}

} // namespace local
Expand Down
12 changes: 8 additions & 4 deletions src/wallet/init.cpp
Expand Up @@ -53,7 +53,6 @@ std::string GetWalletHelpString(bool showDebug)

bool WalletParameterInteraction()
{
gArgs.SoftSetArg("-wallet", DEFAULT_WALLET_DAT);
const bool is_multiwallet = gArgs.GetArgs("-wallet").size() > 1;

if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET))
Expand Down Expand Up @@ -228,14 +227,19 @@ bool WalletVerify()
return true;
}

bool InitLoadWallet(ipc::Chain& ipc_chain)
void MakeWalletClients(ipc::Chain& ipc_chain, ipc::Chain::Clients& ipc_clients)
{
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
LogPrintf("Wallet disabled!\n");
return true;
return;
}
gArgs.SoftSetArg("-wallet", DEFAULT_WALLET_DAT);
ipc_clients.emplace_back(ipc::MakeChainClient(ipc::LOCAL, ipc_chain, ipc::WalletOptions(gArgs.GetArgs("-wallet"))));
}

for (const std::string& walletFile : gArgs.GetArgs("-wallet")) {
bool InitLoadWallet(ipc::Chain& ipc_chain, ipc::Chain::Client& ipc_client, const std::vector<std::string>& wallet_filenames)
{
for (const std::string& walletFile : wallet_filenames) {
CWallet * const pwallet = CWallet::CreateWalletFromFile(ipc_chain, walletFile);
if (!pwallet) {
return false;
Expand Down
5 changes: 4 additions & 1 deletion src/wallet/init.h
Expand Up @@ -20,7 +20,10 @@ bool WalletParameterInteraction();
// being loaded (CWallet::ParameterInteraction forbids -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet).
bool WalletVerify();

//! Create wallet IPC clients.
void MakeWalletClients(ipc::Chain& ipc_chain, ipc::Chain::Clients& ipc_clients);

//! Load wallet databases.
bool InitLoadWallet(ipc::Chain& ipc_chain);
bool InitLoadWallet(ipc::Chain& ipc_chain, ipc::Chain::Client& ipc_client, const std::vector<std::string>& wallet_filenames);

#endif // BITCOIN_WALLET_INIT_H
3 changes: 0 additions & 3 deletions src/wallet/rpcwallet.cpp
Expand Up @@ -3301,9 +3301,6 @@ static const CRPCCommand commands[] =

void RegisterWalletRPCCommands(CRPCTable &t)
{
if (gArgs.GetBoolArg("-disablewallet", false))
return;

for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
t.appendCommand(commands[vcidx].name, &commands[vcidx]);
}

0 comments on commit 7bfb409

Please sign in to comment.