Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
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,
41 std::vector<std::vector<uint8_t>> vSolutions;
42 whichType = Solver(scriptPubKey, vSolutions);
43
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.
84 reason = "tx-size";
85 return false;
86 }
87
88 for (const CTxIn &txin : tx.vin) {
89 if (txin.scriptSig.size() > MAX_TX_IN_SCRIPT_SIG_SIZE) {
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;
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
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
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);
158 return false;
159 }
160 }
161
162 return true;
163}
164
166 unsigned int bytes_per_sigCheck) {
167 return std::max(nSize, nSigChecks * bytes_per_sigCheck);
168}
169
175
int flags
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition coins.h:221
Fee rate in satoshis per kilobyte: Amount / kB.
Definition feerate.h:21
Serialized script, used inside transaction inputs and outputs.
Definition script.h:424
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.
static constexpr int32_t MAX_VERSION
const std::vector< CTxOut > vout
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
bool IsCoinBase() const
const int32_t nVersion
const std::vector< CTxIn > vin
static constexpr int32_t MIN_VERSION
An input of a transaction.
Definition transaction.h:59
An output of a transaction.
CScript scriptPubKey
Amount nValue
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
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
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
static constexpr Amount zero() noexcept
Definition amount.h:32
static const int PROTOCOL_VERSION
network protocol versioning
Definition version.h:11