Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 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 <coins.h>
11 : : #include <consensus/amount.h>
12 : : #include <consensus/consensus.h>
13 : : #include <consensus/validation.h>
14 : : #include <policy/feerate.h>
15 : : #include <primitives/transaction.h>
16 : : #include <script/interpreter.h>
17 : : #include <script/script.h>
18 : : #include <script/solver.h>
19 : : #include <serialize.h>
20 : : #include <span.h>
21 : :
22 : : #include <algorithm>
23 : : #include <cstddef>
24 : : #include <vector>
25 : :
26 : 13978943 : CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
27 : : {
28 : : // "Dust" is defined in terms of dustRelayFee,
29 : : // which has units satoshis-per-kilobyte.
30 : : // If you'd pay more in fees than the value of the output
31 : : // to spend something, then we consider it dust.
32 : : // A typical spendable non-segwit txout is 34 bytes big, and will
33 : : // need a CTxIn of at least 148 bytes to spend:
34 : : // so dust is a spendable txout less than
35 : : // 182*dustRelayFee/1000 (in satoshis).
36 : : // 546 satoshis at the default rate of 3000 sat/kvB.
37 : : // A typical spendable segwit P2WPKH txout is 31 bytes big, and will
38 : : // need a CTxIn of at least 67 bytes to spend:
39 : : // so dust is a spendable txout less than
40 : : // 98*dustRelayFee/1000 (in satoshis).
41 : : // 294 satoshis at the default rate of 3000 sat/kvB.
42 [ + + ]: 13978943 : if (txout.scriptPubKey.IsUnspendable())
43 : : return 0;
44 : :
45 : 13966694 : size_t nSize = GetSerializeSize(txout);
46 : 13966694 : int witnessversion = 0;
47 : 13966694 : std::vector<unsigned char> witnessprogram;
48 : :
49 : : // Note this computation is for spending a Segwit v0 P2WPKH output (a 33 bytes
50 : : // public key + an ECDSA signature). For Segwit v1 Taproot outputs the minimum
51 : : // satisfaction is lower (a single BIP340 signature) but this computation was
52 : : // kept to not further reduce the dust level.
53 : : // See discussion in https://github.com/bitcoin/bitcoin/pull/22779 for details.
54 [ + - + + ]: 13966694 : if (txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
55 : : // sum the sizes of the parts of a transaction input
56 : : // with 75% segwit discount applied to the script size.
57 : 13799395 : nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
58 : : } else {
59 : 167299 : nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
60 : : }
61 : :
62 [ + - ]: 13966694 : return dustRelayFeeIn.GetFee(nSize);
63 : 13966694 : }
64 : :
65 : 13942199 : bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
66 : : {
67 : 13942199 : return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
68 : : }
69 : :
70 : 3763249 : std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate)
71 : : {
72 : 3763249 : std::vector<uint32_t> dust_outputs;
73 [ + + ]: 16690019 : for (uint32_t i{0}; i < tx.vout.size(); ++i) {
74 [ + - + + : 12926770 : if (IsDust(tx.vout[i], dust_relay_rate)) dust_outputs.push_back(i);
+ - ]
75 : : }
76 : 3763249 : return dust_outputs;
77 : 0 : }
78 : :
79 : 4196065 : bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType)
80 : : {
81 : 4196065 : std::vector<std::vector<unsigned char> > vSolutions;
82 [ + - ]: 4196065 : whichType = Solver(scriptPubKey, vSolutions);
83 : :
84 [ + + ]: 4196065 : if (whichType == TxoutType::NONSTANDARD) {
85 : : return false;
86 [ + + ]: 4172448 : } else if (whichType == TxoutType::MULTISIG) {
87 [ + + ]: 1283 : unsigned char m = vSolutions.front()[0];
88 : 1283 : unsigned char n = vSolutions.back()[0];
89 : : // Support up to x-of-3 multisig txns as standard
90 [ + + ]: 1283 : if (n < 1 || n > 3)
91 : : return false;
92 [ - + ]: 1144 : if (m < 1 || m > n)
93 : 0 : return false;
94 [ + + ]: 4171165 : } else if (whichType == TxoutType::NULL_DATA) {
95 [ + + + + : 7161 : if (!max_datacarrier_bytes || scriptPubKey.size() > *max_datacarrier_bytes) {
+ + ]
96 : 199 : return false;
97 : : }
98 : : }
99 : :
100 : : return true;
101 : 4196065 : }
102 : :
103 : 414475 : bool IsStandardTx(const CTransaction& tx, const std::optional<unsigned>& max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate& dust_relay_fee, std::string& reason)
104 : : {
105 [ + + ]: 414475 : if (tx.version > TX_MAX_STANDARD_VERSION || tx.version < 1) {
106 : 12981 : reason = "version";
107 : 12981 : return false;
108 : : }
109 : :
110 : : // Extremely large transactions with lots of inputs can cost the network
111 : : // almost as much to process as they cost the sender in fees, because
112 : : // computing signature hashes is O(ninputs*txsize). Limiting transactions
113 : : // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
114 : 401494 : unsigned int sz = GetTransactionWeight(tx);
115 [ + + ]: 401494 : if (sz > MAX_STANDARD_TX_WEIGHT) {
116 : 734 : reason = "tx-size";
117 : 734 : return false;
118 : : }
119 : :
120 [ + + ]: 2925257 : for (const CTxIn& txin : tx.vin)
121 : : {
122 : : // Biggest 'standard' txin involving only keys is a 15-of-15 P2SH
123 : : // multisig with compressed keys (remember the MAX_SCRIPT_ELEMENT_SIZE byte limit on
124 : : // redeemScript size). That works out to a (15*(33+1))+3=513 byte
125 : : // redeemScript, 513+1+15*(73+1)+3=1627 bytes of scriptSig, which
126 : : // we round off to 1650(MAX_STANDARD_SCRIPTSIG_SIZE) bytes for
127 : : // some minor future-proofing. That's also enough to spend a
128 : : // 20-of-20 CHECKMULTISIG scriptPubKey, though such a scriptPubKey
129 : : // is not considered standard.
130 [ + + + + ]: 2528596 : if (txin.scriptSig.size() > MAX_STANDARD_SCRIPTSIG_SIZE) {
131 : 100 : reason = "scriptsig-size";
132 : 100 : return false;
133 : : }
134 [ + + ]: 2528496 : if (!txin.scriptSig.IsPushOnly()) {
135 : 3999 : reason = "scriptsig-not-pushonly";
136 : 3999 : return false;
137 : : }
138 : : }
139 : :
140 : 396661 : unsigned int nDataOut = 0;
141 : 396661 : TxoutType whichType;
142 [ + + ]: 4567443 : for (const CTxOut& txout : tx.vout) {
143 [ + + ]: 4194100 : if (!::IsStandard(txout.scriptPubKey, max_datacarrier_bytes, whichType)) {
144 : 23106 : reason = "scriptpubkey";
145 : 23106 : return false;
146 : : }
147 : :
148 [ + + ]: 4170994 : if (whichType == TxoutType::NULL_DATA)
149 : 6054 : nDataOut++;
150 [ + + + + ]: 4164940 : else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) {
151 : 212 : reason = "bare-multisig";
152 : 212 : return false;
153 : : }
154 : : }
155 : :
156 : : // Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust)
157 [ + + ]: 373343 : if (GetDust(tx, dust_relay_fee).size() > MAX_DUST_OUTPUTS_PER_TX) {
158 : 749 : reason = "dust";
159 : 749 : return false;
160 : : }
161 : :
162 : : // only one OP_RETURN txout is permitted
163 [ + + ]: 372594 : if (nDataOut > 1) {
164 : 116 : reason = "multi-op-return";
165 : 116 : return false;
166 : : }
167 : :
168 : : return true;
169 : : }
170 : :
171 : : /**
172 : : * Check transaction inputs to mitigate two
173 : : * potential denial-of-service attacks:
174 : : *
175 : : * 1. scriptSigs with extra data stuffed into them,
176 : : * not consumed by scriptPubKey (or P2SH script)
177 : : * 2. P2SH scripts with a crazy number of expensive
178 : : * CHECKSIG/CHECKMULTISIG operations
179 : : *
180 : : * Why bother? To avoid denial-of-service attacks; an attacker
181 : : * can submit a standard HASH... OP_EQUAL transaction,
182 : : * which will get accepted into blocks. The redemption
183 : : * script can be anything; an attacker could use a very
184 : : * expensive-to-check-upon-redemption script like:
185 : : * DUP CHECKSIG DROP ... repeated 100 times... OP_1
186 : : *
187 : : * Note that only the non-witness portion of the transaction is checked here.
188 : : */
189 : 205491 : bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
190 : : {
191 [ + + ]: 205491 : if (tx.IsCoinBase()) {
192 : : return true; // Coinbases don't use vin normally
193 : : }
194 : :
195 [ + + ]: 1234688 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
196 : 1031111 : const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
197 : :
198 : 1031111 : std::vector<std::vector<unsigned char> > vSolutions;
199 [ + - ]: 1031111 : TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
200 [ + + ]: 1031111 : if (whichType == TxoutType::NONSTANDARD || whichType == TxoutType::WITNESS_UNKNOWN) {
201 : : // WITNESS_UNKNOWN failures are typically also caught with a policy
202 : : // flag in the script interpreter, but it can be helpful to catch
203 : : // this type of NONSTANDARD transaction earlier in transaction
204 : : // validation.
205 : : return false;
206 [ + + ]: 1029746 : } else if (whichType == TxoutType::SCRIPTHASH) {
207 : 1347 : std::vector<std::vector<unsigned char> > stack;
208 : : // convert the scriptSig into a stack, so we can inspect the redeemScript
209 [ + - + + ]: 1347 : if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
210 : : return false;
211 [ + + ]: 833 : if (stack.empty())
212 : : return false;
213 : 830 : CScript subscript(stack.back().begin(), stack.back().end());
214 [ + - + + ]: 830 : if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
215 : 17 : return false;
216 : : }
217 : 1347 : }
218 : 1031111 : }
219 : :
220 : : return true;
221 : : }
222 : :
223 : 202850 : bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
224 : : {
225 [ + + ]: 202850 : if (tx.IsCoinBase())
226 : : return true; // Coinbases are skipped
227 : :
228 [ + + ]: 1296360 : for (unsigned int i = 0; i < tx.vin.size(); i++)
229 : : {
230 : : // We don't care if witness for this input is empty, since it must not be bloated.
231 : : // If the script is invalid without witness, it would be caught sooner or later during validation.
232 [ + + ]: 1094756 : if (tx.vin[i].scriptWitness.IsNull())
233 : 70513 : continue;
234 : :
235 : 1024243 : const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
236 : :
237 : : // get the scriptPubKey corresponding to this input:
238 : 1024243 : CScript prevScript = prev.scriptPubKey;
239 : :
240 : : // witness stuffing detected
241 [ + - + + ]: 1024243 : if (prevScript.IsPayToAnchor()) {
242 : : return false;
243 : : }
244 : :
245 : 1024242 : bool p2sh = false;
246 [ + - + + ]: 1024242 : if (prevScript.IsPayToScriptHash()) {
247 : 30 : std::vector <std::vector<unsigned char> > stack;
248 : : // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
249 : : // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
250 : : // If the check fails at this stage, we know that this txid must be a bad one.
251 [ + - + + ]: 30 : if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
252 : : return false;
253 [ + + ]: 19 : if (stack.empty())
254 : : return false;
255 : 18 : prevScript = CScript(stack.back().begin(), stack.back().end());
256 : 18 : p2sh = true;
257 : 30 : }
258 : :
259 : 1024230 : int witnessversion = 0;
260 : 1024230 : std::vector<unsigned char> witnessprogram;
261 : :
262 : : // Non-witness program must not be associated with any witness
263 [ + - + + ]: 1024230 : if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
264 : : return false;
265 : :
266 : : // Check P2WSH standard limits
267 [ + + + + ]: 1023474 : if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
268 [ + + ]: 1021668 : if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
269 : : return false;
270 [ + + ]: 1021639 : size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
271 [ + + ]: 1021639 : if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
272 : : return false;
273 [ + + ]: 1323640 : for (unsigned int j = 0; j < sizeWitnessStack; j++) {
274 [ + + ]: 302425 : if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
275 : : return false;
276 : : }
277 : : }
278 : :
279 : : // Check policy limits for Taproot spends:
280 : : // - MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE limit for stack item size
281 : : // - No annexes
282 [ + + + + : 1023021 : if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE && !p2sh) {
+ - ]
283 : : // Taproot spend (non-P2SH-wrapped, version 1, witness program size 32; see BIP 341)
284 [ + + ]: 842 : Span stack{tx.vin[i].scriptWitness.stack};
285 [ + + + + : 842 : if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) {
+ + ]
286 : : // Annexes are nonstandard as long as no semantics are defined for them.
287 : : return false;
288 : : }
289 [ + + ]: 840 : if (stack.size() >= 2) {
290 : : // Script path spend (2 or more stack elements after removing optional annex)
291 : 289 : const auto& control_block = SpanPopBack(stack);
292 : 289 : SpanPopBack(stack); // Ignore script
293 [ + + ]: 289 : if (control_block.empty()) return false; // Empty control block is invalid
294 [ + + ]: 288 : if ((control_block[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) {
295 : : // Leaf version 0xc0 (aka Tapscript, see BIP 342)
296 [ + + ]: 366 : for (const auto& item : stack) {
297 [ + + ]: 338 : if (item.size() > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE) return false;
298 : : }
299 : : }
300 [ + - ]: 551 : } else if (stack.size() == 1) {
301 : : // Key path spend (1 stack element after removing optional annex)
302 : : // (no policy rules apply)
303 : : } else {
304 : : // 0 stack elements; this is already invalid by consensus rules
305 : : return false;
306 : : }
307 : : }
308 : 1025461 : }
309 : : return true;
310 : : }
311 : :
312 : 650690845 : int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
313 : : {
314 [ + + ]: 650690845 : return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
315 : : }
316 : :
317 : 122870 : int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop)
318 : : {
319 : 122870 : return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost, bytes_per_sigop);
320 : : }
321 : :
322 : 66 : int64_t GetVirtualTransactionInputSize(const CTxIn& txin, int64_t nSigOpCost, unsigned int bytes_per_sigop)
323 : : {
324 : 66 : return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop);
325 : : }
|