diff --git a/src/amount.h b/src/amount.h --- a/src/amount.h +++ b/src/amount.h @@ -83,6 +83,12 @@ constexpr Amount operator/(const int b) const { return Amount(amount / b); } // DO NOT IMPLEMENT constexpr Amount operator/(const double b) const = delete; + constexpr Amount operator%(const int64_t b) const { + return Amount(amount % b); + } + constexpr Amount operator%(const int b) const { return Amount(amount % b); } + // DO NOT IMPLEMENT + constexpr Amount operator%(const double b) const = delete; // ostream support friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -319,15 +319,15 @@ fImmatureWatchCreditCached = false; fAvailableWatchCreditCached = false; fChangeCached = false; - nDebitCached = 0; - nCreditCached = 0; - nImmatureCreditCached = 0; - nAvailableCreditCached = 0; - nWatchDebitCached = 0; - nWatchCreditCached = 0; - nAvailableWatchCreditCached = 0; - nImmatureWatchCreditCached = 0; - nChangeCached = 0; + nDebitCached = Amount(0); + nCreditCached = Amount(0); + nImmatureCreditCached = Amount(0); + nAvailableCreditCached = Amount(0); + nWatchDebitCached = Amount(0); + nWatchCreditCached = Amount(0); + nAvailableWatchCreditCached = Amount(0); + nImmatureWatchCreditCached = Amount(0); + nChangeCached = Amount(0); nOrderPos = -1; } @@ -411,7 +411,7 @@ const isminefilter &filter) const; bool IsFromMe(const isminefilter &filter) const { - return (GetDebit(filter) > 0); + return (GetDebit(filter) > Amount(0)); } // True if only scriptSigs are different @@ -492,7 +492,7 @@ CAccountingEntry() { SetNull(); } void SetNull() { - nCreditDebit = 0; + nCreditDebit = Amount(0); nTime = 0; strAccount.clear(); strOtherAccount.clear(); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1373,7 +1373,7 @@ } } - return 0; + return Amount(0); } isminetype CWallet::IsMine(const CTxOut &txout) const { @@ -1420,7 +1420,7 @@ ": value out of range"); } - return (IsChange(txout) ? txout.nValue : 0); + return (IsChange(txout) ? txout.nValue : Amount(0)); } bool CWallet::IsMine(const CTransaction &tx) const { @@ -1434,12 +1434,12 @@ } bool CWallet::IsFromMe(const CTransaction &tx) const { - return GetDebit(tx, ISMINE_ALL) > 0; + return GetDebit(tx, ISMINE_ALL) > Amount(0); } Amount CWallet::GetDebit(const CTransaction &tx, const isminefilter &filter) const { - Amount nDebit = 0; + Amount nDebit(0); for (const CTxIn &txin : tx.vin) { nDebit += GetDebit(txin, filter); if (!MoneyRange(nDebit)) { @@ -1479,7 +1479,7 @@ Amount CWallet::GetCredit(const CTransaction &tx, const isminefilter &filter) const { - Amount nCredit = 0; + Amount nCredit(0); for (const CTxOut &txout : tx.vout) { nCredit += GetCredit(txout, filter); if (!MoneyRange(nCredit)) { @@ -1492,7 +1492,7 @@ } Amount CWallet::GetChange(const CTransaction &tx) const { - Amount nChange = 0; + Amount nChange(0); for (const CTxOut &txout : tx.vout) { nChange += GetChange(txout); if (!MoneyRange(nChange)) { @@ -1611,7 +1611,7 @@ std::list &listSent, Amount &nFee, std::string &strSentAccount, const isminefilter &filter) const { - nFee = 0; + nFee = Amount(0); listReceived.clear(); listSent.clear(); strSentAccount = strFromAccount; @@ -1619,7 +1619,7 @@ // Compute fee: Amount nDebit = GetDebit(filter); // debit>0 means we signed/sent this transaction. - if (nDebit > 0) { + if (nDebit > Amount(0)) { Amount nValueOut = tx->GetValueOut(); nFee = (nDebit - nValueOut); } @@ -1670,7 +1670,7 @@ Amount &nReceived, Amount &nSent, Amount &nFee, const isminefilter &filter) const { - nReceived = nSent = nFee = 0; + nReceived = nSent = nFee = Amount(0); Amount allFee; std::string strSentAccount; @@ -1841,9 +1841,9 @@ } Amount CWalletTx::GetDebit(const isminefilter &filter) const { - if (tx->vin.empty()) return 0; + if (tx->vin.empty()) return Amount(0); - Amount debit = 0; + Amount debit(0); if (filter & ISMINE_SPENDABLE) { if (fDebitCached) { debit += nDebitCached; @@ -1871,10 +1871,10 @@ // Must wait until coinbase is safely deep enough in the chain before // valuing it. if (IsCoinBase() && GetBlocksToMaturity() > 0) { - return 0; + return Amount(0); } - Amount credit = 0; + Amount credit(0); if (filter & ISMINE_SPENDABLE) { // GetBalance can assume transactions in mapWallet won't change. if (fCreditCached) { @@ -1907,25 +1907,25 @@ return nImmatureCreditCached; } - return 0; + return Amount(0); } Amount CWalletTx::GetAvailableCredit(bool fUseCache) const { if (pwallet == 0) { - return 0; + return Amount(0); } // Must wait until coinbase is safely deep enough in the chain before // valuing it. if (IsCoinBase() && GetBlocksToMaturity() > 0) { - return 0; + return Amount(0); } if (fUseCache && fAvailableCreditCached) { return nAvailableCreditCached; } - Amount nCredit = 0; + Amount nCredit(0); uint256 hashTx = GetId(); for (unsigned int i = 0; i < tx->vout.size(); i++) { if (!pwallet->IsSpent(hashTx, i)) { @@ -1955,25 +1955,25 @@ return nImmatureWatchCreditCached; } - return 0; + return Amount(0); } Amount CWalletTx::GetAvailableWatchOnlyCredit(const bool &fUseCache) const { if (pwallet == 0) { - return 0; + return Amount(0); } // Must wait until coinbase is safely deep enough in the chain before // valuing it. if (IsCoinBase() && GetBlocksToMaturity() > 0) { - return 0; + return Amount(0); } if (fUseCache && fAvailableWatchCreditCached) { return nAvailableWatchCreditCached; } - Amount nCredit = 0; + Amount nCredit(0); for (unsigned int i = 0; i < tx->vout.size(); i++) { if (!pwallet->IsSpent(GetId(), i)) { const CTxOut &txout = tx->vout[i]; @@ -2134,7 +2134,7 @@ Amount CWallet::GetBalance() const { LOCK2(cs_main, cs_wallet); - Amount nTotal = 0; + Amount nTotal(0); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; @@ -2149,7 +2149,7 @@ Amount CWallet::GetUnconfirmedBalance() const { LOCK2(cs_main, cs_wallet); - Amount nTotal = 0; + Amount nTotal(0); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; @@ -2165,7 +2165,7 @@ Amount CWallet::GetImmatureBalance() const { LOCK2(cs_main, cs_wallet); - Amount nTotal = 0; + Amount nTotal(0); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; @@ -2178,7 +2178,7 @@ Amount CWallet::GetWatchOnlyBalance() const { LOCK2(cs_main, cs_wallet); - Amount nTotal = 0; + Amount nTotal(0); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; @@ -2193,7 +2193,7 @@ Amount CWallet::GetUnconfirmedWatchOnlyBalance() const { LOCK2(cs_main, cs_wallet); - Amount nTotal = 0; + Amount nTotal(0); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; @@ -2209,7 +2209,7 @@ Amount CWallet::GetImmatureWatchOnlyBalance() const { LOCK2(cs_main, cs_wallet); - Amount nTotal = 0; + Amount nTotal(0); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; @@ -2279,7 +2279,7 @@ isminetype mine = IsMine(pcoin->tx->vout[i]); if (!(IsSpent(wtxid, i)) && mine != ISMINE_NO && !IsLockedCoin((*it).first, i) && - (pcoin->tx->vout[i].nValue > 0 || fIncludeZeroValue) && + (pcoin->tx->vout[i].nValue > Amount(0) || fIncludeZeroValue) && (!coinControl || !coinControl->HasSelected() || coinControl->fAllowOtherInputs || coinControl->IsSelected(COutPoint((*it).first, i)))) { @@ -2309,7 +2309,7 @@ for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++) { vfIncluded.assign(vValue.size(), false); - Amount nTotal = 0; + Amount nTotal(0); bool fReachedTarget = false; for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++) { for (size_t i = 0; i < vValue.size(); i++) { @@ -2343,7 +2343,7 @@ std::set> &setCoinsRet, Amount &nValueRet) const { setCoinsRet.clear(); - nValueRet = 0; + nValueRet = Amount(0); // List of values less than target std::pair> @@ -2352,7 +2352,7 @@ coinLowestLarger.second.first = nullptr; std::vector>> vValue; - Amount nTotalLower = 0; + Amount nTotalLower(0); random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt); @@ -2475,7 +2475,7 @@ // Calculate value from preset inputs and store them. std::set> setPresetCoins; - Amount nValueFromPresetInputs = 0; + Amount nValueFromPresetInputs(0); std::vector vPresetInputs; if (coinControl) { @@ -2623,7 +2623,7 @@ Amount &nFeeRet, int &nChangePosInOut, std::string &strFailReason, const CCoinControl *coinControl, bool sign) { - Amount nValue = 0; + Amount nValue(0); int nChangePosRequest = nChangePosInOut; unsigned int nSubtractFeeFromAmount = 0; for (const auto &recipient : vecSend) { @@ -2688,7 +2688,7 @@ std::vector vAvailableCoins; AvailableCoins(vAvailableCoins, true, coinControl); - nFeeRet = 0; + nFeeRet = Amount(0); // Start with no fee and loop until there is enough fee. while (true) { nChangePosInOut = nChangePosRequest; @@ -2709,21 +2709,20 @@ if (recipient.fSubtractFeeFromAmount) { // Subtract fee equally from each selected recipient. - txout.nValue -= - Amount(nFeeRet.GetSatoshis() / nSubtractFeeFromAmount); + txout.nValue -= nFeeRet / int(nSubtractFeeFromAmount); // First receiver pays the remainder not divisible by output // count. if (fFirst) { fFirst = false; - txout.nValue -= - nFeeRet.GetSatoshis() % nSubtractFeeFromAmount; + txout.nValue -= nFeeRet % int(nSubtractFeeFromAmount); } } if (txout.IsDust(dustRelayFee)) { - if (recipient.fSubtractFeeFromAmount && nFeeRet > 0) { - if (txout.nValue < 0) { + if (recipient.fSubtractFeeFromAmount && + nFeeRet > Amount(0)) { + if (txout.nValue < Amount(0)) { strFailReason = _("The transaction amount is " "too small to pay the fee"); } else { @@ -2742,7 +2741,7 @@ } // Choose coins to use. - Amount nValueIn = 0; + Amount nValueIn(0); setCoins.clear(); if (!SelectCoins(vAvailableCoins, nValueToSelect, setCoins, nValueIn, coinControl)) { @@ -2764,7 +2763,7 @@ } const Amount nChange = nValueIn - nValueToSelect; - if (nChange > 0) { + if (nChange > Amount(0)) { // Fill a vout to ourself. // TODO: pass in scriptChange instead of reservekey so change // transaction isn't always pay-to-bitcoin-address. @@ -2901,7 +2900,7 @@ Amount nFeeNeeded = GetMinimumFee(nBytes, currentConfirmationTarget, mempool); - if (coinControl && nFeeNeeded > 0 && + if (coinControl && nFeeNeeded > Amount(0) && coinControl->nMinimumTotalFee > nFeeNeeded) { nFeeNeeded = coinControl->nMinimumTotalFee; } @@ -3002,7 +3001,8 @@ DEFAULT_WALLET_REJECT_LONG_CHAINS)) { // Lastly, ensure this tx will pass the mempool's chain limits. LockPoints lp; - CTxMemPoolEntry entry(wtxNew.tx, 0, 0, 0, 0, 0, false, 0, lp); + CTxMemPoolEntry entry(wtxNew.tx, Amount(0), 0, 0, 0, Amount(0), false, + 0, lp); CTxMemPool::setEntries setAncestors; size_t nLimitAncestors = GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT); @@ -3109,13 +3109,13 @@ const CTxMemPool &pool, Amount targetFee) { Amount nFeeNeeded = targetFee; // User didn't set: use -txconfirmtarget to estimate... - if (nFeeNeeded == 0) { + if (nFeeNeeded == Amount(0)) { int estimateFoundTarget = nConfirmTarget; nFeeNeeded = pool.estimateSmartFee(nConfirmTarget, &estimateFoundTarget) .GetFee(nTxBytes); // ... unless we don't have enough mempool data for estimatefee, then // use fallbackFee. - if (nFeeNeeded == 0) { + if (nFeeNeeded == Amount(0)) { nFeeNeeded = fallbackFee.GetFee(nTxBytes); } } @@ -3469,10 +3469,11 @@ continue; } - Amount n = - IsSpent(walletEntry.first, i) ? 0 : pcoin->tx->vout[i].nValue; + Amount n = IsSpent(walletEntry.first, i) + ? Amount(0) + : pcoin->tx->vout[i].nValue; - if (!balances.count(addr)) balances[addr] = 0; + if (!balances.count(addr)) balances[addr] = Amount(0); balances[addr] += n; } } @@ -3593,7 +3594,7 @@ Amount CWallet::GetAccountBalance(CWalletDB &walletdb, const std::string &strAccount, int nMinDepth, const isminefilter &filter) { - Amount nBalance = 0; + Amount nBalance(0); // Tally wallet transactions. for (std::map::iterator it = mapWallet.begin(); @@ -4286,8 +4287,9 @@ } if (IsArgSet("-mintxfee")) { - Amount n = 0; - if (!ParseMoney(GetArg("-mintxfee", ""), n) || 0 == n) { + Amount n(0); + auto parsed = ParseMoney(GetArg("-mintxfee", ""), n); + if (!parsed || Amount(0) == n) { return InitError(AmountErrMsg("mintxfee", GetArg("-mintxfee", ""))); } @@ -4301,7 +4303,7 @@ } if (IsArgSet("-fallbackfee")) { - Amount nFeePerK = 0; + Amount nFeePerK(0); if (!ParseMoney(GetArg("-fallbackfee", ""), nFeePerK)) { return InitError( strprintf(_("Invalid amount for -fallbackfee=: '%s'"), @@ -4318,7 +4320,7 @@ } if (IsArgSet("-paytxfee")) { - Amount nFeePerK = 0; + Amount nFeePerK(0); if (!ParseMoney(GetArg("-paytxfee", ""), nFeePerK)) { return InitError(AmountErrMsg("paytxfee", GetArg("-paytxfee", ""))); } @@ -4339,7 +4341,7 @@ } if (IsArgSet("-maxtxfee")) { - Amount nMaxFee = 0; + Amount nMaxFee(0); if (!ParseMoney(GetArg("-maxtxfee", ""), nMaxFee)) { return InitError(AmountErrMsg("maxtxfee", GetArg("-maxtxfee", ""))); }