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.
This commit is contained in:
Gavin Andresen
2014-04-10 14:14:18 -04:00
parent 345cb52e8b
commit c6cb21d17a
16 changed files with 123 additions and 115 deletions

View File

@@ -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;