diff options
author | Gavin Andresen <gavinandresen@gmail.com> | 2014-04-10 14:14:18 -0400 |
---|---|---|
committer | Gavin Andresen <gavinandresen@gmail.com> | 2014-06-06 10:34:18 -0400 |
commit | c6cb21d17ab8097b6a425d37e48c955fbb0e9f0c (patch) | |
tree | 297b740beca0274be8a85d8c355acecafecbbc3e /src/core.h | |
parent | 345cb52e8ba878ca3e2590d5792b733ec11a1f0d (diff) |
Type-safe CFeeRate class
Use CFeeRate instead of an int64_t for quantities that are
fee-per-size.
Helps prevent unit-conversion mismatches between the wallet,
relaying, and mining code.
Diffstat (limited to 'src/core.h')
-rw-r--r-- | src/core.h | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/src/core.h b/src/core.h index ba7f691119..9fccffc4b2 100644 --- a/src/core.h +++ b/src/core.h @@ -112,6 +112,28 @@ public: +/** Type-safe wrapper class to for fee rates + * (how much to pay based on transaction size) + */ +class CFeeRate +{ +private: + int64_t nSatoshisPerK; // unit is satoshis-per-1,000-bytes +public: + explicit CFeeRate(int64_t _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { } + CFeeRate(int64_t nFeePaid, size_t nSize); + CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; } + + int64_t GetFee(size_t size); // unit returned is satoshis + int64_t GetFeePerK() { return GetFee(1000); } // satoshis-per-1000-bytes + + friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; } + friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; } + friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; } + + std::string ToString() const; +}; + /** An output of a transaction. It contains the public key that the next input * must be able to sign with to claim it. @@ -148,17 +170,18 @@ public: uint256 GetHash() const; - bool IsDust(int64_t nMinRelayTxFee) const + bool IsDust(CFeeRate minRelayTxFee) const { - // "Dust" is defined in terms of CTransaction::nMinRelayTxFee, + // "Dust" is defined in terms of CTransaction::minRelayTxFee, // which has units satoshis-per-kilobyte. // If you'd pay more than 1/3 in fees // to spend something, then we consider it dust. // A typical txout is 34 bytes big, and will - // need a CTxIn of at least 148 bytes to spend, + // need a CTxIn of at least 148 bytes to spend: // so dust is a txout less than 546 satoshis - // with default nMinRelayTxFee. - return ((nValue*1000)/(3*((int)GetSerializeSize(SER_DISK,0)+148)) < nMinRelayTxFee); + // with default minRelayTxFee. + size_t nSize = GetSerializeSize(SER_DISK,0)+148u; + return (nValue < 3*minRelayTxFee.GetFee(nSize)); } friend bool operator==(const CTxOut& a, const CTxOut& b) @@ -183,8 +206,8 @@ public: class CTransaction { public: - static int64_t nMinTxFee; - static int64_t nMinRelayTxFee; + static CFeeRate minTxFee; + static CFeeRate minRelayTxFee; static const int CURRENT_VERSION=1; int nVersion; std::vector<CTxIn> vin; |