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