Bitcoin ABC  0.26.3
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 <policy/policy.h>
11 #include <script/interpreter.h>
12 #include <util/system.h>
13 
14 Amount 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 
34 bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn) {
35  return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
36 }
37 
38 bool IsStandard(const CScript &scriptPubKey, TxoutType &whichType) {
39  std::vector<std::vector<uint8_t>> vSolutions;
40  whichType = Solver(scriptPubKey, vSolutions);
41 
42  if (whichType == TxoutType::NONSTANDARD) {
43  return false;
44  } else if (whichType == TxoutType::MULTISIG) {
45  uint8_t m = vSolutions.front()[0];
46  uint8_t n = vSolutions.back()[0];
47  // Support up to x-of-3 multisig txns as standard
48  if (n < 1 || n > 3) {
49  return false;
50  }
51  if (m < 1 || m > n) {
52  return false;
53  }
54  } else if (whichType == TxoutType::NULL_DATA) {
55  if (!fAcceptDatacarrier) {
56  return false;
57  }
58 
59  unsigned nMaxDatacarrierBytes =
60  gArgs.GetIntArg("-datacarriersize", MAX_OP_RETURN_RELAY);
61  if (scriptPubKey.size() > nMaxDatacarrierBytes) {
62  return false;
63  }
64  }
65 
66  return true;
67 }
68 
69 bool IsStandardTx(const CTransaction &tx, bool permit_bare_multisig,
70  const CFeeRate &dust_relay_fee, std::string &reason) {
71  // Only allow these tx versions, there is no point accepting a tx that
72  // violates the consensus rules
75  reason = "version";
76  return false;
77  }
78 
79  // Extremely large transactions with lots of inputs can cost the network
80  // almost as much to process as they cost the sender in fees, because
81  // computing signature hashes is O(ninputs*txsize). Limiting transactions
82  // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
83  uint32_t sz = tx.GetTotalSize();
84  if (sz > MAX_STANDARD_TX_SIZE) {
85  reason = "tx-size";
86  return false;
87  }
88 
89  for (const CTxIn &txin : tx.vin) {
91  reason = "scriptsig-size";
92  return false;
93  }
94  if (!txin.scriptSig.IsPushOnly()) {
95  reason = "scriptsig-not-pushonly";
96  return false;
97  }
98  }
99 
100  unsigned int nDataOut = 0;
101  TxoutType whichType;
102  for (const CTxOut &txout : tx.vout) {
103  if (!::IsStandard(txout.scriptPubKey, 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 
145 bool 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 
165 int64_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 
176 int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigChecks,
177  unsigned int bytes_per_sigCheck) {
179  nSigChecks, bytes_per_sigCheck);
180 }
int flags
Definition: bitcoin-tx.cpp:533
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:634
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:203
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Definition: coins.cpp:192
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:431
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:548
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:48
size_type size() const
Definition: prevector.h:386
unsigned int nSigChecks
Amount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:14
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, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
Definition: policy.cpp:69
bool IsStandard(const CScript &scriptPubKey, TxoutType &whichType)
Definition: policy.cpp:38
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:33
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:43
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1276
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:15
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:111
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:37
TxoutType
Definition: standard.h:45
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
ArgsManager gArgs
Definition: system.cpp:79
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11