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