Dash Core  0.12.2.1
P2P Digital Currency
policy.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin 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 "tinyformat.h"
12 #include "util.h"
13 #include "utilstrencodings.h"
14 
15 #include <boost/foreach.hpp>
16 
37 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType)
38 {
39  std::vector<std::vector<unsigned char> > vSolutions;
40  if (!Solver(scriptPubKey, whichType, vSolutions))
41  return false;
42 
43  if (whichType == TX_MULTISIG)
44  {
45  unsigned char m = vSolutions.front()[0];
46  unsigned char 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  if (m < 1 || m > n)
51  return false;
52  } else if (whichType == TX_NULL_DATA &&
53  (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes))
54  return false;
55 
56  return whichType != TX_NONSTANDARD;
57 }
58 
59 bool IsStandardTx(const CTransaction& tx, std::string& reason)
60 {
62  reason = "version";
63  return false;
64  }
65 
66  // Extremely large transactions with lots of inputs can cost the network
67  // almost as much to process as they cost the sender in fees, because
68  // computing signature hashes is O(ninputs*txsize). Limiting transactions
69  // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
70  unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
71  if (sz >= MAX_STANDARD_TX_SIZE) {
72  reason = "tx-size";
73  return false;
74  }
75 
76  BOOST_FOREACH(const CTxIn& txin, tx.vin)
77  {
78  // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
79  // keys. (remember the 520 byte limit on redeemScript size) That works
80  // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
81  // bytes of scriptSig, which we round off to 1650 bytes for some minor
82  // future-proofing. That's also enough to spend a 20-of-20
83  // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
84  // considered standard)
85  if (txin.scriptSig.size() > 1650) {
86  reason = "scriptsig-size";
87  return false;
88  }
89  if (!txin.scriptSig.IsPushOnly()) {
90  reason = "scriptsig-not-pushonly";
91  return false;
92  }
93  }
94 
95  unsigned int nDataOut = 0;
96  txnouttype whichType;
97  BOOST_FOREACH(const CTxOut& txout, tx.vout) {
98  if (!::IsStandard(txout.scriptPubKey, whichType)) {
99  reason = "scriptpubkey";
100  return false;
101  }
102 
103  if (whichType == TX_NULL_DATA)
104  nDataOut++;
105  else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
106  reason = "bare-multisig";
107  return false;
108  } else if (txout.IsDust(::minRelayTxFee)) {
109  reason = "dust";
110  return false;
111  }
112  }
113 
114  // only one OP_RETURN txout is permitted
115  if (nDataOut > 1) {
116  reason = "multi-op-return";
117  return false;
118  }
119 
120  return true;
121 }
122 
123 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
124 {
125  if (tx.IsCoinBase())
126  return true; // Coinbases don't use vin normally
127 
128  for (unsigned int i = 0; i < tx.vin.size(); i++)
129  {
130  const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
131 
132  std::vector<std::vector<unsigned char> > vSolutions;
133  txnouttype whichType;
134  // get the scriptPubKey corresponding to this input:
135  const CScript& prevScript = prev.scriptPubKey;
136  if (!Solver(prevScript, whichType, vSolutions))
137  return false;
138 
139  if (whichType == TX_SCRIPTHASH)
140  {
141  std::vector<std::vector<unsigned char> > stack;
142  // convert the scriptSig into a stack, so we can inspect the redeemScript
143  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), 0))
144  return false;
145  if (stack.empty())
146  return false;
147  CScript subscript(stack.back().begin(), stack.back().end());
148  if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
149  return false;
150  }
151  }
152  }
153 
154  return true;
155 }
static const int32_t CURRENT_VERSION
Definition: transaction.h:219
bool fAcceptDatacarrier
bytes (+1 for OP_RETURN, +2 for the pushdata opcodes)
Definition: standard.cpp:19
const CTxOut & GetOutputFor(const CTxIn &input) const
Definition: coins.cpp:227
bool fIsBareMultisigStd
Definition: validation.cpp:78
CScript scriptSig
Definition: transaction.h:62
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Definition: policy.cpp:123
const int32_t nVersion
Definition: transaction.h:232
CFeeRate minRelayTxFee
Definition: validation.cpp:94
static const unsigned int MAX_P2SH_SIGOPS
Definition: policy.h:25
bool EvalScript(vector< vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
CScript scriptPubKey
Definition: transaction.h:137
txnouttype
Definition: standard.h:45
const std::vector< CTxIn > vin
Definition: transaction.h:233
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:225
bool IsDust(const CFeeRate &minRelayTxFee) const
Definition: transaction.h:185
bool IsCoinBase() const
Definition: transaction.h:284
bool IsPushOnly(const_iterator pc) const
Definition: script.cpp:247
const std::vector< CTxOut > vout
Definition: transaction.h:234
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet, vector< vector< unsigned char > > &vSolutionsRet)
Definition: standard.cpp:41
bool IsStandardTx(const CTransaction &tx, std::string &reason)
Definition: policy.cpp:59
bool IsStandard(const CScript &scriptPubKey, txnouttype &whichType)
Definition: policy.cpp:37
size_type size() const
Definition: prevector.h:262
unsigned nMaxDatacarrierBytes
Definition: standard.cpp:20
static const unsigned int MAX_STANDARD_TX_SIZE
Definition: policy.h:23