diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -137,6 +137,7 @@ coins.cpp compressor.cpp dstencode.cpp + feerate.cpp globals.cpp core_read.cpp core_write.cpp diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -304,6 +304,7 @@ consensus/merkle.h \ consensus/params.h \ consensus/validation.h \ + feerate.h \ hash.cpp \ hash.h \ prevector.h \ @@ -345,6 +346,7 @@ coins.cpp \ compressor.cpp \ dstencode.cpp \ + feerate.cpp \ globals.cpp \ core_read.cpp \ core_write.cpp \ diff --git a/src/amount.h b/src/amount.h --- a/src/amount.h +++ b/src/amount.h @@ -1,5 +1,6 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers +// Copyright (c) 2017-2018 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -163,66 +164,4 @@ return (nValue >= Amount(0) && nValue <= MAX_MONEY); } -/** - * Fee rate in satoshis per kilobyte: Amount / kB - */ -class CFeeRate { -private: - // unit is satoshis-per-1,000-bytes - Amount nSatoshisPerK; - -public: - /** Fee rate of 0 satoshis per kB */ - CFeeRate() : nSatoshisPerK(0) {} - explicit CFeeRate(const Amount _nSatoshisPerK) - : nSatoshisPerK(_nSatoshisPerK) {} - /** - * Constructor for a fee rate in satoshis per kB. The size in bytes must not - * exceed (2^63 - 1) - */ - CFeeRate(const Amount nFeePaid, size_t nBytes); - CFeeRate(const CFeeRate &other) { nSatoshisPerK = other.nSatoshisPerK; } - /** - * Return the fee in satoshis for the given size in bytes. - */ - Amount GetFee(size_t nBytes) const; - /** - * Return the ceiling of a fee calculation in satoshis for the given size in - * bytes. - */ - Amount GetFeeCeiling(size_t nBytes) const; - - /** - * Return the fee in satoshis for a size of 1000 bytes - */ - Amount GetFeePerK() const { return GetFee(1000); } - 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; - } - 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; - } - CFeeRate &operator+=(const CFeeRate &a) { - nSatoshisPerK += a.nSatoshisPerK; - return *this; - } - std::string ToString() const; - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream &s, Operation ser_action) { - READWRITE(nSatoshisPerK); - } -}; - #endif // BITCOIN_AMOUNT_H diff --git a/src/amount.cpp b/src/amount.cpp --- a/src/amount.cpp +++ b/src/amount.cpp @@ -1,5 +1,6 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers +// Copyright (c) 2017-2018 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -11,55 +12,4 @@ std::string Amount::ToString() const { return strprintf("%d.%08d %s", *this / COIN, *this % COIN, CURRENCY_UNIT); -} - -CFeeRate::CFeeRate(const Amount nFeePaid, size_t nBytes_) { - assert(nBytes_ <= uint64_t(std::numeric_limits::max())); - int64_t nSize = int64_t(nBytes_); - - if (nSize > 0) { - nSatoshisPerK = 1000 * nFeePaid / nSize; - } else { - nSatoshisPerK = Amount(0); - } -} - -template -static Amount GetFee(size_t nBytes_, Amount nSatoshisPerK) { - assert(nBytes_ <= uint64_t(std::numeric_limits::max())); - int64_t nSize = int64_t(nBytes_); - - // Ensure fee is rounded up when truncated if ceil is true. - Amount nFee(0); - if (ceil) { - nFee = Amount(nSize * nSatoshisPerK % 1000 > Amount(0) - ? nSize * nSatoshisPerK / 1000 + Amount(1) - : nSize * nSatoshisPerK / 1000); - } else { - nFee = nSize * nSatoshisPerK / 1000; - } - - if (nFee == Amount(0) && nSize != 0) { - if (nSatoshisPerK > Amount(0)) { - nFee = Amount(1); - } - if (nSatoshisPerK < Amount(0)) { - nFee = Amount(-1); - } - } - - return nFee; -} - -Amount CFeeRate::GetFee(size_t nBytes) const { - return ::GetFee(nBytes, nSatoshisPerK); -} - -Amount CFeeRate::GetFeeCeiling(size_t nBytes) const { - return ::GetFee(nBytes, nSatoshisPerK); -} - -std::string CFeeRate::ToString() const { - return strprintf("%d.%08d %s/kB", nSatoshisPerK / COIN, - nSatoshisPerK % COIN, CURRENCY_UNIT); -} +} \ No newline at end of file diff --git a/src/config.h b/src/config.h --- a/src/config.h +++ b/src/config.h @@ -6,6 +6,7 @@ #define BITCOIN_CONFIG_H #include "amount.h" +#include "feerate.h" #include diff --git a/src/feerate.h b/src/feerate.h new file mode 100644 --- /dev/null +++ b/src/feerate.h @@ -0,0 +1,80 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2016 The Bitcoin Core developers +// Copyright (c) 2017-2018 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_FEERATE_H +#define BITCOIN_FEERATE_H + +#include "amount.h" +#include "serialize.h" + +#include +#include +#include +#include + +/** + * Fee rate in satoshis per kilobyte: Amount / kB + */ +class CFeeRate { +private: + // unit is satoshis-per-1,000-bytes + Amount nSatoshisPerK; + +public: + /** Fee rate of 0 satoshis per kB */ + CFeeRate() : nSatoshisPerK(0) {} + explicit CFeeRate(const Amount _nSatoshisPerK) + : nSatoshisPerK(_nSatoshisPerK) {} + /** + * Constructor for a fee rate in satoshis per kB. The size in bytes must not + * exceed (2^63 - 1) + */ + CFeeRate(const Amount nFeePaid, size_t nBytes); + CFeeRate(const CFeeRate &other) { nSatoshisPerK = other.nSatoshisPerK; } + /** + * Return the fee in satoshis for the given size in bytes. + */ + Amount GetFee(size_t nBytes) const; + /** + * Return the ceiling of a fee calculation in satoshis for the given size in + * bytes. + */ + Amount GetFeeCeiling(size_t nBytes) const; + + /** + * Return the fee in satoshis for a size of 1000 bytes + */ + Amount GetFeePerK() const { return GetFee(1000); } + 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; + } + 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; + } + CFeeRate &operator+=(const CFeeRate &a) { + nSatoshisPerK += a.nSatoshisPerK; + return *this; + } + std::string ToString() const; + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream &s, Operation ser_action) { + READWRITE(nSatoshisPerK); + } +}; + +#endif // BITCOIN_FEERATE_H diff --git a/src/amount.cpp b/src/feerate.cpp copy from src/amount.cpp copy to src/feerate.cpp --- a/src/amount.cpp +++ b/src/feerate.cpp @@ -1,18 +1,14 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers +// Copyright (c) 2017-2018 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "feerate.h" #include "amount.h" #include "tinyformat.h" -const std::string CURRENCY_UNIT = "BCH"; - -std::string Amount::ToString() const { - return strprintf("%d.%08d %s", *this / COIN, *this % COIN, CURRENCY_UNIT); -} - CFeeRate::CFeeRate(const Amount nFeePaid, size_t nBytes_) { assert(nBytes_ <= uint64_t(std::numeric_limits::max())); int64_t nSize = int64_t(nBytes_); diff --git a/src/policy/policy.h b/src/policy/policy.h --- a/src/policy/policy.h +++ b/src/policy/policy.h @@ -7,6 +7,7 @@ #define BITCOIN_POLICY_POLICY_H #include "consensus/consensus.h" +#include "feerate.h" #include "script/standard.h" #include diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -7,6 +7,7 @@ #define BITCOIN_PRIMITIVES_TRANSACTION_H #include "amount.h" +#include "feerate.h" #include "script/script.h" #include "serialize.h" #include "uint256.h"