Skip to content

Commit

Permalink
wallet: Add AddWallet, RemoveWallet, GetWallet and GetWallets
Browse files Browse the repository at this point in the history
With these new functions all vpwallets usage are removed
and vpwallets is now a static variable (no external linkage).
  • Loading branch information
promag committed Apr 18, 2018
1 parent 6efd964 commit 432ea7d
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/interfaces/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class NodeImpl : public Node
{
#ifdef ENABLE_WALLET
std::vector<std::unique_ptr<Wallet>> wallets;
for (CWallet* wallet : ::vpwallets) {
for (CWallet* wallet : GetWallets()) {
wallets.emplace_back(MakeWallet(*wallet));
}
return wallets;
Expand Down
6 changes: 3 additions & 3 deletions src/qt/test/wallettests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ void TestGUI()
TransactionView transactionView(platformStyle.get());
auto node = interfaces::MakeNode();
OptionsModel optionsModel(*node);
vpwallets.insert(vpwallets.begin(), &wallet);
WalletModel walletModel(std::move(node->getWallets()[0]), *node, platformStyle.get(), &optionsModel);
vpwallets.erase(vpwallets.begin());
AddWallet(&wallet);
WalletModel walletModel(std::move(node->getWallets().back()), *node, platformStyle.get(), &optionsModel);
RemoveWallet(&wallet);
sendCoinsDialog.setModel(&walletModel);
transactionView.setModel(&walletModel);

Expand Down
2 changes: 1 addition & 1 deletion src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ UniValue validateaddress(const JSONRPCRequest& request)
{

#ifdef ENABLE_WALLET
if (!::vpwallets.empty() && IsDeprecatedRPCEnabled("validateaddress")) {
if (!GetWallets().empty() && IsDeprecatedRPCEnabled("validateaddress")) {
ret.pushKVs(getaddressinfo(request));
}
#endif
Expand Down
12 changes: 6 additions & 6 deletions src/wallet/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,37 +315,37 @@ bool WalletInit::Open() const
if (!pwallet) {
return false;
}
vpwallets.push_back(pwallet);
AddWallet(pwallet);
}

return true;
}

void WalletInit::Start(CScheduler& scheduler) const
{
for (CWallet* pwallet : vpwallets) {
for (CWallet* pwallet : GetWallets()) {
pwallet->postInitProcess(scheduler);
}
}

void WalletInit::Flush() const
{
for (CWallet* pwallet : vpwallets) {
for (CWallet* pwallet : GetWallets()) {
pwallet->Flush(false);
}
}

void WalletInit::Stop() const
{
for (CWallet* pwallet : vpwallets) {
for (CWallet* pwallet : GetWallets()) {
pwallet->Flush(true);
}
}

void WalletInit::Close() const
{
for (CWallet* pwallet : vpwallets) {
for (CWallet* pwallet : GetWallets()) {
RemoveWallet(pwallet);
delete pwallet;
}
vpwallets.clear();
}
18 changes: 8 additions & 10 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,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 (CWallet* pwallet : ::vpwallets) {
if (pwallet->GetName() == requestedWallet) {
return pwallet;
}
}
throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
CWallet* pwallet = GetWallet(requestedWallet);
if (!pwallet) throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
return pwallet;
}
return ::vpwallets.size() == 1 || (request.fHelp && ::vpwallets.size() > 0) ? ::vpwallets[0] : nullptr;

std::vector<CWallet*> wallets = GetWallets();
return wallets.size() == 1 || (request.fHelp && wallets.size() > 0) ? wallets[0] : nullptr;
}

std::string HelpRequiringPassphrase(CWallet * const pwallet)
Expand All @@ -67,7 +66,7 @@ bool EnsureWalletIsAvailable(CWallet * const pwallet, bool avoidException)
{
if (pwallet) return true;
if (avoidException) return false;
if (::vpwallets.empty()) {
if (GetWallets().empty()) {
// 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 @@ -2862,8 +2861,7 @@ UniValue listwallets(const JSONRPCRequest& request)

UniValue obj(UniValue::VARR);

for (CWallet* pwallet : vpwallets) {

for (CWallet* pwallet : GetWallets()) {
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
return NullUniValue;
}
Expand Down
12 changes: 6 additions & 6 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
// after.
{
CWallet wallet("dummy", WalletDatabase::CreateDummy());
vpwallets.insert(vpwallets.begin(), &wallet);
AddWallet(&wallet);
UniValue keys;
keys.setArray();
UniValue key;
Expand Down Expand Up @@ -105,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());
RemoveWallet(&wallet);
}
}

Expand Down Expand Up @@ -140,9 +140,9 @@ 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);
AddWallet(&wallet);
::dumpwallet(request);
vpwallets.erase(vpwallets.begin());
RemoveWallet(&wallet);
}

// Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME
Expand All @@ -153,9 +153,9 @@ 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);
AddWallet(&wallet);
::importwallet(request);
vpwallets.erase(vpwallets.begin());
RemoveWallet(&wallet);

LOCK(wallet.cs_wallet);
BOOST_CHECK_EQUAL(wallet.mapWallet.size(), 3U);
Expand Down
31 changes: 30 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,40 @@
#include <wallet/fees.h>

#include <assert.h>
#include <algorithm>
#include <future>

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

std::vector<CWallet*> vpwallets;
static std::vector<CWallet*> vpwallets;

void AddWallet(CWallet* wallet)
{
assert(wallet);
auto i = std::find(vpwallets.begin(), vpwallets.end(), wallet);
if (i == vpwallets.end()) vpwallets.push_back(wallet);
}

void RemoveWallet(CWallet* wallet)
{
assert(wallet);
auto i = std::find(vpwallets.begin(), vpwallets.end(), wallet);
if (i != vpwallets.end()) vpwallets.erase(i);
}

std::vector<CWallet*> GetWallets()
{
return vpwallets;
}

CWallet* GetWallet(const std::string& name)
{
for (CWallet* wallet : vpwallets) {
if (wallet->GetName() == name) return wallet;
}
return nullptr;
}

/** Transaction fee set by the user */
CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE);
unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET;
Expand Down
5 changes: 4 additions & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
#include <utility>
#include <vector>

extern std::vector<CWallet*> vpwallets;
void AddWallet(CWallet* wallet);
void RemoveWallet(CWallet* wallet);
std::vector<CWallet*> GetWallets();
CWallet* GetWallet(const std::string& name);

/**
* Settings
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ void MaybeCompactWalletDB()
return;
}

for (CWallet* pwallet : vpwallets) {
for (CWallet* pwallet : GetWallets()) {
WalletDatabase& dbh = pwallet->GetDBHandle();

unsigned int nUpdateCounter = dbh.nUpdateCounter;
Expand Down

0 comments on commit 432ea7d

Please sign in to comment.