Bitcoin ABC 0.30.7
P2P Digital Currency
policy.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 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// NOTE: This file is intended to be customised by the end user, and includes
7// only local node policy logic
8
9#include <coins.h>
10#include <common/system.h>
11#include <policy/policy.h>
12#include <script/interpreter.h>
13
14Amount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn) {
22 if (txout.scriptPubKey.IsUnspendable()) {
23 return Amount::zero();
24 }
25
26 size_t nSize = GetSerializeSize(txout);
27
28 // the 148 mentioned above
29 nSize += (32 + 4 + 1 + 107 + 4);
30
31 return 3 * dustRelayFeeIn.GetFee(nSize);
32}
33
34bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn) {
35 return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
36}
37
38bool IsStandard(const CScript &scriptPubKey,
39 const std::optional<unsigned> &max_datacarrier_bytes,
40 TxoutType &whichType) {
41 std::vector<std::vector<uint8_t>> vSolutions;
42 whichType = Solver(scriptPubKey, vSolutions);
43
44 if (whichType == TxoutType::NONSTANDARD) {
45 return false;
46 } else if (whichType == TxoutType::MULTISIG) {
47 uint8_t m = vSolutions.front()[0];
48 uint8_t n = vSolutions.back()[0];
49 // Support up to x-of-3 multisig txns as standard
50 if (n < 1 || n > 3) {
51 return false;
52 }
53 if (m < 1 || m > n) {
54 return false;
55 }
56 } else if (whichType == TxoutType::NULL_DATA) {
57 if (!max_datacarrier_bytes ||
58 scriptPubKey.size() > *max_datacarrier_bytes) {
59 return false;
60 }
61 }
62
63 return true;
64}
65
67 const std::optional<unsigned> &max_datacarrier_bytes,
68 bool permit_bare_multisig, const CFeeRate &dust_relay_fee,
69 std::string &reason) {
70 // Only allow these tx versions, there is no point accepting a tx that
71 // violates the consensus rules
74 reason = "version";
75 return false;
76 }
77
78 // Extremely large transactions with lots of inputs can cost the network
79 // almost as much to process as they cost the sender in fees, because
80 // computing signature hashes is O(ninputs*txsize). Limiting transactions
81 // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
82 uint32_t sz = tx.GetTotalSize();
83 if (sz > MAX_STANDARD_TX_SIZE) {
84 reason = "tx-size";
85 return false;
86 }
87
88 for (const CTxIn &txin : tx.vin) {
90 reason = "scriptsig-size";
91 return false;
92 }
93 if (!txin.scriptSig.IsPushOnly()) {
94 reason = "scriptsig-not-pushonly";
95 return false;
96 }
97 }
98
99 unsigned int nDataOut = 0;
100 TxoutType whichType;
101 for (const CTxOut &txout : tx.vout) {
102 if (!::IsStandard(txout.scriptPubKey, max_datacarrier_bytes,
103 whichType)) {
104 reason = "scriptpubkey";
105 return false;
106 }
107
108 if (whichType == TxoutType::NULL_DATA) {
109 nDataOut++;
110 } else if ((whichType == TxoutType::MULTISIG) &&
111 (!permit_bare_multisig)) {
112 reason = "bare-multisig";
113 return false;
114 } else if (IsDust(txout, dust_relay_fee)) {
115 reason = "dust";
116 return false;
117 }
118 }
119
120 // only one OP_RETURN txout is permitted
121 if (nDataOut > 1) {
122 reason = "multi-op-return";
123 return false;
124 }
125
126 return true;
127}
128
145bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs,
146 uint32_t flags) {
147 if (tx.IsCoinBase()) {
148 // Coinbases don't use vin normally.
149 return true;
150 }
151
152 for (const CTxIn &in : tx.vin) {
153 const CTxOut &prev = mapInputs.AccessCoin(in.prevout).GetTxOut();
154
155 std::vector<std::vector<uint8_t>> vSolutions;
156 TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
157 if (whichType == TxoutType::NONSTANDARD) {
158 return false;
159 }
160 }
161
162 return true;
163}
164
165int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigChecks,
166 unsigned int bytes_per_sigCheck) {
167 return std::max(nSize, nSigChecks * bytes_per_sigCheck);
168}
169
171 unsigned int bytes_per_sigCheck) {
173 nSigChecks, bytes_per_sigCheck);
174}
175
177 unsigned int bytes_per_sigCheck) {
179 nSigChecks, bytes_per_sigCheck);
180}
int flags
Definition: bitcoin-tx.cpp:541
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:221
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Definition: coins.cpp:196
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
Amount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:49
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:424
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:404
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:541
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
static constexpr int32_t MAX_VERSION
Definition: transaction.h:199
const std::vector< CTxOut > vout
Definition: transaction.h:207
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:92
bool IsCoinBase() const
Definition: transaction.h:252
const int32_t nVersion
Definition: transaction.h:208
const std::vector< CTxIn > vin
Definition: transaction.h:206
static constexpr int32_t MIN_VERSION
Definition: transaction.h:199
An input of a transaction.
Definition: transaction.h:59
CScript scriptSig
Definition: transaction.h:62
COutPoint prevout
Definition: transaction.h:61
An output of a transaction.
Definition: transaction.h:128
CScript scriptPubKey
Definition: transaction.h:131
Amount nValue
Definition: transaction.h:130
CTxOut & GetTxOut()
Definition: coins.h:49
size_type size() const
Definition: prevector.h:394
unsigned int nSigChecks
Amount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:14
bool IsStandard(const CScript &scriptPubKey, const std::optional< unsigned > &max_datacarrier_bytes, TxoutType &whichType)
Definition: policy.cpp:38
int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigChecks, unsigned int bytes_per_sigCheck)
Definition: policy.cpp:176
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs, uint32_t flags)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:145
bool IsStandardTx(const CTransaction &tx, const std::optional< unsigned > &max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
Definition: policy.cpp:66
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:34
int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigChecks, unsigned int bytes_per_sigCheck)
Compute the virtual transaction size (size, or more if sigChecks are too dense).
Definition: policy.cpp:165
static constexpr unsigned int MAX_STANDARD_TX_SIZE
The maximum size for transactions we're willing to relay/mine.
Definition: policy.h:34
static constexpr unsigned int MAX_TX_IN_SCRIPT_SIG_SIZE
Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed keys (remember the 520 byte limit...
Definition: policy.h:44
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1258
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< uint8_t > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:108
TxoutType
Definition: standard.h:38
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11