Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 1,374 Lines • ▼ Show 20 Lines | CAmount CWallet::GetDebit(const CTxIn &txin, const isminefilter &filter) const { | ||||
return 0; | return 0; | ||||
} | } | ||||
isminetype CWallet::IsMine(const CTxOut &txout) const { | isminetype CWallet::IsMine(const CTxOut &txout) const { | ||||
return ::IsMine(*this, txout.scriptPubKey); | return ::IsMine(*this, txout.scriptPubKey); | ||||
} | } | ||||
CAmount CWallet::GetCredit(const CTxOut &txout, | Amount CWallet::GetCredit(const CTxOut &txout, | ||||
const isminefilter &filter) const { | const isminefilter &filter) const { | ||||
if (!MoneyRange(txout.nValue)) { | if (!MoneyRange(txout.nValue)) { | ||||
throw std::runtime_error(std::string(__func__) + | throw std::runtime_error(std::string(__func__) + | ||||
": value out of range"); | ": value out of range"); | ||||
} | } | ||||
return (IsMine(txout) & filter) ? txout.nValue.GetSatoshis() : 0; | return (IsMine(txout) & filter) ? txout.nValue : Amount(0); | ||||
} | } | ||||
bool CWallet::IsChange(const CTxOut &txout) const { | bool CWallet::IsChange(const CTxOut &txout) const { | ||||
// TODO: fix handling of 'change' outputs. The assumption is that any | // TODO: fix handling of 'change' outputs. The assumption is that any | ||||
// payment to a script that is ours, but is not in the address book is | // payment to a script that is ours, but is not in the address book is | ||||
// change. That assumption is likely to break when we implement | // change. That assumption is likely to break when we implement | ||||
// multisignature wallets that return change back into a | // multisignature wallets that return change back into a | ||||
// multi-signature-protected address; a better way of identifying which | // multi-signature-protected address; a better way of identifying which | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | for (const CTxIn &txin : tx.vin) { | ||||
if (!(IsMine(prev.tx->vout[txin.prevout.n]) & filter)) { | if (!(IsMine(prev.tx->vout[txin.prevout.n]) & filter)) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
CAmount CWallet::GetCredit(const CTransaction &tx, | Amount CWallet::GetCredit(const CTransaction &tx, | ||||
const isminefilter &filter) const { | const isminefilter &filter) const { | ||||
CAmount nCredit = 0; | Amount nCredit = 0; | ||||
for (const CTxOut &txout : tx.vout) { | for (const CTxOut &txout : tx.vout) { | ||||
nCredit += GetCredit(txout, filter); | nCredit += 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"); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 371 Lines • ▼ Show 20 Lines | if (filter & ISMINE_WATCH_ONLY) { | ||||
fWatchDebitCached = true; | fWatchDebitCached = true; | ||||
debit += nWatchDebitCached; | debit += nWatchDebitCached; | ||||
} | } | ||||
} | } | ||||
return debit; | return debit; | ||||
} | } | ||||
CAmount CWalletTx::GetCredit(const isminefilter &filter) const { | Amount CWalletTx::GetCredit(const isminefilter &filter) const { | ||||
// 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 (IsCoinBase() && GetBlocksToMaturity() > 0) { | if (IsCoinBase() && GetBlocksToMaturity() > 0) { | ||||
return 0; | return 0; | ||||
} | } | ||||
CAmount credit = 0; | Amount credit = 0; | ||||
if (filter & ISMINE_SPENDABLE) { | if (filter & ISMINE_SPENDABLE) { | ||||
// GetBalance can assume transactions in mapWallet won't change. | // GetBalance can assume transactions in mapWallet won't change. | ||||
if (fCreditCached) { | if (fCreditCached) { | ||||
credit += nCreditCached; | credit += nCreditCached; | ||||
} else { | } else { | ||||
nCreditCached = pwallet->GetCredit(*this, ISMINE_SPENDABLE); | nCreditCached = pwallet->GetCredit(*this, ISMINE_SPENDABLE); | ||||
fCreditCached = true; | fCreditCached = true; | ||||
credit += nCreditCached; | credit += nCreditCached; | ||||
} | } | ||||
} | } | ||||
if (filter & ISMINE_WATCH_ONLY) { | if (filter & ISMINE_WATCH_ONLY) { | ||||
if (fWatchCreditCached) { | if (fWatchCreditCached) { | ||||
credit += nWatchCreditCached; | credit += nWatchCreditCached; | ||||
} else { | } else { | ||||
nWatchCreditCached = pwallet->GetCredit(*this, ISMINE_WATCH_ONLY); | nWatchCreditCached = | ||||
pwallet->GetCredit(*this, ISMINE_WATCH_ONLY).GetSatoshis(); | |||||
fWatchCreditCached = true; | fWatchCreditCached = true; | ||||
credit += nWatchCreditCached; | credit += nWatchCreditCached; | ||||
} | } | ||||
} | } | ||||
return credit; | return credit; | ||||
} | } | ||||
CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const { | CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const { | ||||
if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain()) { | if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain()) { | ||||
if (fUseCache && fImmatureCreditCached) return nImmatureCreditCached; | if (fUseCache && fImmatureCreditCached) return nImmatureCreditCached; | ||||
nImmatureCreditCached = pwallet->GetCredit(*this, ISMINE_SPENDABLE); | nImmatureCreditCached = | ||||
pwallet->GetCredit(*this, ISMINE_SPENDABLE).GetSatoshis(); | |||||
fImmatureCreditCached = true; | fImmatureCreditCached = true; | ||||
return nImmatureCreditCached; | return nImmatureCreditCached; | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const { | CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const { | ||||
Show All 11 Lines | if (fUseCache && fAvailableCreditCached) { | ||||
return nAvailableCreditCached; | return nAvailableCreditCached; | ||||
} | } | ||||
CAmount nCredit = 0; | CAmount nCredit = 0; | ||||
uint256 hashTx = GetId(); | uint256 hashTx = GetId(); | ||||
for (unsigned int i = 0; i < tx->vout.size(); i++) { | for (unsigned int i = 0; i < tx->vout.size(); i++) { | ||||
if (!pwallet->IsSpent(hashTx, i)) { | if (!pwallet->IsSpent(hashTx, i)) { | ||||
const CTxOut &txout = tx->vout[i]; | const CTxOut &txout = tx->vout[i]; | ||||
nCredit += pwallet->GetCredit(txout, ISMINE_SPENDABLE); | nCredit += | ||||
pwallet->GetCredit(txout, ISMINE_SPENDABLE).GetSatoshis(); | |||||
if (!MoneyRange(nCredit)) { | if (!MoneyRange(nCredit)) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"CWalletTx::GetAvailableCredit() : value out of range"); | "CWalletTx::GetAvailableCredit() : value out of range"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
nAvailableCreditCached = nCredit; | nAvailableCreditCached = nCredit; | ||||
fAvailableCreditCached = true; | fAvailableCreditCached = true; | ||||
return nCredit; | return nCredit; | ||||
} | } | ||||
CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool &fUseCache) const { | CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool &fUseCache) const { | ||||
if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain()) { | if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain()) { | ||||
if (fUseCache && fImmatureWatchCreditCached) { | if (fUseCache && fImmatureWatchCreditCached) { | ||||
return nImmatureWatchCreditCached; | return nImmatureWatchCreditCached; | ||||
} | } | ||||
nImmatureWatchCreditCached = | nImmatureWatchCreditCached = | ||||
pwallet->GetCredit(*this, ISMINE_WATCH_ONLY); | pwallet->GetCredit(*this, ISMINE_WATCH_ONLY).GetSatoshis(); | ||||
fImmatureWatchCreditCached = true; | fImmatureWatchCreditCached = true; | ||||
return nImmatureWatchCreditCached; | return nImmatureWatchCreditCached; | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
CAmount CWalletTx::GetAvailableWatchOnlyCredit(const bool &fUseCache) const { | CAmount CWalletTx::GetAvailableWatchOnlyCredit(const bool &fUseCache) const { | ||||
if (pwallet == 0) { | if (pwallet == 0) { | ||||
return 0; | return 0; | ||||
} | } | ||||
// 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 (IsCoinBase() && GetBlocksToMaturity() > 0) { | if (IsCoinBase() && GetBlocksToMaturity() > 0) { | ||||
return 0; | return 0; | ||||
} | } | ||||
if (fUseCache && fAvailableWatchCreditCached) { | if (fUseCache && fAvailableWatchCreditCached) { | ||||
return nAvailableWatchCreditCached; | return nAvailableWatchCreditCached; | ||||
} | } | ||||
CAmount nCredit = 0; | Amount nCredit = 0; | ||||
for (unsigned int i = 0; i < tx->vout.size(); i++) { | for (unsigned int i = 0; i < tx->vout.size(); i++) { | ||||
if (!pwallet->IsSpent(GetId(), i)) { | if (!pwallet->IsSpent(GetId(), i)) { | ||||
const CTxOut &txout = tx->vout[i]; | const CTxOut &txout = tx->vout[i]; | ||||
nCredit += pwallet->GetCredit(txout, ISMINE_WATCH_ONLY); | nCredit += pwallet->GetCredit(txout, ISMINE_WATCH_ONLY); | ||||
if (!MoneyRange(nCredit)) { | if (!MoneyRange(nCredit)) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"CWalletTx::GetAvailableCredit() : value out of range"); | "CWalletTx::GetAvailableCredit() : value out of range"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
nAvailableWatchCreditCached = nCredit; | nAvailableWatchCreditCached = nCredit.GetSatoshis(); | ||||
fAvailableWatchCreditCached = true; | fAvailableWatchCreditCached = true; | ||||
return nCredit; | return nCredit.GetSatoshis(); | ||||
} | } | ||||
Amount CWalletTx::GetChange() const { | Amount CWalletTx::GetChange() const { | ||||
if (fChangeCached) { | if (fChangeCached) { | ||||
return nChangeCached; | return nChangeCached; | ||||
} | } | ||||
nChangeCached = pwallet->GetChange(*this); | nChangeCached = pwallet->GetChange(*this); | ||||
▲ Show 20 Lines • Show All 2,491 Lines • Show Last 20 Lines |