/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 | 562k | { |
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 | 562k | if (txout.scriptPubKey.IsUnspendable()) Branch (42:9): [True: 52.9k, False: 510k]
|
43 | 52.9k | return 0; |
44 | | |
45 | 510k | size_t nSize = GetSerializeSize(txout); |
46 | 510k | int witnessversion = 0; |
47 | 510k | 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 | 510k | if (txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) { Branch (54:9): [True: 488k, False: 21.8k]
|
55 | | // sum the sizes of the parts of a transaction input |
56 | | // with 75% segwit discount applied to the script size. |
57 | 488k | nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4); |
58 | 488k | } else { |
59 | 21.8k | nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above |
60 | 21.8k | } |
61 | | |
62 | 510k | return dustRelayFeeIn.GetFee(nSize); |
63 | 562k | } |
64 | | |
65 | | bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn) |
66 | 562k | { |
67 | 562k | return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn)); |
68 | 562k | } |
69 | | |
70 | | std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate) |
71 | 452k | { |
72 | 452k | std::vector<uint32_t> dust_outputs; |
73 | 975k | for (uint32_t i{0}; i < tx.vout.size(); ++i) { Branch (73:25): [True: 522k, False: 452k]
|
74 | 522k | if (IsDust(tx.vout[i], dust_relay_rate)) dust_outputs.push_back(i); Branch (74:13): [True: 74.1k, False: 448k]
|
75 | 522k | } |
76 | 452k | return dust_outputs; |
77 | 452k | } |
78 | | |
79 | | bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType) |
80 | 446k | { |
81 | 446k | std::vector<std::vector<unsigned char> > vSolutions; |
82 | 446k | whichType = Solver(scriptPubKey, vSolutions); |
83 | | |
84 | 446k | if (whichType == TxoutType::NONSTANDARD) { Branch (84:9): [True: 203, False: 446k]
|
85 | 203 | return false; |
86 | 446k | } else if (whichType == TxoutType::MULTISIG) { Branch (86:16): [True: 0, False: 446k]
|
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) Branch (90:13): [True: 0, False: 0]
Branch (90:22): [True: 0, False: 0]
|
91 | 0 | return false; |
92 | 0 | if (m < 1 || m > n) Branch (92:13): [True: 0, False: 0]
Branch (92:22): [True: 0, False: 0]
|
93 | 0 | return false; |
94 | 446k | } else if (whichType == TxoutType::NULL_DATA) { Branch (94:16): [True: 42.7k, False: 403k]
|
95 | 42.7k | if (!max_datacarrier_bytes || scriptPubKey.size() > *max_datacarrier_bytes) { Branch (95:13): [True: 0, False: 42.7k]
Branch (95:39): [True: 0, False: 42.7k]
|
96 | 0 | return false; |
97 | 0 | } |
98 | 42.7k | } |
99 | | |
100 | 446k | return true; |
101 | 446k | } |
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 | 403k | { |
105 | 403k | if (tx.version > TX_MAX_STANDARD_VERSION || tx.version < 1) { Branch (105:9): [True: 4.71k, False: 398k]
Branch (105:49): [True: 1.03k, False: 397k]
|
106 | 5.75k | reason = "version"; |
107 | 5.75k | return false; |
108 | 5.75k | } |
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 | 397k | unsigned int sz = GetTransactionWeight(tx); |
115 | 397k | if (sz > MAX_STANDARD_TX_WEIGHT) { Branch (115:9): [True: 1.44k, False: 395k]
|
116 | 1.44k | reason = "tx-size"; |
117 | 1.44k | return false; |
118 | 1.44k | } |
119 | | |
120 | 395k | for (const CTxIn& txin : tx.vin) Branch (120:28): [True: 477k, False: 395k]
|
121 | 477k | { |
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 | 477k | if (txin.scriptSig.size() > MAX_STANDARD_SCRIPTSIG_SIZE) { Branch (130:13): [True: 20, False: 477k]
|
131 | 20 | reason = "scriptsig-size"; |
132 | 20 | return false; |
133 | 20 | } |
134 | 477k | if (!txin.scriptSig.IsPushOnly()) { Branch (134:13): [True: 46, False: 477k]
|
135 | 46 | reason = "scriptsig-not-pushonly"; |
136 | 46 | return false; |
137 | 46 | } |
138 | 477k | } |
139 | | |
140 | 395k | unsigned int nDataOut = 0; |
141 | 395k | TxoutType whichType; |
142 | 446k | for (const CTxOut& txout : tx.vout) { Branch (142:30): [True: 446k, False: 395k]
|
143 | 446k | if (!::IsStandard(txout.scriptPubKey, max_datacarrier_bytes, whichType)) { Branch (143:13): [True: 203, False: 446k]
|
144 | 203 | reason = "scriptpubkey"; |
145 | 203 | return false; |
146 | 203 | } |
147 | | |
148 | 446k | if (whichType == TxoutType::NULL_DATA) Branch (148:13): [True: 42.7k, False: 403k]
|
149 | 42.7k | nDataOut++; |
150 | 403k | else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) { Branch (150:18): [True: 0, False: 403k]
Branch (150:56): [True: 0, False: 0]
|
151 | 0 | reason = "bare-multisig"; |
152 | 0 | return false; |
153 | 0 | } |
154 | 446k | } |
155 | | |
156 | | // Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust) |
157 | 395k | if (GetDust(tx, dust_relay_fee).size() > MAX_DUST_OUTPUTS_PER_TX) { Branch (157:9): [True: 3.05k, False: 392k]
|
158 | 3.05k | reason = "dust"; |
159 | 3.05k | return false; |
160 | 3.05k | } |
161 | | |
162 | | // only one OP_RETURN txout is permitted |
163 | 392k | if (nDataOut > 1) { Branch (163:9): [True: 100, False: 392k]
|
164 | 100 | reason = "multi-op-return"; |
165 | 100 | return false; |
166 | 100 | } |
167 | | |
168 | 392k | return true; |
169 | 392k | } |
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 | 63.3k | { |
189 | 63.3k | if (tx.IsCoinBase()) { Branch (189:9): [True: 0, False: 63.3k]
|
190 | 0 | return true; // Coinbases don't use vin normally |
191 | 0 | } |
192 | | |
193 | 138k | for (unsigned int i = 0; i < tx.vin.size(); i++) { Branch (193:30): [True: 75.5k, False: 63.3k]
|
194 | 75.5k | const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out; |
195 | | |
196 | 75.5k | std::vector<std::vector<unsigned char> > vSolutions; |
197 | 75.5k | TxoutType whichType = Solver(prev.scriptPubKey, vSolutions); |
198 | 75.5k | if (whichType == TxoutType::NONSTANDARD || whichType == TxoutType::WITNESS_UNKNOWN) { Branch (198:13): [True: 0, False: 75.5k]
Branch (198:52): [True: 0, False: 75.5k]
|
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 | 75.5k | } else if (whichType == TxoutType::SCRIPTHASH) { Branch (204:20): [True: 4.04k, False: 71.5k]
|
205 | 4.04k | std::vector<std::vector<unsigned char> > stack; |
206 | | // convert the scriptSig into a stack, so we can inspect the redeemScript |
207 | 4.04k | if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE)) Branch (207:17): [True: 17, False: 4.02k]
|
208 | 17 | return false; |
209 | 4.02k | if (stack.empty()) Branch (209:17): [True: 0, False: 4.02k]
|
210 | 0 | return false; |
211 | 4.02k | CScript subscript(stack.back().begin(), stack.back().end()); |
212 | 4.02k | if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) { Branch (212:17): [True: 31, False: 3.99k]
|
213 | 31 | return false; |
214 | 31 | } |
215 | 4.02k | } |
216 | 75.5k | } |
217 | | |
218 | 63.3k | return true; |
219 | 63.3k | } |
220 | | |
221 | | bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) |
222 | 53.4k | { |
223 | 53.4k | if (tx.IsCoinBase()) Branch (223:9): [True: 0, False: 53.4k]
|
224 | 0 | return true; // Coinbases are skipped |
225 | | |
226 | 116k | for (unsigned int i = 0; i < tx.vin.size(); i++) Branch (226:30): [True: 63.0k, False: 53.3k]
|
227 | 63.0k | { |
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 | 63.0k | if (tx.vin[i].scriptWitness.IsNull()) Branch (230:13): [True: 2.68k, False: 60.3k]
|
231 | 2.68k | continue; |
232 | | |
233 | 60.3k | const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out; |
234 | | |
235 | | // get the scriptPubKey corresponding to this input: |
236 | 60.3k | CScript prevScript = prev.scriptPubKey; |
237 | | |
238 | | // witness stuffing detected |
239 | 60.3k | if (prevScript.IsPayToAnchor()) { Branch (239:13): [True: 0, False: 60.3k]
|
240 | 0 | return false; |
241 | 0 | } |
242 | | |
243 | 60.3k | bool p2sh = false; |
244 | 60.3k | if (prevScript.IsPayToScriptHash()) { Branch (244:13): [True: 0, False: 60.3k]
|
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)) Branch (249:17): [True: 0, False: 0]
|
250 | 0 | return false; |
251 | 0 | if (stack.empty()) Branch (251:17): [True: 0, False: 0]
|
252 | 0 | return false; |
253 | 0 | prevScript = CScript(stack.back().begin(), stack.back().end()); |
254 | 0 | p2sh = true; |
255 | 0 | } |
256 | | |
257 | 60.3k | int witnessversion = 0; |
258 | 60.3k | std::vector<unsigned char> witnessprogram; |
259 | | |
260 | | // Non-witness program must not be associated with any witness |
261 | 60.3k | if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram)) Branch (261:13): [True: 0, False: 60.3k]
|
262 | 0 | return false; |
263 | | |
264 | | // Check P2WSH standard limits |
265 | 60.3k | if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) { Branch (265:13): [True: 60.3k, False: 0]
Branch (265:36): [True: 60.3k, False: 0]
|
266 | 60.3k | if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE) Branch (266:17): [True: 0, False: 60.3k]
|
267 | 0 | return false; |
268 | 60.3k | size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1; |
269 | 60.3k | if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS) Branch (269:17): [True: 0, False: 60.3k]
|
270 | 0 | return false; |
271 | 61.0k | for (unsigned int j = 0; j < sizeWitnessStack; j++) { Branch (271:38): [True: 684, False: 60.3k]
|
272 | 684 | if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE) Branch (272:21): [True: 30, False: 654]
|
273 | 30 | return false; |
274 | 684 | } |
275 | 60.3k | } |
276 | | |
277 | | // Check policy limits for Taproot spends: |
278 | | // - MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE limit for stack item size |
279 | | // - No annexes |
280 | 60.3k | if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE && !p2sh) { Branch (280:13): [True: 0, False: 60.3k]
Branch (280:36): [True: 0, False: 0]
Branch (280:88): [True: 0, False: 0]
|
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) { Branch (283:17): [True: 0, False: 0]
Branch (283:38): [True: 0, False: 0]
Branch (283:63): [True: 0, False: 0]
|
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) { Branch (287:17): [True: 0, False: 0]
|
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 Branch (291:21): [True: 0, False: 0]
|
292 | 0 | if ((control_block[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) { Branch (292:21): [True: 0, False: 0]
|
293 | | // Leaf version 0xc0 (aka Tapscript, see BIP 342) |
294 | 0 | for (const auto& item : stack) { Branch (294:43): [True: 0, False: 0]
|
295 | 0 | if (item.size() > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE) return false; Branch (295:29): [True: 0, False: 0]
|
296 | 0 | } |
297 | 0 | } |
298 | 0 | } else if (stack.size() == 1) { Branch (298:24): [True: 0, False: 0]
|
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 | 60.3k | } |
307 | 53.3k | return true; |
308 | 53.4k | } |
309 | | |
310 | | int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop) |
311 | 45.6M | { |
312 | 45.6M | return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR; |
313 | 45.6M | } |
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 | } |