Browse Source

Wallet getDebit with excluded keva.

cn
Jianping Wu 6 years ago
parent
commit
5977428951
  1. 31
      src/wallet/wallet.cpp
  2. 30
      src/wallet/wallet.h

31
src/wallet/wallet.cpp

@ -1316,7 +1316,7 @@ isminetype CWallet::IsMine(const CTxIn &txin) const
// Note that this function doesn't distinguish between a 0-valued input, // Note that this function doesn't distinguish between a 0-valued input,
// and a not-"is mine" (according to the filter) input. // and a not-"is mine" (according to the filter) input.
CAmount CWallet::GetDebit(const CTxIn &txin, const isminefilter& filter) const CAmount CWallet::GetDebit(const CTxIn &txin, const isminefilter& filter, bool fExcludeKeva) const
{ {
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
@ -1325,8 +1325,15 @@ CAmount CWallet::GetDebit(const CTxIn &txin, const isminefilter& filter) const
{ {
const CWalletTx& prev = (*mi).second; const CWalletTx& prev = (*mi).second;
if (txin.prevout.n < prev.tx->vout.size()) if (txin.prevout.n < prev.tx->vout.size())
{
const CTxOut& prevout = prev.tx->vout[txin.prevout.n];
if (fExcludeKeva
&& CKevaScript::isKevaScript(prevout.scriptPubKey))
return 0;
if (IsMine(prev.tx->vout[txin.prevout.n]) & filter) if (IsMine(prev.tx->vout[txin.prevout.n]) & filter)
return prev.tx->vout[txin.prevout.n].nValue; return prevout.nValue;
}
} }
} }
return 0; return 0;
@ -1386,12 +1393,12 @@ bool CWallet::IsFromMe(const CTransaction& tx) const
return (GetDebit(tx, ISMINE_ALL) > 0); return (GetDebit(tx, ISMINE_ALL) > 0);
} }
CAmount CWallet::GetDebit(const CTransaction& tx, const isminefilter& filter) const CAmount CWallet::GetDebit(const CTransaction& tx, const isminefilter& filter, bool fExcludeKeva) const
{ {
CAmount nDebit = 0; CAmount nDebit = 0;
for (const CTxIn& txin : tx.vin) for (const CTxIn& txin : tx.vin)
{ {
nDebit += GetDebit(txin, filter); nDebit += GetDebit(txin, filter, fExcludeKeva);
if (!MoneyRange(nDebit)) if (!MoneyRange(nDebit))
throw std::runtime_error(std::string(__func__) + ": value out of range"); throw std::runtime_error(std::string(__func__) + ": value out of range");
} }
@ -1520,7 +1527,7 @@ void CWalletTx::GetAmounts(std::list<COutputEntry>& listReceived,
CAmount nDebit = GetDebit(filter); CAmount nDebit = GetDebit(filter);
if (nDebit > 0) // debit>0 means we signed/sent this transaction if (nDebit > 0) // debit>0 means we signed/sent this transaction
{ {
CAmount nValueOut = tx->GetValueOut(); CAmount nValueOut = tx->GetValueOut(true);
nFee = nDebit - nValueOut; nFee = nDebit - nValueOut;
} }
@ -1749,7 +1756,7 @@ std::set<uint256> CWalletTx::GetConflicts() const
return result; return result;
} }
CAmount CWalletTx::GetDebit(const isminefilter& filter) const CAmount CWalletTx::GetDebit(const isminefilter& filter, bool fExcludeKeva) const
{ {
if (tx->vin.empty()) if (tx->vin.empty())
return 0; return 0;
@ -1757,19 +1764,21 @@ CAmount CWalletTx::GetDebit(const isminefilter& filter) const
CAmount debit = 0; CAmount debit = 0;
if(filter & ISMINE_SPENDABLE) if(filter & ISMINE_SPENDABLE)
{ {
if (fDebitCached) if (fDebitCached) {
debit += nDebitCached; debit += (fExcludeKeva ? nDebitCached : nDebitWithKevaCached);
}
else else
{ {
nDebitCached = pwallet->GetDebit(*tx, ISMINE_SPENDABLE); nDebitCached = pwallet->GetDebit(*tx, ISMINE_SPENDABLE, false);
fDebitCached = true; fDebitCached = true;
debit += nDebitCached; debit += nDebitCached;
} }
} }
if(filter & ISMINE_WATCH_ONLY) if(filter & ISMINE_WATCH_ONLY)
{ {
if(fWatchDebitCached) if(fWatchDebitCached) {
debit += nWatchDebitCached; debit += (fExcludeKeva ? nWatchDebitCached : nWatchDebitWithKevaCached, false);
}
else else
{ {
nWatchDebitCached = pwallet->GetDebit(*tx, ISMINE_WATCH_ONLY); nWatchDebitCached = pwallet->GetDebit(*tx, ISMINE_WATCH_ONLY);

30
src/wallet/wallet.h

@ -107,7 +107,9 @@ enum OutputType : int
OUTPUT_TYPE_P2SH_SEGWIT, OUTPUT_TYPE_P2SH_SEGWIT,
OUTPUT_TYPE_BECH32, OUTPUT_TYPE_BECH32,
OUTPUT_TYPE_DEFAULT = OUTPUT_TYPE_P2SH_SEGWIT //OUTPUT_TYPE_DEFAULT = OUTPUT_TYPE_P2SH_SEGWIT
// JWU TODO FIXME: Update once we have segwit on Kevacoin.
OUTPUT_TYPE_DEFAULT = OUTPUT_TYPE_LEGACY
}; };
extern OutputType g_address_type; extern OutputType g_address_type;
@ -270,7 +272,7 @@ public:
bool IsCoinBase() const { return tx->IsCoinBase(); } bool IsCoinBase() const { return tx->IsCoinBase(); }
}; };
/** /**
* A transaction with a bunch of additional info that only the owner cares about. * A transaction with a bunch of additional info that only the owner cares about.
* It includes any unrecorded transactions needed to link it back to the block chain. * It includes any unrecorded transactions needed to link it back to the block chain.
*/ */
@ -341,10 +343,12 @@ public:
mutable bool fChangeCached; mutable bool fChangeCached;
mutable bool fInMempool; mutable bool fInMempool;
mutable CAmount nDebitCached; mutable CAmount nDebitCached;
mutable CAmount nDebitWithKevaCached;
mutable CAmount nCreditCached; mutable CAmount nCreditCached;
mutable CAmount nImmatureCreditCached; mutable CAmount nImmatureCreditCached;
mutable CAmount nAvailableCreditCached; mutable CAmount nAvailableCreditCached;
mutable CAmount nWatchDebitCached; mutable CAmount nWatchDebitCached;
mutable CAmount nWatchDebitWithKevaCached;
mutable CAmount nWatchCreditCached; mutable CAmount nWatchCreditCached;
mutable CAmount nImmatureWatchCreditCached; mutable CAmount nImmatureWatchCreditCached;
mutable CAmount nAvailableWatchCreditCached; mutable CAmount nAvailableWatchCreditCached;
@ -381,10 +385,12 @@ public:
fChangeCached = false; fChangeCached = false;
fInMempool = false; fInMempool = false;
nDebitCached = 0; nDebitCached = 0;
nDebitWithKevaCached = 0;
nCreditCached = 0; nCreditCached = 0;
nImmatureCreditCached = 0; nImmatureCreditCached = 0;
nAvailableCreditCached = 0; nAvailableCreditCached = 0;
nWatchDebitCached = 0; nWatchDebitCached = 0;
nWatchDebitWithKevaCached = 0;
nWatchCreditCached = 0; nWatchCreditCached = 0;
nAvailableWatchCreditCached = 0; nAvailableWatchCreditCached = 0;
nImmatureWatchCreditCached = 0; nImmatureWatchCreditCached = 0;
@ -456,7 +462,7 @@ public:
} }
//! filter decides which addresses will count towards the debit //! filter decides which addresses will count towards the debit
CAmount GetDebit(const isminefilter& filter) const; CAmount GetDebit(const isminefilter& filter, bool fExcludeKeva = true) const;
CAmount GetCredit(const isminefilter& filter) const; CAmount GetCredit(const isminefilter& filter) const;
CAmount GetImmatureCredit(bool fUseCache=true) const; CAmount GetImmatureCredit(bool fUseCache=true) const;
CAmount GetAvailableCredit(bool fUseCache=true) const; CAmount GetAvailableCredit(bool fUseCache=true) const;
@ -661,7 +667,7 @@ private:
class WalletRescanReserver; //forward declarations for ScanForWalletTransactions/RescanFromTime class WalletRescanReserver; //forward declarations for ScanForWalletTransactions/RescanFromTime
/** /**
* A CWallet is an extension of a keystore, which also maintains a set of transactions and balances, * A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
* and provides the ability to create new transactions. * and provides the ability to create new transactions.
*/ */
@ -933,7 +939,7 @@ public:
void GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) const; void GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) const;
unsigned int ComputeTimeSmart(const CWalletTx& wtx) const; unsigned int ComputeTimeSmart(const CWalletTx& wtx) const;
/** /**
* Increment the next transaction order id * Increment the next transaction order id
* @return next transaction order id * @return next transaction order id
*/ */
@ -1027,7 +1033,7 @@ public:
* Returns amount of debit if the input matches the * Returns amount of debit if the input matches the
* filter, otherwise returns 0 * filter, otherwise returns 0
*/ */
CAmount GetDebit(const CTxIn& txin, const isminefilter& filter) const; CAmount GetDebit(const CTxIn& txin, const isminefilter& filter, bool fExcludeKeva = true) const;
isminetype IsMine(const CTxOut& txout) const; isminetype IsMine(const CTxOut& txout) const;
CAmount GetCredit(const CTxOut& txout, const isminefilter& filter) const; CAmount GetCredit(const CTxOut& txout, const isminefilter& filter) const;
bool IsChange(const CTxOut& txout) const; bool IsChange(const CTxOut& txout) const;
@ -1035,7 +1041,7 @@ public:
bool IsMine(const CTransaction& tx) const; bool IsMine(const CTransaction& tx) const;
/** should probably be renamed to IsRelevantToMe */ /** should probably be renamed to IsRelevantToMe */
bool IsFromMe(const CTransaction& tx) const; bool IsFromMe(const CTransaction& tx) const;
CAmount GetDebit(const CTransaction& tx, const isminefilter& filter) const; CAmount GetDebit(const CTransaction& tx, const isminefilter& filter, bool fExcludeKeva = true) const;
/** Returns whether all of the inputs match the filter */ /** Returns whether all of the inputs match the filter */
bool IsAllFromMe(const CTransaction& tx, const isminefilter& filter) const; bool IsAllFromMe(const CTransaction& tx, const isminefilter& filter) const;
CAmount GetCredit(const CTransaction& tx, const isminefilter& filter) const; CAmount GetCredit(const CTransaction& tx, const isminefilter& filter) const;
@ -1053,7 +1059,7 @@ public:
const std::string& GetAccountName(const CScript& scriptPubKey) const; const std::string& GetAccountName(const CScript& scriptPubKey) const;
void GetScriptForMining(std::shared_ptr<CReserveScript> &script); void GetScriptForMining(std::shared_ptr<CReserveScript> &script);
unsigned int GetKeyPoolSize() unsigned int GetKeyPoolSize()
{ {
AssertLockHeld(cs_wallet); // set{Ex,In}ternalKeyPool AssertLockHeld(cs_wallet); // set{Ex,In}ternalKeyPool
@ -1078,7 +1084,7 @@ public:
//! Flush wallet (bitdb flush) //! Flush wallet (bitdb flush)
void Flush(bool shutdown=false); void Flush(bool shutdown=false);
/** /**
* Address book entry changed. * Address book entry changed.
* @note called with lock cs_wallet held. * @note called with lock cs_wallet held.
*/ */
@ -1087,7 +1093,7 @@ public:
const std::string &purpose, const std::string &purpose,
ChangeType status)> NotifyAddressBookChanged; ChangeType status)> NotifyAddressBookChanged;
/** /**
* Wallet transaction added, removed or updated. * Wallet transaction added, removed or updated.
* @note called with lock cs_wallet held. * @note called with lock cs_wallet held.
*/ */
@ -1134,7 +1140,7 @@ public:
/* Generates a new HD master key (will not be activated) */ /* Generates a new HD master key (will not be activated) */
CPubKey GenerateNewHDMasterKey(); CPubKey GenerateNewHDMasterKey();
/* Set the current HD master key (will reset the chain child index counters) /* Set the current HD master key (will reset the chain child index counters)
Sets the master key's version based on the current wallet version (so the Sets the master key's version based on the current wallet version (so the
caller must ensure the current wallet version is correct before calling caller must ensure the current wallet version is correct before calling
@ -1202,7 +1208,7 @@ public:
}; };
/** /**
* Account information. * Account information.
* Stored in wallet with key "acc"+string account name. * Stored in wallet with key "acc"+string account name.
*/ */

Loading…
Cancel
Save