Bitcoin ABC 0.34.2
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) {
24 if (txout.scriptPubKey.IsUnspendable()) {
25 return Amount::zero();
26 }
27
28 size_t nSize = GetSerializeSize(txout);
29
30 // the 148 mentioned above
31 nSize += (32 + 4 + 1 + 107 + 4);
32
33 return dustRelayFeeIn.GetFee(nSize);
34}
35
36bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn) {
37 return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
38}
39
40bool IsStandard(const CScript &scriptPubKey,
41 const std::optional<unsigned> &max_datacarrier_bytes,
42 TxoutType &whichType) {
43 std::vector<std::vector<uint8_t>> vSolutions;
44 whichType = Solver(scriptPubKey, vSolutions);
45
46 if (whichType == TxoutType::NONSTANDARD) {
47 return false;
48 } else if (whichType == TxoutType::MULTISIG) {
49 uint8_t m = vSolutions.front()[0];
50 uint8_t n = vSolutions.back()[0];
51 // Support up to x-of-3 multisig txns as standard
52 if (n < 1 || n > 3) {
53 return false;
54 }
55 if (m < 1 || m > n) {
56 return false;
57 }
58 } else if (whichType == TxoutType::NULL_DATA) {
59 if (!max_datacarrier_bytes ||
60 scriptPubKey.size() > *max_datacarrier_bytes) {
61 return false;
62 }
63 }
64
65 return true;
66}
67
69 const std::optional<unsigned> &max_datacarrier_bytes,
70 bool permit_bare_multisig, const CFeeRate &dust_relay_fee,
71 std::string &reason) {
72 // Only allow these tx versions, there is no point accepting a tx that
73 // violates the consensus rules
76 reason = "version";
77 return false;
78 }
79
80 // Extremely large transactions with lots of inputs can cost the network
81 // almost as much to process as they cost the sender in fees, because
82 // computing signature hashes is O(ninputs*txsize). Limiting transactions
83 // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
84 uint32_t sz = tx.GetTotalSize();
85 if (sz > MAX_STANDARD_TX_SIZE) {
86 reason = "tx-size";
87 return false;
88 }
89
90 for (const CTxIn &txin : tx.vin) {
92 reason = "scriptsig-size";
93 return false;
94 }
95 if (!txin.scriptSig.IsPushOnly()) {
96 reason = "scriptsig-not-pushonly";
97 return false;
98 }
99 }
100
101 unsigned int nDataOut = 0;
102 TxoutType whichType;
103 for (const CTxOut &txout : tx.vout) {
104 if (!::IsStandard(txout.scriptPubKey, max_datacarrier_bytes,
105 whichType)) {
106 reason = "scriptpubkey";
107 return false;
108 }
109
110 if (whichType == TxoutType::NULL_DATA) {
111 nDataOut++;
112 } else if ((whichType == TxoutType::MULTISIG) &&
113 (!permit_bare_multisig)) {
114 reason = "bare-multisig";
115 return false;
116 } else if (IsDust(txout, dust_relay_fee)) {
117 reason = "dust";
118 return false;
119 }
120 }
121
122 // only one OP_RETURN txout is permitted
123 if (nDataOut > 1) {
124 reason = "multi-op-return";
125 return false;
126 }
127
128 return true;
129}
130
147bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs,
148 uint32_t flags) {
149 if (tx.IsCoinBase()) {
150 // Coinbases don't use vin normally.
151 return true;
152 }
153
154 for (const CTxIn &in : tx.vin) {
155 const CTxOut &prev = mapInputs.AccessCoin(in.prevout).GetTxOut();
156
157 std::vector<std::vector<uint8_t>> vSolutions;
158 TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
159 if (whichType == TxoutType::NONSTANDARD) {
160 return false;
161 }
162 }
163
164 return true;
165}
166
167int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigChecks,
168 unsigned int bytes_per_sigCheck) {
169 return std::max(nSize, nSigChecks * bytes_per_sigCheck);
170}
171
173 unsigned int bytes_per_sigCheck) {
175 bytes_per_sigCheck);
176}
177
179 unsigned int bytes_per_sigCheck) {
181 bytes_per_sigCheck);
182}
int flags
Definition: bitcoin-tx.cpp:544
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:357
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Definition: coins.cpp:199
Fee rate in satoshis per virtualbyte: Amount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:24
Amount GetFee(int32_t virtual_bytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:23
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:434
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:407
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:551
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
static constexpr uint32_t MIN_VERSION
Definition: transaction.h:199
const std::vector< CTxOut > vout
Definition: transaction.h:207
const uint32_t nVersion
Definition: transaction.h:208
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:92
bool IsCoinBase() const
Definition: transaction.h:252
static constexpr uint32_t MAX_VERSION
Definition: transaction.h:199
const std::vector< CTxIn > vin
Definition: transaction.h:206
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:52
size_type size() const
Definition: prevector.h:398
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:40
int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigChecks, unsigned int bytes_per_sigCheck)
Definition: policy.cpp:178
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:147
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:68
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:36
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:167
static constexpr unsigned int MAX_STANDARD_TX_SIZE
The maximum size for transactions we're willing to relay/mine.
Definition: policy.h:36
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:46
size_t GetSerializeSize(const T &t)
Definition: serialize.h:1264
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< uint8_t > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: solver.cpp:93
TxoutType
Definition: solver.h:15
Definition: amount.h:23
static constexpr Amount zero() noexcept
Definition: amount.h:36