Skip to content

Introduce g_wallet_manager, prepare for better dynamic wallet loading/unloading #12587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ BITCOIN_CORE_H = \
wallet/rpcwallet.h \
wallet/wallet.h \
wallet/walletdb.h \
wallet/walletmanager.h \
wallet/walletutil.h \
wallet/coinselection.h \
warnings.h \
Expand Down Expand Up @@ -252,6 +253,7 @@ libbitcoin_wallet_a_SOURCES = \
wallet/rpcdump.cpp \
wallet/rpcwallet.cpp \
wallet/wallet.cpp \
wallet/walletmanager.cpp \
wallet/walletdb.cpp \
wallet/walletutil.cpp \
wallet/coinselection.cpp \
Expand Down
11 changes: 6 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <validationinterface.h>
#ifdef ENABLE_WALLET
#include <wallet/init.h>
#include <wallet/walletmanager.h>
#endif
#include <warnings.h>
#include <stdint.h>
Expand Down Expand Up @@ -190,7 +191,7 @@ void Shutdown()
StopRPC();
StopHTTPServer();
#ifdef ENABLE_WALLET
FlushWallets();
g_wallet_manager->FlushWallets();
#endif
StopMapPort();

Expand Down Expand Up @@ -250,7 +251,7 @@ void Shutdown()
pblocktree.reset();
}
#ifdef ENABLE_WALLET
StopWallets();
g_wallet_manager->StopWallets();
#endif

#if ENABLE_ZMQ
Expand All @@ -272,7 +273,7 @@ void Shutdown()
GetMainSignals().UnregisterBackgroundSignalScheduler();
GetMainSignals().UnregisterWithMempoolSignals(mempool);
#ifdef ENABLE_WALLET
CloseWallets();
g_wallet_manager->CloseWallets();
#endif
globalVerifyHandle.reset();
ECC_Stop();
Expand Down Expand Up @@ -1592,7 +1593,7 @@ bool AppInitMain()

// ********************************************************* Step 8: load wallet
#ifdef ENABLE_WALLET
if (!OpenWallets())
if (!g_wallet_manager->OpenWallets())
return false;
#else
LogPrintf("No wallet support compiled in!\n");
Expand Down Expand Up @@ -1743,7 +1744,7 @@ bool AppInitMain()
uiInterface.InitMessage(_("Done loading"));

#ifdef ENABLE_WALLET
StartWallets(scheduler);
g_wallet_manager->StartWallets(scheduler);
#endif

return true;
Expand Down
7 changes: 3 additions & 4 deletions src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <warnings.h>

#ifdef ENABLE_WALLET
#include <wallet/wallet.h>
#include <wallet/walletmanager.h>
#endif

#include <stdint.h>
Expand Down Expand Up @@ -484,9 +484,8 @@ void BitcoinApplication::initializeResult(bool success)

#ifdef ENABLE_WALLET
bool fFirstWallet = true;
for (CWalletRef pwallet : vpwallets) {
g_wallet_manager->ForEachWallet([this, &fFirstWallet](CWallet *pwallet) {
WalletModel * const walletModel = new WalletModel(platformStyle, pwallet, optionsModel);

window->addWallet(walletModel);
if (fFirstWallet) {
window->setCurrentWallet(walletModel->getWalletName());
Expand All @@ -497,7 +496,7 @@ void BitcoinApplication::initializeResult(bool success)
paymentServer, SLOT(fetchPaymentACK(CWallet*,const SendCoinsRecipient&,QByteArray)));

m_wallet_models.push_back(walletModel);
}
});
#endif

// If -min option passed, start window minimized.
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include <utilstrencodings.h>
#ifdef ENABLE_WALLET
#include <wallet/rpcwallet.h>
#include <wallet/wallet.h>
#include <wallet/walletdb.h>
#include <wallet/walletmanager.h>
#endif
#include <warnings.h>

Expand Down Expand Up @@ -69,7 +69,7 @@ UniValue validateaddress(const JSONRPCRequest& request)
{

#ifdef ENABLE_WALLET
if (!::vpwallets.empty() && IsDeprecatedRPCEnabled("validateaddress")) {
if (g_wallet_manager->HasWallets() && IsDeprecatedRPCEnabled("validateaddress")) {
ret.pushKVs(getaddressinfo(request));
}
#endif
Expand Down
43 changes: 0 additions & 43 deletions src/wallet/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,46 +267,3 @@ bool VerifyWallets()

return true;
}

bool OpenWallets()
{
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
LogPrintf("Wallet disabled!\n");
return true;
}

for (const std::string& walletFile : gArgs.GetArgs("-wallet")) {
CWallet * const pwallet = CWallet::CreateWalletFromFile(walletFile, fs::absolute(walletFile, GetWalletDir()));
if (!pwallet) {
return false;
}
vpwallets.push_back(pwallet);
}

return true;
}

void StartWallets(CScheduler& scheduler) {
for (CWalletRef pwallet : vpwallets) {
pwallet->postInitProcess(scheduler);
}
}

void FlushWallets() {
for (CWalletRef pwallet : vpwallets) {
pwallet->Flush(false);
}
}

void StopWallets() {
for (CWalletRef pwallet : vpwallets) {
pwallet->Flush(true);
}
}

void CloseWallets() {
for (CWalletRef pwallet : vpwallets) {
delete pwallet;
}
vpwallets.clear();
}
15 changes: 0 additions & 15 deletions src/wallet/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,4 @@ void RegisterWalletRPC(CRPCTable &tableRPC);
// being loaded (WalletParameterInteraction forbids -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet).
bool VerifyWallets();

//! Load wallet databases.
bool OpenWallets();

//! Complete startup of wallets.
void StartWallets(CScheduler& scheduler);

//! Flush all wallets in preparation for shutdown.
void FlushWallets();

//! Stop all wallets. Wallets will be flushed first.
void StopWallets();

//! Close all wallets.
void CloseWallets();

#endif // BITCOIN_WALLET_INIT_H
25 changes: 12 additions & 13 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include <wallet/coincontrol.h>
#include <wallet/feebumper.h>
#include <wallet/rpcwallet.h>
#include <wallet/wallet.h>
#include <wallet/walletdb.h>
#include <wallet/walletmanager.h>
#include <wallet/walletutil.h>

#include <init.h> // For StartShutdown
Expand All @@ -43,15 +43,13 @@ CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
{
if (request.URI.substr(0, WALLET_ENDPOINT_BASE.size()) == WALLET_ENDPOINT_BASE) {
// wallet endpoint was used
std::string requestedWallet = urlDecode(request.URI.substr(WALLET_ENDPOINT_BASE.size()));
for (CWalletRef pwallet : ::vpwallets) {
if (pwallet->GetName() == requestedWallet) {
return pwallet;
}
CWallet *wallet = g_wallet_manager->FindWalletByName(urlDecode(request.URI.substr(WALLET_ENDPOINT_BASE.size())));
if (wallet) {
return wallet;
}
throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
}
return ::vpwallets.size() == 1 || (request.fHelp && ::vpwallets.size() > 0) ? ::vpwallets[0] : nullptr;
return g_wallet_manager->CountWallets() == 1 || (request.fHelp && g_wallet_manager->CountWallets() > 0) ? g_wallet_manager->GetWalletAtPos(0) : nullptr;
}

std::string HelpRequiringPassphrase(CWallet * const pwallet)
Expand All @@ -65,7 +63,7 @@ bool EnsureWalletIsAvailable(CWallet * const pwallet, bool avoidException)
{
if (pwallet) return true;
if (avoidException) return false;
if (::vpwallets.empty()) {
if (!g_wallet_manager->HasWallets()) {
// Note: It isn't currently possible to trigger this error because
// wallet RPC methods aren't registered unless a wallet is loaded. But
// this error is being kept as a precaution, because it's possible in
Expand Down Expand Up @@ -2845,18 +2843,19 @@ UniValue listwallets(const JSONRPCRequest& request)

UniValue obj(UniValue::VARR);

for (CWalletRef pwallet : vpwallets) {

bool not_available_found = false;
g_wallet_manager->ForEachWallet([&not_available_found, &request,&obj](CWallet *pwallet) {
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
return NullUniValue;
not_available_found = true;
return;
}

LOCK(pwallet->cs_wallet);

obj.push_back(pwallet->GetName());
}
});

return obj;
return not_available_found ? NullUniValue : obj;
}

UniValue resendwallettransactions(const JSONRPCRequest& request)
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/test/wallet_test_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <rpc/server.h>
#include <wallet/db.h>
#include <wallet/wallet.h>
#include <wallet/walletmanager.h>

WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
TestingSetup(chainName), m_wallet("mock", CWalletDBWrapper::CreateMock())
Expand Down
12 changes: 7 additions & 5 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <validation.h>
#include <wallet/coincontrol.h>
#include <wallet/test/wallet_test_fixture.h>
#include <wallet/walletmanager.h>

#include <boost/test/unit_test.hpp>
#include <univalue.h>
Expand Down Expand Up @@ -73,7 +74,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// after.
{
CWallet wallet("dummy", CWalletDBWrapper::CreateDummy());
vpwallets.insert(vpwallets.begin(), &wallet);
g_wallet_manager->AddWallet(&wallet);
UniValue keys;
keys.setArray();
UniValue key;
Expand Down Expand Up @@ -104,7 +105,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
"downloading and rescanning the relevant blocks (see -reindex and -rescan "
"options).\"}},{\"success\":true}]",
0, oldTip->GetBlockTimeMax(), TIMESTAMP_WINDOW));
vpwallets.erase(vpwallets.begin());
g_wallet_manager->DeallocWallet(0);
}
}

Expand Down Expand Up @@ -139,7 +140,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
JSONRPCRequest request;
request.params.setArray();
request.params.push_back((pathTemp / "wallet.backup").string());
vpwallets.insert(vpwallets.begin(), &wallet);
g_wallet_manager->AddWallet(&wallet);
::dumpwallet(request);
}

Expand All @@ -151,7 +152,8 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
JSONRPCRequest request;
request.params.setArray();
request.params.push_back((pathTemp / "wallet.backup").string());
vpwallets[0] = &wallet;
g_wallet_manager->DeallocWallet(0);
g_wallet_manager->AddWallet(&wallet);
::importwallet(request);

LOCK(wallet.cs_wallet);
Expand All @@ -165,7 +167,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
}

SetMockTime(0);
vpwallets.erase(vpwallets.begin());
g_wallet_manager->DeallocWallet(0);
}

// Check that GetImmatureCredit() returns a newly calculated value instead of
Expand Down
1 change: 0 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

#include <boost/algorithm/string/replace.hpp>

std::vector<CWalletRef> vpwallets;
/** Transaction fee set by the user */
CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE);
unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET;
Expand Down
7 changes: 2 additions & 5 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
#include <utility>
#include <vector>

typedef CWallet* CWalletRef;
extern std::vector<CWalletRef> vpwallets;

/**
* Settings
*/
Expand Down Expand Up @@ -1247,10 +1244,10 @@ std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key);
class WalletRescanReserver
{
private:
CWalletRef m_wallet;
CWallet* m_wallet;
bool m_could_reserve;
public:
explicit WalletRescanReserver(CWalletRef w) : m_wallet(w), m_could_reserve(false) {}
explicit WalletRescanReserver(CWallet* w) : m_wallet(w), m_could_reserve(false) {}

bool reserve()
{
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <sync.h>
#include <util.h>
#include <utiltime.h>
#include <wallet/wallet.h>
#include <wallet/walletmanager.h>

#include <atomic>

Expand Down Expand Up @@ -748,7 +748,7 @@ void MaybeCompactWalletDB()
return;
}

for (CWalletRef pwallet : vpwallets) {
g_wallet_manager->ForEachWallet([](CWallet* pwallet) {
CWalletDBWrapper& dbh = pwallet->GetDBHandle();

unsigned int nUpdateCounter = dbh.nUpdateCounter;
Expand All @@ -763,7 +763,7 @@ void MaybeCompactWalletDB()
dbh.nLastFlushed = nUpdateCounter;
}
}
}
});

fOneThread = false;
}
Expand Down
Loading