/root/bitcoin/src/policy/policy.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present 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 | | CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn) |
27 | 4.41M | { |
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 | 4.41M | if (txout.scriptPubKey.IsUnspendable()) |
43 | 0 | return 0; |
44 | | |
45 | 4.41M | size_t nSize = GetSerializeSize(txout); |
46 | 4.41M | int witnessversion = 0; |
47 | 4.41M | 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 | 4.41M | 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 | 4.41M | nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4); |
58 | 4.41M | } else { |
59 | 0 | nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above |
60 | 0 | } |
61 | | |
62 | 4.41M | return dustRelayFeeIn.GetFee(nSize); |
63 | 4.41M | } |
64 | | |
65 | | bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn) |
66 | 4.41M | { |
67 | 4.41M | return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn)); |
68 | 4.41M | } |
69 | | |
70 | | std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate) |
71 | 69.7k | { |
72 | 69.7k | std::vector<uint32_t> dust_outputs; |
73 | 4.27M | for (uint32_t i{0}; i < tx.vout.size(); ++i) { |
74 | 4.20M | if (IsDust(tx.vout[i], dust_relay_rate)) dust_outputs.push_back(i); |
75 | 4.20M | } |
76 | 69.7k | return dust_outputs; |
77 | 69.7k | } |
78 | | |
79 | | bool IsStandard(const CScript& scriptPubKey, TxoutType& whichType) |
80 | 2.86M | { |
81 | 2.86M | std::vector<std::vector<unsigned char> > vSolutions; |
82 | 2.86M | whichType = Solver(scriptPubKey, vSolutions); |
83 | | |
84 | 2.86M | if (whichType == TxoutType::NONSTANDARD) { |
85 | 0 | return false; |
86 | 2.86M | } else if (whichType == TxoutType::MULTISIG) { |
87 | 0 | unsigned char m = vSolutions.front()[0]; |
88 | 0 | unsigned char n = vSolutions.back()[0]; |
89 | | // Support up to x-of-3 multisig txns as standard |
90 | 0 | if (n < 1 || n > 3) |
91 | 0 | return false; |
92 | 0 | if (m < 1 || m > n) |
93 | 0 | return false; |
94 | 0 | } |
95 | | |
96 | 2.86M | return true; |
97 | 2.86M | } |
98 | | |
99 | | bool IsStandardTx(const CTransaction& tx, const std::optional<unsigned>& max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate& dust_relay_fee, std::string& reason) |
100 | 43.7k | { |
101 | 43.7k | if (tx.version > TX_MAX_STANDARD_VERSION || tx.version < TX_MIN_STANDARD_VERSION) { |
102 | 0 | reason = "version"; |
103 | 0 | return false; |
104 | 0 | } |
105 | | |
106 | | // Extremely large transactions with lots of inputs can cost the network |
107 | | // almost as much to process as they cost the sender in fees, because |
108 | | // computing signature hashes is O(ninputs*txsize). Limiting transactions |
109 | | // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks. |
110 | 43.7k | unsigned int sz = GetTransactionWeight(tx); |
111 | 43.7k | if (sz > MAX_STANDARD_TX_WEIGHT) { |
112 | 294 | reason = "tx-size"; |
113 | 294 | return false; |
114 | 294 | } |
115 | | |
116 | 43.4k | for (const CTxIn& txin : tx.vin) |
117 | 960k | { |
118 | | // Biggest 'standard' txin involving only keys is a 15-of-15 P2SH |
119 | | // multisig with compressed keys (remember the MAX_SCRIPT_ELEMENT_SIZE byte limit on |
120 | | // redeemScript size). That works out to a (15*(33+1))+3=513 byte |
121 | | // redeemScript, 513+1+15*(73+1)+3=1627 bytes of scriptSig, which |
122 | | // we round off to 1650(MAX_STANDARD_SCRIPTSIG_SIZE) bytes for |
123 | | // some minor future-proofing. That's also enough to spend a |
124 | | // 20-of-20 CHECKMULTISIG scriptPubKey, though such a scriptPubKey |
125 | | // is not considered standard. |
126 | 960k | if (txin.scriptSig.size() > MAX_STANDARD_SCRIPTSIG_SIZE) { |
127 | 0 | reason = "scriptsig-size"; |
128 | 0 | return false; |
129 | 0 | } |
130 | 960k | if (!txin.scriptSig.IsPushOnly()) { |
131 | 0 | reason = "scriptsig-not-pushonly"; |
132 | 0 | return false; |
133 | 0 | } |
134 | 960k | } |
135 | | |
136 | 43.4k | unsigned int datacarrier_bytes_left = max_datacarrier_bytes.value_or(0); |
137 | 43.4k | TxoutType whichType; |
138 | 2.86M | for (const CTxOut& txout : tx.vout) { |
139 | 2.86M | if (!::IsStandard(txout.scriptPubKey, whichType)) { |
140 | 0 | reason = "scriptpubkey"; |
141 | 0 | return false; |
142 | 0 | } |
143 | | |
144 | 2.86M | if (whichType == TxoutType::NULL_DATA) { |
145 | 0 | unsigned int size = txout.scriptPubKey.size(); |
146 | 0 | if (size > datacarrier_bytes_left) { |
147 | 0 | reason = "datacarrier"; |
148 | 0 | return false; |
149 | 0 | } |
150 | 0 | datacarrier_bytes_left -= size; |
151 | 2.86M | } else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) { |
152 | 0 | reason = "bare-multisig"; |
153 | 0 | return false; |
154 | 0 | } |
155 | 2.86M | } |
156 | | |
157 | | // Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust) |
158 | 43.4k | if (GetDust(tx, dust_relay_fee).size() > MAX_DUST_OUTPUTS_PER_TX) { |
159 | 350 | reason = "dust"; |
160 | 350 | return false; |
161 | 350 | } |
162 | | |
163 | 43.0k | return true; |
164 | 43.4k | } |
165 | | |
166 | | /** |
167 | | * Check the total number of non-witness sigops across the whole transaction, as per BIP54. |
168 | | */ |
169 | | static bool CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inputs) |
170 | 26.4k | { |
171 | 26.4k | Assert(!tx.IsCoinBase()); |
172 | | |
173 | 26.4k | unsigned int sigops{0}; |
174 | 402k | for (const auto& txin: tx.vin) { |
175 | 402k | const auto& prev_txo{inputs.AccessCoin(txin.prevout).out}; |
176 | | |
177 | | // Unlike the existing block wide sigop limit which counts sigops present in the block |
178 | | // itself (including the scriptPubKey which is not executed until spending later), BIP54 |
179 | | // counts sigops in the block where they are potentially executed (only). |
180 | | // This means sigops in the spent scriptPubKey count toward the limit. |
181 | | // `fAccurate` means correctly accounting sigops for CHECKMULTISIGs(VERIFY) with 16 pubkeys |
182 | | // or fewer. This method of accounting was introduced by BIP16, and BIP54 reuses it. |
183 | | // The GetSigOpCount call on the previous scriptPubKey counts both bare and P2SH sigops. |
184 | 402k | sigops += txin.scriptSig.GetSigOpCount(/*fAccurate=*/true); |
185 | 402k | sigops += prev_txo.scriptPubKey.GetSigOpCount(txin.scriptSig); |
186 | | |
187 | 402k | if (sigops > MAX_TX_LEGACY_SIGOPS) { |
188 | 0 | return false; |
189 | 0 | } |
190 | 402k | } |
191 | | |
192 | 26.4k | return true; |
193 | 26.4k | } |
194 | | |
195 | | /** |
196 | | * Check transaction inputs. |
197 | | * |
198 | | * This does three things: |
199 | | * * Prevents mempool acceptance of spends of future |
200 | | * segwit versions we don't know how to validate |
201 | | * * Mitigates a potential denial-of-service attack with |
202 | | * P2SH scripts with a crazy number of expensive |
203 | | * CHECKSIG/CHECKMULTISIG operations. |
204 | | * * Prevents spends of unknown/irregular scriptPubKeys, |
205 | | * which mitigates potential denial-of-service attacks |
206 | | * involving expensive scripts and helps reserve them |
207 | | * as potential new upgrade hooks. |
208 | | * |
209 | | * Note that only the non-witness portion of the transaction is checked here. |
210 | | * |
211 | | * We also check the total number of non-witness sigops across the whole transaction, as per BIP54. |
212 | | */ |
213 | | bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) |
214 | 26.4k | { |
215 | 26.4k | if (tx.IsCoinBase()) { |
216 | 0 | return true; // Coinbases don't use vin normally |
217 | 0 | } |
218 | | |
219 | 26.4k | if (!CheckSigopsBIP54(tx, mapInputs)) { |
220 | 0 | return false; |
221 | 0 | } |
222 | | |
223 | 428k | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
224 | 402k | const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out; |
225 | | |
226 | 402k | std::vector<std::vector<unsigned char> > vSolutions; |
227 | 402k | TxoutType whichType = Solver(prev.scriptPubKey, vSolutions); |
228 | 402k | if (whichType == TxoutType::NONSTANDARD || whichType == TxoutType::WITNESS_UNKNOWN) { |
229 | | // WITNESS_UNKNOWN failures are typically also caught with a policy |
230 | | // flag in the script interpreter, but it can be helpful to catch |
231 | | // this type of NONSTANDARD transaction earlier in transaction |
232 | | // validation. |
233 | 0 | return false; |
234 | 402k | } else if (whichType == TxoutType::SCRIPTHASH) { |
235 | 0 | std::vector<std::vector<unsigned char> > stack; |
236 | | // convert the scriptSig into a stack, so we can inspect the redeemScript |
237 | 0 | if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE)) |
238 | 0 | return false; |
239 | 0 | if (stack.empty()) |
240 | 0 | return false; |
241 | 0 | CScript subscript(stack.back().begin(), stack.back().end()); |
242 | 0 | if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) { |
243 | 0 | return false; |
244 | 0 | } |
245 | 0 | } |
246 | 402k | } |
247 | | |
248 | 26.4k | return true; |
249 | 26.4k | } |
250 | | |
251 | | bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) |
252 | 26.4k | { |
253 | 26.4k | if (tx.IsCoinBase()) |
254 | 0 | return true; // Coinbases are skipped |
255 | | |
256 | 428k | for (unsigned int i = 0; i < tx.vin.size(); i++) |
257 | 402k | { |
258 | | // We don't care if witness for this input is empty, since it must not be bloated. |
259 | | // If the script is invalid without witness, it would be caught sooner or later during validation. |
260 | 402k | if (tx.vin[i].scriptWitness.IsNull()) |
261 | 0 | continue; |
262 | | |
263 | 402k | const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out; |
264 | | |
265 | | // get the scriptPubKey corresponding to this input: |
266 | 402k | CScript prevScript = prev.scriptPubKey; |
267 | | |
268 | | // witness stuffing detected |
269 | 402k | if (prevScript.IsPayToAnchor()) { |
270 | 0 | return false; |
271 | 0 | } |
272 | | |
273 | 402k | bool p2sh = false; |
274 | 402k | if (prevScript.IsPayToScriptHash()) { |
275 | 0 | std::vector <std::vector<unsigned char> > stack; |
276 | | // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig |
277 | | // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway. |
278 | | // If the check fails at this stage, we know that this txid must be a bad one. |
279 | 0 | if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE)) |
280 | 0 | return false; |
281 | 0 | if (stack.empty()) |
282 | 0 | return false; |
283 | 0 | prevScript = CScript(stack.back().begin(), stack.back().end()); |
284 | 0 | p2sh = true; |
285 | 0 | } |
286 | | |
287 | 402k | int witnessversion = 0; |
288 | 402k | std::vector<unsigned char> witnessprogram; |
289 | | |
290 | | // Non-witness program must not be associated with any witness |
291 | 402k | if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram)) |
292 | 0 | return false; |
293 | | |
294 | | // Check P2WSH standard limits |
295 | 402k | if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) { |
296 | 402k | if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE) |
297 | 0 | return false; |
298 | 402k | size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1; |
299 | 402k | if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS) |
300 | 0 | return false; |
301 | 402k | for (unsigned int j = 0; j < sizeWitnessStack; j++) { |
302 | 0 | if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE) |
303 | 0 | return false; |
304 | 0 | } |
305 | 402k | } |
306 | | |
307 | | // Check policy limits for Taproot spends: |
308 | | // - MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE limit for stack item size |
309 | | // - No annexes |
310 | 402k | if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE && !p2sh) { |
311 | | // Taproot spend (non-P2SH-wrapped, version 1, witness program size 32; see BIP 341) |
312 | 0 | std::span stack{tx.vin[i].scriptWitness.stack}; |
313 | 0 | if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) { |
314 | | // Annexes are nonstandard as long as no semantics are defined for them. |
315 | 0 | return false; |
316 | 0 | } |
317 | 0 | if (stack.size() >= 2) { |
318 | | // Script path spend (2 or more stack elements after removing optional annex) |
319 | 0 | const auto& control_block = SpanPopBack(stack); |
320 | 0 | SpanPopBack(stack); // Ignore script |
321 | 0 | if (control_block.empty()) return false; // Empty control block is invalid |
322 | 0 | if ((control_block[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) { |
323 | | // Leaf version 0xc0 (aka Tapscript, see BIP 342) |
324 | 0 | for (const auto& item : stack) { |
325 | 0 | if (item.size() > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE) return false; |
326 | 0 | } |
327 | 0 | } |
328 | 0 | } else if (stack.size() == 1) { |
329 | | // Key path spend (1 stack element after removing optional annex) |
330 | | // (no policy rules apply) |
331 | 0 | } else { |
332 | | // 0 stack elements; this is already invalid by consensus rules |
333 | 0 | return false; |
334 | 0 | } |
335 | 0 | } |
336 | 402k | } |
337 | 26.4k | return true; |
338 | 26.4k | } |
339 | | |
340 | | bool SpendsNonAnchorWitnessProg(const CTransaction& tx, const CCoinsViewCache& prevouts) |
341 | 0 | { |
342 | 0 | if (tx.IsCoinBase()) { |
343 | 0 | return false; |
344 | 0 | } |
345 | | |
346 | 0 | int version; |
347 | 0 | std::vector<uint8_t> program; |
348 | 0 | for (const auto& txin: tx.vin) { |
349 | 0 | const auto& prev_spk{prevouts.AccessCoin(txin.prevout).out.scriptPubKey}; |
350 | | |
351 | | // Note this includes not-yet-defined witness programs. |
352 | 0 | if (prev_spk.IsWitnessProgram(version, program) && !prev_spk.IsPayToAnchor(version, program)) { |
353 | 0 | return true; |
354 | 0 | } |
355 | | |
356 | | // For P2SH extract the redeem script and check if it spends a non-Taproot witness program. Note |
357 | | // this is fine to call EvalScript (as done in AreInputsStandard/IsWitnessStandard) because this |
358 | | // function is only ever called after IsStandardTx, which checks the scriptsig is pushonly. |
359 | 0 | if (prev_spk.IsPayToScriptHash()) { |
360 | | // If EvalScript fails or results in an empty stack, the transaction is invalid by consensus. |
361 | 0 | std::vector <std::vector<uint8_t>> stack; |
362 | 0 | if (!EvalScript(stack, txin.scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker{}, SigVersion::BASE) |
363 | 0 | || stack.empty()) { |
364 | 0 | continue; |
365 | 0 | } |
366 | 0 | const CScript redeem_script{stack.back().begin(), stack.back().end()}; |
367 | 0 | if (redeem_script.IsWitnessProgram(version, program)) { |
368 | 0 | return true; |
369 | 0 | } |
370 | 0 | } |
371 | 0 | } |
372 | | |
373 | 0 | return false; |
374 | 0 | } |
375 | | |
376 | | int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop) |
377 | 1.54M | { |
378 | 1.54M | return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR; |
379 | 1.54M | } |
380 | | |
381 | | int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop) |
382 | 0 | { |
383 | 0 | return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost, bytes_per_sigop); |
384 | 0 | } |
385 | | |
386 | | int64_t GetVirtualTransactionInputSize(const CTxIn& txin, int64_t nSigOpCost, unsigned int bytes_per_sigop) |
387 | 0 | { |
388 | 0 | return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop); |
389 | 0 | } |