Bitcoin ABC  0.22.12
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, txnouttype &whichType) {
39  std::vector<std::vector<uint8_t>> vSolutions;
40  whichType = Solver(scriptPubKey, vSolutions);
41 
42  if (whichType == TX_NONSTANDARD) {
43  return false;
44  } else if (whichType == TX_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 == TX_NULL_DATA) {
55  if (!fAcceptDatacarrier) {
56  return false;
57  }
58 
59  unsigned nMaxDatacarrierBytes =
60  gArgs.GetArg("-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) {
72  reason = "version";
73  return false;
74  }
75 
76  // Extremely large transactions with lots of inputs can cost the network
77  // almost as much to process as they cost the sender in fees, because
78  // computing signature hashes is O(ninputs*txsize). Limiting transactions
79  // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
80  uint32_t sz = tx.GetTotalSize();
81  if (sz > MAX_STANDARD_TX_SIZE) {
82  reason = "tx-size";
83  return false;
84  }
85 
86  for (const CTxIn &txin : tx.vin) {
88  reason = "scriptsig-size";
89  return false;
90  }
91  if (!txin.scriptSig.IsPushOnly()) {
92  reason = "scriptsig-not-pushonly";
93  return false;
94  }
95  }
96 
97  unsigned int nDataOut = 0;
98  txnouttype whichType;
99  for (const CTxOut &txout : tx.vout) {
100  if (!::IsStandard(txout.scriptPubKey, whichType)) {
101  reason = "scriptpubkey";
102  return false;
103  }
104 
105  if (whichType == TX_NULL_DATA) {
106  nDataOut++;
107  } else if ((whichType == TX_MULTISIG) && (!permit_bare_multisig)) {
108  reason = "bare-multisig";
109  return false;
110  } else if (IsDust(txout, dust_relay_fee)) {
111  reason = "dust";
112  return false;
113  }
114  }
115 
116  // only one OP_RETURN txout is permitted
117  if (nDataOut > 1) {
118  reason = "multi-op-return";
119  return false;
120  }
121 
122  return true;
123 }
124 
141 bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs,
142  uint32_t flags) {
143  if (tx.IsCoinBase()) {
144  // Coinbases don't use vin normally.
145  return true;
146  }
147 
148  for (const CTxIn &in : tx.vin) {
149  const CTxOut &prev = mapInputs.AccessCoin(in.prevout).GetTxOut();
150 
151  std::vector<std::vector<uint8_t>> vSolutions;
152  txnouttype whichType = Solver(prev.scriptPubKey, vSolutions);
153  if (whichType == TX_NONSTANDARD) {
154  return false;
155  }
156  }
157 
158  return true;
159 }
160 
161 int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigOpCount,
162  unsigned int bytes_per_sigop) {
163  return std::max(nSize, nSigOpCount * bytes_per_sigop);
164 }
165 
167  unsigned int bytes_per_sigop) {
169  nSigOpCount, bytes_per_sigop);
170 }
171 
172 int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigOpCount,
173  unsigned int bytes_per_sigop) {
175  nSigOpCount, bytes_per_sigop);
176 }
txnouttype Solver(const CScript &scriptPubKey, std::vector< std::vector< uint8_t >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:102
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:220
static constexpr Amount zero()
Definition: amount.h:35
int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigOpCount, unsigned int bytes_per_sigop)
Definition: policy.cpp:172
CScript scriptPubKey
Definition: transaction.h:144
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or a pruned one if not found.
Definition: coins.cpp:166
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:33
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:13
Definition: amount.h:17
bool IsCoinBase() const
Definition: transaction.h:275
const std::vector< CTxIn > vin
Definition: transaction.h:227
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1193
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:571
Amount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:14
Amount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:51
An input of a transaction.
Definition: transaction.h:67
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:424
const std::vector< CTxOut > vout
Definition: transaction.h:228
unsigned int nSigOpCount
An output of a transaction.
Definition: transaction.h:141
static const unsigned int MAX_TX_IN_SCRIPT_SIG_SIZE
Biggest &#39;standard&#39; txin is a 15-of-15 P2SH multisig with compressed keys (remember the 520 byte limit...
Definition: policy.h:43
CScript scriptSig
Definition: transaction.h:70
int flags
Definition: bitcoin-tx.cpp:529
txnouttype
Definition: standard.h:41
const int32_t nVersion
Definition: transaction.h:229
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:429
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
CTxOut & GetTxOut()
Definition: coins.h:48
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:479
ArgsManager gArgs
Definition: system.cpp:76
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
bool IsStandard(const CScript &scriptPubKey, txnouttype &whichType)
Definition: policy.cpp:38
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:34
size_type size() const
Definition: prevector.h:384
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:86
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:204
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
COutPoint prevout
Definition: transaction.h:69
int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigOpCount, unsigned int bytes_per_sigop)
Compute the virtual transaction size (size, or more if sigops are too dense).
Definition: policy.cpp:161
Amount nValue
Definition: transaction.h:143
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:141
static const unsigned int MAX_STANDARD_TX_SIZE
The maximum size for transactions we&#39;re willing to relay/mine.
Definition: policy.h:33