Wallet: Replace pwalletMain with a vector of wallet pointers

This commit is contained in:
Luke Dashjr 2016-09-09 08:42:30 +00:00
parent 19b3648bb5
commit b124cf04ea
10 changed files with 51 additions and 45 deletions

View File

@ -198,8 +198,9 @@ void Shutdown()
StopRPC(); StopRPC();
StopHTTPServer(); StopHTTPServer();
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
if (pwalletMain) for (CWalletRef pwallet : vpwallets) {
pwalletMain->Flush(false); pwallet->Flush(false);
}
#endif #endif
MapPort(false); MapPort(false);
UnregisterValidationInterface(peerLogic.get()); UnregisterValidationInterface(peerLogic.get());
@ -239,8 +240,9 @@ void Shutdown()
pblocktree = NULL; pblocktree = NULL;
} }
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
if (pwalletMain) for (CWalletRef pwallet : vpwallets) {
pwalletMain->Flush(true); pwallet->Flush(true);
}
#endif #endif
#if ENABLE_ZMQ #if ENABLE_ZMQ
@ -260,8 +262,10 @@ void Shutdown()
#endif #endif
UnregisterAllValidationInterfaces(); UnregisterAllValidationInterfaces();
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
delete pwalletMain; for (CWalletRef pwallet : vpwallets) {
pwalletMain = NULL; delete pwallet;
}
vpwallets.clear();
#endif #endif
globalVerifyHandle.reset(); globalVerifyHandle.reset();
ECC_Stop(); ECC_Stop();
@ -1673,8 +1677,9 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
uiInterface.InitMessage(_("Done loading")); uiInterface.InitMessage(_("Done loading"));
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
if (pwalletMain) for (CWalletRef pwallet : vpwallets) {
pwalletMain->postInitProcess(scheduler); pwallet->postInitProcess(scheduler);
}
#endif #endif
return !fRequestShutdown; return !fRequestShutdown;

View File

@ -474,9 +474,10 @@ void BitcoinApplication::initializeResult(bool success)
window->setClientModel(clientModel); window->setClientModel(clientModel);
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
if(pwalletMain) // TODO: Expose secondary wallets
if (!vpwallets.empty())
{ {
walletModel = new WalletModel(platformStyle, pwalletMain, optionsModel); walletModel = new WalletModel(platformStyle, vpwallets[0], optionsModel);
window->addWallet(BitcoinGUI::DEFAULT_WALLET, walletModel); window->addWallet(BitcoinGUI::DEFAULT_WALLET, walletModel);
window->setCurrentWallet(BitcoinGUI::DEFAULT_WALLET); window->setCurrentWallet(BitcoinGUI::DEFAULT_WALLET);

View File

@ -439,11 +439,6 @@ void CWalletDBWrapper::IncrementUpdateCounter()
++nUpdateCounter; ++nUpdateCounter;
} }
unsigned int CWalletDBWrapper::GetUpdateCounter()
{
return nUpdateCounter.load();
}
void CDB::Close() void CDB::Close()
{ {
if (!pdb) if (!pdb)

View File

@ -93,13 +93,13 @@ class CWalletDBWrapper
friend class CDB; friend class CDB;
public: public:
/** Create dummy DB handle */ /** Create dummy DB handle */
CWalletDBWrapper(): env(nullptr) CWalletDBWrapper() : nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr)
{ {
} }
/** Create DB handle to real database */ /** Create DB handle to real database */
CWalletDBWrapper(CDBEnv *env_in, const std::string &strFile_in): CWalletDBWrapper(CDBEnv *env_in, const std::string &strFile_in) :
env(env_in), strFile(strFile_in) nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(env_in), strFile(strFile_in)
{ {
} }
@ -120,13 +120,16 @@ public:
void Flush(bool shutdown); void Flush(bool shutdown);
void IncrementUpdateCounter(); void IncrementUpdateCounter();
unsigned int GetUpdateCounter();
std::atomic<unsigned int> nUpdateCounter;
unsigned int nLastSeen;
unsigned int nLastFlushed;
int64_t nLastWalletUpdate;
private: private:
/** BerkeleyDB specific */ /** BerkeleyDB specific */
CDBEnv *env; CDBEnv *env;
std::string strFile; std::string strFile;
std::atomic<unsigned int> nUpdateCounter;
/** Return whether this database handle is a dummy for testing. /** Return whether this database handle is a dummy for testing.
* Only to be used at a low level, application should ideally not care * Only to be used at a low level, application should ideally not care

View File

@ -33,7 +33,8 @@
CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request) CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
{ {
return pwalletMain; // TODO: Some way to access secondary wallets
return vpwallets.empty() ? nullptr : vpwallets[0];
} }
std::string HelpRequiringPassphrase(CWallet * const pwallet) std::string HelpRequiringPassphrase(CWallet * const pwallet)

View File

@ -8,6 +8,8 @@
#include "wallet/db.h" #include "wallet/db.h"
#include "wallet/wallet.h" #include "wallet/wallet.h"
CWallet *pwalletMain;
WalletTestingSetup::WalletTestingSetup(const std::string& chainName): WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
TestingSetup(chainName) TestingSetup(chainName)
{ {

View File

@ -19,6 +19,8 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <univalue.h> #include <univalue.h>
extern CWallet* pwalletMain;
extern UniValue importmulti(const JSONRPCRequest& request); extern UniValue importmulti(const JSONRPCRequest& request);
extern UniValue dumpwallet(const JSONRPCRequest& request); extern UniValue dumpwallet(const JSONRPCRequest& request);
extern UniValue importwallet(const JSONRPCRequest& request); extern UniValue importwallet(const JSONRPCRequest& request);
@ -402,8 +404,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// after. // after.
{ {
CWallet wallet; CWallet wallet;
CWallet *backup = ::pwalletMain; vpwallets.insert(vpwallets.begin(), &wallet);
::pwalletMain = &wallet;
UniValue keys; UniValue keys;
keys.setArray(); keys.setArray();
UniValue key; UniValue key;
@ -434,7 +435,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
"downloading and rescanning the relevant blocks (see -reindex and -rescan " "downloading and rescanning the relevant blocks (see -reindex and -rescan "
"options).\"}},{\"success\":true}]", "options).\"}},{\"success\":true}]",
0, oldTip->GetBlockTimeMax(), TIMESTAMP_WINDOW)); 0, oldTip->GetBlockTimeMax(), TIMESTAMP_WINDOW));
::pwalletMain = backup; vpwallets.erase(vpwallets.begin());
} }
} }
@ -444,7 +445,6 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// than or equal to key birthday. // than or equal to key birthday.
BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
{ {
CWallet *pwalletMainBackup = ::pwalletMain;
LOCK(cs_main); LOCK(cs_main);
// Create two blocks with same timestamp to verify that importwallet rescan // Create two blocks with same timestamp to verify that importwallet rescan
@ -470,7 +470,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
JSONRPCRequest request; JSONRPCRequest request;
request.params.setArray(); request.params.setArray();
request.params.push_back("wallet.backup"); request.params.push_back("wallet.backup");
::pwalletMain = &wallet; vpwallets.insert(vpwallets.begin(), &wallet);
::dumpwallet(request); ::dumpwallet(request);
} }
@ -482,7 +482,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
JSONRPCRequest request; JSONRPCRequest request;
request.params.setArray(); request.params.setArray();
request.params.push_back("wallet.backup"); request.params.push_back("wallet.backup");
::pwalletMain = &wallet; vpwallets[0] = &wallet;
::importwallet(request); ::importwallet(request);
BOOST_CHECK_EQUAL(wallet.mapWallet.size(), 3); BOOST_CHECK_EQUAL(wallet.mapWallet.size(), 3);
@ -495,7 +495,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
} }
SetMockTime(0); SetMockTime(0);
::pwalletMain = pwalletMainBackup; vpwallets.erase(vpwallets.begin());
} }
// Check that GetImmatureCredit() returns a newly calculated value instead of // Check that GetImmatureCredit() returns a newly calculated value instead of

View File

@ -35,7 +35,7 @@
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
#include <boost/thread.hpp> #include <boost/thread.hpp>
CWallet* pwalletMain = NULL; std::vector<CWalletRef> vpwallets;
/** Transaction fee set by the user */ /** Transaction fee set by the user */
CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE); CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE);
unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET; unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET;
@ -3926,7 +3926,6 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
bool CWallet::InitLoadWallet() bool CWallet::InitLoadWallet()
{ {
if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) { if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
pwalletMain = NULL;
LogPrintf("Wallet disabled!\n"); LogPrintf("Wallet disabled!\n");
return true; return true;
} }
@ -3943,7 +3942,7 @@ bool CWallet::InitLoadWallet()
if (!pwallet) { if (!pwallet) {
return false; return false;
} }
pwalletMain = pwallet; vpwallets.push_back(pwallet);
return true; return true;
} }

View File

@ -29,7 +29,8 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
extern CWallet* pwalletMain; typedef CWallet* CWalletRef;
extern std::vector<CWalletRef> vpwallets;
/** /**
* Settings * Settings

View File

@ -760,24 +760,23 @@ void MaybeCompactWalletDB()
return; return;
} }
CWalletDBWrapper& dbh = pwalletMain->GetDBHandle(); for (CWalletRef pwallet : vpwallets) {
CWalletDBWrapper& dbh = pwallet->GetDBHandle();
static unsigned int nLastSeen = dbh.GetUpdateCounter(); unsigned int nUpdateCounter = dbh.nUpdateCounter;
static unsigned int nLastFlushed = dbh.GetUpdateCounter();
static int64_t nLastWalletUpdate = GetTime();
if (nLastSeen != dbh.GetUpdateCounter()) if (dbh.nLastSeen != nUpdateCounter) {
{ dbh.nLastSeen = nUpdateCounter;
nLastSeen = dbh.GetUpdateCounter(); dbh.nLastWalletUpdate = GetTime();
nLastWalletUpdate = GetTime(); }
}
if (nLastFlushed != dbh.GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2) if (dbh.nLastFlushed != nUpdateCounter && GetTime() - dbh.nLastWalletUpdate >= 2) {
{ if (CDB::PeriodicFlush(dbh)) {
if (CDB::PeriodicFlush(dbh)) { dbh.nLastFlushed = nUpdateCounter;
nLastFlushed = dbh.GetUpdateCounter(); }
} }
} }
fOneThread = false; fOneThread = false;
} }