Fabcoin Core  0.16.2
P2P Digital Currency
policy.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 lius-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 lius).
28  // 546 lius 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 lius).
33  // 294 lius at the default rate of 3000 sat/kB.
34  if (txout.scriptPubKey.IsUnspendable())
35  return 0;
36 
37  size_t nSize = GetSerializeSize(txout, SER_DISK, 0);
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  !txout.scriptPubKey.HasOpCreate() && !txout.scriptPubKey.HasOpCall());
56 }
57 
75 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType, const bool witnessEnabled)
76 {
77  std::vector<std::vector<unsigned char> > vSolutions;
78  if (!Solver(scriptPubKey, whichType, vSolutions))
79  return false;
80 
81  if (whichType == TX_MULTISIG)
82  {
83  unsigned char m = vSolutions.front()[0];
84  unsigned char n = vSolutions.back()[0];
85  // Support up to x-of-3 multisig txns as standard
86  if (n < 1 || n > 3)
87  return false;
88  if (m < 1 || m > n)
89  return false;
90  } else if (whichType == TX_NULL_DATA &&
91  (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes))
92  return false;
93 
94  else if (!witnessEnabled && (whichType == TX_WITNESS_V0_KEYHASH || whichType == TX_WITNESS_V0_SCRIPTHASH))
95  return false;
96 
97  return whichType != TX_NONSTANDARD;
98 }
99 
100 bool IsStandardTx(const CTransaction& tx, std::string& reason, const bool witnessEnabled)
101 {
103  reason = "version";
104  return false;
105  }
106 
107  // Extremely large transactions with lots of inputs can cost the network
108  // almost as much to process as they cost the sender in fees, because
109  // computing signature hashes is O(ninputs*txsize). Limiting transactions
110  // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
111  unsigned int sz = GetTransactionWeight(tx);
112  if (sz >= MAX_STANDARD_TX_WEIGHT) {
113  reason = "tx-size";
114  return false;
115  }
116 
117  for (const CTxIn& txin : tx.vin)
118  {
119  // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
120  // keys (remember the 520 byte limit on redeemScript size). That works
121  // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
122  // bytes of scriptSig, which we round off to 1650 bytes for some minor
123  // future-proofing. That's also enough to spend a 20-of-20
124  // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
125  // considered standard.
126  if (txin.scriptSig.size() > 1650) {
127  reason = "scriptsig-size";
128  return false;
129  }
130  if (!txin.scriptSig.IsPushOnly()) {
131  reason = "scriptsig-not-pushonly";
132  return false;
133  }
134  }
135 
136  unsigned int nDataOut = 0;
137  txnouttype whichType;
138  for (const CTxOut& txout : tx.vout) {
139  if (!::IsStandard(txout.scriptPubKey, whichType, witnessEnabled)) {
140  reason = "scriptpubkey";
141  return false;
142  }
143 
144  if (whichType == TX_NULL_DATA)
145  nDataOut++;
146  else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
147  reason = "bare-multisig";
148  return false;
149  } else if (IsDust(txout, ::dustRelayFee)) {
150  reason = "dust";
151  return false;
152  }
153  }
154 
155  // only one OP_RETURN txout is permitted
156  if (nDataOut > 1) {
157  reason = "multi-op-return";
158  return false;
159  }
160 
161  return true;
162 }
163 
164 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
165 {
166  if (tx.IsCoinBase())
167  return true; // Coinbases don't use vin normally
168 
169  for (unsigned int i = 0; i < tx.vin.size(); i++)
170  {
171  const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
172 
173  std::vector<std::vector<unsigned char> > vSolutions;
174  txnouttype whichType;
175  // get the scriptPubKey corresponding to this input:
176  const CScript& prevScript = prev.scriptPubKey;
177  if (!Solver(prevScript, whichType, vSolutions))
178  return false;
179 
180  if (whichType == TX_SCRIPTHASH)
181  {
182  std::vector<std::vector<unsigned char> > stack;
183  // convert the scriptSig into a stack, so we can inspect the redeemScript
184  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
185  return false;
186  if (stack.empty())
187  return false;
188  CScript subscript(stack.back().begin(), stack.back().end());
189  if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
190  return false;
191  }
192  }
193  }
194 
195  return true;
196 }
197 
198 bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
199 {
200  if (tx.IsCoinBase())
201  return true; // Coinbases are skipped
202 
203  for (unsigned int i = 0; i < tx.vin.size(); i++)
204  {
205  // We don't care if witness for this input is empty, since it must not be bloated.
206  // If the script is invalid without witness, it would be caught sooner or later during validation.
207  if (tx.vin[i].scriptWitness.IsNull())
208  continue;
209 
210  const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
211 
212  // get the scriptPubKey corresponding to this input:
213  CScript prevScript = prev.scriptPubKey;
214 
215  if (prevScript.IsPayToScriptHash()) {
216  std::vector <std::vector<unsigned char> > stack;
217  // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
218  // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
219  // If the check fails at this stage, we know that this txid must be a bad one.
220  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
221  return false;
222  if (stack.empty())
223  return false;
224  prevScript = CScript(stack.back().begin(), stack.back().end());
225  }
226 
227  int witnessversion = 0;
228  std::vector<unsigned char> witnessprogram;
229 
230  // Non-witness program must not be associated with any witness
231  if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
232  return false;
233 
234  // Check P2WSH standard limits
235  if (witnessversion == 0 && witnessprogram.size() == 32) {
236  if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
237  return false;
238  size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
239  if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
240  return false;
241  for (unsigned int j = 0; j < sizeWitnessStack; j++) {
242  if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
243  return false;
244  }
245  }
246  }
247  return true;
248 }
249 
250 CFeeRate incrementalRelayFee = CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE);
251 CFeeRate dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
252 unsigned int nBytesPerSigOp = DEFAULT_BYTES_PER_SIGOP;
253 
254 int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
255 {
256  return (std::max(nWeight, nSigOpCost * nBytesPerSigOp) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
257 }
258 
259 int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost)
260 {
261  return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost);
262 }
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:254
bool HasOpCreate() const
Definition: script.h:697
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< std::vector< unsigned char > > &vSolutionsRet, bool contractConsensus)
Return public keys or hashes from scriptPubKey, for &#39;standard&#39; transaction types. ...
Definition: standard.cpp:46
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:285
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:268
int64_t GetTransactionWeight(const CTransaction &tx)
CScript scriptPubKey
Definition: transaction.h:135
bool IsPayToScriptHash() const
Definition: script.cpp:207
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:989
CTxOut out
unspent transaction output
Definition: coins.h:33
bool fAcceptDatacarrier
Definition: standard.cpp:20
unsigned int nBytesPerSigOp
Definition: policy.cpp:252
bool fIsBareMultisigStd
Definition: validation.cpp:91
const std::vector< CTxIn > vin
Definition: transaction.h:292
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size...
Definition: policy.cpp:198
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check for standard transaction types.
Definition: policy.cpp:164
size_type size() const
Definition: prevector.h:282
An input of a transaction.
Definition: transaction.h:61
bool HasOpCall() const
Definition: script.h:702
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:252
const std::vector< CTxOut > vout
Definition: transaction.h:293
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:119
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:690
An output of a transaction.
Definition: transaction.h:131
bool IsStandardTx(const CTransaction &tx, std::string &reason, const bool witnessEnabled)
Check for standard transaction types.
Definition: policy.cpp:100
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
CAmount GetFee(size_t nBytes) const
Return the fee in liu for the given size in bytes.
Definition: feerate.cpp:23
txnouttype
Definition: standard.h:51
const int32_t nVersion
Definition: transaction.h:294
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:417
Fee rate in liu per kilobyte: CAmount / kB.
Definition: feerate.h:20
bool IsCoinBase() const
Definition: transaction.h:349
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:52
bool IsStandard(const CScript &scriptPubKey, txnouttype &whichType, const bool witnessEnabled)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:75
dev::WithExisting max(dev::WithExisting _a, dev::WithExisting _b)
Definition: Common.h:326
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:275
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:201
CFeeRate incrementalRelayFee
Definition: policy.cpp:250
unsigned nMaxDatacarrierBytes
Definition: standard.cpp:21
CFeeRate dustRelayFee
Definition: policy.cpp:251