Skip to content
Bitcoin Roots

v29.4-roots.1 · Commit 3: [PATCH 03/13] policy: restore conservative relay and network controls

src/policy/policy.cpp

Before: src/policy/policy.cpp · After: src/policy/policy.cpp

File metadata
index 5942747d60999d761aa8ab14c7e8505499b3ac23..cc9c5432d52230bcf2657d81d4bb316c83c233d9 100644

Download original patch · Release notes

Diff rows 1–400 of 470. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/policy/policy.cpp CHANGED
@@ -11,7 +11,9 @@
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>
@@ -21,8 +23,12 @@
21
 
22
  #include <algorithm>
23
  #include <cstddef>
 
 
24
  #include <vector>
25
 
 
 
26
  CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
27
  {
28
  // "Dust" is defined in terms of dustRelayFee,
@@ -76,6 +82,10 @@ std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate)
76
  return dust_outputs;
77
  }
78
 
 
 
 
 
79
  bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType)
80
  {
81
  std::vector<std::vector<unsigned char> > vSolutions;
@@ -100,11 +110,27 @@ bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_
100
  return true;
101
  }
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
  {
 
 
105
  if (tx.version > TX_MAX_STANDARD_VERSION || tx.version < 1) {
106
- reason = "version";
107
- return false;
108
  }
109
 
110
  // Extremely large transactions with lots of inputs can cost the network
@@ -113,8 +139,11 @@ bool IsStandardTx(const CTransaction& tx, const std::optional<unsigned>& max_dat
113
  // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
114
  unsigned int sz = GetTransactionWeight(tx);
115
  if (sz > MAX_STANDARD_TX_WEIGHT) {
116
- reason = "tx-size";
117
- return false;
 
 
 
118
  }
119
 
120
  for (const CTxIn& txin : tx.vin)
@@ -127,51 +156,102 @@ bool IsStandardTx(const CTransaction& tx, const std::optional<unsigned>& max_dat
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
- if (txin.scriptSig.size() > MAX_STANDARD_SCRIPTSIG_SIZE) {
131
- reason = "scriptsig-size";
132
- return false;
133
  }
134
  if (!txin.scriptSig.IsPushOnly()) {
135
- reason = "scriptsig-not-pushonly";
136
- return false;
137
  }
138
  }
139
 
140
  unsigned int nDataOut = 0;
 
 
141
  TxoutType whichType;
142
- for (const CTxOut& txout : tx.vout) {
143
- if (!::IsStandard(txout.scriptPubKey, max_datacarrier_bytes, whichType)) {
144
- reason = "scriptpubkey";
145
- return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  }
147
 
148
- if (whichType == TxoutType::NULL_DATA)
 
 
 
149
  nDataOut++;
150
- else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) {
151
- reason = "bare-multisig";
152
- return false;
 
 
 
 
 
 
 
153
  }
154
  }
155
 
156
  // Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust)
157
- if (GetDust(tx, dust_relay_fee).size() > MAX_DUST_OUTPUTS_PER_TX) {
158
- reason = "dust";
159
- return false;
160
  }
161
 
162
  // only one OP_RETURN txout is permitted
163
  if (nDataOut > 1) {
164
- reason = "multi-op-return";
165
- return false;
 
 
 
 
 
 
 
 
166
  }
167
 
168
  return true;
169
  }
170
 
 
 
 
 
 
 
 
 
 
171
  /**
172
  * Check the total number of non-witness sigops across the whole transaction, as per BIP54.
173
  */
174
- static bool CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inputs)
175
  {
176
  Assert(!tx.IsCoinBase());
177
 
@@ -189,7 +269,7 @@ static bool CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inpu
189
  sigops += txin.scriptSig.GetSigOpCount(/*fAccurate=*/true);
190
  sigops += prev_txo.scriptPubKey.GetSigOpCount(txin.scriptSig);
191
 
192
- if (sigops > MAX_TX_LEGACY_SIGOPS) {
193
  return false;
194
  }
195
  }
@@ -217,37 +297,60 @@ static bool CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inpu
217
  *
218
  * We also check the total number of non-witness sigops across the whole transaction, as per BIP54.
219
  */
220
- bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
221
  {
222
  if (tx.IsCoinBase()) {
223
  return true; // Coinbases don't use vin normally
224
  }
225
 
226
- if (!CheckSigopsBIP54(tx, mapInputs)) {
227
- return false;
228
  }
229
 
230
  for (unsigned int i = 0; i < tx.vin.size(); i++) {
231
  const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
232
 
 
 
 
 
233
  std::vector<std::vector<unsigned char> > vSolutions;
234
  TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
235
- if (whichType == TxoutType::NONSTANDARD || whichType == TxoutType::WITNESS_UNKNOWN) {
 
 
236
  // WITNESS_UNKNOWN failures are typically also caught with a policy
237
  // flag in the script interpreter, but it can be helpful to catch
238
  // this type of NONSTANDARD transaction earlier in transaction
239
  // validation.
240
- return false;
241
  } else if (whichType == TxoutType::SCRIPTHASH) {
 
 
 
 
 
 
242
  std::vector<std::vector<unsigned char> > stack;
243
  // convert the scriptSig into a stack, so we can inspect the redeemScript
244
  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
 
 
 
245
  return false;
 
246
  if (stack.empty())
 
 
 
247
  return false;
 
248
  CScript subscript(stack.back().begin(), stack.back().end());
 
 
 
249
  if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
250
- return false;
251
  }
252
  }
253
  }
@@ -255,7 +358,14 @@ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
255
  return true;
256
  }
257
 
258
- bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
 
 
 
 
 
 
 
259
  {
260
  if (tx.IsCoinBase())
261
  return true; // Coinbases are skipped
@@ -274,7 +384,7 @@ bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
274
 
275
  // witness stuffing detected
276
  if (prevScript.IsPayToAnchor()) {
277
- return false;
278
  }
279
 
280
  bool p2sh = false;
@@ -284,9 +394,15 @@ bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
284
  // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
285
  // If the check fails at this stage, we know that this txid must be a bad one.
286
  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
 
 
287
  return false;
 
288
  if (stack.empty())
 
 
289
  return false;
 
290
  prevScript = CScript(stack.back().begin(), stack.back().end());
291
  p2sh = true;
292
  }
@@ -296,18 +412,25 @@ bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
296
 
297
  // Non-witness program must not be associated with any witness
298
  if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
 
 
299
  return false;
 
 
 
 
 
300
 
301
  // Check P2WSH standard limits
302
  if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
303
  if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
304
- return false;
305
  size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
306
  if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
307
- return false;
308
  for (unsigned int j = 0; j < sizeWitnessStack; j++) {
309
  if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
310
- return false;
311
  }
312
  }
313
 
@@ -319,17 +442,28 @@ bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
319
  Span stack{tx.vin[i].scriptWitness.stack};
320
  if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) {
321
  // Annexes are nonstandard as long as no semantics are defined for them.
322
- return false;
 
 
323
  }
324
  if (stack.size() >= 2) {
325
  // Script path spend (2 or more stack elements after removing optional annex)
326
  const auto& control_block = SpanPopBack(stack);
327
  SpanPopBack(stack); // Ignore script
328
- if (control_block.empty()) return false; // Empty control block is invalid
 
 
 
 
329
  if ((control_block[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) {
330
  // Leaf version 0xc0 (aka Tapscript, see BIP 342)
 
331
  for (const auto& item : stack) {
332
- if (item.size() > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE) return false;
 
 
 
 
333
  }
334
  }
335
  } else if (stack.size() == 1) {
@@ -337,6 +471,7 @@ bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
337
  // (no policy rules apply)
338
  } else {
339
  // 0 stack elements; this is already invalid by consensus rules
 
340
  return false;
341
  }
342
  }
@@ -394,3 +529,97 @@ int64_t GetVirtualTransactionInputSize(const CTxIn& txin, int64_t nSigOpCost, un
394
  {
395
  return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop);
396
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  #include <consensus/amount.h>
12
  #include <consensus/consensus.h>
13
  #include <consensus/validation.h>
14
+ #include <kernel/mempool_options.h>
15
  #include <policy/feerate.h>
16
+ #include <policy/settings.h>
17
  #include <primitives/transaction.h>
18
  #include <script/interpreter.h>
19
  #include <script/script.h>
 
23
 
24
  #include <algorithm>
25
  #include <cstddef>
26
+ #include <limits>
27
+ #include <utility>
28
  #include <vector>
29
 
30
+ unsigned int g_script_size_policy_limit{MAX_SCRIPT_SIZE};
31
+
32
  CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
33
  {
34
  // "Dust" is defined in terms of dustRelayFee,
 
82
  return dust_outputs;
83
  }
84
 
85
+ /**
86
+ * Note this must assign whichType even if returning false, in case
87
+ * IsStandardTx ignores the "scriptpubkey" rejection.
88
+ */
89
  bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType)
90
  {
91
  std::vector<std::vector<unsigned char> > vSolutions;
 
110
  return true;
111
  }
112
 
113
+ static inline bool MaybeReject_(std::string& out_reason, const std::string& reason, const std::string& reason_prefix, const ignore_rejects_type& ignore_rejects) {
114
+ if (ignore_rejects.count(reason_prefix + reason)) {
115
+ return false;
116
+ }
117
+
118
+ out_reason = reason_prefix + reason;
119
+ return true;
120
+ }
121
+
122
+ #define MaybeReject(reason) do { \
123
+ if (MaybeReject_(out_reason, reason, reason_prefix, ignore_rejects)) { \
124
+ return false; \
125
+ } \
126
+ } while(0)
127
+
128
+ bool IsStandardTx(const CTransaction& tx, const kernel::MemPoolOptions& opts, std::string& out_reason, const ignore_rejects_type& ignore_rejects)
129
  {
130
+ const std::string reason_prefix;
131
+
132
  if (tx.version > TX_MAX_STANDARD_VERSION || tx.version < 1) {
133
+ MaybeReject("version");
 
134
  }
135
 
136
  // Extremely large transactions with lots of inputs can cost the network
 
139
  // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
140
  unsigned int sz = GetTransactionWeight(tx);
141
  if (sz > MAX_STANDARD_TX_WEIGHT) {
142
+ MaybeReject("tx-size");
143
+ }
144
+
145
+ if (tx.nLockTime == 21 && opts.reject_parasites) {
146
+ MaybeReject("parasite-cat21");
147
  }
148
 
149
  for (const CTxIn& txin : tx.vin)
 
156
  // some minor future-proofing. That's also enough to spend a
157
  // 20-of-20 CHECKMULTISIG scriptPubKey, though such a scriptPubKey
158
  // is not considered standard.
159
+ if (txin.scriptSig.size() > std::min(MAX_STANDARD_SCRIPTSIG_SIZE, g_script_size_policy_limit)) {
160
+ MaybeReject("scriptsig-size");
 
161
  }
162
  if (!txin.scriptSig.IsPushOnly()) {
163
+ MaybeReject("scriptsig-not-pushonly");
 
164
  }
165
  }
166
 
167
  unsigned int nDataOut = 0;
168
+ unsigned int n_dust{0};
169
+ unsigned int n_monetary{0};
170
  TxoutType whichType;
171
+ for (size_t i{tx.vout.size()}; i; ) {
172
+ const CTxOut& txout = tx.vout[--i];
173
+
174
+ if (txout.scriptPubKey.size() > g_script_size_policy_limit) {
175
+ MaybeReject("scriptpubkey-size");
176
+ }
177
+
178
+ if (!::IsStandard(txout.scriptPubKey, opts.max_datacarrier_bytes, whichType)) {
179
+ MaybeReject("scriptpubkey");
180
+ }
181
+
182
+ if (whichType == TxoutType::WITNESS_UNKNOWN && !opts.acceptunknownwitness) {
183
+ MaybeReject("scriptpubkey-unknown-witnessversion");
184
+ }
185
+
186
+ if (whichType == TxoutType::ANCHOR && !opts.permitephemeral_anchor) {
187
+ MaybeReject("anchor");
188
+ }
189
+
190
+ if (IsDust(txout, opts.dust_relay_feerate)) {
191
+ if (whichType != TxoutType::ANCHOR && !opts.permitephemeral_send) {
192
+ MaybeReject("dust-nonanchor");
193
+ }
194
+ if (txout.nValue && !opts.permitephemeral_dust) {
195
+ MaybeReject("dust-nonzero");
196
+ }
197
+ ++n_dust;
198
+ } else if (whichType != TxoutType::NULL_DATA) {
199
+ ++n_monetary;
200
  }
201
 
202
+ if (whichType == TxoutType::NULL_DATA) {
203
+ if (txout.scriptPubKey.size() > 2 && txout.scriptPubKey[1] == OP_13 && opts.reject_tokens) {
204
+ MaybeReject("tokens-runes");
205
+ }
206
  nDataOut++;
207
+ continue;
208
+ }
209
+ else if ((whichType == TxoutType::PUBKEY) && (!opts.permit_bare_pubkey)) {
210
+ MaybeReject("bare-pubkey");
211
+ }
212
+ else if ((whichType == TxoutType::MULTISIG) && (!opts.permit_bare_multisig)) {
213
+ MaybeReject("bare-multisig");
214
+ }
215
+ else if (whichType == TxoutType::WITNESS_V0_SCRIPTHASH && opts.reject_tokens && txout.scriptPubKey.IsOLGA(tx.vout.size() - i)) {
216
+ MaybeReject("tokens-olga");
217
  }
218
  }
219
 
220
  // Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust)
221
+ if (n_dust > MAX_DUST_OUTPUTS_PER_TX) {
222
+ MaybeReject("dust");
 
223
  }
224
 
225
  // only one OP_RETURN txout is permitted
226
  if (nDataOut > 1) {
227
+ MaybeReject("multi-op-return");
228
+ }
229
+
230
+ if (!n_monetary) {
231
+ if (nDataOut && !opts.permitbaredatacarrier) {
232
+ MaybeReject("bare-datacarrier");
233
+ }
234
+ if ((!nDataOut) && !opts.permitbareanchor) {
235
+ MaybeReject("bare-anchor");
236
+ }
237
  }
238
 
239
  return true;
240
  }
241
 
242
+ bool IsStandardTx(const CTransaction& tx, unsigned int max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate& dust_relay_feerate, std::string& out_reason)
243
+ {
244
+ kernel::MemPoolOptions opts;
245
+ opts.max_datacarrier_bytes = max_datacarrier_bytes;
246
+ opts.permit_bare_multisig = permit_bare_multisig;
247
+ opts.dust_relay_feerate = dust_relay_feerate;
248
+ return IsStandardTx(tx, opts, out_reason);
249
+ }
250
+
251
  /**
252
  * Check the total number of non-witness sigops across the whole transaction, as per BIP54.
253
  */
254
+ static bool CheckSigopsBIP54(const CTransaction& tx, const CCoinsViewCache& inputs, const kernel::MemPoolOptions& opts)
255
  {
256
  Assert(!tx.IsCoinBase());
257
 
 
269
  sigops += txin.scriptSig.GetSigOpCount(/*fAccurate=*/true);
270
  sigops += prev_txo.scriptPubKey.GetSigOpCount(txin.scriptSig);
271
 
272
+ if (sigops > opts.maxtxlegacysigops) {
273
  return false;
274
  }
275
  }
 
297
  *
298
  * We also check the total number of non-witness sigops across the whole transaction, as per BIP54.
299
  */
300
+ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs, const kernel::MemPoolOptions& opts, const std::string& reason_prefix, std::string& out_reason, const ignore_rejects_type& ignore_rejects)
301
  {
302
  if (tx.IsCoinBase()) {
303
  return true; // Coinbases don't use vin normally
304
  }
305
 
306
+ if (!CheckSigopsBIP54(tx, mapInputs, opts)) {
307
+ MaybeReject("sigops-toomany-overall");
308
  }
309
 
310
  for (unsigned int i = 0; i < tx.vin.size(); i++) {
311
  const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
312
 
313
+ if (prev.scriptPubKey.size() > g_script_size_policy_limit) {
314
+ MaybeReject("script-size");
315
+ }
316
+
317
  std::vector<std::vector<unsigned char> > vSolutions;
318
  TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
319
+ if (whichType == TxoutType::NONSTANDARD) {
320
+ MaybeReject("script-unknown");
321
+ } else if (whichType == TxoutType::WITNESS_UNKNOWN) {
322
  // WITNESS_UNKNOWN failures are typically also caught with a policy
323
  // flag in the script interpreter, but it can be helpful to catch
324
  // this type of NONSTANDARD transaction earlier in transaction
325
  // validation.
326
+ MaybeReject("witness-unknown");
327
  } else if (whichType == TxoutType::SCRIPTHASH) {
328
+ if (!tx.vin[i].scriptSig.IsPushOnly()) {
329
+ // The only way we got this far, is if the user ignored scriptsig-not-pushonly.
330
+ // However, this case is invalid, and will be caught later on.
331
+ // But for now, we don't want to run the [possibly expensive] script here.
332
+ continue;
333
+ }
334
  std::vector<std::vector<unsigned char> > stack;
335
  // convert the scriptSig into a stack, so we can inspect the redeemScript
336
  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
337
+ {
338
+ // This case is also invalid or a bug
339
+ out_reason = reason_prefix + "scriptsig-failure";
340
  return false;
341
+ }
342
  if (stack.empty())
343
+ {
344
+ // Also invalid
345
+ out_reason = reason_prefix + "scriptcheck-missing";
346
  return false;
347
+ }
348
  CScript subscript(stack.back().begin(), stack.back().end());
349
+ if (subscript.size() > g_script_size_policy_limit) {
350
+ MaybeReject("scriptcheck-size");
351
+ }
352
  if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
353
+ MaybeReject("scriptcheck-sigops");
354
  }
355
  }
356
  }
 
358
  return true;
359
  }
360
 
361
+ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& map_inputs)
362
+ {
363
+ kernel::MemPoolOptions opts;
364
+ std::string reason;
365
+ return AreInputsStandard(tx, map_inputs, opts, "", reason);
366
+ }
367
+
368
+ bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs, const std::string& reason_prefix, std::string& out_reason, const ignore_rejects_type& ignore_rejects)
369
  {
370
  if (tx.IsCoinBase())
371
  return true; // Coinbases are skipped
 
384
 
385
  // witness stuffing detected
386
  if (prevScript.IsPayToAnchor()) {
387
+ MaybeReject("anchor-not-empty");
388
  }
389
 
390
  bool p2sh = false;
 
394
  // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
395
  // If the check fails at this stage, we know that this txid must be a bad one.
396
  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
397
+ {
398
+ out_reason = reason_prefix + "scriptsig-failure";
399
  return false;
400
+ }
401
  if (stack.empty())
402
+ {
403
+ out_reason = reason_prefix + "scriptcheck-missing";
404
  return false;
405
+ }
406
  prevScript = CScript(stack.back().begin(), stack.back().end());
407
  p2sh = true;
408
  }
 
412
 
413
  // Non-witness program must not be associated with any witness
414
  if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
415
+ {
416
+ out_reason = reason_prefix + "nonwitness-input";
417
  return false;
418
+ }
419
+
420
+ if (GetSerializeSize(tx.vin[i].scriptWitness.stack) > g_script_size_policy_limit) {
421
+ MaybeReject("witness-size");
422
+ }
423
 
424
  // Check P2WSH standard limits
425
  if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
426
  if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
427
+ MaybeReject("script-size");
428
  size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
429
  if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
430
+ MaybeReject("stackitem-count");
431
  for (unsigned int j = 0; j < sizeWitnessStack; j++) {
432
  if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
433
+ MaybeReject("stackitem-size");
434
  }
435
  }
436
 
 
442
  Span stack{tx.vin[i].scriptWitness.stack};
443
  if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) {
444
  // Annexes are nonstandard as long as no semantics are defined for them.
445
+ MaybeReject("taproot-annex");
446
+ // If reject reason is ignored, continue as if the annex wasn't there.
447
+ SpanPopBack(stack);
448
  }
449
  if (stack.size() >= 2) {
450
  // Script path spend (2 or more stack elements after removing optional annex)
451
  const auto& control_block = SpanPopBack(stack);
452
  SpanPopBack(stack); // Ignore script
453
+ if (control_block.empty()) {
454
+ // Empty control block is invalid
455
+ out_reason = reason_prefix + "taproot-control-missing";
456
+ return false;
457
+ }
458
  if ((control_block[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) {
459
  // Leaf version 0xc0 (aka Tapscript, see BIP 342)
460
+ if (!ignore_rejects.count(reason_prefix + "taproot-stackitem-size")) {
461
  for (const auto& item : stack) {
462
+ if (item.size() > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE) {
463
+ out_reason = reason_prefix + "taproot-stackitem-size";
464
+ return false;
465
+ }
466
+ }
467
  }
468
  }
469
  } else if (stack.size() == 1) {
 
471
  // (no policy rules apply)
472
  } else {
473
  // 0 stack elements; this is already invalid by consensus rules
474
+ out_reason = reason_prefix + "taproot-witness-missing";
475
  return false;
476
  }
477
  }
 
529
  {
530
  return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop);
531
  }
532
+
533
+ std::pair<CScript, unsigned int> GetScriptForTransactionInput(CScript prevScript, const CTxIn& txin)
534
+ {
535
+ bool p2sh = false;
536
+ if (prevScript.IsPayToScriptHash()) {
537
+ std::vector <std::vector<unsigned char> > stack;
538
+ if (!EvalScript(stack, txin.scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE)) {
539
+ return std::make_pair(CScript(), 0);
540
+ }
541
+ if (stack.empty()) {
542
+ return std::make_pair(CScript(), 0);
543
+ }
544
+ prevScript = CScript(stack.back().begin(), stack.back().end());
545
+ p2sh = true;
546
+ }
547
+
548
+ int witnessversion = 0;
549
+ std::vector<unsigned char> witnessprogram;
550
+
551
+ if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram)) {
552
+ // For P2SH, scriptSig is always push-only, so the actual script is only the last stack item
553
+ // For non-P2SH, prevScript is likely the real script, but not part of this transaction, and scriptSig could very well be executable, so return the latter instead
554
+ return std::make_pair(p2sh ? prevScript : txin.scriptSig, WITNESS_SCALE_FACTOR);
555
+ }