Skip to content

Commit b124cf0

Browse files
committed
Wallet: Replace pwalletMain with a vector of wallet pointers
1 parent 19b3648 commit b124cf0

File tree

10 files changed

+51
-45
lines changed

10 files changed

+51
-45
lines changed

src/init.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ void Shutdown()
198198
StopRPC();
199199
StopHTTPServer();
200200
#ifdef ENABLE_WALLET
201-
if (pwalletMain)
202-
pwalletMain->Flush(false);
201+
for (CWalletRef pwallet : vpwallets) {
202+
pwallet->Flush(false);
203+
}
203204
#endif
204205
MapPort(false);
205206
UnregisterValidationInterface(peerLogic.get());
@@ -239,8 +240,9 @@ void Shutdown()
239240
pblocktree = NULL;
240241
}
241242
#ifdef ENABLE_WALLET
242-
if (pwalletMain)
243-
pwalletMain->Flush(true);
243+
for (CWalletRef pwallet : vpwallets) {
244+
pwallet->Flush(true);
245+
}
244246
#endif
245247

246248
#if ENABLE_ZMQ
@@ -260,8 +262,10 @@ void Shutdown()
260262
#endif
261263
UnregisterAllValidationInterfaces();
262264
#ifdef ENABLE_WALLET
263-
delete pwalletMain;
264-
pwalletMain = NULL;
265+
for (CWalletRef pwallet : vpwallets) {
266+
delete pwallet;
267+
}
268+
vpwallets.clear();
265269
#endif
266270
globalVerifyHandle.reset();
267271
ECC_Stop();
@@ -1673,8 +1677,9 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16731677
uiInterface.InitMessage(_("Done loading"));
16741678

16751679
#ifdef ENABLE_WALLET
1676-
if (pwalletMain)
1677-
pwalletMain->postInitProcess(scheduler);
1680+
for (CWalletRef pwallet : vpwallets) {
1681+
pwallet->postInitProcess(scheduler);
1682+
}
16781683
#endif
16791684

16801685
return !fRequestShutdown;

src/qt/bitcoin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,10 @@ void BitcoinApplication::initializeResult(bool success)
474474
window->setClientModel(clientModel);
475475

476476
#ifdef ENABLE_WALLET
477-
if(pwalletMain)
477+
// TODO: Expose secondary wallets
478+
if (!vpwallets.empty())
478479
{
479-
walletModel = new WalletModel(platformStyle, pwalletMain, optionsModel);
480+
walletModel = new WalletModel(platformStyle, vpwallets[0], optionsModel);
480481

481482
window->addWallet(BitcoinGUI::DEFAULT_WALLET, walletModel);
482483
window->setCurrentWallet(BitcoinGUI::DEFAULT_WALLET);

src/wallet/db.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,6 @@ void CWalletDBWrapper::IncrementUpdateCounter()
439439
++nUpdateCounter;
440440
}
441441

442-
unsigned int CWalletDBWrapper::GetUpdateCounter()
443-
{
444-
return nUpdateCounter.load();
445-
}
446-
447442
void CDB::Close()
448443
{
449444
if (!pdb)

src/wallet/db.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ class CWalletDBWrapper
9393
friend class CDB;
9494
public:
9595
/** Create dummy DB handle */
96-
CWalletDBWrapper(): env(nullptr)
96+
CWalletDBWrapper() : nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr)
9797
{
9898
}
9999

100100
/** Create DB handle to real database */
101-
CWalletDBWrapper(CDBEnv *env_in, const std::string &strFile_in):
102-
env(env_in), strFile(strFile_in)
101+
CWalletDBWrapper(CDBEnv *env_in, const std::string &strFile_in) :
102+
nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(env_in), strFile(strFile_in)
103103
{
104104
}
105105

@@ -120,13 +120,16 @@ class CWalletDBWrapper
120120
void Flush(bool shutdown);
121121

122122
void IncrementUpdateCounter();
123-
unsigned int GetUpdateCounter();
123+
124+
std::atomic<unsigned int> nUpdateCounter;
125+
unsigned int nLastSeen;
126+
unsigned int nLastFlushed;
127+
int64_t nLastWalletUpdate;
124128

125129
private:
126130
/** BerkeleyDB specific */
127131
CDBEnv *env;
128132
std::string strFile;
129-
std::atomic<unsigned int> nUpdateCounter;
130133

131134
/** Return whether this database handle is a dummy for testing.
132135
* Only to be used at a low level, application should ideally not care

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
3535
{
36-
return pwalletMain;
36+
// TODO: Some way to access secondary wallets
37+
return vpwallets.empty() ? nullptr : vpwallets[0];
3738
}
3839

3940
std::string HelpRequiringPassphrase(CWallet * const pwallet)

src/wallet/test/wallet_test_fixture.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "wallet/db.h"
99
#include "wallet/wallet.h"
1010

11+
CWallet *pwalletMain;
12+
1113
WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
1214
TestingSetup(chainName)
1315
{

src/wallet/test/wallet_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <boost/test/unit_test.hpp>
2020
#include <univalue.h>
2121

22+
extern CWallet* pwalletMain;
23+
2224
extern UniValue importmulti(const JSONRPCRequest& request);
2325
extern UniValue dumpwallet(const JSONRPCRequest& request);
2426
extern UniValue importwallet(const JSONRPCRequest& request);
@@ -402,8 +404,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
402404
// after.
403405
{
404406
CWallet wallet;
405-
CWallet *backup = ::pwalletMain;
406-
::pwalletMain = &wallet;
407+
vpwallets.insert(vpwallets.begin(), &wallet);
407408
UniValue keys;
408409
keys.setArray();
409410
UniValue key;
@@ -434,7 +435,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
434435
"downloading and rescanning the relevant blocks (see -reindex and -rescan "
435436
"options).\"}},{\"success\":true}]",
436437
0, oldTip->GetBlockTimeMax(), TIMESTAMP_WINDOW));
437-
::pwalletMain = backup;
438+
vpwallets.erase(vpwallets.begin());
438439
}
439440
}
440441

@@ -444,7 +445,6 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
444445
// than or equal to key birthday.
445446
BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
446447
{
447-
CWallet *pwalletMainBackup = ::pwalletMain;
448448
LOCK(cs_main);
449449

450450
// Create two blocks with same timestamp to verify that importwallet rescan
@@ -470,7 +470,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
470470
JSONRPCRequest request;
471471
request.params.setArray();
472472
request.params.push_back("wallet.backup");
473-
::pwalletMain = &wallet;
473+
vpwallets.insert(vpwallets.begin(), &wallet);
474474
::dumpwallet(request);
475475
}
476476

@@ -482,7 +482,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
482482
JSONRPCRequest request;
483483
request.params.setArray();
484484
request.params.push_back("wallet.backup");
485-
::pwalletMain = &wallet;
485+
vpwallets[0] = &wallet;
486486
::importwallet(request);
487487

488488
BOOST_CHECK_EQUAL(wallet.mapWallet.size(), 3);
@@ -495,7 +495,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
495495
}
496496

497497
SetMockTime(0);
498-
::pwalletMain = pwalletMainBackup;
498+
vpwallets.erase(vpwallets.begin());
499499
}
500500

501501
// Check that GetImmatureCredit() returns a newly calculated value instead of

src/wallet/wallet.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include <boost/algorithm/string/replace.hpp>
3636
#include <boost/thread.hpp>
3737

38-
CWallet* pwalletMain = NULL;
38+
std::vector<CWalletRef> vpwallets;
3939
/** Transaction fee set by the user */
4040
CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE);
4141
unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET;
@@ -3926,7 +3926,6 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
39263926
bool CWallet::InitLoadWallet()
39273927
{
39283928
if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
3929-
pwalletMain = NULL;
39303929
LogPrintf("Wallet disabled!\n");
39313930
return true;
39323931
}
@@ -3943,7 +3942,7 @@ bool CWallet::InitLoadWallet()
39433942
if (!pwallet) {
39443943
return false;
39453944
}
3946-
pwalletMain = pwallet;
3945+
vpwallets.push_back(pwallet);
39473946

39483947
return true;
39493948
}

src/wallet/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
#include <utility>
3030
#include <vector>
3131

32-
extern CWallet* pwalletMain;
32+
typedef CWallet* CWalletRef;
33+
extern std::vector<CWalletRef> vpwallets;
3334

3435
/**
3536
* Settings

src/wallet/walletdb.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -760,24 +760,23 @@ void MaybeCompactWalletDB()
760760
return;
761761
}
762762

763-
CWalletDBWrapper& dbh = pwalletMain->GetDBHandle();
763+
for (CWalletRef pwallet : vpwallets) {
764+
CWalletDBWrapper& dbh = pwallet->GetDBHandle();
764765

765-
static unsigned int nLastSeen = dbh.GetUpdateCounter();
766-
static unsigned int nLastFlushed = dbh.GetUpdateCounter();
767-
static int64_t nLastWalletUpdate = GetTime();
766+
unsigned int nUpdateCounter = dbh.nUpdateCounter;
768767

769-
if (nLastSeen != dbh.GetUpdateCounter())
770-
{
771-
nLastSeen = dbh.GetUpdateCounter();
772-
nLastWalletUpdate = GetTime();
773-
}
768+
if (dbh.nLastSeen != nUpdateCounter) {
769+
dbh.nLastSeen = nUpdateCounter;
770+
dbh.nLastWalletUpdate = GetTime();
771+
}
774772

775-
if (nLastFlushed != dbh.GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2)
776-
{
777-
if (CDB::PeriodicFlush(dbh)) {
778-
nLastFlushed = dbh.GetUpdateCounter();
773+
if (dbh.nLastFlushed != nUpdateCounter && GetTime() - dbh.nLastWalletUpdate >= 2) {
774+
if (CDB::PeriodicFlush(dbh)) {
775+
dbh.nLastFlushed = nUpdateCounter;
776+
}
779777
}
780778
}
779+
781780
fOneThread = false;
782781
}
783782

0 commit comments

Comments
 (0)