Skip to content
Merged
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
36 changes: 15 additions & 21 deletions src/coinjoin/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,16 @@ void CCoinJoinClientSession::ProcessMessage(CNode& peer, CChainState& active_cha
}

bool CCoinJoinClientManager::StartMixing() {
bool expected{false};
return fMixing.compare_exchange_strong(expected, true);
return m_wallet->StartMixing();
}

void CCoinJoinClientManager::StopMixing() {
fMixing = false;
m_wallet->StopMixing();
}

bool CCoinJoinClientManager::IsMixing() const
{
return fMixing;
return m_wallet->IsMixing();
}

void CCoinJoinClientSession::ResetPool()
Expand Down Expand Up @@ -1905,7 +1904,10 @@ void CoinJoinWalletManager::Add(const std::shared_ptr<CWallet>& wallet)
m_mn_sync, m_isman, m_queueman,
m_is_masternode));
}
g_wallet_init_interface.InitCoinJoinSettings(*this);
if (!CCoinJoinClientOptions::IsEnabled()) return;
DoForClient(wallet->GetName(), [](CCoinJoinClientManager& manager) {
g_wallet_init_interface.InitCoinJoinSettings(manager);
});
}

void CoinJoinWalletManager::DoMaintenance(CConnman& connman)
Expand All @@ -1916,26 +1918,18 @@ void CoinJoinWalletManager::DoMaintenance(CConnman& connman)
}
}

void CoinJoinWalletManager::Remove(const std::string& name) {
{
LOCK(cs_wallet_manager_map);
m_wallet_manager_map.erase(name);
}
g_wallet_init_interface.InitCoinJoinSettings(*this);
}

void CoinJoinWalletManager::Flush(const std::string& name)
void CoinJoinWalletManager::Remove(const std::string& name)
{
auto clientman = Assert(Get(name));
clientman->ResetPool();
clientman->StopMixing();
LOCK(cs_wallet_manager_map);
m_wallet_manager_map.erase(name);
}

CCoinJoinClientManager* CoinJoinWalletManager::Get(const std::string& name) const
void CoinJoinWalletManager::Flush(const std::string& name)
{
LOCK(cs_wallet_manager_map);
auto it = m_wallet_manager_map.find(name);
return (it != m_wallet_manager_map.end()) ? it->second.get() : nullptr;
DoForClient(name, [](CCoinJoinClientManager& manager) {
manager.ResetPool();
manager.StopMixing();
});
}

CDeterministicMNCPtr CCoinJoinClientManager::GetRandomUsedMasternode()
Expand Down
14 changes: 11 additions & 3 deletions src/coinjoin/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ class CoinJoinWalletManager {
void Remove(const std::string& name);
void Flush(const std::string& name);

CCoinJoinClientManager* Get(const std::string& name) const;
//! Invoke func under the manager lock. The client must not escape func.
//! Return false without calling func when the wallet has been removed.
template <typename Callable>
bool DoForClient(const std::string& name, Callable&& func) EXCLUSIVE_LOCKS_REQUIRED(!cs_wallet_manager_map)
{
LOCK(cs_wallet_manager_map);
const auto it = m_wallet_manager_map.find(name);
if (it == m_wallet_manager_map.end()) return false;
func(*it->second);
return true;
}

template <typename Callable>
void ForEachCJClientMan(Callable&& func)
Expand Down Expand Up @@ -283,8 +293,6 @@ class CCoinJoinClientManager
// TODO: or map<denom, CCoinJoinClientSession> ??
std::deque<CCoinJoinClientSession> deqSessions GUARDED_BY(cs_deqsessions);

std::atomic<bool> fMixing{false};

int nCachedLastSuccessBlock{0};
int nMinBlocksToWait{1}; // how many blocks to wait for after one successful mixing tx in non-multisession mode
bilingual_str strAutoDenomResult;
Expand Down
15 changes: 11 additions & 4 deletions src/coinjoin/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <interfaces/coinjoin.h>

#include <coinjoin/client.h>
#include <coinjoin/options.h>
#include <util/system.h>
#include <wallet/wallet.h>

#include <memory>
Expand Down Expand Up @@ -65,7 +67,10 @@ class CoinJoinLoaderImpl : public interfaces::CoinJoin::Loader

public:
explicit CoinJoinLoaderImpl(CoinJoinWalletManager& walletman)
: m_walletman(walletman) {}
: m_walletman(walletman)
{
CCoinJoinClientOptions::SetEnabled(gArgs.GetBoolArg("-enablecoinjoin", true));
}

void AddWallet(const std::shared_ptr<CWallet>& wallet) override { m_walletman.Add(wallet); }
void RemoveWallet(const std::string& name) override
Expand All @@ -76,10 +81,12 @@ class CoinJoinLoaderImpl : public interfaces::CoinJoin::Loader
{
m_walletman.Flush(name);
}
std::unique_ptr<interfaces::CoinJoin::Client> GetClient(const std::string& name) override
bool WithClient(const std::string& name, const std::function<void(interfaces::CoinJoin::Client&)>& func) override
{
auto clientman = m_walletman.Get(name);
return clientman ? std::make_unique<CoinJoinClientImpl>(*clientman) : nullptr;
return m_walletman.DoForClient(name, [&](CCoinJoinClientManager& manager) {
CoinJoinClientImpl client{manager};
func(client);
});
}
CoinJoinWalletManager& walletman() override
{
Expand Down
2 changes: 1 addition & 1 deletion src/dummywallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DummyWalletInit : public WalletInitInterface {

// Dash Specific WalletInitInterface InitCoinJoinSettings
void AutoLockMasternodeCollaterals() const override {}
void InitCoinJoinSettings(const CoinJoinWalletManager& cjwalletman) const override {}
void InitCoinJoinSettings(CCoinJoinClientManager& manager) const override {}
bool InitAutoBackup() const override {return true;}
};

Expand Down
1 change: 0 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)

#ifdef ENABLE_WALLET
node.coinjoin_loader = interfaces::MakeCoinJoinLoader(*node.cj_ctx->walletman);
g_wallet_init_interface.InitCoinJoinSettings(*node.cj_ctx->walletman);
#endif // ENABLE_WALLET

// ********************************************************* Step 7d: Setup other Dash services
Expand Down
5 changes: 4 additions & 1 deletion src/interfaces/coinjoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BITCOIN_INTERFACES_COINJOIN_H
#define BITCOIN_INTERFACES_COINJOIN_H

#include <functional>
#include <memory>
#include <string>

Expand Down Expand Up @@ -37,7 +38,9 @@ class Loader
//! Remove wallet from CoinJoin client manager
virtual void RemoveWallet(const std::string&) = 0;
virtual void FlushWallet(const std::string&) = 0;
virtual std::unique_ptr<CoinJoin::Client> GetClient(const std::string&) = 0;
//! Invoke func while the named client is protected from concurrent removal.
//! References to the client must not escape the callback.
virtual bool WithClient(const std::string& name, const std::function<void(Client&)>& func) = 0;
virtual CoinJoinWalletManager& walletman() = 0;
};
} // namespace CoinJoin
Expand Down
2 changes: 1 addition & 1 deletion src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,7 @@ void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, const QStri
#ifdef ENABLE_WALLET
if (enableWallet) {
for (const auto& wallet : m_node.walletLoader().getWallets()) {
disableAppNap |= m_node.coinJoinLoader()->GetClient(wallet->getWalletName())->isMixing();
m_node.coinJoinLoader()->WithClient(wallet->getWalletName(), [&](auto& client) { disableAppNap |= client.isMixing(); });
}
}
#endif // ENABLE_WALLET
Expand Down
2 changes: 1 addition & 1 deletion src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ void OptionsDialog::on_okButton_clicked()
#ifdef ENABLE_WALLET
if (m_enable_wallet) {
for (auto& wallet : model->node().walletLoader().getWallets()) {
model->node().coinJoinLoader()->GetClient(wallet->getWalletName())->resetCachedBlocks();
model->node().coinJoinLoader()->WithClient(wallet->getWalletName(), [](auto& client) { client.resetCachedBlocks(); });
wallet->markDirty();
}
}
Expand Down
72 changes: 44 additions & 28 deletions src/qt/overviewpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ void OverviewPage::setWalletModel(WalletModel *model)

// Disable coinJoinClient builtin support for automatic backups while we are in GUI,
// we'll handle automatic backups and user warnings in coinJoinStatus()
walletModel->coinJoin()->disableAutobackups();
walletModel->withCoinJoin([](auto& client) { client.disableAutobackups(); });

connect(ui->toggleCoinJoin, &QPushButton::clicked, this, &OverviewPage::toggleCoinJoin);

Expand Down Expand Up @@ -918,7 +918,11 @@ void OverviewPage::coinJoinStatus(bool fForce)
int nBestHeight = clientModel->node().getNumBlocks();

// We are processing more than 1 block per second, we'll just leave
if (nBestHeight > walletModel->coinJoin()->getCachedBlocks() && GetTime() - nLastDSProgressBlockTime <= 1) return;
bool tooFast{false};
walletModel->withCoinJoin([&](auto& client) {
tooFast = nBestHeight > client.getCachedBlocks() && GetTime() - nLastDSProgressBlockTime <= 1;
});
if (tooFast) return;
nLastDSProgressBlockTime = GetTime();

QString strKeysLeftText(tr("keys left: %1").arg(walletModel->getKeysLeftSinceAutoBackup()));
Expand All @@ -932,12 +936,17 @@ void OverviewPage::coinJoinStatus(bool fForce)
ui->labelCoinJoinEnabled->setToolTip(strKeysLeftText);

QString strCoinJoinName = QString::fromStdString(gCoinJoinName);
if (!walletModel->coinJoin()->isMixing()) {
if (nBestHeight != walletModel->coinJoin()->getCachedBlocks()) {
walletModel->coinJoin()->setCachedBlocks(nBestHeight);
updateCoinJoinProgress();
bool notMixing{false};
bool refreshProgress{false};
walletModel->withCoinJoin([&](auto& client) {
notMixing = !client.isMixing();
if (notMixing && nBestHeight != client.getCachedBlocks()) {
client.setCachedBlocks(nBestHeight);
refreshProgress = true;
}

});
if (refreshProgress) updateCoinJoinProgress();
if (notMixing) {
setWidgetsVisible(false);
ui->toggleCoinJoin->setText(tr("Start %1").arg(strCoinJoinName));

Expand Down Expand Up @@ -995,7 +1004,8 @@ void OverviewPage::coinJoinStatus(bool fForce)
}
}

QString strEnabled = walletModel->coinJoin()->isMixing() ? tr("Enabled") : tr("Disabled");
QString strEnabled;
walletModel->withCoinJoin([&](auto& client) { strEnabled = client.isMixing() ? tr("Enabled") : tr("Disabled"); });
// Show how many keys left in advanced PS UI mode only
if(fShowAdvancedCJUI && !strKeysLeftText.isEmpty()) strEnabled += ", " + strKeysLeftText;
ui->labelCoinJoinEnabled->setText(strEnabled);
Expand All @@ -1019,15 +1029,18 @@ void OverviewPage::coinJoinStatus(bool fForce)
}

// check coinjoin status and unlock if needed
if(nBestHeight != walletModel->coinJoin()->getCachedBlocks()) {
// Balance and number of transactions might have changed
walletModel->coinJoin()->setCachedBlocks(nBestHeight);
updateCoinJoinProgress();
}
bool refreshProgressTail{false};
walletModel->withCoinJoin([&](auto& client) {
if (nBestHeight != client.getCachedBlocks()) {
// Balance and number of transactions might have changed
client.setCachedBlocks(nBestHeight);
refreshProgressTail = true;
}
ui->labelSubmittedDenom->setText(m_privacy ? "####" : QString(client.getSessionDenoms().c_str()));
});
if (refreshProgressTail) updateCoinJoinProgress();

setWidgetsVisible(true);

ui->labelSubmittedDenom->setText(m_privacy ? "####" : QString(walletModel->coinJoin()->getSessionDenoms().c_str()));
}

void OverviewPage::toggleCoinJoin(){
Expand All @@ -1042,7 +1055,9 @@ void OverviewPage::toggleCoinJoin(){
settings.setValue("hasMixed", "hasMixed");
}

if (!walletModel->coinJoin()->isMixing()) {
bool mixing{false};
walletModel->withCoinJoin([&](auto& client) { mixing = client.isMixing(); });
if (!mixing) {
auto& options = walletModel->node().coinJoinOptions();
const CAmount nMinAmount = options.getSmallestDenomination() + options.getMaxCollateralAmount();
if(m_balances.balance < nMinAmount) {
Expand All @@ -1060,7 +1075,7 @@ void OverviewPage::toggleCoinJoin(){
if(!ctx.isValid())
{
//unlock was cancelled
walletModel->coinJoin()->resetCachedBlocks();
walletModel->withCoinJoin([](auto& client) { client.resetCachedBlocks(); });
QMessageBox::warning(this, strCoinJoinName,
tr("Wallet is locked and user declined to unlock. Disabling %1.").arg(strCoinJoinName),
QMessageBox::Ok, QMessageBox::Ok);
Expand All @@ -1071,16 +1086,17 @@ void OverviewPage::toggleCoinJoin(){

}

walletModel->coinJoin()->resetCachedBlocks();

if (walletModel->coinJoin()->isMixing()) {
ui->toggleCoinJoin->setText(tr("Start %1").arg(strCoinJoinName));
walletModel->coinJoin()->resetPool();
walletModel->coinJoin()->stopMixing();
} else {
ui->toggleCoinJoin->setText(tr("Stop %1").arg(strCoinJoinName));
walletModel->coinJoin()->startMixing();
}
walletModel->withCoinJoin([&](auto& client) {
client.resetCachedBlocks();
if (client.isMixing()) {
ui->toggleCoinJoin->setText(tr("Start %1").arg(strCoinJoinName));
client.resetPool();
client.stopMixing();
} else {
ui->toggleCoinJoin->setText(tr("Stop %1").arg(strCoinJoinName));
client.startMixing();
}
});
}

void OverviewPage::SetupTransactionList(int nNumItems)
Expand Down Expand Up @@ -1119,5 +1135,5 @@ void OverviewPage::DisableCoinJoinCompletely()
if (nWalletBackups <= 0) {
ui->labelCoinJoinEnabled->setText("<span style='" + GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR) + "'>(" + tr("Disabled") + ")</span>");
}
walletModel->coinJoin()->stopMixing();
walletModel->withCoinJoin([](auto& client) { client.stopMixing(); });
}
4 changes: 2 additions & 2 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ void WalletModel::setClientModel(ClientModel* client_model)
if (!m_client_model) timer->stop();
}

std::unique_ptr<interfaces::CoinJoin::Client> WalletModel::coinJoin() const
bool WalletModel::withCoinJoin(const std::function<void(interfaces::CoinJoin::Client&)>& func) const
{
return m_node.coinJoinLoader()->GetClient(m_wallet->getWalletName());
return m_node.coinJoinLoader()->WithClient(m_wallet->getWalletName(), func);
}

void WalletModel::updateStatus()
Expand Down
3 changes: 2 additions & 1 deletion src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <interfaces/wallet.h>
#include <support/allocators/secure.h>

#include <functional>
#include <vector>

#include <QObject>
Expand Down Expand Up @@ -150,7 +151,7 @@ class WalletModel : public QObject
interfaces::Node& node() const { return m_node; }
interfaces::Wallet& wallet() const { return *m_wallet; }
void setClientModel(ClientModel* client_model);
std::unique_ptr<interfaces::CoinJoin::Client> coinJoin() const;
bool withCoinJoin(const std::function<void(interfaces::CoinJoin::Client&)>& func) const;

QString getWalletName() const;
QString getDisplayName() const;
Expand Down
Loading
Loading