Bitcoin Core  0.19.99
P2P Digital Currency
policy.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 only local node policy logic
7 
8 #include <policy/policy.h>
9 
10 #include <consensus/validation.h>
11 #include <coins.h>
12 
13 
14 CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
15 {
16  // "Dust" is defined in terms of dustRelayFee,
17  // which has units satoshis-per-kilobyte.
18  // If you'd pay more in fees than the value of the output
19  // to spend something, then we consider it dust.
20  // A typical spendable non-segwit txout is 34 bytes big, and will
21  // need a CTxIn of at least 148 bytes to spend:
22  // so dust is a spendable txout less than
23  // 182*dustRelayFee/1000 (in satoshis).
24  // 546 satoshis at the default rate of 3000 sat/kB.
25  // A typical spendable segwit txout is 31 bytes big, and will
26  // need a CTxIn of at least 67 bytes to spend:
27  // so dust is a spendable txout less than
28  // 98*dustRelayFee/1000 (in satoshis).
29  // 294 satoshis at the default rate of 3000 sat/kB.
30  if (txout.scriptPubKey.IsUnspendable())
31  return 0;
32 
33  size_t nSize = GetSerializeSize(txout);
34  int witnessversion = 0;
35  std::vector<unsigned char> witnessprogram;
36 
37  if (txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
38  // sum the sizes of the parts of a transaction input
39  // with 75% segwit discount applied to the script size.
40  nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
41  } else {
42  nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
43  }
44 
45  return dustRelayFeeIn.GetFee(nSize);
46 }
47 
48 bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
49 {
50  return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
51 }
52 
53 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType)
54 {
55  std::vector<std::vector<unsigned char> > vSolutions;
56  whichType = Solver(scriptPubKey, vSolutions);
57 
58  if (whichType == TX_NONSTANDARD) {
59  return false;
60  } else if (whichType == TX_MULTISIG) {
61  unsigned char m = vSolutions.front()[0];
62  unsigned char n = vSolutions.back()[0];
63  // Support up to x-of-3 multisig txns as standard
64  if (n < 1 || n > 3)
65  return false;
66  if (m < 1 || m > n)
67  return false;
68  } else if (whichType == TX_NULL_DATA &&
69  (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes)) {
70  return false;
71  }
72 
73  return true;
74 }
75 
76 bool IsStandardTx(const CTransaction& tx, bool permit_bare_multisig, const CFeeRate& dust_relay_fee, std::string& reason)
77 {
79  reason = "version";
80  return false;
81  }
82 
83  // Extremely large transactions with lots of inputs can cost the network
84  // almost as much to process as they cost the sender in fees, because
85  // computing signature hashes is O(ninputs*txsize). Limiting transactions
86  // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
87  unsigned int sz = GetTransactionWeight(tx);
88  if (sz > MAX_STANDARD_TX_WEIGHT) {
89  reason = "tx-size";
90  return false;
91  }
92 
93  for (const CTxIn& txin : tx.vin)
94  {
95  // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
96  // keys (remember the 520 byte limit on redeemScript size). That works
97  // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
98  // bytes of scriptSig, which we round off to 1650 bytes for some minor
99  // future-proofing. That's also enough to spend a 20-of-20
100  // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
101  // considered standard.
102  if (txin.scriptSig.size() > 1650) {
103  reason = "scriptsig-size";
104  return false;
105  }
106  if (!txin.scriptSig.IsPushOnly()) {
107  reason = "scriptsig-not-pushonly";
108  return false;
109  }
110  }
111 
112  unsigned int nDataOut = 0;
113  txnouttype whichType;
114  for (const CTxOut& txout : tx.vout) {
115  if (!::IsStandard(txout.scriptPubKey, whichType)) {
116  reason = "scriptpubkey";
117  return false;
118  }
119 
120  if (whichType == TX_NULL_DATA)
121  nDataOut++;
122  else if ((whichType == TX_MULTISIG) && (!permit_bare_multisig)) {
123  reason = "bare-multisig";
124  return false;
125  } else if (IsDust(txout, dust_relay_fee)) {
126  reason = "dust";
127  return false;
128  }
129  }
130 
131  // only one OP_RETURN txout is permitted
132  if (nDataOut > 1) {
133  reason = "multi-op-return";
134  return false;
135  }
136 
137  return true;
138 }
139 
156 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
157 {
158  if (tx.IsCoinBase())
159  return true; // Coinbases don't use vin normally
160 
161  for (unsigned int i = 0; i < tx.vin.size(); i++)
162  {
163  const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
164 
165  std::vector<std::vector<unsigned char> > vSolutions;
166  txnouttype whichType = Solver(prev.scriptPubKey, vSolutions);
167  if (whichType == TX_NONSTANDARD) {
168  return false;
169  } else if (whichType == TX_SCRIPTHASH) {
170  std::vector<std::vector<unsigned char> > stack;
171  // convert the scriptSig into a stack, so we can inspect the redeemScript
172  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
173  return false;
174  if (stack.empty())
175  return false;
176  CScript subscript(stack.back().begin(), stack.back().end());
177  if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
178  return false;
179  }
180  }
181  }
182 
183  return true;
184 }
185 
186 bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
187 {
188  if (tx.IsCoinBase())
189  return true; // Coinbases are skipped
190 
191  for (unsigned int i = 0; i < tx.vin.size(); i++)
192  {
193  // We don't care if witness for this input is empty, since it must not be bloated.
194  // If the script is invalid without witness, it would be caught sooner or later during validation.
195  if (tx.vin[i].scriptWitness.IsNull())
196  continue;
197 
198  const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
199 
200  // get the scriptPubKey corresponding to this input:
201  CScript prevScript = prev.scriptPubKey;
202 
203  if (prevScript.IsPayToScriptHash()) {
204  std::vector <std::vector<unsigned char> > stack;
205  // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
206  // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
207  // If the check fails at this stage, we know that this txid must be a bad one.
208  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
209  return false;
210  if (stack.empty())
211  return false;
212  prevScript = CScript(stack.back().begin(), stack.back().end());
213  }
214 
215  int witnessversion = 0;
216  std::vector<unsigned char> witnessprogram;
217 
218  // Non-witness program must not be associated with any witness
219  if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
220  return false;
221 
222  // Check P2WSH standard limits
223  if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
224  if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
225  return false;
226  size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
227  if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
228  return false;
229  for (unsigned int j = 0; j < sizeWitnessStack; j++) {
230  if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
231  return false;
232  }
233  }
234  }
235  return true;
236 }
237 
238 int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
239 {
240  return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
241 }
242 
243 int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop)
244 {
245  return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost, bytes_per_sigop);
246 }
247 
248 int64_t GetVirtualTransactionInputSize(const CTxIn& txin, int64_t nSigOpCost, unsigned int bytes_per_sigop)
249 {
250  return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop);
251 }
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:148
CAmount nValue
Definition: transaction.h:136
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:280
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
unspendable OP_RETURN script that carries data
Definition: standard.h:63
CScript scriptPubKey
Definition: transaction.h:137
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:118
bool IsPayToScriptHash() const
Definition: script.cpp:196
FeeReason reason
Definition: fees.h:78
CTxOut out
unspent transaction output
Definition: coins.h:34
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:14
static const unsigned int MAX_STANDARD_P2WSH_SCRIPT_SIZE
The maximum size of a standard witnessScript.
Definition: policy.h:44
txnouttype Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:91
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:215
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:238
bool IsCoinBase() const
Definition: transaction.h:337
const std::vector< CTxIn > vin
Definition: transaction.h:287
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size...
Definition: policy.cpp:186
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1004
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:557
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:156
int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Definition: policy.cpp:248
static const unsigned int MAX_P2SH_SIGOPS
Maximum number of signature check operations in an IsStandard() P2SH script.
Definition: policy.h:28
An input of a transaction.
Definition: transaction.h:63
static int64_t GetTransactionInputWeight(const CTxIn &txin)
Definition: validation.h:156
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:231
const std::vector< CTxOut > vout
Definition: transaction.h:288
An output of a transaction.
Definition: transaction.h:133
static constexpr size_t WITNESS_V0_SCRIPTHASH_SIZE
Signature hash sizes.
Definition: interpreter.h:137
static const unsigned int MAX_STANDARD_P2WSH_STACK_ITEMS
The maximum number of witness stack items in a standard P2WSH script.
Definition: policy.h:40
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror)
CAmount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:14
CScript scriptSig
Definition: transaction.h:67
static const unsigned int MAX_STANDARD_P2WSH_STACK_ITEM_SIZE
The maximum size of each witness stack item in a standard P2WSH script.
Definition: policy.h:42
txnouttype
Definition: standard.h:55
const int32_t nVersion
Definition: transaction.h:289
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
static const unsigned int MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we&#39;re willing to relay/mine.
Definition: policy.h:24
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
bool IsStandard(const CScript &scriptPubKey, txnouttype &whichType)
Definition: policy.cpp:53
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:48
size_type size() const
Definition: prevector.h:277
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:210
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:76
unsigned nMaxDatacarrierBytes
Maximum size of TX_NULL_DATA scripts that this node considers standard.
Definition: standard.cpp:15
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:23