[WIP] [wallet] Remove Wallet dependencies from init.cpp #10762

Open
wants to merge 16 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+489 −305
Split
View
@@ -158,13 +158,15 @@ BITCOIN_CORE_H = \
validation.h \
validationinterface.h \
versionbits.h \
+ walletinitinterface.h \
wallet/coincontrol.h \
wallet/crypter.h \
wallet/db.h \
wallet/feebumper.h \
wallet/rpcwallet.h \
wallet/wallet.h \
wallet/walletdb.h \
+ wallet/walletinit.h \
warnings.h \
zmq/zmqabstractnotifier.h \
zmq/zmqconfig.h\
@@ -219,6 +221,7 @@ libbitcoin_server_a_SOURCES = \
validation.cpp \
validationinterface.cpp \
versionbits.cpp \
+ walletinitinterface.cpp \
$(BITCOIN_CORE_H)
if ENABLE_ZMQ
@@ -243,6 +246,7 @@ libbitcoin_wallet_a_SOURCES = \
wallet/rpcwallet.cpp \
wallet/wallet.cpp \
wallet/walletdb.cpp \
+ wallet/walletinit.cpp \
$(BITCOIN_CORE_H)
# crypto primitives library
View
@@ -19,6 +19,10 @@
#include "httpserver.h"
#include "httprpc.h"
#include "utilstrencodings.h"
+#if ENABLE_WALLET
+#include "walletinitinterface.h"
+#include "wallet/walletinit.h"
+#endif
#include <boost/thread.hpp>
@@ -67,6 +71,12 @@ bool AppInit(int argc, char* argv[])
bool fRet = false;
+#if ENABLE_WALLET
+ // Register wallet initialization callbacks
+ WalletInit wallet_init;
+ RegisterWalletInitInterface(&wallet_init);
+#endif
+
//
// Parameters
//
View
@@ -41,10 +41,8 @@
#include "ui_interface.h"
#include "util.h"
#include "utilmoneystr.h"
+#include "walletinitinterface.h"
#include "validationinterface.h"
-#ifdef ENABLE_WALLET
-#include "wallet/wallet.h"
-#endif
#include "warnings.h"
#include <stdint.h>
#include <stdio.h>
@@ -187,11 +185,6 @@ void Shutdown()
StopREST();
StopRPC();
StopHTTPServer();
-#ifdef ENABLE_WALLET
- for (CWalletRef pwallet : vpwallets) {
- pwallet->Flush(false);
- }
-#endif
MapPort(false);
UnregisterValidationInterface(peerLogic.get());
peerLogic.reset();
@@ -229,11 +222,7 @@ void Shutdown()
delete pblocktree;
pblocktree = NULL;
}
-#ifdef ENABLE_WALLET
- for (CWalletRef pwallet : vpwallets) {
- pwallet->Flush(true);
- }
-#endif
+ GetWalletInitSignals().ShutdownWallets();
#if ENABLE_ZMQ
if (pzmqNotificationInterface) {
@@ -251,12 +240,7 @@ void Shutdown()
}
#endif
UnregisterAllValidationInterfaces();
-#ifdef ENABLE_WALLET
- for (CWalletRef pwallet : vpwallets) {
- delete pwallet;
- }
- vpwallets.clear();
-#endif
+ GetWalletInitSignals().DeleteWallets();
globalVerifyHandle.reset();
ECC_Stop();
LogPrintf("%s: done\n", __func__);
@@ -403,9 +387,8 @@ std::string HelpMessage(HelpMessageMode mode)
" " + _("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"));
strUsage += HelpMessageOpt("-maxuploadtarget=<n>", strprintf(_("Tries to keep outbound traffic under the given target (in MiB per 24h), 0 = no limit (default: %d)"), DEFAULT_MAX_UPLOAD_TARGET));
-#ifdef ENABLE_WALLET
- strUsage += CWallet::GetWalletHelpString(showDebug);
-#endif
+ boost::optional<std::string> wallet_rpc_help_string = GetWalletInitSignals().GetWalletHelpString(showDebug);
+ if (wallet_rpc_help_string) strUsage += *wallet_rpc_help_string;
#if ENABLE_ZMQ
strUsage += HelpMessageGroup(_("ZeroMQ notification options:"));
@@ -1006,9 +989,7 @@ bool AppInitParameterInteraction()
}
RegisterAllCoreRPCCommands(tableRPC);
-#ifdef ENABLE_WALLET
- RegisterWalletRPCCommands(tableRPC);
-#endif
+ GetWalletInitSignals().RegisterWalletRPC(tableRPC);
nConnectTimeout = GetArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
if (nConnectTimeout <= 0)
@@ -1051,10 +1032,8 @@ bool AppInitParameterInteraction()
return InitError(strprintf("acceptnonstdtxn is not currently supported for %s chain", chainparams.NetworkIDString()));
nBytesPerSigOp = GetArg("-bytespersigop", nBytesPerSigOp);
-#ifdef ENABLE_WALLET
- if (!CWallet::ParameterInteraction())
- return false;
-#endif
+ boost::optional<bool> param_interaction_success = GetWalletInitSignals().WalletParameterInteraction();
+ if (param_interaction_success && !(*param_interaction_success)) return false;
fIsBareMultisigStd = GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG);
fAcceptDatacarrier = GetBoolArg("-datacarrier", DEFAULT_ACCEPT_DATACARRIER);
@@ -1218,10 +1197,9 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
int64_t nStart;
// ********************************************************* Step 5: verify wallet database integrity
-#ifdef ENABLE_WALLET
- if (!CWallet::Verify())
- return false;
-#endif
+ boost::optional<bool> wallet_verify_success = GetWalletInitSignals().VerifyWallets();
+ if (wallet_verify_success && !(*wallet_verify_success)) return false;
+
// ********************************************************* Step 6: network initialization
// Note that we absolutely cannot open any actual connections
// until the very end ("start node") as the UTXO/block state
@@ -1506,12 +1484,13 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
fFeeEstimatesInitialized = true;
// ********************************************************* Step 8: load wallet
-#ifdef ENABLE_WALLET
- if (!CWallet::InitLoadWallet())
+
+ boost::optional<bool> wallet_load_success = GetWalletInitSignals().InitLoadWallets();
+ if (!wallet_load_success) {
+ LogPrintf("No wallet support compiled in!\n");
+ } else if (!(*wallet_load_success)) {
return false;
-#else
- LogPrintf("No wallet support compiled in!\n");
-#endif
+ }
// ********************************************************* Step 9: data directory maintenance
@@ -1637,11 +1616,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
SetRPCWarmupFinished();
uiInterface.InitMessage(_("Done loading"));
-#ifdef ENABLE_WALLET
- for (CWalletRef pwallet : vpwallets) {
- pwallet->postInitProcess(scheduler);
- }
-#endif
+ GetWalletInitSignals().WalletCompleteStartup(scheduler);
return !fRequestShutdown;
}
View
@@ -34,6 +34,8 @@
#include "warnings.h"
#ifdef ENABLE_WALLET
+#include "walletinitinterface.h"
+#include "wallet/walletinit.h"
#include "wallet/wallet.h"
#endif
@@ -662,6 +664,10 @@ int main(int argc, char *argv[])
// Start up the payment server early, too, so impatient users that click on
// bitcoin: links repeatedly have their payment requests routed to this process:
app.createPaymentServer();
+
+ // Register wallet initialization callbacks
+ WalletInit wallet_init;
+ RegisterWalletInitInterface(&wallet_init);
#endif
/// 9. Main GUI initialization
@@ -184,7 +184,7 @@ void TestSendCoins()
BumpFee(transactionView, txid2, false /* expect disabled */, {} /* expected error */, false /* cancel */);
BumpFee(transactionView, txid2, true /* expect disabled */, "already bumped" /* expected error */, false /* cancel */);
- bitdb.Flush(true);
+ bitdb.Flush("wallet_test.dat");
bitdb.Reset();
}
View
@@ -226,40 +226,12 @@ bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*reco
bool CDB::VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr)
{
- LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0));
- LogPrintf("Using wallet %s\n", walletFile);
-
- // Wallet file must be a plain filename without a directory
- if (walletFile != fs::basename(walletFile) + fs::extension(walletFile))
- {
- errorStr = strprintf(_("Wallet %s resides outside data directory %s"), walletFile, dataDir.string());
- return false;
- }
-
- if (!bitdb.Open(dataDir))
- {
- // try moving the database env out of the way
- fs::path pathDatabase = dataDir / "database";
- fs::path pathDatabaseBak = dataDir / strprintf("database.%d.bak", GetTime());
- try {
- fs::rename(pathDatabase, pathDatabaseBak);
- LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string(), pathDatabaseBak.string());
- } catch (const fs::filesystem_error&) {
- // failure is ok (well, not really, but it's not worse than what we started with)
- }
-
- // try again
- if (!bitdb.Open(dataDir)) {
- // if it still fails, it probably means we can't even create the database env
- errorStr = strprintf(_("Error initializing wallet database environment %s!"), GetDataDir());
- return false;
- }
- }
return true;
}
bool CDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc)
{
+ LogPrintf("Using wallet %s\n", walletFile);
if (fs::exists(dataDir / walletFile))
{
std::string backup_filename;
@@ -557,22 +529,32 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
return false;
}
+void CDBEnv::Shutdown()
+{
+ char** listp;
+ if (mapFileUseCount.empty()) {
+ dbenv->log_archive(&listp, DB_ARCH_REMOVE);
+ Close();
+ if (!fMockDb)
+ fs::remove_all(fs::path(strPath) / "database");
+ }
+}
-void CDBEnv::Flush(bool fShutdown)
+void CDBEnv::Flush(std::string strFile)
{
int64_t nStart = GetTimeMillis();
// Flush log data to the actual data file on all files that are not in use
- LogPrint(BCLog::DB, "CDBEnv::Flush: Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started");
+ LogPrint(BCLog::DB, "CDBEnv::Flush: Flush%s\n", fDbEnvInit ? "" : " database not started");
if (!fDbEnvInit)
return;
{
LOCK(cs_db);
std::map<std::string, int>::iterator mi = mapFileUseCount.begin();
while (mi != mapFileUseCount.end()) {
- std::string strFile = (*mi).first;
+ std::string foundStrFile = (*mi).first;
int nRefCount = (*mi).second;
- LogPrint(BCLog::DB, "CDBEnv::Flush: Flushing %s (refcount = %d)...\n", strFile, nRefCount);
- if (nRefCount == 0) {
+ if (foundStrFile == strFile && nRefCount == 0) {
+ LogPrint(BCLog::DB, "CDBEnv::Flush: Flushing %s (refcount = %d)...\n", strFile, nRefCount);
// Move log data to the dat file
CloseDb(strFile);
LogPrint(BCLog::DB, "CDBEnv::Flush: %s checkpoint\n", strFile);
@@ -585,16 +567,7 @@ void CDBEnv::Flush(bool fShutdown)
} else
mi++;
}
- LogPrint(BCLog::DB, "CDBEnv::Flush: Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started", GetTimeMillis() - nStart);
- if (fShutdown) {
- char** listp;
- if (mapFileUseCount.empty()) {
- dbenv->log_archive(&listp, DB_ARCH_REMOVE);
- Close();
- if (!fMockDb)
- fs::remove_all(fs::path(strPath) / "database");
- }
- }
+ LogPrint(BCLog::DB, "CDBEnv::Flush: Flush took %15dms\n", GetTimeMillis() - nStart);
}
}
@@ -683,9 +656,9 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
return false;
}
-void CWalletDBWrapper::Flush(bool shutdown)
+void CWalletDBWrapper::Flush()
{
if (!IsDummy()) {
- env->Flush(shutdown);
+ env->Flush(strFile);
}
}
View
@@ -70,7 +70,8 @@ class CDBEnv
bool Open(const fs::path& path);
void Close();
- void Flush(bool fShutdown);
+ void Flush(std::string strFile);
+ void Shutdown();
void CheckpointLSN(const std::string& strFile);
void CloseDb(const std::string& strFile);
@@ -119,7 +120,7 @@ class CWalletDBWrapper
/** Make sure all changes are flushed to disk.
*/
- void Flush(bool shutdown);
+ void Flush();
void IncrementUpdateCounter();
View
@@ -3038,9 +3038,8 @@ static const CRPCCommand commands[] =
void RegisterWalletRPCCommands(CRPCTable &t)
{
- if (GetBoolArg("-disablewallet", false))
- return;
-
- for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
+ for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) {
t.appendCommand(commands[vcidx].name, &commands[vcidx]);
+ }
}
+
@@ -30,6 +30,7 @@ WalletTestingSetup::~WalletTestingSetup()
delete pwalletMain;
pwalletMain = NULL;
- bitdb.Flush(true);
+ bitdb.Flush("wallet_test.dat");
+ bitdb.Shutdown();
bitdb.Reset();
}
@@ -606,7 +606,8 @@ class ListCoinsTestingSetup : public TestChain100Setup
~ListCoinsTestingSetup()
{
wallet.reset();
- ::bitdb.Flush(true);
+ ::bitdb.Flush("wallet_test.dat");
+ ::bitdb.Shutdown();
::bitdb.Reset();
}
Oops, something went wrong.