Branch data Line data Source code
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 : 411986 : 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 [ + + ]: 411986 : if (txout.scriptPubKey.IsUnspendable())
43 : : return 0;
44 : :
45 : 405483 : size_t nSize = GetSerializeSize(txout);
46 : 405483 : int witnessversion = 0;
47 : 405483 : 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 [ + - + + ]: 405483 : 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 : 373183 : nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
58 : : } else {
59 : 32300 : nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
60 : : }
61 : :
62 [ + - ]: 405483 : return dustRelayFeeIn.GetFee(nSize);
63 : 405483 : }
64 : :
65 : 408644 : bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
66 : : {
67 : 408644 : return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
68 : : }
69 : :
70 : 62036 : std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate)
71 : : {
72 : 62036 : std::vector<uint32_t> dust_outputs;
73 [ + + ]: 285929 : for (uint32_t i{0}; i < tx.vout.size(); ++i) {
74 [ + - + + : 223893 : if (IsDust(tx.vout[i], dust_relay_rate)) dust_outputs.push_back(i);
+ - ]
75 : : }
76 : 62036 : return dust_outputs;
77 : 0 : }
78 : :
79 : 112937 : bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType)
80 : : {
81 : 112937 : std::vector<std::vector<unsigned char> > vSolutions;
82 [ + - ]: 112937 : whichType = Solver(scriptPubKey, vSolutions);
83 : :
84 [ + + ]: 112937 : if (whichType == TxoutType::NONSTANDARD) {
85 : : return false;
86 [ + + ]: 112917 : } else if (whichType == TxoutType::MULTISIG) {
87 [ + + ]: 92 : unsigned char m = vSolutions.front()[0];
88 : 92 : unsigned char n = vSolutions.back()[0];
89 : : // Support up to x-of-3 multisig txns as standard
90 [ + + ]: 92 : if (n < 1 || n > 3)
91 : : return false;
92 [ - + ]: 91 : if (m < 1 || m > n)
93 : 0 : return false;
94 [ + + ]: 112825 : } else if (whichType == TxoutType::NULL_DATA) {
95 [ + + + + : 6428 : if (!max_datacarrier_bytes || scriptPubKey.size() > *max_datacarrier_bytes) {
+ + ]
96 : 22 : return false;
97 : : }
98 : : }
99 : :
100 : : return true;
101 : 112937 : }
102 : :
103 : 33121 : 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 [ + + ]: 33121 : if (tx.version > TX_MAX_STANDARD_VERSION || tx.version < 1) {
106 : 1278 : reason = "version";
107 : 1278 : 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 : 31843 : unsigned int sz = GetTransactionWeight(tx);
115 [ + + ]: 31843 : if (sz > MAX_STANDARD_TX_WEIGHT) {
116 : 4 : reason = "tx-size";
117 : 4 : return false;
118 : : }
119 : :
120 [ + + ]: 91242 : 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 [ + + + + ]: 59643 : if (txin.scriptSig.size() > MAX_STANDARD_SCRIPTSIG_SIZE) {
131 : 2 : reason = "scriptsig-size";
132 : 2 : return false;
133 : : }
134 [ + + ]: 59641 : if (!txin.scriptSig.IsPushOnly()) {
135 : 238 : reason = "scriptsig-not-pushonly";
136 : 238 : return false;
137 : : }
138 : : }
139 : :
140 : 31599 : unsigned int nDataOut = 0;
141 : 31599 : TxoutType whichType;
142 [ + + ]: 144485 : for (const CTxOut& txout : tx.vout) {
143 [ + + ]: 112927 : if (!::IsStandard(txout.scriptPubKey, max_datacarrier_bytes, whichType)) {
144 : 36 : reason = "scriptpubkey";
145 : 36 : return false;
146 : : }
147 : :
148 [ + + ]: 112891 : if (whichType == TxoutType::NULL_DATA)
149 : 3252 : nDataOut++;
150 [ + + + + ]: 109639 : else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) {
151 : 5 : reason = "bare-multisig";
152 : 5 : return false;
153 : : }
154 : : }
155 : :
156 : : // Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust)
157 [ + + ]: 31558 : if (GetDust(tx, dust_relay_fee).size() > MAX_DUST_OUTPUTS_PER_TX) {
158 : 30 : reason = "dust";
159 : 30 : return false;
160 : : }
161 : :
162 : : // only one OP_RETURN txout is permitted
163 [ + + ]: 31528 : if (nDataOut > 1) {
164 : 4 : reason = "multi-op-return";
165 : 4 : return false;
166 : : }
167 : :
168 : : return true;
169 : : }
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 : 30715 : bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
188 : : {
189 [ + - ]: 30715 : if (tx.IsCoinBase()) {
190 : : return true; // Coinbases don't use vin normally
191 : : }
192 : :
193 [ + + ]: 84371 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
194 : 53872 : const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
195 : :
196 : 53872 : std::vector<std::vector<unsigned char> > vSolutions;
197 [ + - ]: 53872 : TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
198 [ + + ]: 53872 : 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 : : return false;
204 [ + + ]: 53658 : } else if (whichType == TxoutType::SCRIPTHASH) {
205 : 1566 : std::vector<std::vector<unsigned char> > stack;
206 : : // convert the scriptSig into a stack, so we can inspect the redeemScript
207 [ + - + - ]: 1566 : if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
208 : : return false;
209 [ + - ]: 1566 : if (stack.empty())
210 : : return false;
211 : 1566 : CScript subscript(stack.back().begin(), stack.back().end());
212 [ + - + + ]: 1566 : if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
213 : 2 : return false;
214 : : }
215 : 1566 : }
216 : 53872 : }
217 : :
218 : : return true;
219 : : }
220 : :
221 : 29445 : bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
222 : : {
223 [ + - ]: 29445 : if (tx.IsCoinBase())
224 : : return true; // Coinbases are skipped
225 : :
226 [ + + ]: 80553 : for (unsigned int i = 0; i < tx.vin.size(); i++)
227 : : {
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 [ + + ]: 51258 : if (tx.vin[i].scriptWitness.IsNull())
231 : 913 : continue;
232 : :
233 : 50345 : const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
234 : :
235 : : // get the scriptPubKey corresponding to this input:
236 : 50345 : CScript prevScript = prev.scriptPubKey;
237 : :
238 : : // witness stuffing detected
239 [ + - + + ]: 50345 : if (prevScript.IsPayToAnchor()) {
240 : : return false;
241 : : }
242 : :
243 : 50344 : bool p2sh = false;
244 [ + - + + ]: 50344 : if (prevScript.IsPayToScriptHash()) {
245 : 1433 : 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 [ + - + - ]: 1433 : if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
250 : : return false;
251 [ + - ]: 1433 : if (stack.empty())
252 : : return false;
253 : 1433 : prevScript = CScript(stack.back().begin(), stack.back().end());
254 : 1433 : p2sh = true;
255 : 1433 : }
256 : :
257 : 50344 : int witnessversion = 0;
258 : 50344 : std::vector<unsigned char> witnessprogram;
259 : :
260 : : // Non-witness program must not be associated with any witness
261 [ + - + + ]: 50344 : if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
262 : : return false;
263 : :
264 : : // Check P2WSH standard limits
265 [ + + + + ]: 50342 : if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
266 [ + + ]: 472 : if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
267 : : return false;
268 [ + + ]: 470 : size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
269 [ + + ]: 470 : if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
270 : : return false;
271 [ + + ]: 1244 : for (unsigned int j = 0; j < sizeWitnessStack; j++) {
272 [ + + ]: 778 : if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
273 : : return false;
274 : : }
275 : : }
276 : :
277 : : // Check policy limits for Taproot spends:
278 : : // - MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE limit for stack item size
279 : : // - No annexes
280 [ + + + + : 50336 : 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 [ + + ]: 41476 : std::span stack{tx.vin[i].scriptWitness.stack};
283 [ + + + + : 41476 : 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 : : return false;
286 : : }
287 [ + + ]: 41367 : if (stack.size() >= 2) {
288 : : // Script path spend (2 or more stack elements after removing optional annex)
289 : 40192 : const auto& control_block = SpanPopBack(stack);
290 : 40192 : SpanPopBack(stack); // Ignore script
291 [ + + ]: 40192 : if (control_block.empty()) return false; // Empty control block is invalid
292 [ + + ]: 40191 : if ((control_block[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) {
293 : : // Leaf version 0xc0 (aka Tapscript, see BIP 342)
294 [ + + ]: 315772 : for (const auto& item : stack) {
295 [ + + ]: 276610 : if (item.size() > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE) return false;
296 : : }
297 : : }
298 [ + - ]: 1175 : } else if (stack.size() == 1) {
299 : : // Key path spend (1 stack element after removing optional annex)
300 : : // (no policy rules apply)
301 : : } else {
302 : : // 0 stack elements; this is already invalid by consensus rules
303 : : return false;
304 : : }
305 : : }
306 : 50494 : }
307 : : return true;
308 : : }
309 : :
310 : 399615657 : int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
311 : : {
312 [ + + ]: 399615657 : return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
313 : : }
314 : :
315 : 56398 : int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop)
316 : : {
317 : 56398 : return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost, bytes_per_sigop);
318 : : }
319 : :
320 : 2 : int64_t GetVirtualTransactionInputSize(const CTxIn& txin, int64_t nSigOpCost, unsigned int bytes_per_sigop)
321 : : {
322 : 2 : return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop);
323 : : }
|