diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index 25c776f5d9..98ac7305ca 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -812,8 +812,10 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr return false; } + const auto bal = mixingWallet.GetBalance(); + // check if there is anything left to do - CAmount nBalanceAnonymized = mixingWallet.GetAnonymizedBalance(); + CAmount nBalanceAnonymized = bal.m_anonymized; nBalanceNeedsAnonymized = CCoinJoinClientOptions::GetAmount() * COIN - nBalanceAnonymized; if (nBalanceNeedsAnonymized < 0) { @@ -843,8 +845,8 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr // excluding denoms CAmount nBalanceAnonimizableNonDenom = mixingWallet.GetAnonymizableBalance(true); // denoms - CAmount nBalanceDenominatedConf = mixingWallet.GetDenominatedBalance(); - CAmount nBalanceDenominatedUnconf = mixingWallet.GetDenominatedBalance(true); + CAmount nBalanceDenominatedConf = bal.m_denominated_trusted; + CAmount nBalanceDenominatedUnconf = bal.m_denominated_untrusted_pending; CAmount nBalanceDenominated = nBalanceDenominatedConf + nBalanceDenominatedUnconf; CAmount nBalanceToDenominate = CCoinJoinClientOptions::GetAmount() * COIN - nBalanceDenominated; diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp index d51bf0ce46..ab67ec904f 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -388,7 +388,7 @@ public: result.balance = bal.m_mine_trusted; result.unconfirmed_balance = bal.m_mine_untrusted_pending; result.immature_balance = bal.m_mine_immature; - result.anonymized_balance = m_wallet->GetAnonymizedBalance(); + result.anonymized_balance = bal.m_anonymized; result.have_watch_only = m_wallet->HaveWatchOnly(); if (result.have_watch_only) { result.watch_only_balance = bal.m_watchonly_trusted; @@ -419,11 +419,16 @@ public: } CAmount getAnonymizedBalance() override { - return m_wallet->GetAnonymizedBalance(); + return m_wallet->GetBalance().m_anonymized; } CAmount getDenominatedBalance(bool unconfirmed) override { - return m_wallet->GetDenominatedBalance(unconfirmed); + const auto bal = m_wallet->GetBalance(); + if (unconfirmed) { + return bal.m_denominated_untrusted_pending; + } else { + return bal.m_denominated_trusted; + } } CAmount getNormalizedAnonymizedBalance() override { @@ -436,7 +441,7 @@ public: CAmount getAvailableBalance(const CCoinControl& coin_control) override { if (coin_control.IsUsingCoinJoin()) { - return m_wallet->GetAnonymizedBalance(&coin_control); + return m_wallet->GetBalance(0, false, &coin_control).m_anonymized; } else { return m_wallet->GetAvailableBalance(&coin_control); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index dd454230e2..7889d9fa04 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2648,7 +2648,7 @@ static UniValue getwalletinfo(const JSONRPCRequest& request) obj.pushKV("walletname", pwallet->GetName()); obj.pushKV("walletversion", pwallet->GetVersion()); obj.pushKV("balance", ValueFromAmount(bal.m_mine_trusted)); - obj.pushKV("coinjoin_balance", ValueFromAmount(pwallet->GetAnonymizedBalance())); + obj.pushKV("coinjoin_balance", ValueFromAmount(bal.m_anonymized)); obj.pushKV("unconfirmed_balance", ValueFromAmount(bal.m_mine_untrusted_pending)); obj.pushKV("immature_balance", ValueFromAmount(bal.m_mine_immature)); obj.pushKV("txcount", (int)pwallet->mapWallet.size()); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index c94e6ec775..b1a80350a4 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2649,7 +2649,7 @@ std::unordered_set CWallet::GetSpendableTXs() return ret; } -CWallet::Balance CWallet::GetBalance(const int min_depth, const bool fAddLocked) const +CWallet::Balance CWallet::GetBalance(const int min_depth, const bool fAddLocked, const CCoinControl* coinControl) const { Balance ret; { @@ -2669,6 +2669,11 @@ CWallet::Balance CWallet::GetBalance(const int min_depth, const bool fAddLocked) } ret.m_mine_immature += pcoin->GetImmatureCredit(); ret.m_watchonly_immature += pcoin->GetImmatureWatchOnlyCredit(); + if (CCoinJoinClientOptions::IsEnabled()) { + ret.m_anonymized += pcoin->GetAnonymizedCredit(coinControl); + ret.m_denominated_trusted += pcoin->GetDenominatedCredit(false); + ret.m_denominated_untrusted_pending += pcoin->GetDenominatedCredit(true); + } } } return ret; @@ -2696,21 +2701,6 @@ CAmount CWallet::GetAnonymizableBalance(bool fSkipDenominated, bool fSkipUnconfi return nTotal; } -CAmount CWallet::GetAnonymizedBalance(const CCoinControl* coinControl) const -{ - if (!CCoinJoinClientOptions::IsEnabled()) return 0; - - CAmount nTotal = 0; - - LOCK2(cs_main, cs_wallet); - - for (auto pcoin : GetSpendableTXs()) { - nTotal += pcoin->GetAnonymizedCredit(coinControl); - } - - return nTotal; -} - // Note: calculated including unconfirmed, // that's ok as long as we use it for informational purposes only float CWallet::GetAverageAnonymizedRounds() const @@ -2757,21 +2747,6 @@ CAmount CWallet::GetNormalizedAnonymizedBalance() const return nTotal; } -CAmount CWallet::GetDenominatedBalance(bool unconfirmed) const -{ - if (!CCoinJoinClientOptions::IsEnabled()) return 0; - - CAmount nTotal = 0; - - LOCK2(cs_main, cs_wallet); - - for (auto pcoin : GetSpendableTXs()) { - nTotal += pcoin->GetDenominatedCredit(unconfirmed); - } - - return nTotal; -} - // Calculate total balance in a different way from GetBalance. The biggest // difference is that GetBalance sums up all unspent TxOuts paying to the // wallet, while this sums up both spent and unspent TxOuts paying to the diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 2c8eb15eef..a6ffb25d7b 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -994,15 +994,16 @@ public: CAmount m_watchonly_trusted{0}; CAmount m_watchonly_untrusted_pending{0}; CAmount m_watchonly_immature{0}; + CAmount m_anonymized{0}; + CAmount m_denominated_trusted{0}; + CAmount m_denominated_untrusted_pending{0}; }; CAmount GetLegacyBalance(const isminefilter& filter, int minDepth, const bool fAddLocked) const; - Balance GetBalance(int min_depth = 0, const bool fAddLocked = false) const; + Balance GetBalance(int min_depth = 0, const bool fAddLocked = false, const CCoinControl* coinControl = nullptr) const; CAmount GetAnonymizableBalance(bool fSkipDenominated = false, bool fSkipUnconfirmed = true) const; - CAmount GetAnonymizedBalance(const CCoinControl* coinControl = nullptr) const; float GetAverageAnonymizedRounds() const; CAmount GetNormalizedAnonymizedBalance() const; - CAmount GetDenominatedBalance(bool unconfirmed=false) const; bool GetBudgetSystemCollateralTX(CTransactionRef& tx, uint256 hash, CAmount amount, const COutPoint& outpoint=COutPoint()/*defaults null*/); CAmount GetAvailableBalance(const CCoinControl* coinControl = nullptr) const;