Skip to content

Commit 08a7316

Browse files
committed
Merge #9279: Consensus: Move CFeeRate out of libconsensus
381a46e Consensus: Policy: MOVEONLY: Move CFeeRate out of the consensus module (Jorge Timón) 330bb5a Consensus: Minimal way to move dust out of consensus (Jorge Timón) Tree-SHA512: 19a2ea8169afd5a9d3f940d8974e34cfaead153e3ff3068ac82fccdb8694d19d9b45938904ec9e8cd095bd5ca3a0080364da29372f6aaf56b11a6c2ccd6c7a4d
2 parents 6a796b2 + 381a46e commit 08a7316

23 files changed

+124
-96
lines changed

src/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ BITCOIN_CORE_H = \
116116
netbase.h \
117117
netmessagemaker.h \
118118
noui.h \
119+
policy/feerate.h \
119120
policy/fees.h \
120121
policy/policy.h \
121122
policy/rbf.h \
@@ -301,7 +302,6 @@ libbitcoin_consensus_a_SOURCES = \
301302
libbitcoin_common_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
302303
libbitcoin_common_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
303304
libbitcoin_common_a_SOURCES = \
304-
amount.cpp \
305305
base58.cpp \
306306
chainparams.cpp \
307307
coins.cpp \
@@ -312,6 +312,7 @@ libbitcoin_common_a_SOURCES = \
312312
keystore.cpp \
313313
netaddress.cpp \
314314
netbase.cpp \
315+
policy/feerate.cpp \
315316
protocol.cpp \
316317
scheduler.cpp \
317318
script/sign.cpp \

src/amount.h

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@
66
#ifndef BITCOIN_AMOUNT_H
77
#define BITCOIN_AMOUNT_H
88

9-
#include "serialize.h"
10-
11-
#include <stdlib.h>
12-
#include <string>
9+
#include <stdint.h>
1310

1411
/** Amount in satoshis (Can be negative) */
1512
typedef int64_t CAmount;
1613

1714
static const CAmount COIN = 100000000;
1815
static const CAmount CENT = 1000000;
1916

20-
extern const std::string CURRENCY_UNIT;
21-
2217
/** No amount larger than this (in satoshi) is valid.
2318
*
2419
* Note that this constant is *not* the total money supply, which in Bitcoin
@@ -31,42 +26,4 @@ extern const std::string CURRENCY_UNIT;
3126
static const CAmount MAX_MONEY = 21000000 * COIN;
3227
inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
3328

34-
/**
35-
* Fee rate in satoshis per kilobyte: CAmount / kB
36-
*/
37-
class CFeeRate
38-
{
39-
private:
40-
CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes
41-
public:
42-
/** Fee rate of 0 satoshis per kB */
43-
CFeeRate() : nSatoshisPerK(0) { }
44-
explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { }
45-
/** Constructor for a fee rate in satoshis per kB. The size in bytes must not exceed (2^63 - 1)*/
46-
CFeeRate(const CAmount& nFeePaid, size_t nBytes);
47-
CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; }
48-
/**
49-
* Return the fee in satoshis for the given size in bytes.
50-
*/
51-
CAmount GetFee(size_t nBytes) const;
52-
/**
53-
* Return the fee in satoshis for a size of 1000 bytes
54-
*/
55-
CAmount GetFeePerK() const { return GetFee(1000); }
56-
friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
57-
friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
58-
friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
59-
friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; }
60-
friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
61-
CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; }
62-
std::string ToString() const;
63-
64-
ADD_SERIALIZE_METHODS;
65-
66-
template <typename Stream, typename Operation>
67-
inline void SerializationOp(Stream& s, Operation ser_action) {
68-
READWRITE(nSatoshisPerK);
69-
}
70-
};
71-
7229
#endif // BITCOIN_AMOUNT_H

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "netbase.h"
2626
#include "net.h"
2727
#include "net_processing.h"
28+
#include "policy/feerate.h"
2829
#include "policy/fees.h"
2930
#include "policy/policy.h"
3031
#include "rpc/server.h"

src/miner.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "hash.h"
1616
#include "validation.h"
1717
#include "net.h"
18+
#include "policy/feerate.h"
1819
#include "policy/policy.h"
1920
#include "pow.h"
2021
#include "primitives/transaction.h"

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "hash.h"
1515
#include "limitedmap.h"
1616
#include "netaddress.h"
17+
#include "policy/feerate.h"
1718
#include "protocol.h"
1819
#include "random.h"
1920
#include "streams.h"

src/amount.cpp renamed to src/policy/feerate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
#include "amount.h"
6+
#include "feerate.h"
77

88
#include "tinyformat.h"
99

src/policy/feerate.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2015 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_POLICY_FEERATE_H
7+
#define BITCOIN_POLICY_FEERATE_H
8+
9+
#include "amount.h"
10+
#include "serialize.h"
11+
12+
#include <string>
13+
14+
extern const std::string CURRENCY_UNIT;
15+
16+
/**
17+
* Fee rate in satoshis per kilobyte: CAmount / kB
18+
*/
19+
class CFeeRate
20+
{
21+
private:
22+
CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes
23+
public:
24+
/** Fee rate of 0 satoshis per kB */
25+
CFeeRate() : nSatoshisPerK(0) { }
26+
explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { }
27+
/** Constructor for a fee rate in satoshis per kB. The size in bytes must not exceed (2^63 - 1)*/
28+
CFeeRate(const CAmount& nFeePaid, size_t nBytes);
29+
CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; }
30+
/**
31+
* Return the fee in satoshis for the given size in bytes.
32+
*/
33+
CAmount GetFee(size_t nBytes) const;
34+
/**
35+
* Return the fee in satoshis for a size of 1000 bytes
36+
*/
37+
CAmount GetFeePerK() const { return GetFee(1000); }
38+
friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
39+
friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
40+
friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
41+
friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; }
42+
friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
43+
CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; }
44+
std::string ToString() const;
45+
46+
ADD_SERIALIZE_METHODS;
47+
48+
template <typename Stream, typename Operation>
49+
inline void SerializationOp(Stream& s, Operation ser_action) {
50+
READWRITE(nSatoshisPerK);
51+
}
52+
};
53+
54+
#endif // BITCOIN_POLICY_FEERATE_H

src/policy/fees.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_POLICYESTIMATOR_H
77

88
#include "amount.h"
9+
#include "feerate.h"
910
#include "uint256.h"
1011
#include "random.h"
1112
#include "sync.h"

src/policy/policy.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,43 @@
1515

1616
#include <boost/foreach.hpp>
1717

18+
CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFee)
19+
{
20+
// "Dust" is defined in terms of dustRelayFee,
21+
// which has units satoshis-per-kilobyte.
22+
// If you'd pay more than 1/3 in fees
23+
// to spend something, then we consider it dust.
24+
// A typical spendable non-segwit txout is 34 bytes big, and will
25+
// need a CTxIn of at least 148 bytes to spend:
26+
// so dust is a spendable txout less than
27+
// 546*dustRelayFee/1000 (in satoshis).
28+
// A typical spendable segwit txout is 31 bytes big, and will
29+
// need a CTxIn of at least 67 bytes to spend:
30+
// so dust is a spendable txout less than
31+
// 294*dustRelayFee/1000 (in satoshis).
32+
if (txout.scriptPubKey.IsUnspendable())
33+
return 0;
34+
35+
size_t nSize = GetSerializeSize(txout, SER_DISK, 0);
36+
int witnessversion = 0;
37+
std::vector<unsigned char> witnessprogram;
38+
39+
if (txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
40+
// sum the sizes of the parts of a transaction input
41+
// with 75% segwit discount applied to the script size.
42+
nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
43+
} else {
44+
nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
45+
}
46+
47+
return 3 * dustRelayFee.GetFee(nSize);
48+
}
49+
50+
bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFee)
51+
{
52+
return (txout.nValue < GetDustThreshold(txout, dustRelayFee));
53+
}
54+
1855
/**
1956
* Check transaction inputs to mitigate two
2057
* potential denial-of-service attacks:
@@ -106,7 +143,7 @@ bool IsStandardTx(const CTransaction& tx, std::string& reason, const bool witnes
106143
else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
107144
reason = "bare-multisig";
108145
return false;
109-
} else if (txout.IsDust(dustRelayFee)) {
146+
} else if (IsDust(txout, ::dustRelayFee)) {
110147
reason = "dust";
111148
return false;
112149
}

src/policy/policy.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
#define BITCOIN_POLICY_POLICY_H
88

99
#include "consensus/consensus.h"
10+
#include "feerate.h"
1011
#include "script/interpreter.h"
1112
#include "script/standard.h"
1213

1314
#include <string>
1415

1516
class CCoinsViewCache;
17+
class CTxOut;
1618

1719
/** Default for -blockmaxsize, which controls the maximum size of block the mining code will create **/
1820
static const unsigned int DEFAULT_BLOCK_MAX_SIZE = 750000;
@@ -72,6 +74,10 @@ static const unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS = STANDARD_SCRIPT_
7274
static const unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS = LOCKTIME_VERIFY_SEQUENCE |
7375
LOCKTIME_MEDIAN_TIME_PAST;
7476

77+
CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFee);
78+
79+
bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFee);
80+
7581
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType, const bool witnessEnabled = false);
7682
/**
7783
* Check for standard transaction types

0 commit comments

Comments
 (0)