Skip to content

Commit

Permalink
Merge bitcoin#14437: Refactor: Start to separate wallet from node
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFalke authored and PastaPastaPasta committed Sep 28, 2021
1 parent f2dce0f commit 6deaf36
Show file tree
Hide file tree
Showing 38 changed files with 786 additions and 434 deletions.
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ BITCOIN_CORE_H = \
index/txindex.h \
indirectmap.h \
init.h \
interfaces/chain.h \
interfaces/handler.h \
interfaces/node.h \
interfaces/wallet.h \
Expand Down Expand Up @@ -347,6 +348,7 @@ libdash_server_a_SOURCES = \
index/base.cpp \
index/blockfilterindex.cpp \
index/txindex.cpp \
interfaces/chain.cpp \
interfaces/handler.cpp \
interfaces/node.cpp \
init.cpp \
Expand Down Expand Up @@ -678,6 +680,7 @@ endif
dashd_LDADD = \
$(LIBBITCOIN_SERVER) \
$(LIBBITCOIN_WALLET) \
$(LIBBITCOIN_SERVER) \
$(LIBBITCOIN_COMMON) \
$(LIBUNIVALUE) \
$(LIBBITCOIN_UTIL) \
Expand Down
7 changes: 5 additions & 2 deletions src/bench/coin_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <interfaces/chain.h>
#include <wallet/wallet.h>
#include <wallet/coinselection.h>

Expand Down Expand Up @@ -33,7 +34,8 @@ static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<Ou
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
static void CoinSelection(benchmark::Bench& bench)
{
const CWallet wallet(WalletLocation(), WalletDatabase::CreateDummy());
auto chain = interfaces::MakeChain();
const CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
LOCK(wallet.cs_wallet);

// Add coins.
Expand All @@ -56,7 +58,8 @@ static void CoinSelection(benchmark::Bench& bench)
}

typedef std::set<CInputCoin> CoinSet;
static const CWallet testWallet(WalletLocation(), WalletDatabase::CreateDummy());
static auto testChain = interfaces::MakeChain();
static const CWallet testWallet(*testChain, WalletLocation(), WalletDatabase::CreateDummy());
std::vector<std::unique_ptr<CWalletTx>> wtxn;

// Copied from src/wallet/test/coinselector_tests.cpp
Expand Down
5 changes: 3 additions & 2 deletions src/coinjoin/coinjoin-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1519,13 +1519,14 @@ bool CCoinJoinClientSession::MakeCollateralAmounts(const CompactTallyItem& tally

bool CCoinJoinClientSession::CreateCollateralTransaction(CMutableTransaction& txCollateral, std::string& strReason)
{
LOCK2(cs_main, mixingWallet.cs_wallet);
auto locked_chain = mixingWallet.chain().lock();
LOCK(mixingWallet.cs_wallet);

std::vector<COutput> vCoins;
CCoinControl coin_control;
coin_control.nCoinType = CoinType::ONLY_COINJOIN_COLLATERAL;

mixingWallet.AvailableCoins(vCoins, true, &coin_control);
mixingWallet.AvailableCoins(*locked_chain, vCoins, true, &coin_control);

if (vCoins.empty()) {
strReason = strprintf("%s requires a collateral transaction and could not locate an acceptable input!", gCoinJoinName);
Expand Down
2 changes: 1 addition & 1 deletion src/coinjoin/coinjoin-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ bool CTransactionBuilder::Commit(std::string& strResult)
}

CTransactionRef tx;
if (!pwallet->CreateTransaction(vecSend, tx, dummyReserveKey, nFeeRet, nChangePosRet, strResult, coinControl)) {
if (!pwallet->CreateTransaction(*pwallet->chain().lock(), vecSend, tx, dummyReserveKey, nFeeRet, nChangePosRet, strResult, coinControl)) {
return false;
}

Expand Down
8 changes: 6 additions & 2 deletions src/dashd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <clientversion.h>
#include <compat.h>
#include <fs.h>
#include <interfaces/chain.h>
#include <rpc/server.h>
#include <init.h>
#include <noui.h>
Expand Down Expand Up @@ -59,6 +60,9 @@ static void WaitForShutdown()
//
static bool AppInit(int argc, char* argv[])
{
InitInterfaces interfaces;
interfaces.chain = interfaces::MakeChain();

bool fRet = false;

util::ThreadSetInternalName("init");
Expand Down Expand Up @@ -178,7 +182,7 @@ static bool AppInit(int argc, char* argv[])
// If locking the data directory failed, exit immediately
return false;
}
fRet = AppInitMain();
fRet = AppInitMain(interfaces);
} catch (...) {
PrintExceptionContinue(std::current_exception(), "AppInit()");
}
Expand All @@ -189,7 +193,7 @@ static bool AppInit(int argc, char* argv[])
} else {
WaitForShutdown();
}
Shutdown();
Shutdown(interfaces);

return fRet;
}
Expand Down
8 changes: 1 addition & 7 deletions src/dummywallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ class DummyWalletInit : public WalletInitInterface {
bool HasWalletSupport() const override {return false;}
void AddWalletOptions() const override;
bool ParameterInteraction() const override {return true;}
void RegisterRPC(CRPCTable &) const override {}
bool Verify() const override {return true;}
bool Open() const override {LogPrintf("No wallet support compiled in!\n"); return true;}
void Start(CScheduler& scheduler) const override {}
void Flush() const override {}
void Stop() const override {}
void Close() const override {}
void Construct(InitInterfaces& interfaces) const override {LogPrintf("No wallet support compiled in!\n");}

// Dash Specific WalletInitInterface InitCoinJoinSettings
void AutoLockMasternodeCollaterals() const override {}
Expand Down
47 changes: 36 additions & 11 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <hash.h>
#include <httpserver.h>
#include <httprpc.h>
#include <interfaces/chain.h>
#include <index/blockfilterindex.h>
#include <index/txindex.h>
#include <key.h>
Expand All @@ -40,6 +41,7 @@
#include <rpc/server.h>
#include <rpc/register.h>
#include <rpc/blockchain.h>
#include <rpc/util.h>
#include <script/standard.h>
#include <script/sigcache.h>
#include <scheduler.h>
Expand Down Expand Up @@ -229,7 +231,7 @@ void Interrupt()
}

/** Preparing steps before shutting down or restarting the wallet */
void PrepareShutdown()
void PrepareShutdown(InitInterfaces& interfaces)
{
LogPrintf("%s: In progress...\n", __func__);
static CCriticalSection cs_Shutdown;
Expand All @@ -254,7 +256,9 @@ void PrepareShutdown()
std::string statusmessage;
bool fRPCInWarmup = RPCIsInWarmup(&statusmessage);

g_wallet_init_interface.Flush();
for (const auto& client : interfaces.chain_clients) {
client->flush();
}
StopMapPort();

// Because these depend on each-other, we make sure that neither can be
Expand Down Expand Up @@ -342,7 +346,9 @@ void PrepareShutdown()
deterministicMNManager.reset();
evoDb.reset();
}
g_wallet_init_interface.Stop();
for (const auto& client : interfaces.chain_clients) {
client->stop();
}

#if ENABLE_ZMQ
if (g_zmq_notification_interface) {
Expand Down Expand Up @@ -377,7 +383,7 @@ void PrepareShutdown()
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, e.what());
}
#endif
g_wallet_init_interface.Close();
interfaces.chain_clients.clear();
UnregisterAllValidationInterfaces();
GetMainSignals().UnregisterBackgroundSignalScheduler();
GetMainSignals().UnregisterWithMempoolSignals(mempool);
Expand All @@ -392,11 +398,11 @@ void PrepareShutdown()
* called implicitly when the parent object is deleted. In this case we have to skip the
* PrepareShutdown() part because it was already executed and just delete the wallet instance.
*/
void Shutdown()
void Shutdown(InitInterfaces& interfaces)
{
// Shutdown part 1: prepare shutdown
if(!RestartRequested()) {
PrepareShutdown();
PrepareShutdown(interfaces);
}
// Shutdown part 2: delete wallet instance
globalVerifyHandle.reset();
Expand Down Expand Up @@ -1639,7 +1645,7 @@ bool AppInitLockDataDirectory()
return true;
}

bool AppInitMain()
bool AppInitMain(InitInterfaces& interfaces)
{
const CChainParams& chainparams = Params();
// ********************************************************* Step 4a: application initialization
Expand Down Expand Up @@ -1744,11 +1750,20 @@ bool AppInitMain()

tableRPC.InitPlatformRestrictions();

// Create client interfaces for wallets that are supposed to be loaded
// according to -wallet and -disablewallet options. This only constructs
// the interfaces, it doesn't load wallet data. Wallets actually get loaded
// when load() and start() interface methods are called below.
g_wallet_init_interface.Construct(interfaces);

/* Register RPC commands regardless of -server setting so they will be
* available in the GUI RPC console even if external calls are disabled.
*/
RegisterAllCoreRPCCommands(tableRPC);
g_wallet_init_interface.RegisterRPC(tableRPC);
for (const auto& client : interfaces.chain_clients) {
client->registerRpcs();
}
g_rpc_interfaces = &interfaces;
#if ENABLE_ZMQ
RegisterZMQRPCCommands(tableRPC);
#endif
Expand All @@ -1768,7 +1783,11 @@ bool AppInitMain()
// ********************************************************* Step 5: verify wallet database integrity

if (!g_wallet_init_interface.InitAutoBackup()) return false;
if (!g_wallet_init_interface.Verify()) return false;
for (const auto& client : interfaces.chain_clients) {
if (!client->verify()) {
return false;
}
}

// Initialize KeePass Integration
g_wallet_init_interface.InitKeePass();
Expand Down Expand Up @@ -2194,7 +2213,11 @@ bool AppInitMain()
}

// ********************************************************* Step 9: load wallet
if (!g_wallet_init_interface.Open()) return false;
for (const auto& client : interfaces.chain_clients) {
if (!client->load()) {
return false;
}
}

// As InitLoadWallet can take several minutes, it's possible the user
// requested to kill the GUI during the last operation. If so, exit.
Expand Down Expand Up @@ -2492,7 +2515,9 @@ bool AppInitMain()
SetRPCWarmupFinished();
uiInterface.InitMessage(_("Done loading"));

g_wallet_init_interface.Start(scheduler);
for (const auto& client : interfaces.chain_clients) {
client->start(scheduler);
}

scheduler.scheduleEvery([]{
g_banman->DumpBanlist();
Expand Down
19 changes: 14 additions & 5 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@
#include <string>
#include <util/system.h>

class CScheduler;
class CWallet;
namespace interfaces {
class Chain;
class ChainClient;
} // namespace interfaces

//! Pointers to interfaces used during init and destroyed on shutdown.
struct InitInterfaces
{
std::unique_ptr<interfaces::Chain> chain;
std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
};

namespace boost
{
Expand All @@ -20,7 +29,7 @@ class thread_group;

/** Interrupt threads */
void Interrupt();
void Shutdown();
void Shutdown(InitInterfaces& interfaces);
//!Initialize the logging infrastructure
void InitLogging();
//!Parameter interaction: change current parameters depending on various rules
Expand Down Expand Up @@ -54,8 +63,8 @@ bool AppInitLockDataDirectory();
* @note This should only be done after daemonization. Call Shutdown() if this function fails.
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
*/
bool AppInitMain();
void PrepareShutdown();
bool AppInitMain(InitInterfaces& interfaces);
void PrepareShutdown(InitInterfaces& interfaces);

/**
* Setup the arguments for gArgs
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The following interfaces are defined here:

* [`Chain`](chain.h) — used by wallet to access blockchain and mempool state. Added in [#10973](https://github.com/bitcoin/bitcoin/pull/10973).

* [`Chain::Client`](chain.h) — used by node to start & stop `Chain` clients. Added in [#10973](https://github.com/bitcoin/bitcoin/pull/10973).
* [`ChainClient`](chain.h) — used by node to start & stop `Chain` clients. Added in [#10973](https://github.com/bitcoin/bitcoin/pull/10973).

* [`Node`](node.h) — used by GUI to start & stop bitcoin node. Added in [#10244](https://github.com/bitcoin/bitcoin/pull/10244).

Expand Down
44 changes: 44 additions & 0 deletions src/interfaces/chain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <interfaces/chain.h>

#include <sync.h>
#include <util/system.h>
#include <validation.h>

#include <memory>
#include <utility>

namespace interfaces {
namespace {

class LockImpl : public Chain::Lock
{
};

class LockingStateImpl : public LockImpl, public UniqueLock<CCriticalSection>
{
using UniqueLock::UniqueLock;
};

class ChainImpl : public Chain
{
public:
std::unique_ptr<Chain::Lock> lock(bool try_lock) override
{
auto result = MakeUnique<LockingStateImpl>(::cs_main, "cs_main", __FILE__, __LINE__, try_lock);
if (try_lock && result && !*result) return {};
// std::move necessary on some compilers due to conversion from
// LockingStateImpl to Lock pointer
return std::move(result);
}
std::unique_ptr<Chain::Lock> assumeLocked() override { return MakeUnique<LockImpl>(); }
};

} // namespace

std::unique_ptr<Chain> MakeChain() { return MakeUnique<ChainImpl>(); }

} // namespace interfaces
Loading

0 comments on commit 6deaf36

Please sign in to comment.