Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 2,075 Lines • ▼ Show 20 Lines | if (IsImmatureCoinBase() && IsInMainChain()) { | ||||
nImmatureCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE); | nImmatureCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE); | ||||
fImmatureCreditCached = true; | fImmatureCreditCached = true; | ||||
return nImmatureCreditCached; | return nImmatureCreditCached; | ||||
} | } | ||||
return Amount::zero(); | return Amount::zero(); | ||||
} | } | ||||
Amount CWalletTx::GetAvailableCredit(bool fUseCache) const { | Amount CWalletTx::GetAvailableCredit(bool fUseCache, | ||||
const isminefilter &filter) const { | |||||
if (pwallet == nullptr) { | if (pwallet == nullptr) { | ||||
return Amount::zero(); | return Amount::zero(); | ||||
} | } | ||||
// Must wait until coinbase is safely deep enough in the chain before | // Must wait until coinbase is safely deep enough in the chain before | ||||
// valuing it. | // valuing it. | ||||
if (IsImmatureCoinBase()) { | if (IsImmatureCoinBase()) { | ||||
return Amount::zero(); | return Amount::zero(); | ||||
} | } | ||||
if (fUseCache && fAvailableCreditCached) { | Amount *cache = nullptr; | ||||
return nAvailableCreditCached; | bool *cache_used = nullptr; | ||||
if (filter == ISMINE_SPENDABLE) { | |||||
cache = &nAvailableCreditCached; | |||||
cache_used = &fAvailableCreditCached; | |||||
} else if (filter == ISMINE_WATCH_ONLY) { | |||||
cache = &nAvailableWatchCreditCached; | |||||
cache_used = &fAvailableWatchCreditCached; | |||||
} | |||||
if (fUseCache && cache_used && *cache_used) { | |||||
return *cache; | |||||
} | } | ||||
Amount nCredit = Amount::zero(); | Amount nCredit = Amount::zero(); | ||||
const TxId &txid = GetId(); | const TxId &txid = GetId(); | ||||
for (uint32_t i = 0; i < tx->vout.size(); i++) { | for (uint32_t i = 0; i < tx->vout.size(); i++) { | ||||
if (!pwallet->IsSpent(COutPoint(txid, i))) { | if (!pwallet->IsSpent(COutPoint(txid, i))) { | ||||
const CTxOut &txout = tx->vout[i]; | const CTxOut &txout = tx->vout[i]; | ||||
nCredit += pwallet->GetCredit(txout, ISMINE_SPENDABLE); | nCredit += pwallet->GetCredit(txout, filter); | ||||
if (!MoneyRange(nCredit)) { | if (!MoneyRange(nCredit)) { | ||||
throw std::runtime_error(std::string(__func__) + | throw std::runtime_error(std::string(__func__) + | ||||
" : value out of range"); | " : value out of range"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
nAvailableCreditCached = nCredit; | if (cache) { | ||||
fAvailableCreditCached = true; | *cache = nCredit; | ||||
*cache_used = true; | |||||
} | |||||
return nCredit; | return nCredit; | ||||
} | } | ||||
Amount CWalletTx::GetImmatureWatchOnlyCredit(const bool fUseCache) const { | Amount CWalletTx::GetImmatureWatchOnlyCredit(const bool fUseCache) const { | ||||
if (IsImmatureCoinBase() && IsInMainChain()) { | if (IsImmatureCoinBase() && IsInMainChain()) { | ||||
if (fUseCache && fImmatureWatchCreditCached) { | if (fUseCache && fImmatureWatchCreditCached) { | ||||
return nImmatureWatchCreditCached; | return nImmatureWatchCreditCached; | ||||
} | } | ||||
nImmatureWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY); | nImmatureWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY); | ||||
fImmatureWatchCreditCached = true; | fImmatureWatchCreditCached = true; | ||||
return nImmatureWatchCreditCached; | return nImmatureWatchCreditCached; | ||||
} | } | ||||
return Amount::zero(); | return Amount::zero(); | ||||
} | } | ||||
Amount CWalletTx::GetAvailableWatchOnlyCredit(const bool fUseCache) const { | |||||
if (pwallet == nullptr) { | |||||
return Amount::zero(); | |||||
} | |||||
// Must wait until coinbase is safely deep enough in the chain before | |||||
// valuing it. | |||||
if (IsCoinBase() && GetBlocksToMaturity() > 0) { | |||||
return Amount::zero(); | |||||
} | |||||
if (fUseCache && fAvailableWatchCreditCached) { | |||||
return nAvailableWatchCreditCached; | |||||
} | |||||
Amount nCredit = Amount::zero(); | |||||
const TxId &txid = GetId(); | |||||
for (uint32_t i = 0; i < tx->vout.size(); i++) { | |||||
if (!pwallet->IsSpent(COutPoint(txid, i))) { | |||||
const CTxOut &txout = tx->vout[i]; | |||||
nCredit += pwallet->GetCredit(txout, ISMINE_WATCH_ONLY); | |||||
if (!MoneyRange(nCredit)) { | |||||
throw std::runtime_error(std::string(__func__) + | |||||
": value out of range"); | |||||
} | |||||
} | |||||
} | |||||
nAvailableWatchCreditCached = nCredit; | |||||
fAvailableWatchCreditCached = true; | |||||
return nCredit; | |||||
} | |||||
Amount CWalletTx::GetChange() const { | Amount CWalletTx::GetChange() const { | ||||
if (fChangeCached) { | if (fChangeCached) { | ||||
return nChangeCached; | return nChangeCached; | ||||
} | } | ||||
nChangeCached = pwallet->GetChange(*tx); | nChangeCached = pwallet->GetChange(*tx); | ||||
fChangeCached = true; | fChangeCached = true; | ||||
return nChangeCached; | return nChangeCached; | ||||
▲ Show 20 Lines • Show All 121 Lines • ▼ Show 20 Lines | |||||
/** @} */ // end of mapWallet | /** @} */ // end of mapWallet | ||||
/** | /** | ||||
* @defgroup Actions | * @defgroup Actions | ||||
* | * | ||||
* @{ | * @{ | ||||
*/ | */ | ||||
Amount CWallet::GetBalance() const { | Amount CWallet::GetBalance(const isminefilter &filter, | ||||
const int min_depth) const { | |||||
LOCK2(cs_main, cs_wallet); | LOCK2(cs_main, cs_wallet); | ||||
Amount nTotal = Amount::zero(); | Amount nTotal = Amount::zero(); | ||||
for (const auto &entry : mapWallet) { | for (const auto &entry : mapWallet) { | ||||
const CWalletTx *pcoin = &entry.second; | const CWalletTx *pcoin = &entry.second; | ||||
if (pcoin->IsTrusted()) { | if (pcoin->IsTrusted() && pcoin->GetDepthInMainChain() >= min_depth) { | ||||
nTotal += pcoin->GetAvailableCredit(); | nTotal += pcoin->GetAvailableCredit(true, filter); | ||||
} | } | ||||
} | } | ||||
return nTotal; | return nTotal; | ||||
} | } | ||||
Amount CWallet::GetUnconfirmedBalance() const { | Amount CWallet::GetUnconfirmedBalance() const { | ||||
LOCK2(cs_main, cs_wallet); | LOCK2(cs_main, cs_wallet); | ||||
Show All 17 Lines | Amount CWallet::GetImmatureBalance() const { | ||||
for (const auto &entry : mapWallet) { | for (const auto &entry : mapWallet) { | ||||
const CWalletTx *pcoin = &entry.second; | const CWalletTx *pcoin = &entry.second; | ||||
nTotal += pcoin->GetImmatureCredit(); | nTotal += pcoin->GetImmatureCredit(); | ||||
} | } | ||||
return nTotal; | return nTotal; | ||||
} | } | ||||
Amount CWallet::GetWatchOnlyBalance() const { | |||||
LOCK2(cs_main, cs_wallet); | |||||
Amount nTotal = Amount::zero(); | |||||
for (const auto &entry : mapWallet) { | |||||
const CWalletTx *pcoin = &entry.second; | |||||
if (pcoin->IsTrusted()) { | |||||
nTotal += pcoin->GetAvailableWatchOnlyCredit(); | |||||
} | |||||
} | |||||
return nTotal; | |||||
} | |||||
Amount CWallet::GetUnconfirmedWatchOnlyBalance() const { | Amount CWallet::GetUnconfirmedWatchOnlyBalance() const { | ||||
LOCK2(cs_main, cs_wallet); | LOCK2(cs_main, cs_wallet); | ||||
Amount nTotal = Amount::zero(); | Amount nTotal = Amount::zero(); | ||||
for (const auto &entry : mapWallet) { | for (const auto &entry : mapWallet) { | ||||
const CWalletTx *pcoin = &entry.second; | const CWalletTx *pcoin = &entry.second; | ||||
if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && | if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && | ||||
pcoin->InMempool()) { | pcoin->InMempool()) { | ||||
nTotal += pcoin->GetAvailableWatchOnlyCredit(); | nTotal += pcoin->GetAvailableCredit(true, ISMINE_WATCH_ONLY); | ||||
} | } | ||||
} | } | ||||
return nTotal; | return nTotal; | ||||
} | } | ||||
Amount CWallet::GetImmatureWatchOnlyBalance() const { | Amount CWallet::GetImmatureWatchOnlyBalance() const { | ||||
LOCK2(cs_main, cs_wallet); | LOCK2(cs_main, cs_wallet); | ||||
▲ Show 20 Lines • Show All 2,571 Lines • Show Last 20 Lines |