@@ -410,14 +410,14 @@ void CWallet::RecalculateMixedCredit(const uint256 hash)
410410 fAnonymizableTallyCachedNonDenom = false ;
411411}
412412
413- CAmount CWallet:: GetBalanceAnonymized (const CCoinControl& coinControl) const
413+ CAmount GetBalanceAnonymized (const CWallet& wallet, const CCoinControl& coinControl)
414414{
415415 if (!CCoinJoinClientOptions::IsEnabled ()) return 0 ;
416416
417417 CAmount anonymized_amount{0 };
418- LOCK (cs_wallet);
419- for (auto pcoin : GetSpendableTXs ()) {
420- anonymized_amount += pcoin-> GetAnonymizedCredit ( coinControl);
418+ LOCK (wallet. cs_wallet );
419+ for (auto pcoin : wallet. GetSpendableTXs ()) {
420+ anonymized_amount += CachedTxGetAnonymizedCredit (wallet, *pcoin, coinControl);
421421 }
422422 return anonymized_amount;
423423}
@@ -490,32 +490,29 @@ CAmount CWallet::GetNormalizedAnonymizedBalance() const
490490 return nTotal;
491491}
492492
493- CAmount CWalletTx::GetAnonymizedCredit (const CCoinControl& coinControl) const
493+ CAmount CachedTxGetAnonymizedCredit (const CWallet& wallet, const CWalletTx& wtx, const CCoinControl& coinControl)
494494{
495- if (!pwallet)
496- return 0 ;
497-
498- AssertLockHeld (pwallet->cs_wallet );
495+ AssertLockHeld (wallet.cs_wallet );
499496
500497 // Exclude coinbase and conflicted txes
501- if (IsCoinBase () || GetDepthInMainChain () < 0 )
498+ if (wtx. IsCoinBase () || wtx. GetDepthInMainChain () < 0 )
502499 return 0 ;
503500
504501 CAmount nCredit = 0 ;
505- uint256 hashTx = GetHash ();
506- for (unsigned int i = 0 ; i < tx->vout .size (); i++)
502+ uint256 hashTx = wtx. GetHash ();
503+ for (unsigned int i = 0 ; i < wtx. tx ->vout .size (); i++)
507504 {
508- const CTxOut &txout = tx->vout [i];
505+ const CTxOut &txout = wtx. tx ->vout [i];
509506 const COutPoint outpoint = COutPoint (hashTx, i);
510507
511508 if (coinControl.HasSelected () && !coinControl.IsSelected (outpoint)) {
512509 continue ;
513510 }
514511
515- if (pwallet-> IsSpent (hashTx, i) || !CoinJoin::IsDenominatedAmount (txout.nValue )) continue ;
512+ if (wallet. IsSpent (hashTx, i) || !CoinJoin::IsDenominatedAmount (txout.nValue )) continue ;
516513
517- if (pwallet-> IsFullyMixed (outpoint)) {
518- nCredit += pwallet-> GetCredit (txout, ISMINE_SPENDABLE);
514+ if (wallet. IsFullyMixed (outpoint)) {
515+ nCredit += wallet. GetCredit (txout, ISMINE_SPENDABLE);
519516 if (!MoneyRange (nCredit))
520517 throw std::runtime_error (std::string (__func__) + " : value out of range" );
521518 }
@@ -524,40 +521,38 @@ CAmount CWalletTx::GetAnonymizedCredit(const CCoinControl& coinControl) const
524521 return nCredit;
525522}
526523
527- CWalletTx:: CoinJoinCredits CWalletTx::GetAvailableCoinJoinCredits () const
524+ CoinJoinCredits CachedTxGetAvailableCoinJoinCredits ( const CWallet& wallet, const CWalletTx& wtx)
528525{
529- CWalletTx::CoinJoinCredits ret;
530- if (pwallet == nullptr )
531- return ret;
526+ CoinJoinCredits ret;
532527
533- AssertLockHeld (pwallet-> cs_wallet );
528+ AssertLockHeld (wallet. cs_wallet );
534529
535530 // Must wait until coinbase is safely deep enough in the chain before valuing it
536- if (IsCoinBase () && GetBlocksToMaturity () > 0 )
531+ if (wtx. IsCoinBase () && wtx. GetBlocksToMaturity () > 0 )
537532 return ret;
538533
539- int nDepth = GetDepthInMainChain ();
534+ int nDepth = wtx. GetDepthInMainChain ();
540535 if (nDepth < 0 ) return ret;
541536
542- ret.is_unconfirmed = IsTrusted () && nDepth == 0 ;
537+ ret.is_unconfirmed = wtx. IsTrusted () && nDepth == 0 ;
543538
544- if (m_amounts[ANON_CREDIT].m_cached [ISMINE_SPENDABLE]) {
545- if (ret.is_unconfirmed && m_amounts[DENOM_UCREDIT].m_cached [ISMINE_SPENDABLE]) {
546- return {m_amounts[ANON_CREDIT].m_value [ISMINE_SPENDABLE], m_amounts[DENOM_UCREDIT].m_value [ISMINE_SPENDABLE], ret.is_unconfirmed };
547- } else if (!ret.is_unconfirmed && m_amounts[DENOM_CREDIT].m_cached [ISMINE_SPENDABLE]) {
548- return {m_amounts[ANON_CREDIT].m_value [ISMINE_SPENDABLE], m_amounts[DENOM_CREDIT].m_value [ISMINE_SPENDABLE], ret.is_unconfirmed };
539+ if (wtx. m_amounts [CWalletTx:: ANON_CREDIT].m_cached [ISMINE_SPENDABLE]) {
540+ if (ret.is_unconfirmed && wtx. m_amounts [CWalletTx:: DENOM_UCREDIT].m_cached [ISMINE_SPENDABLE]) {
541+ return {wtx. m_amounts [CWalletTx:: ANON_CREDIT].m_value [ISMINE_SPENDABLE], wtx. m_amounts [CWalletTx:: DENOM_UCREDIT].m_value [ISMINE_SPENDABLE], ret.is_unconfirmed };
542+ } else if (!ret.is_unconfirmed && wtx. m_amounts [CWalletTx:: DENOM_CREDIT].m_cached [ISMINE_SPENDABLE]) {
543+ return {wtx. m_amounts [CWalletTx:: ANON_CREDIT].m_value [ISMINE_SPENDABLE], wtx. m_amounts [CWalletTx:: DENOM_CREDIT].m_value [ISMINE_SPENDABLE], ret.is_unconfirmed };
549544 }
550545 }
551546
552- uint256 hashTx = GetHash ();
553- for (unsigned int i = 0 ; i < tx->vout .size (); i++) {
554- const CTxOut &txout = tx->vout [i];
547+ uint256 hashTx = wtx. GetHash ();
548+ for (unsigned int i = 0 ; i < wtx. tx ->vout .size (); i++) {
549+ const CTxOut &txout = wtx. tx ->vout [i];
555550 const COutPoint outpoint = COutPoint (hashTx, i);
556551
557- if (pwallet-> IsSpent (hashTx, i) || !CoinJoin::IsDenominatedAmount (txout.nValue )) continue ;
558- const CAmount credit = pwallet-> GetCredit (txout, ISMINE_SPENDABLE);
552+ if (wallet. IsSpent (hashTx, i) || !CoinJoin::IsDenominatedAmount (txout.nValue )) continue ;
553+ const CAmount credit = wallet. GetCredit (txout, ISMINE_SPENDABLE);
559554
560- if (pwallet-> IsFullyMixed (outpoint)) {
555+ if (wallet. IsFullyMixed (outpoint)) {
561556 ret.m_anonymized += credit;
562557 if (!MoneyRange (ret.m_anonymized ))
563558 throw std::runtime_error (std::string (__func__) + " : value out of range" );
@@ -568,11 +563,11 @@ CWalletTx::CoinJoinCredits CWalletTx::GetAvailableCoinJoinCredits() const
568563 throw std::runtime_error (std::string (__func__) + " : value out of range" );
569564 }
570565
571- m_amounts[ANON_CREDIT].Set (ISMINE_SPENDABLE, ret.m_anonymized );
566+ wtx. m_amounts [CWalletTx:: ANON_CREDIT].Set (ISMINE_SPENDABLE, ret.m_anonymized );
572567 if (ret.is_unconfirmed ) {
573- m_amounts[DENOM_UCREDIT].Set (ISMINE_SPENDABLE, ret.m_denominated );
568+ wtx. m_amounts [CWalletTx:: DENOM_UCREDIT].Set (ISMINE_SPENDABLE, ret.m_denominated );
574569 } else {
575- m_amounts[DENOM_CREDIT].Set (ISMINE_SPENDABLE, ret.m_denominated );
570+ wtx. m_amounts [CWalletTx:: DENOM_CREDIT].Set (ISMINE_SPENDABLE, ret.m_denominated );
576571 }
577572 return ret;
578573}
0 commit comments