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