Merge #10682: Trivial: Move the AreInputsStandard documentation next to its implement...
[bitcoinplatinum.git] / src / policy / policy.cpp
blob56912d0375fee39e0bd95dce9c22edb7256b124a
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 "consensus/validation.h"
11 #include "validation.h"
12 #include "coins.h"
13 #include "tinyformat.h"
14 #include "util.h"
15 #include "utilstrencodings.h"
18 CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
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;
37 size_t nSize = GetSerializeSize(txout, SER_DISK, 0);
38 int witnessversion = 0;
39 std::vector<unsigned char> witnessprogram;
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
49 return dustRelayFeeIn.GetFee(nSize);
52 bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
54 return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
57 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType, const bool witnessEnabled)
59 std::vector<std::vector<unsigned char> > vSolutions;
60 if (!Solver(scriptPubKey, whichType, vSolutions))
61 return false;
63 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;
76 else if (!witnessEnabled && (whichType == TX_WITNESS_V0_KEYHASH || whichType == TX_WITNESS_V0_SCRIPTHASH))
77 return false;
79 return whichType != TX_NONSTANDARD;
82 bool IsStandardTx(const CTransaction& tx, std::string& reason, const bool witnessEnabled)
84 if (tx.nVersion > CTransaction::MAX_STANDARD_VERSION || tx.nVersion < 1) {
85 reason = "version";
86 return false;
89 // Extremely large transactions with lots of inputs can cost the network
90 // almost as much to process as they cost the sender in fees, because
91 // computing signature hashes is O(ninputs*txsize). Limiting transactions
92 // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
93 unsigned int sz = GetTransactionWeight(tx);
94 if (sz >= MAX_STANDARD_TX_WEIGHT) {
95 reason = "tx-size";
96 return false;
99 for (const CTxIn& txin : tx.vin)
101 // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
102 // keys (remember the 520 byte limit on redeemScript size). That works
103 // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
104 // bytes of scriptSig, which we round off to 1650 bytes for some minor
105 // future-proofing. That's also enough to spend a 20-of-20
106 // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
107 // considered standard.
108 if (txin.scriptSig.size() > 1650) {
109 reason = "scriptsig-size";
110 return false;
112 if (!txin.scriptSig.IsPushOnly()) {
113 reason = "scriptsig-not-pushonly";
114 return false;
118 unsigned int nDataOut = 0;
119 txnouttype whichType;
120 for (const CTxOut& txout : tx.vout) {
121 if (!::IsStandard(txout.scriptPubKey, whichType, witnessEnabled)) {
122 reason = "scriptpubkey";
123 return false;
126 if (whichType == TX_NULL_DATA)
127 nDataOut++;
128 else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
129 reason = "bare-multisig";
130 return false;
131 } else if (IsDust(txout, ::dustRelayFee)) {
132 reason = "dust";
133 return false;
137 // only one OP_RETURN txout is permitted
138 if (nDataOut > 1) {
139 reason = "multi-op-return";
140 return false;
143 return true;
147 * Check transaction inputs to mitigate two
148 * potential denial-of-service attacks:
150 * 1. scriptSigs with extra data stuffed into them,
151 * not consumed by scriptPubKey (or P2SH script)
152 * 2. P2SH scripts with a crazy number of expensive
153 * CHECKSIG/CHECKMULTISIG operations
155 * Why bother? To avoid denial-of-service attacks; an attacker
156 * can submit a standard HASH... OP_EQUAL transaction,
157 * which will get accepted into blocks. The redemption
158 * script can be anything; an attacker could use a very
159 * expensive-to-check-upon-redemption script like:
160 * DUP CHECKSIG DROP ... repeated 100 times... OP_1
162 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
164 if (tx.IsCoinBase())
165 return true; // Coinbases don't use vin normally
167 for (unsigned int i = 0; i < tx.vin.size(); i++)
169 const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
171 std::vector<std::vector<unsigned char> > vSolutions;
172 txnouttype whichType;
173 // get the scriptPubKey corresponding to this input:
174 const CScript& prevScript = prev.scriptPubKey;
175 if (!Solver(prevScript, whichType, vSolutions))
176 return false;
178 if (whichType == TX_SCRIPTHASH)
180 std::vector<std::vector<unsigned char> > stack;
181 // convert the scriptSig into a stack, so we can inspect the redeemScript
182 if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
183 return false;
184 if (stack.empty())
185 return false;
186 CScript subscript(stack.back().begin(), stack.back().end());
187 if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
188 return false;
193 return true;
196 bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
198 if (tx.IsCoinBase())
199 return true; // Coinbases are skipped
201 for (unsigned int i = 0; i < tx.vin.size(); i++)
203 // We don't care if witness for this input is empty, since it must not be bloated.
204 // If the script is invalid without witness, it would be caught sooner or later during validation.
205 if (tx.vin[i].scriptWitness.IsNull())
206 continue;
208 const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
210 // get the scriptPubKey corresponding to this input:
211 CScript prevScript = prev.scriptPubKey;
213 if (prevScript.IsPayToScriptHash()) {
214 std::vector <std::vector<unsigned char> > stack;
215 // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
216 // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
217 // If the check fails at this stage, we know that this txid must be a bad one.
218 if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
219 return false;
220 if (stack.empty())
221 return false;
222 prevScript = CScript(stack.back().begin(), stack.back().end());
225 int witnessversion = 0;
226 std::vector<unsigned char> witnessprogram;
228 // Non-witness program must not be associated with any witness
229 if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
230 return false;
232 // Check P2WSH standard limits
233 if (witnessversion == 0 && witnessprogram.size() == 32) {
234 if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
235 return false;
236 size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
237 if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
238 return false;
239 for (unsigned int j = 0; j < sizeWitnessStack; j++) {
240 if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
241 return false;
245 return true;
248 CFeeRate incrementalRelayFee = CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE);
249 CFeeRate dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
250 unsigned int nBytesPerSigOp = DEFAULT_BYTES_PER_SIGOP;
252 int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
254 return (std::max(nWeight, nSigOpCost * nBytesPerSigOp) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
257 int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost)
259 return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost);