Consensus: Policy: MOVEONLY: Move CFeeRate out of the consensus module
[bitcoinplatinum.git] / src / policy / policy.cpp
blob2b19a6714b428016fa3f363fdb95cbfb41ab94b3
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 CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFee)
20 // "Dust" is defined in terms of dustRelayFee,
21 // which has units satoshis-per-kilobyte.
22 // If you'd pay more than 1/3 in fees
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 // 546*dustRelayFee/1000 (in satoshis).
28 // A typical spendable segwit txout is 31 bytes big, and will
29 // need a CTxIn of at least 67 bytes to spend:
30 // so dust is a spendable txout less than
31 // 294*dustRelayFee/1000 (in satoshis).
32 if (txout.scriptPubKey.IsUnspendable())
33 return 0;
35 size_t nSize = GetSerializeSize(txout, SER_DISK, 0);
36 int witnessversion = 0;
37 std::vector<unsigned char> witnessprogram;
39 if (txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
40 // sum the sizes of the parts of a transaction input
41 // with 75% segwit discount applied to the script size.
42 nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
43 } else {
44 nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
47 return 3 * dustRelayFee.GetFee(nSize);
50 bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFee)
52 return (txout.nValue < GetDustThreshold(txout, dustRelayFee));
55 /**
56 * Check transaction inputs to mitigate two
57 * potential denial-of-service attacks:
59 * 1. scriptSigs with extra data stuffed into them,
60 * not consumed by scriptPubKey (or P2SH script)
61 * 2. P2SH scripts with a crazy number of expensive
62 * CHECKSIG/CHECKMULTISIG operations
64 * Why bother? To avoid denial-of-service attacks; an attacker
65 * can submit a standard HASH... OP_EQUAL transaction,
66 * which will get accepted into blocks. The redemption
67 * script can be anything; an attacker could use a very
68 * expensive-to-check-upon-redemption script like:
69 * DUP CHECKSIG DROP ... repeated 100 times... OP_1
72 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType, const bool witnessEnabled)
74 std::vector<std::vector<unsigned char> > vSolutions;
75 if (!Solver(scriptPubKey, whichType, vSolutions))
76 return false;
78 if (whichType == TX_MULTISIG)
80 unsigned char m = vSolutions.front()[0];
81 unsigned char n = vSolutions.back()[0];
82 // Support up to x-of-3 multisig txns as standard
83 if (n < 1 || n > 3)
84 return false;
85 if (m < 1 || m > n)
86 return false;
87 } else if (whichType == TX_NULL_DATA &&
88 (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes))
89 return false;
91 else if (!witnessEnabled && (whichType == TX_WITNESS_V0_KEYHASH || whichType == TX_WITNESS_V0_SCRIPTHASH))
92 return false;
94 return whichType != TX_NONSTANDARD;
97 bool IsStandardTx(const CTransaction& tx, std::string& reason, const bool witnessEnabled)
99 if (tx.nVersion > CTransaction::MAX_STANDARD_VERSION || tx.nVersion < 1) {
100 reason = "version";
101 return false;
104 // Extremely large transactions with lots of inputs can cost the network
105 // almost as much to process as they cost the sender in fees, because
106 // computing signature hashes is O(ninputs*txsize). Limiting transactions
107 // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
108 unsigned int sz = GetTransactionWeight(tx);
109 if (sz >= MAX_STANDARD_TX_WEIGHT) {
110 reason = "tx-size";
111 return false;
114 BOOST_FOREACH(const CTxIn& txin, tx.vin)
116 // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
117 // keys (remember the 520 byte limit on redeemScript size). That works
118 // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
119 // bytes of scriptSig, which we round off to 1650 bytes for some minor
120 // future-proofing. That's also enough to spend a 20-of-20
121 // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
122 // considered standard.
123 if (txin.scriptSig.size() > 1650) {
124 reason = "scriptsig-size";
125 return false;
127 if (!txin.scriptSig.IsPushOnly()) {
128 reason = "scriptsig-not-pushonly";
129 return false;
133 unsigned int nDataOut = 0;
134 txnouttype whichType;
135 BOOST_FOREACH(const CTxOut& txout, tx.vout) {
136 if (!::IsStandard(txout.scriptPubKey, whichType, witnessEnabled)) {
137 reason = "scriptpubkey";
138 return false;
141 if (whichType == TX_NULL_DATA)
142 nDataOut++;
143 else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
144 reason = "bare-multisig";
145 return false;
146 } else if (IsDust(txout, ::dustRelayFee)) {
147 reason = "dust";
148 return false;
152 // only one OP_RETURN txout is permitted
153 if (nDataOut > 1) {
154 reason = "multi-op-return";
155 return false;
158 return true;
161 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
163 if (tx.IsCoinBase())
164 return true; // Coinbases don't use vin normally
166 for (unsigned int i = 0; i < tx.vin.size(); i++)
168 const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
170 std::vector<std::vector<unsigned char> > vSolutions;
171 txnouttype whichType;
172 // get the scriptPubKey corresponding to this input:
173 const CScript& prevScript = prev.scriptPubKey;
174 if (!Solver(prevScript, whichType, vSolutions))
175 return false;
177 if (whichType == TX_SCRIPTHASH)
179 std::vector<std::vector<unsigned char> > stack;
180 // convert the scriptSig into a stack, so we can inspect the redeemScript
181 if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
182 return false;
183 if (stack.empty())
184 return false;
185 CScript subscript(stack.back().begin(), stack.back().end());
186 if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
187 return false;
192 return true;
195 bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
197 if (tx.IsCoinBase())
198 return true; // Coinbases are skipped
200 for (unsigned int i = 0; i < tx.vin.size(); i++)
202 // We don't care if witness for this input is empty, since it must not be bloated.
203 // If the script is invalid without witness, it would be caught sooner or later during validation.
204 if (tx.vin[i].scriptWitness.IsNull())
205 continue;
207 const CTxOut &prev = mapInputs.GetOutputFor(tx.vin[i]);
209 // get the scriptPubKey corresponding to this input:
210 CScript prevScript = prev.scriptPubKey;
212 if (prevScript.IsPayToScriptHash()) {
213 std::vector <std::vector<unsigned char> > stack;
214 // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
215 // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
216 // If the check fails at this stage, we know that this txid must be a bad one.
217 if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
218 return false;
219 if (stack.empty())
220 return false;
221 prevScript = CScript(stack.back().begin(), stack.back().end());
224 int witnessversion = 0;
225 std::vector<unsigned char> witnessprogram;
227 // Non-witness program must not be associated with any witness
228 if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
229 return false;
231 // Check P2WSH standard limits
232 if (witnessversion == 0 && witnessprogram.size() == 32) {
233 if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
234 return false;
235 size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
236 if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
237 return false;
238 for (unsigned int j = 0; j < sizeWitnessStack; j++) {
239 if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
240 return false;
244 return true;
247 CFeeRate incrementalRelayFee = CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE);
248 CFeeRate dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
249 unsigned int nBytesPerSigOp = DEFAULT_BYTES_PER_SIGOP;
251 int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
253 return (std::max(nWeight, nSigOpCost * nBytesPerSigOp) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
256 int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost)
258 return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost);