Skip to content
Bitcoin Roots

v30.3-roots.1 · Commit 3: [PATCH 03/68] feat(policy): preserve Roots relay and network controls on Core 30.3

src/policy/policy.cpp

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

File metadata
index 3da6cb7489b1731ee51d6afe1b272b24c9e9e313..79f46f540f7fbdfb71284714eeec68374a292dd9 100644

Download original patch · Release notes

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