From eda37330911b005f4be0c2d934346b26247d50f5 Mon Sep 17 00:00:00 2001 From: jtimon Date: Thu, 23 Oct 2014 02:05:11 +0200 Subject: [PATCH] MOVEONLY: Move CFeeRate and Amount constants to amount.o --- src/Makefile.am | 1 + src/amount.cpp | 31 +++++++++++++++++++++++++++++ src/amount.h | 43 ++++++++++++++++++++++++++++++++++++++++- src/core.cpp | 23 ---------------------- src/core.h | 41 --------------------------------------- src/init.cpp | 1 + src/main.h | 1 + src/miner.cpp | 1 + src/qt/optionsmodel.cpp | 1 + src/rpcmining.cpp | 1 + src/rpcwallet.cpp | 1 + src/txmempool.h | 1 + src/wallet.h | 1 + 13 files changed, 82 insertions(+), 65 deletions(-) create mode 100644 src/amount.cpp diff --git a/src/Makefile.am b/src/Makefile.am index dbefa71fc..d5abbc17d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -208,6 +208,7 @@ univalue_libbitcoin_univalue_a_SOURCES = \ libbitcoin_common_a_CPPFLAGS = $(BITCOIN_INCLUDES) libbitcoin_common_a_SOURCES = \ allocators.cpp \ + amount.cpp \ base58.cpp \ chainparams.cpp \ coins.cpp \ diff --git a/src/amount.cpp b/src/amount.cpp new file mode 100644 index 000000000..e6f5b7d44 --- /dev/null +++ b/src/amount.cpp @@ -0,0 +1,31 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2014 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "amount.h" + +#include "tinyformat.h" + +CFeeRate::CFeeRate(const CAmount& nFeePaid, size_t nSize) +{ + if (nSize > 0) + nSatoshisPerK = nFeePaid*1000/nSize; + else + nSatoshisPerK = 0; +} + +CAmount CFeeRate::GetFee(size_t nSize) const +{ + CAmount nFee = nSatoshisPerK*nSize / 1000; + + if (nFee == 0 && nSatoshisPerK > 0) + nFee = nSatoshisPerK; + + return nFee; +} + +std::string CFeeRate::ToString() const +{ + return strprintf("%d.%08d BTC/kB", nSatoshisPerK / COIN, nSatoshisPerK % COIN); +} diff --git a/src/amount.h b/src/amount.h index 831fa1f6c..c0d37954c 100644 --- a/src/amount.h +++ b/src/amount.h @@ -6,8 +6,49 @@ #ifndef BITCOIN_AMOUNT_H #define BITCOIN_AMOUNT_H -#include +#include "serialize.h" + +#include +#include typedef int64_t CAmount; +static const CAmount COIN = 100000000; +static const CAmount CENT = 1000000; + +/** No amount larger than this (in satoshi) is valid */ +static const CAmount MAX_MONEY = 21000000 * COIN; +inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } + +/** Type-safe wrapper class to for fee rates + * (how much to pay based on transaction size) + */ +class CFeeRate +{ +private: + CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes +public: + CFeeRate() : nSatoshisPerK(0) { } + explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { } + CFeeRate(const CAmount& nFeePaid, size_t nSize); + CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; } + + CAmount GetFee(size_t size) const; // unit returned is satoshis + CAmount GetFeePerK() const { 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; } + 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; + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + READWRITE(nSatoshisPerK); + } +}; + #endif // BITCOIN_AMOUNT_H diff --git a/src/core.cpp b/src/core.cpp index 73e6de88e..ac180cc50 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -59,29 +59,6 @@ std::string CTxOut::ToString() const return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30)); } -CFeeRate::CFeeRate(const CAmount& nFeePaid, size_t nSize) -{ - if (nSize > 0) - nSatoshisPerK = nFeePaid*1000/nSize; - else - nSatoshisPerK = 0; -} - -CAmount CFeeRate::GetFee(size_t nSize) const -{ - CAmount nFee = nSatoshisPerK*nSize / 1000; - - if (nFee == 0 && nSatoshisPerK > 0) - nFee = nSatoshisPerK; - - return nFee; -} - -std::string CFeeRate::ToString() const -{ - return strprintf("%d.%08d BTC/kB", nSatoshisPerK / COIN, nSatoshisPerK % COIN); -} - CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {} diff --git a/src/core.h b/src/core.h index a024dad74..f7a46da2c 100644 --- a/src/core.h +++ b/src/core.h @@ -16,13 +16,6 @@ class CTransaction; -static const int64_t COIN = 100000000; -static const int64_t CENT = 1000000; - -/** No amount larger than this (in satoshi) is valid */ -static const CAmount MAX_MONEY = 21000000 * COIN; -inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } - /** An outpoint - a combination of a transaction hash and an index n into its vout */ class COutPoint { @@ -109,40 +102,6 @@ public: std::string ToString() const; }; - - -/** Type-safe wrapper class to for fee rates - * (how much to pay based on transaction size) - */ -class CFeeRate -{ -private: - CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes -public: - CFeeRate() : nSatoshisPerK(0) { } - explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { } - CFeeRate(const CAmount& nFeePaid, size_t nSize); - CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; } - - CAmount GetFee(size_t size) const; // unit returned is satoshis - CAmount GetFeePerK() const { 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; } - 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; - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(nSatoshisPerK); - } -}; - - /** An output of a transaction. It contains the public key that the next input * must be able to sign with to claim it. */ diff --git a/src/init.cpp b/src/init.cpp index 70ac5190d..d928094bb 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -10,6 +10,7 @@ #include "init.h" #include "addrman.h" +#include "amount.h" #include "checkpoints.h" #include "compat/sanity.h" #include "key.h" diff --git a/src/main.h b/src/main.h index 1ef51918c..2686ed948 100644 --- a/src/main.h +++ b/src/main.h @@ -10,6 +10,7 @@ #include "config/bitcoin-config.h" #endif +#include "amount.h" #include "chain.h" #include "chainparams.h" #include "coins.h" diff --git a/src/miner.cpp b/src/miner.cpp index eefccfd64..897f7c56d 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -5,6 +5,7 @@ #include "miner.h" +#include "amount.h" #include "core.h" #include "hash.h" #include "main.h" diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp index 6db654dff..c941ebd4c 100644 --- a/src/qt/optionsmodel.cpp +++ b/src/qt/optionsmodel.cpp @@ -11,6 +11,7 @@ #include "bitcoinunits.h" #include "guiutil.h" +#include "amount.h" #include "init.h" #include "main.h" #include "net.h" diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index c767835a2..8af3c4634 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -3,6 +3,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "amount.h" #include "chainparams.h" #include "core_io.h" #include "init.h" diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 68bb4068b..2728fb4fb 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -3,6 +3,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "amount.h" #include "base58.h" #include "core_io.h" #include "rpcserver.h" diff --git a/src/txmempool.h b/src/txmempool.h index 85cf5310f..df2fbc78c 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -8,6 +8,7 @@ #include +#include "amount.h" #include "coins.h" #include "core.h" #include "sync.h" diff --git a/src/wallet.h b/src/wallet.h index 06706655f..6a4b4b7f8 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -6,6 +6,7 @@ #ifndef BITCOIN_WALLET_H #define BITCOIN_WALLET_H +#include "amount.h" #include "core.h" #include "crypter.h" #include "key.h"