Skip to content

Commit 68cec8d

Browse files
committed
refactor: use interfaces::WalletLoader in Dash-specific wallet init
1 parent c9c5275 commit 68cec8d

File tree

11 files changed

+49
-25
lines changed

11 files changed

+49
-25
lines changed

src/coinjoin/interfaces.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class CoinJoinLoaderImpl : public interfaces::CoinJoin::Loader
8282
return *Assert(Assert(m_node.cj_ctx)->walletman);
8383
}
8484

85+
interfaces::WalletLoader& wallet_loader()
86+
{
87+
return *Assert(m_node.wallet_loader);
88+
}
89+
8590
public:
8691
explicit CoinJoinLoaderImpl(NodeContext& node) :
8792
m_node(node)
@@ -93,12 +98,12 @@ class CoinJoinLoaderImpl : public interfaces::CoinJoin::Loader
9398
void AddWallet(const std::shared_ptr<CWallet>& wallet) override
9499
{
95100
walletman().Add(wallet);
96-
g_wallet_init_interface.InitCoinJoinSettings(*this);
101+
g_wallet_init_interface.InitCoinJoinSettings(*this, wallet_loader());
97102
}
98103
void RemoveWallet(const std::string& name) override
99104
{
100105
walletman().Remove(name);
101-
g_wallet_init_interface.InitCoinJoinSettings(*this);
106+
g_wallet_init_interface.InitCoinJoinSettings(*this, wallet_loader());
102107
}
103108
void FlushWallet(const std::string& name) override
104109
{

src/dummywallet.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Chain;
1414
class Handler;
1515
class Wallet;
1616
class WalletClient;
17+
class WalletLoader;
1718
namespace CoinJoin {
1819
class Loader;
1920
} // namespcae CoinJoin
@@ -28,8 +29,8 @@ class DummyWalletInit : public WalletInitInterface {
2829
void Construct(NodeContext& node) const override {LogPrintf("No wallet support compiled in!\n");}
2930

3031
// Dash Specific WalletInitInterface InitCoinJoinSettings
31-
void AutoLockMasternodeCollaterals() const override {}
32-
void InitCoinJoinSettings(interfaces::CoinJoin::Loader& coinjoin_loader) const override {}
32+
void AutoLockMasternodeCollaterals(interfaces::WalletLoader& wallet_loader) const override {}
33+
void InitCoinJoinSettings(interfaces::CoinJoin::Loader& coinjoin_loader, interfaces::WalletLoader& wallet_loader) const override {}
3334
bool InitAutoBackup() const override {return true;}
3435
};
3536

src/init.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
22022202
chainman.m_load_block = std::thread(&util::TraceThread, "loadblk", [=, &args, &chainman, &node] {
22032203
ThreadImport(chainman, *node.dmnman, *g_ds_notification_interface, vImportFiles, node.mn_activeman.get(), args);
22042204
});
2205+
#ifdef ENABLE_WALLET
2206+
if (!args.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
2207+
g_wallet_init_interface.AutoLockMasternodeCollaterals(*node.wallet_loader);
2208+
}
2209+
#endif // ENABLE_WALLET
22052210

22062211
// Wait for genesis block to be processed
22072212
{

src/interfaces/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class Wallet
8686
//! Abort a rescan.
8787
virtual void abortRescan() = 0;
8888

89+
//! Lock masternode collaterals
90+
virtual void autoLockMasternodeCollaterals() = 0;
91+
8992
//! Back up wallet.
9093
virtual bool backupWallet(const std::string& filename) = 0;
9194

src/node/blockstorage.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,5 @@ void ThreadImport(ChainstateManager& chainman, CDeterministicMNManager& dmnman,
926926
mn_activeman->Init(chainman.ActiveTip());
927927
}
928928

929-
g_wallet_init_interface.AutoLockMasternodeCollaterals();
930-
931929
chainman.ActiveChainstate().LoadMempool(args);
932930
}

src/qt/test/addressbooktests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ void EditAddressAndSubmit(
6363
void TestAddAddressesToSendBook(interfaces::Node& node)
6464
{
6565
TestChain100Setup test;
66-
auto wallet_loader = interfaces::MakeWalletLoader(*test.m_node.chain, *Assert(test.m_node.args), *Assert(test.m_node.coinjoin_loader));
67-
test.m_node.wallet_loader = wallet_loader.get();
6866
node.setContext(&test.m_node);
6967
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), node.context()->coinjoin_loader.get(), "", CreateMockWalletDatabase());
7068
wallet->SetupLegacyScriptPubKeyMan();

src/qt/test/wallettests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ void TestGUI(interfaces::Node& node)
108108
for (int i = 0; i < 5; ++i) {
109109
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
110110
}
111-
auto wallet_loader = interfaces::MakeWalletLoader(*test.m_node.chain, *Assert(test.m_node.args), *Assert(test.m_node.coinjoin_loader));
112-
test.m_node.wallet_loader = wallet_loader.get();
113111
node.setContext(&test.m_node);
114112
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), node.context()->coinjoin_loader.get(), "", CreateMockWalletDatabase());
115113
AddWallet(wallet);

src/test/util/setup_common.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
#ifdef ENABLE_WALLET
6565
#include <interfaces/coinjoin.h>
66+
#include <interfaces/wallet.h>
6667
#endif // ENABLE_WALLET
6768

6869
#include <stdexcept>
@@ -315,8 +316,16 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
315316
m_node.cj_ctx = std::make_unique<CJContext>(*m_node.chainman, *m_node.connman, *m_node.dmnman, *m_node.mn_metaman, *m_node.mempool,
316317
/*mn_activeman=*/nullptr, *m_node.mn_sync, *m_node.llmq_ctx->isman, m_node.peerman,
317318
/*relay_txes=*/true);
319+
318320
#ifdef ENABLE_WALLET
321+
// WalletInit::Construct()-like logic needed for wallet tests that run on
322+
// TestingSetup and its children (e.g. TestChain100Setup) instead of
323+
// WalletTestingSetup
319324
m_node.coinjoin_loader = interfaces::MakeCoinJoinLoader(m_node);
325+
326+
auto wallet_loader = interfaces::MakeWalletLoader(*m_node.chain, *m_node.args, *m_node.coinjoin_loader);
327+
m_node.wallet_loader = wallet_loader.get();
328+
m_node.chain_clients.emplace_back(std::move(wallet_loader));
320329
#endif // ENABLE_WALLET
321330

322331
BlockValidationState state;
@@ -328,6 +337,11 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
328337
TestingSetup::~TestingSetup()
329338
{
330339
#ifdef ENABLE_WALLET
340+
for (auto& client : m_node.chain_clients) {
341+
client.reset();
342+
}
343+
m_node.wallet_loader = nullptr;
344+
331345
m_node.coinjoin_loader.reset();
332346
#endif // ENABLE_WALLET
333347
m_node.cj_ctx.reset();

src/wallet/init.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class WalletInit : public WalletInitInterface
4747
void Construct(NodeContext& node) const override;
4848

4949
// Dash Specific Wallet Init
50-
void AutoLockMasternodeCollaterals() const override;
51-
void InitCoinJoinSettings(interfaces::CoinJoin::Loader& coinjoin_loader) const override;
50+
void AutoLockMasternodeCollaterals(interfaces::WalletLoader& wallet_loader) const override;
51+
void InitCoinJoinSettings(interfaces::CoinJoin::Loader& coinjoin_loader, interfaces::WalletLoader& wallet_loader) const override;
5252
bool InitAutoBackup() const override;
5353
};
5454

@@ -196,30 +196,30 @@ void WalletInit::Construct(NodeContext& node) const
196196
}
197197

198198

199-
void WalletInit::AutoLockMasternodeCollaterals() const
199+
void WalletInit::AutoLockMasternodeCollaterals(interfaces::WalletLoader& wallet_loader) const
200200
{
201201
// we can't do this before DIP3 is fully initialized
202-
for (const auto& pwallet : GetWallets()) {
203-
pwallet->AutoLockMasternodeCollaterals();
202+
for (const auto& wallet : wallet_loader.getWallets()) {
203+
wallet->autoLockMasternodeCollaterals();
204204
}
205205
}
206206

207-
void WalletInit::InitCoinJoinSettings(interfaces::CoinJoin::Loader& coinjoin_loader) const
207+
void WalletInit::InitCoinJoinSettings(interfaces::CoinJoin::Loader& coinjoin_loader, interfaces::WalletLoader& wallet_loader) const
208208
{
209-
CCoinJoinClientOptions::SetEnabled(!GetWallets().empty() ? gArgs.GetBoolArg("-enablecoinjoin", true) : false);
209+
const auto& wallets{wallet_loader.getWallets()};
210+
CCoinJoinClientOptions::SetEnabled(!wallets.empty() ? gArgs.GetBoolArg("-enablecoinjoin", true) : false);
210211
if (!CCoinJoinClientOptions::IsEnabled()) {
211212
return;
212213
}
213214
bool fAutoStart = gArgs.GetBoolArg("-coinjoinautostart", DEFAULT_COINJOIN_AUTOSTART);
214-
for (auto& pwallet : GetWallets()) {
215-
auto manager = coinjoin_loader.GetClient(pwallet->GetName());
216-
assert(manager != nullptr);
217-
if (pwallet->IsLocked()) {
215+
for (auto& wallet : wallets) {
216+
auto manager = Assert(coinjoin_loader.GetClient(wallet->getWalletName()));
217+
if (wallet->isLocked(/*fForMixing=*/false)) {
218218
manager->stopMixing();
219-
LogPrintf("CoinJoin: Mixing stopped for locked wallet \"%s\"\n", pwallet->GetName());
219+
LogPrintf("CoinJoin: Mixing stopped for locked wallet \"%s\"\n", wallet->getWalletName());
220220
} else if (fAutoStart) {
221221
manager->startMixing();
222-
LogPrintf("CoinJoin: Automatic mixing started for wallet \"%s\"\n", pwallet->GetName());
222+
LogPrintf("CoinJoin: Automatic mixing started for wallet \"%s\"\n", wallet->getWalletName());
223223
}
224224
}
225225
LogPrintf("CoinJoin: autostart=%d, multisession=%d," /* Continued */

src/wallet/interfaces.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class WalletImpl : public Wallet
147147
return m_wallet->ChangeWalletPassphrase(old_wallet_passphrase, new_wallet_passphrase);
148148
}
149149
void abortRescan() override { m_wallet->AbortRescan(); }
150+
void autoLockMasternodeCollaterals() override { m_wallet->AutoLockMasternodeCollaterals(); }
150151
bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); }
151152
bool autoBackupWallet(const fs::path& wallet_path, bilingual_str& error_string, std::vector<bilingual_str>& warnings) override
152153
{

0 commit comments

Comments
 (0)