Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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 only local node policy logic
7 
8 #include <policy/policy.h>
9 
10 #include <validation.h>
11 #include <coins.h>
12 #include <tinyformat.h>
13 #include <util.h>
14 #include <utilstrencodings.h>
15 
16 
17 CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
18 {
19  // "Dust" is defined in terms of dustRelayFee,
20  // which has units duffs-per-kilobyte.
21  // If you'd pay more in fees than the value of the output
22  // to spend something, then we consider it dust.
23  // A typical spendable txout is 34 bytes big, and will
24  // need a CTxIn of at least 148 bytes to spend:
25  // so dust is a spendable txout less than
26  // 182*dustRelayFee/1000 (in duffs).
27  // 546 duffs at the default rate of 3000 duff/kB.
28  if (txout.scriptPubKey.IsUnspendable())
29  return 0;
30 
31  size_t nSize = GetSerializeSize(txout, SER_DISK, 0)+148u;
32  return dustRelayFeeIn.GetFee(nSize);
33 }
34 
35 bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
36 {
37  return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
38 }
39 
40 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType)
41 {
42  std::vector<std::vector<unsigned char> > vSolutions;
43  if (!Solver(scriptPubKey, whichType, vSolutions))
44  return false;
45 
46  if (whichType == TX_MULTISIG)
47  {
48  unsigned char m = vSolutions.front()[0];
49  unsigned char n = vSolutions.back()[0];
50  // Support up to x-of-3 multisig txns as standard
51  if (n < 1 || n > 3)
52  return false;
53  if (m < 1 || m > n)
54  return false;
55  } else if (whichType == TX_NULL_DATA &&
56  (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes))
57  return false;
58 
59  return whichType != TX_NONSTANDARD;
60 }
61 
62 bool IsStandardTx(const CTransaction& tx, std::string& reason)
63 {
65  reason = "version";
66  return false;
67  }
68 
69  // Extremely large transactions with lots of inputs can cost the network
70  // almost as much to process as they cost the sender in fees, because
71  // computing signature hashes is O(ninputs*txsize). Limiting transactions
72  // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
74  if (sz >= MAX_STANDARD_TX_SIZE) {
75  reason = "tx-size";
76  return false;
77  }
78 
79  for (const CTxIn& txin : tx.vin)
80  {
81  // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
82  // keys (remember the 520 byte limit on redeemScript size). That works
83  // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
84  // bytes of scriptSig, which we round off to 1650 bytes for some minor
85  // future-proofing. That's also enough to spend a 20-of-20
86  // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
87  // considered standard.
88  if (txin.scriptSig.size() > 1650) {
89  reason = "scriptsig-size";
90  return false;
91  }
92  if (!txin.scriptSig.IsPushOnly()) {
93  reason = "scriptsig-not-pushonly";
94  return false;
95  }
96  }
97 
98  unsigned int nDataOut = 0;
99  txnouttype whichType;
100  for (const CTxOut& txout : tx.vout) {
101  if (!::IsStandard(txout.scriptPubKey, whichType)) {
102  reason = "scriptpubkey";
103  return false;
104  }
105 
106  if (whichType == TX_NULL_DATA)
107  nDataOut++;
108  else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
109  reason = "bare-multisig";
110  return false;
111  } else if (IsDust(txout, ::dustRelayFee)) {
112  reason = "dust";
113  return false;
114  }
115  }
116 
117  // only one OP_RETURN txout is permitted
118  if (nDataOut > 1) {
119  reason = "multi-op-return";
120  return false;
121  }
122 
123  return true;
124 }
125 
142 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
143 {
144  if (tx.IsCoinBase())
145  return true; // Coinbases don't use vin normally
146 
147  for (unsigned int i = 0; i < tx.vin.size(); i++)
148  {
149  const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
150 
151  std::vector<std::vector<unsigned char> > vSolutions;
152  txnouttype whichType;
153  // get the scriptPubKey corresponding to this input:
154  const CScript& prevScript = prev.scriptPubKey;
155  if (!Solver(prevScript, whichType, vSolutions))
156  return false;
157 
158  if (whichType == TX_SCRIPTHASH)
159  {
160  std::vector<std::vector<unsigned char> > stack;
161  // convert the scriptSig into a stack, so we can inspect the redeemScript
162  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
163  return false;
164  if (stack.empty())
165  return false;
166  CScript subscript(stack.back().begin(), stack.back().end());
167  if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
168  return false;
169  }
170  }
171  }
172 
173  return true;
174 }
175 
CAmount nValue
Definition: transaction.h:147
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:208
unspendable OP_RETURN script that carries data
Definition: standard.h:64
CScript scriptPubKey
Definition: transaction.h:148
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
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:1295
FeeReason reason
Definition: fees.h:128
CTxOut out
unspent transaction output
Definition: coins.h:33
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:16
static const unsigned int DEFAULT_INCREMENTAL_RELAY_FEE
Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or BIP...
Definition: policy.h:32
bool IsCoinBase() const
Definition: transaction.h:272
bool fIsBareMultisigStd
Definition: validation.cpp:231
const std::vector< CTxIn > vin
Definition: transaction.h:215
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:659
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:142
static const unsigned int MAX_P2SH_SIGOPS
Maximum number of signature check operations in an IsStandard() P2SH script.
Definition: policy.h:26
An input of a transaction.
Definition: transaction.h:70
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:236
const std::vector< CTxOut > vout
Definition: transaction.h:216
An output of a transaction.
Definition: transaction.h:144
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:17
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:35
const int16_t nVersion
Definition: transaction.h:217
CScript scriptSig
Definition: transaction.h:74
txnouttype
Definition: standard.h:56
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
static const unsigned int DUST_RELAY_TX_FEE
Min feerate for defining dust.
Definition: policy.h:38
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:62
bool IsStandard(const CScript &scriptPubKey, txnouttype &whichType)
Definition: policy.cpp:40
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:35
size_type size() const
Definition: prevector.h:310
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
static const int32_t CURRENT_VERSION
Definition: transaction.h:202
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:201
CFeeRate incrementalRelayFee
Definition: policy.cpp:176
unsigned nMaxDatacarrierBytes
Maximum size of TX_NULL_DATA scripts that this node considers standard.
Definition: standard.cpp:17
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:177
static const unsigned int MAX_STANDARD_TX_SIZE
The maximum size for transactions we&#39;re willing to relay/mine.
Definition: policy.h:24
Released under the MIT license