Update to latest libsecp256k1
[bitcoinplatinum.git] / src / policy / policy.cpp
blob6f8b6c295318e2a14693f6b8a3df60ae4a291095
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.
6 // NOTE: This file is intended to be customised by the end user, and includes only local node policy logic
8 #include "policy/policy.h"
10 #include "validation.h"
11 #include "coins.h"
12 #include "tinyformat.h"
13 #include "util.h"
14 #include "utilstrencodings.h"
16 #include <boost/foreach.hpp>
18 /**
19 * Check transaction inputs to mitigate two
20 * potential denial-of-service attacks:
22 * 1. scriptSigs with extra data stuffed into them,
23 * not consumed by scriptPubKey (or P2SH script)
24 * 2. P2SH scripts with a crazy number of expensive
25 * CHECKSIG/CHECKMULTISIG operations
27 * Why bother? To avoid denial-of-service attacks; an attacker
28 * can submit a standard HASH... OP_EQUAL transaction,
29 * which will get accepted into blocks. The redemption
30 * script can be anything; an attacker could use a very
31 * expensive-to-check-upon-redemption script like:
32 * DUP CHECKSIG DROP ... repeated 100 times... OP_1
35 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType, const bool witnessEnabled)
37 std::vector<std::vector<unsigned char> > vSolutions;
38 if (!Solver(scriptPubKey, whichType, vSolutions))
39 return false;
41 if (whichType == TX_MULTISIG)
43 unsigned char m = vSolutions.front()[0];
44 unsigned char n = vSolutions.back()[0];
45 // Support up to x-of-3 multisig txns as standard
46 if (n < 1 || n > 3)
47 return false;
48 if (m < 1 || m > n)
49 return false;
50 } else if (whichType == TX_NULL_DATA &&
51 (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes))
52 return false;
54 else if (!witnessEnabled && (whichType == TX_WITNESS_V0_KEYHASH || whichType == TX_WITNESS_V0_SCRIPTHASH))
55 return false;
57 return whichType != TX_NONSTANDARD;
60 bool IsStandardTx(const CTransaction& tx, std::string& reason, const bool witnessEnabled)
62 if (tx.nVersion > CTransaction::MAX_STANDARD_VERSION || tx.nVersion < 1) {
63 reason = "version";
64 return false;
67 // Extremely large transactions with lots of inputs can cost the network
68 // almost as much to process as they cost the sender in fees, because
69 // computing signature hashes is O(ninputs*txsize). Limiting transactions
70 // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
71 unsigned int sz = GetTransactionWeight(tx);
72 if (sz >= MAX_STANDARD_TX_WEIGHT) {
73 reason = "tx-size";
74 return false;
77 BOOST_FOREACH(const CTxIn& txin, tx.vin)
79 // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
80 // keys (remember the 520 byte limit on redeemScript size). That works
81 // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
82 // bytes of scriptSig, which we round off to 1650 bytes for some minor
83 // future-proofing. That's also enough to spend a 20-of-20
84 // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
85 // considered standard.
86 if (txin.scriptSig.size() > 1650) {
87 reason = "scriptsig-size";
88 return false;
90 if (!txin.scriptSig.IsPushOnly()) {
91 reason = "scriptsig-not-pushonly";
92 return false;
96 unsigned int nDataOut = 0;
97 txnouttype whichType;
98 BOOST_FOREACH(const CTxOut& txout, tx.vout) {
99 if (!::IsStandard(txout.scriptPubKey, whichType, witnessEnabled)) {
100 reason = "scriptpubkey";
101 return false;
104 if (whichType == TX_NULL_DATA)
105 nDataOut++;
106 else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
107 reason = "bare-multisig";
108 return false;
109 } else if (txout.IsDust(dustRelayFee)) {
110 reason = "dust";
111 return false;
115 // only one OP_RETURN txout is permitted
116 if (nDataOut > 1) {
117 reason = "multi-op-return";
118 return false;
121 return true;
124 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
126 if (tx.IsCoinBase())
127 return true; // Coinbases don't use vin normally
129 for (unsigned int i = 0; i < tx.vin.size(); i++)
131 const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
133 std::vector<std::vector<unsigned char> > vSolutions;
134 txnouttype whichType;
135 // get the scriptPubKey corresponding to this input:
136 const CScript& prevScript = prev.scriptPubKey;
137 if (!Solver(prevScript, whichType, vSolutions))
138 return false;
140 if (whichType == TX_SCRIPTHASH)
142 std::vector<std::vector<unsigned char> > stack;
143 // convert the scriptSig into a stack, so we can inspect the redeemScript
144 if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
145 return false;
146 if (stack.empty())
147 return false;
148 CScript subscript(stack.back().begin(), stack.back().end());
149 if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
150 return false;
155 return true;
158 bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
160 if (tx.IsCoinBase())
161 return true; // Coinbases are skipped
163 for (unsigned int i = 0; i < tx.vin.size(); i++)
165 // We don't care if witness for this input is empty, since it must not be bloated.
166 // If the script is invalid without witness, it would be caught sooner or later during validation.
167 if (tx.vin[i].scriptWitness.IsNull())
168 continue;
170 const CTxOut &prev = mapInputs.GetOutputFor(tx.vin[i]);
172 // get the scriptPubKey corresponding to this input:
173 CScript prevScript = prev.scriptPubKey;
175 if (prevScript.IsPayToScriptHash()) {
176 std::vector <std::vector<unsigned char> > stack;
177 // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
178 // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
179 // If the check fails at this stage, we know that this txid must be a bad one.
180 if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
181 return false;
182 if (stack.empty())
183 return false;
184 prevScript = CScript(stack.back().begin(), stack.back().end());
187 int witnessversion = 0;
188 std::vector<unsigned char> witnessprogram;
190 // Non-witness program must not be associated with any witness
191 if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
192 return false;
194 // Check P2WSH standard limits
195 if (witnessversion == 0 && witnessprogram.size() == 32) {
196 if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
197 return false;
198 size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
199 if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
200 return false;
201 for (unsigned int j = 0; j < sizeWitnessStack; j++) {
202 if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
203 return false;
207 return true;
210 CFeeRate incrementalRelayFee = CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE);
211 CFeeRate dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
212 unsigned int nBytesPerSigOp = DEFAULT_BYTES_PER_SIGOP;
214 int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
216 return (std::max(nWeight, nSigOpCost * nBytesPerSigOp) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
219 int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost)
221 return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost);