From fa0a86fb74ab849710456f1510fa7d25fc3d3dbc Mon Sep 17 00:00:00 2001 From: Gregory Sanders Date: Wed, 14 Dec 2016 10:54:47 -0500 Subject: [PATCH] Move CAmountMap to amount.h --- src/amount.cpp | 34 ++++++++++++++++++++++++++++++++++ src/amount.h | 14 ++++++++++++++ src/wallet/wallet.cpp | 34 ---------------------------------- src/wallet/wallet.h | 15 --------------- 4 files changed, 48 insertions(+), 49 deletions(-) diff --git a/src/amount.cpp b/src/amount.cpp index a5f6bc3cd9..9d4a22893f 100644 --- a/src/amount.cpp +++ b/src/amount.cpp @@ -41,3 +41,37 @@ std::string CFeeRate::ToString() const { return strprintf("%d.%08d %s/kB", nSatoshisPerK / COIN, nSatoshisPerK % COIN, CURRENCY_UNIT); } + +CAmountMap& operator+=(CAmountMap& a, const CAmountMap& b) +{ + for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) + a[it->first] += it->second; + return a; +} + +CAmountMap& operator-=(CAmountMap& a, const CAmountMap& b) +{ + for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) + a[it->first] -= it->second; + return a; +} + +CAmountMap operator+(const CAmountMap& a, const CAmountMap& b) +{ + CAmountMap c; + for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) + c[it->first] += it->second; + for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) + c[it->first] += it->second; + return c; +} + +CAmountMap operator-(const CAmountMap& a, const CAmountMap& b) +{ + CAmountMap c; + for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) + c[it->first] += it->second; + for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) + c[it->first] -= it->second; + return c; +} diff --git a/src/amount.h b/src/amount.h index 90f0bfc541..2aea97c8cd 100644 --- a/src/amount.h +++ b/src/amount.h @@ -33,6 +33,14 @@ typedef uint256 CAssetID; /** The sha256 of Bitcoin genesis block, for easy reference **/ static const CAssetID BITCOINID(uint256S("09f663de96be771f50cab5ded00256ffe63773e2eaa9a604092951cc3d7c6621")); +/** Used for consensus fee and general wallet accounting*/ +typedef std::map CAmountMap; + +CAmountMap& operator+=(CAmountMap& a, const CAmountMap& b); +CAmountMap& operator-=(CAmountMap& a, const CAmountMap& b); +CAmountMap operator+(const CAmountMap& a, const CAmountMap& b); +CAmountMap operator-(const CAmountMap& a, const CAmountMap& b); + /** No amount larger than this (in satoshi) is valid. * * Note that this constant is *not* the total money supply, which in Bitcoin @@ -45,6 +53,12 @@ static const CAssetID BITCOINID(uint256S("09f663de96be771f50cab5ded00256ffe63773 static const CAmount MAX_MONEY = 21000000 * COIN; inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } +inline bool MoneyRange(const CAmountMap& mapValue) { + for(CAmountMap::const_iterator it = mapValue.begin(); it != mapValue.end(); it++) + if (it->second < 0 || it->second > MAX_MONEY) + return false; + return true; +} /** * Fee rate in satoshis per kilobyte: CAmount / kB */ diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index f115a2ab28..f15154f1a8 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4532,37 +4532,3 @@ bool hasNonPostiveValue(const CAmountMap& amount) } return false; } - -CAmountMap& operator+=(CAmountMap& a, const CAmountMap& b) -{ - for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) - a[it->first] += it->second; - return a; -} - -CAmountMap& operator-=(CAmountMap& a, const CAmountMap& b) -{ - for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) - a[it->first] -= it->second; - return a; -} - -CAmountMap operator+(const CAmountMap& a, const CAmountMap& b) -{ - CAmountMap c; - for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) - c[it->first] += it->second; - for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) - c[it->first] += it->second; - return c; -} - -CAmountMap operator-(const CAmountMap& a, const CAmountMap& b) -{ - CAmountMap c; - for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) - c[it->first] += it->second; - for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) - c[it->first] -= it->second; - return c; -} diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 1826f3ff85..ea022af91e 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -75,9 +75,6 @@ static const bool DEFAULT_USE_HD_WALLET = true; extern const char * DEFAULT_WALLET_DAT; -/** Structure used for internal wallet accounting, and not consensus**/ -typedef std::map CAmountMap; - // WARNING: Comparisons are only looking for *complete* ordering. // For strict inequality checks, if any entry would fail the non-strict // inequality, the comparison will fail. Therefore it is possible @@ -109,18 +106,6 @@ bool operator!=(const CAmountMap& a, const CAmountMap& b); bool hasNegativeValue(const CAmountMap& amount); bool hasNonPositiveValue(const CAmountMap& amount); -CAmountMap& operator+=(CAmountMap& a, const CAmountMap& b); -CAmountMap& operator-=(CAmountMap& a, const CAmountMap& b); -CAmountMap operator+(const CAmountMap& a, const CAmountMap& b); -CAmountMap operator-(const CAmountMap& a, const CAmountMap& b); - -inline bool MoneyRange(const CAmountMap& mapValue) { - for(CAmountMap::const_iterator it = mapValue.begin(); it != mapValue.end(); it++) - if (it->second < 0 || it->second > MAX_MONEY) - return false; - return true; -} - class CBlockIndex; class CCoinControl; class COutput;