/root/bitcoin/src/policy/policy.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
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 | 0 | if (txout.scriptPubKey.IsUnspendable()) |
43 | 0 | return 0; |
44 | | |
45 | 0 | size_t nSize = GetSerializeSize(txout); |
46 | 0 | int witnessversion = 0; |
47 | 0 | 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 | 0 | 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 | 0 | nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4); |
58 | 0 | } else { |
59 | 0 | nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above |
60 | 0 | } |
61 | |
|
62 | 0 | return dustRelayFeeIn.GetFee(nSize); |
63 | 0 | } |
64 | | |
65 | | bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn) |
66 | 0 | { |
67 | 0 | return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn)); |
68 | 0 | } |
69 | | |
70 | | std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate) |
71 | 0 | { |
72 | 0 | std::vector<uint32_t> dust_outputs; |
73 | 0 | for (uint32_t i{0}; i < tx.vout.size(); ++i) { |
74 | 0 | if (IsDust(tx.vout[i], dust_relay_rate)) dust_outputs.push_back(i); |
75 | 0 | } |
76 | 0 | return dust_outputs; |
77 | 0 | } |
78 | | |
79 | | bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType) |
80 | 0 | { |
81 | 0 | std::vector<std::vector<unsigned char> > vSolutions; |
82 | 0 | whichType = Solver(scriptPubKey, vSolutions); |
83 | |
|
84 | 0 | if (whichType == TxoutType::NONSTANDARD) { |
85 | 0 | return false; |
86 | 0 | } 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 | } else if (whichType == TxoutType::NULL_DATA) { |
95 | 0 | if (!max_datacarrier_bytes || scriptPubKey.size() > *max_datacarrier_bytes) { |
96 | 0 | return false; |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | 0 | return true; |
101 | 0 | } |
102 | | |
103 | | 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 | 0 | { |
105 | 0 | if (tx.version > TX_MAX_STANDARD_VERSION || tx.version < 1) { |
106 | 0 | reason = "version"; |
107 | 0 | return false; |
108 | 0 | } |
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 | 0 | unsigned int sz = GetTransactionWeight(tx); |
115 | 0 | if (sz > MAX_STANDARD_TX_WEIGHT) { |
116 | 0 | reason = "tx-size"; |
117 | 0 | return false; |
118 | 0 | } |
119 | | |
120 | 0 | for (const CTxIn& txin : tx.vin) |
121 | 0 | { |
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 | 0 | if (txin.scriptSig.size() > MAX_STANDARD_SCRIPTSIG_SIZE) { |
131 | 0 | reason = "scriptsig-size"; |
132 | 0 | return false; |
133 | 0 | } |
134 | 0 | if (!txin.scriptSig.IsPushOnly()) { |
135 | 0 | reason = "scriptsig-not-pushonly"; |
136 | 0 | return false; |
137 | 0 | } |
138 | 0 | } |
139 | | |
140 | 0 | unsigned int nDataOut = 0; |
141 | 0 | TxoutType whichType; |
142 | 0 | for (const CTxOut& txout : tx.vout) { |
143 | 0 | if (!::IsStandard(txout.scriptPubKey, max_datacarrier_bytes, whichType)) { |
144 | 0 | reason = "scriptpubkey"; |
145 | 0 | return false; |
146 | 0 | } |
147 | | |
148 | 0 | if (whichType == TxoutType::NULL_DATA) |
149 | 0 | nDataOut++; |
150 | 0 | else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) { |
151 | 0 | reason = "bare-multisig"; |
152 | 0 | return false; |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | | // Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust) |
157 | 0 | if (GetDust(tx, dust_relay_fee).size() > MAX_DUST_OUTPUTS_PER_TX) { |
158 | 0 | reason = "dust"; |
159 | 0 | return false; |
160 | 0 | } |
161 | | |
162 | | // only one OP_RETURN txout is permitted |
163 | 0 | if (nDataOut > 1) { |
164 | 0 | reason = "multi-op-return"; |
165 | 0 | return false; |
166 | 0 | } |
167 | | |
168 | 0 | return true; |
169 | 0 | } |
170 | | |
171 | | /** |
172 | | * Check transaction inputs. |
173 | | * |
174 | | * This does three things: |
175 | | * * Prevents mempool acceptance of spends of future |
176 | | * segwit versions we don't know how to validate |
177 | | * * Mitigates a potential denial-of-service attack with |
178 | | * P2SH scripts with a crazy number of expensive |
179 | | * CHECKSIG/CHECKMULTISIG operations. |
180 | | * * Prevents spends of unknown/irregular scriptPubKeys, |
181 | | * which mitigates potential denial-of-service attacks |
182 | | * involving expensive scripts and helps reserve them |
183 | | * as potential new upgrade hooks. |
184 | | * |
185 | | * Note that only the non-witness portion of the transaction is checked here. |
186 | | */ |
187 | | bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) |
188 | 0 | { |
189 | 0 | if (tx.IsCoinBase()) { |
190 | 0 | return true; // Coinbases don't use vin normally |
191 | 0 | } |
192 | | |
193 | 0 | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
194 | 0 | const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out; |
195 | |
|
196 | 0 | std::vector<std::vector<unsigned char> > vSolutions; |
197 | 0 | TxoutType whichType = Solver(prev.scriptPubKey, vSolutions); |
198 | 0 | if (whichType == TxoutType::NONSTANDARD || whichType == TxoutType::WITNESS_UNKNOWN) { |
199 | | // WITNESS_UNKNOWN failures are typically also caught with a policy |
200 | | // flag in the script interpreter, but it can be helpful to catch |
201 | | // this type of NONSTANDARD transaction earlier in transaction |
202 | | // validation. |
203 | 0 | return false; |
204 | 0 | } else if (whichType == TxoutType::SCRIPTHASH) { |
205 | 0 | std::vector<std::vector<unsigned char> > stack; |
206 | | // convert the scriptSig into a stack, so we can inspect the redeemScript |
207 | 0 | if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE)) |
208 | 0 | return false; |
209 | 0 | if (stack.empty()) |
210 | 0 | return false; |
211 | 0 | CScript subscript(stack.back().begin(), stack.back().end()); |
212 | 0 | if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) { |
213 | 0 | return false; |
214 | 0 | } |
215 | 0 | } |
216 | 0 | } |
217 | | |
218 | 0 | return true; |
219 | 0 | } |
220 | | |
221 | | bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) |
222 | 0 | { |
223 | 0 | if (tx.IsCoinBase()) |
224 | 0 | return true; // Coinbases are skipped |
225 | | |
226 | 0 | for (unsigned int i = 0; i < tx.vin.size(); i++) |
227 | 0 | { |
228 | | // We don't care if witness for this input is empty, since it must not be bloated. |
229 | | // If the script is invalid without witness, it would be caught sooner or later during validation. |
230 | 0 | if (tx.vin[i].scriptWitness.IsNull()) |
231 | 0 | continue; |
232 | | |
233 | 0 | const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out; |
234 | | |
235 | | // get the scriptPubKey corresponding to this input: |
236 | 0 | CScript prevScript = prev.scriptPubKey; |
237 | | |
238 | | // witness stuffing detected |
239 | 0 | if (prevScript.IsPayToAnchor()) { |
240 | 0 | return false; |
241 | 0 | } |
242 | | |
243 | 0 | bool p2sh = false; |
244 | 0 | if (prevScript.IsPayToScriptHash()) { |
245 | 0 | std::vector <std::vector<unsigned char> > stack; |
246 | | // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig |
247 | | // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway. |
248 | | // If the check fails at this stage, we know that this txid must be a bad one. |
249 | 0 | if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE)) |
250 | 0 | return false; |
251 | 0 | if (stack.empty()) |
252 | 0 | return false; |
253 | 0 | prevScript = CScript(stack.back().begin(), stack.back().end()); |
254 | 0 | p2sh = true; |
255 | 0 | } |
256 | | |
257 | 0 | int witnessversion = 0; |
258 | 0 | std::vector<unsigned char> witnessprogram; |
259 | | |
260 | | // Non-witness program must not be associated with any witness |
261 | 0 | if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram)) |
262 | 0 | return false; |
263 | | |
264 | | // Check P2WSH standard limits |
265 | 0 | if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) { |
266 | 0 | if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE) |
267 | 0 | return false; |
268 | 0 | size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1; |
269 | 0 | if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS) |
270 | 0 | return false; |
271 | 0 | for (unsigned int j = 0; j < sizeWitnessStack; j++) { |
272 | 0 | if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE) |
273 | 0 | return false; |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | // Check policy limits for Taproot spends: |
278 | | // - MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE limit for stack item size |
279 | | // - No annexes |
280 | 0 | if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE && !p2sh) { |
281 | | // Taproot spend (non-P2SH-wrapped, version 1, witness program size 32; see BIP 341) |
282 | 0 | std::span stack{tx.vin[i].scriptWitness.stack}; |
283 | 0 | if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) { |
284 | | // Annexes are nonstandard as long as no semantics are defined for them. |
285 | 0 | return false; |
286 | 0 | } |
287 | 0 | if (stack.size() >= 2) { |
288 | | // Script path spend (2 or more stack elements after removing optional annex) |
289 | 0 | const auto& control_block = SpanPopBack(stack); |
290 | 0 | SpanPopBack(stack); // Ignore script |
291 | 0 | if (control_block.empty()) return false; // Empty control block is invalid |
292 | 0 | if ((control_block[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) { |
293 | | // Leaf version 0xc0 (aka Tapscript, see BIP 342) |
294 | 0 | for (const auto& item : stack) { |
295 | 0 | if (item.size() > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE) return false; |
296 | 0 | } |
297 | 0 | } |
298 | 0 | } else if (stack.size() == 1) { |
299 | | // Key path spend (1 stack element after removing optional annex) |
300 | | // (no policy rules apply) |
301 | 0 | } else { |
302 | | // 0 stack elements; this is already invalid by consensus rules |
303 | 0 | return false; |
304 | 0 | } |
305 | 0 | } |
306 | 0 | } |
307 | 0 | return true; |
308 | 0 | } |
309 | | |
310 | | int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop) |
311 | 0 | { |
312 | 0 | return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR; |
313 | 0 | } |
314 | | |
315 | | int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop) |
316 | 0 | { |
317 | 0 | return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost, bytes_per_sigop); |
318 | 0 | } |
319 | | |
320 | | int64_t GetVirtualTransactionInputSize(const CTxIn& txin, int64_t nSigOpCost, unsigned int bytes_per_sigop) |
321 | 0 | { |
322 | 0 | return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop); |
323 | 0 | } |