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. The two line-number columns refer to before and after.

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