LCOV - code coverage report
Current view: top level - src/policy - policy.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 98.3 % 121 119
Test Date: 2025-06-16 04:13:27 Functions: 100.0 % 10 10
Branches: 89.6 % 144 129

             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                 :    20951979 : 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         [ +  + ]:    20951979 :     if (txout.scriptPubKey.IsUnspendable())
      43                 :             :         return 0;
      44                 :             : 
      45                 :    20922200 :     size_t nSize = GetSerializeSize(txout);
      46                 :    20922200 :     int witnessversion = 0;
      47                 :    20922200 :     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   [ +  -  +  + ]:    20922200 :     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                 :    20908569 :         nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
      58                 :             :     } else {
      59                 :       13631 :         nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
      60                 :             :     }
      61                 :             : 
      62         [ +  - ]:    20922200 :     return dustRelayFeeIn.GetFee(nSize);
      63                 :    20922200 : }
      64                 :             : 
      65                 :    20949033 : bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
      66                 :             : {
      67                 :    20949033 :     return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
      68                 :             : }
      69                 :             : 
      70                 :     6875738 : std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate)
      71                 :             : {
      72                 :     6875738 :     std::vector<uint32_t> dust_outputs;
      73         [ +  + ]:    27440885 :     for (uint32_t i{0}; i < tx.vout.size(); ++i) {
      74   [ +  -  +  +  :    20565147 :         if (IsDust(tx.vout[i], dust_relay_rate)) dust_outputs.push_back(i);
                   +  - ]
      75                 :             :     }
      76                 :     6875738 :     return dust_outputs;
      77                 :           0 : }
      78                 :             : 
      79                 :     5125638 : bool IsStandard(const CScript& scriptPubKey, TxoutType& whichType)
      80                 :             : {
      81                 :     5125638 :     std::vector<std::vector<unsigned char> > vSolutions;
      82         [ +  - ]:     5125638 :     whichType = Solver(scriptPubKey, vSolutions);
      83                 :             : 
      84         [ +  + ]:     5125638 :     if (whichType == TxoutType::NONSTANDARD) {
      85                 :             :         return false;
      86         [ +  + ]:     5099857 :     } else if (whichType == TxoutType::MULTISIG) {
      87         [ +  + ]:        3431 :         unsigned char m = vSolutions.front()[0];
      88                 :        3431 :         unsigned char n = vSolutions.back()[0];
      89                 :             :         // Support up to x-of-3 multisig txns as standard
      90         [ +  + ]:        3431 :         if (n < 1 || n > 3)
      91                 :             :             return false;
      92         [ -  + ]:        1894 :         if (m < 1 || m > n)
      93                 :           0 :             return false;
      94                 :             :     }
      95                 :             : 
      96                 :             :     return true;
      97                 :     5125638 : }
      98                 :             : 
      99                 :      695064 : 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         [ +  + ]:      695064 :     if (tx.version > TX_MAX_STANDARD_VERSION || tx.version < 1) {
     102                 :       20465 :         reason = "version";
     103                 :       20465 :         return false;
     104                 :             :     }
     105                 :             : 
     106                 :             :     // Extremely large transactions with lots of inputs can cost the network
     107                 :             :     // almost as much to process as they cost the sender in fees, because
     108                 :             :     // computing signature hashes is O(ninputs*txsize). Limiting transactions
     109                 :             :     // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
     110                 :      674599 :     unsigned int sz = GetTransactionWeight(tx);
     111         [ +  + ]:      674599 :     if (sz > MAX_STANDARD_TX_WEIGHT) {
     112                 :         379 :         reason = "tx-size";
     113                 :         379 :         return false;
     114                 :             :     }
     115                 :             : 
     116         [ +  + ]:     4289947 :     for (const CTxIn& txin : tx.vin)
     117                 :             :     {
     118                 :             :         // Biggest 'standard' txin involving only keys is a 15-of-15 P2SH
     119                 :             :         // multisig with compressed keys (remember the MAX_SCRIPT_ELEMENT_SIZE byte limit on
     120                 :             :         // redeemScript size). That works out to a (15*(33+1))+3=513 byte
     121                 :             :         // redeemScript, 513+1+15*(73+1)+3=1627 bytes of scriptSig, which
     122                 :             :         // we round off to 1650(MAX_STANDARD_SCRIPTSIG_SIZE) bytes for
     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   [ +  +  +  + ]:     3620777 :         if (txin.scriptSig.size() > MAX_STANDARD_SCRIPTSIG_SIZE) {
     127                 :          97 :             reason = "scriptsig-size";
     128                 :          97 :             return false;
     129                 :             :         }
     130         [ +  + ]:     3620680 :         if (!txin.scriptSig.IsPushOnly()) {
     131                 :        4953 :             reason = "scriptsig-not-pushonly";
     132                 :        4953 :             return false;
     133                 :             :         }
     134                 :             :     }
     135                 :             : 
     136         [ +  + ]:      669170 :     unsigned int datacarrier_bytes_left = max_datacarrier_bytes.value_or(0);
     137                 :      669170 :     TxoutType whichType;
     138         [ +  + ]:     5764755 :     for (const CTxOut& txout : tx.vout) {
     139         [ +  + ]:     5121011 :         if (!::IsStandard(txout.scriptPubKey, whichType)) {
     140                 :       25167 :             reason = "scriptpubkey";
     141                 :       25167 :             return false;
     142                 :             :         }
     143                 :             : 
     144         [ +  + ]:     5095844 :         if (whichType == TxoutType::NULL_DATA) {
     145         [ +  + ]:        9718 :             unsigned int size = txout.scriptPubKey.size();
     146         [ +  + ]:        9718 :             if (size > datacarrier_bytes_left) {
     147                 :          17 :                 reason = "datacarrier";
     148                 :          17 :                 return false;
     149                 :             :             }
     150                 :        9701 :             datacarrier_bytes_left -= size;
     151   [ +  +  +  + ]:     5086126 :         } else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) {
     152                 :         242 :             reason = "bare-multisig";
     153                 :         242 :             return false;
     154                 :             :         }
     155                 :             :     }
     156                 :             : 
     157                 :             :     // Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust)
     158         [ +  + ]:      643744 :     if (GetDust(tx, dust_relay_fee).size() > MAX_DUST_OUTPUTS_PER_TX) {
     159                 :         725 :         reason = "dust";
     160                 :         725 :         return false;
     161                 :             :     }
     162                 :             : 
     163                 :             :     return true;
     164                 :             : }
     165                 :             : 
     166                 :             : /**
     167                 :             :  * Check transaction inputs.
     168                 :             :  *
     169                 :             :  * This does three things:
     170                 :             :  *  * Prevents mempool acceptance of spends of future
     171                 :             :  *    segwit versions we don't know how to validate
     172                 :             :  *  * Mitigates a potential denial-of-service attack with
     173                 :             :  *    P2SH scripts with a crazy number of expensive
     174                 :             :  *    CHECKSIG/CHECKMULTISIG operations.
     175                 :             :  *  * Prevents spends of unknown/irregular scriptPubKeys,
     176                 :             :  *    which mitigates potential denial-of-service attacks
     177                 :             :  *    involving expensive scripts and helps reserve them
     178                 :             :  *    as potential new upgrade hooks.
     179                 :             :  *
     180                 :             :  * Note that only the non-witness portion of the transaction is checked here.
     181                 :             :  */
     182                 :      350326 : bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
     183                 :             : {
     184         [ +  + ]:      350326 :     if (tx.IsCoinBase()) {
     185                 :             :         return true; // Coinbases don't use vin normally
     186                 :             :     }
     187                 :             : 
     188         [ +  + ]:     1855189 :     for (unsigned int i = 0; i < tx.vin.size(); i++) {
     189                 :     1507524 :         const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
     190                 :             : 
     191                 :     1507524 :         std::vector<std::vector<unsigned char> > vSolutions;
     192         [ +  - ]:     1507524 :         TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
     193         [ +  + ]:     1507524 :         if (whichType == TxoutType::NONSTANDARD || whichType == TxoutType::WITNESS_UNKNOWN) {
     194                 :             :             // WITNESS_UNKNOWN failures are typically also caught with a policy
     195                 :             :             // flag in the script interpreter, but it can be helpful to catch
     196                 :             :             // this type of NONSTANDARD transaction earlier in transaction
     197                 :             :             // validation.
     198                 :             :             return false;
     199         [ +  + ]:     1505932 :         } else if (whichType == TxoutType::SCRIPTHASH) {
     200                 :        3618 :             std::vector<std::vector<unsigned char> > stack;
     201                 :             :             // convert the scriptSig into a stack, so we can inspect the redeemScript
     202   [ +  -  +  + ]:        3618 :             if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
     203                 :             :                 return false;
     204         [ +  + ]:        2590 :             if (stack.empty())
     205                 :             :                 return false;
     206                 :        2584 :             CScript subscript(stack.back().begin(), stack.back().end());
     207   [ +  -  +  + ]:        2584 :             if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
     208                 :          22 :                 return false;
     209                 :             :             }
     210                 :        3618 :         }
     211                 :     1507524 :     }
     212                 :             : 
     213                 :             :     return true;
     214                 :             : }
     215                 :             : 
     216                 :      347195 : bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
     217                 :             : {
     218         [ +  + ]:      347195 :     if (tx.IsCoinBase())
     219                 :             :         return true; // Coinbases are skipped
     220                 :             : 
     221         [ +  + ]:     1929297 :     for (unsigned int i = 0; i < tx.vin.size(); i++)
     222                 :             :     {
     223                 :             :         // We don't care if witness for this input is empty, since it must not be bloated.
     224                 :             :         // If the script is invalid without witness, it would be caught sooner or later during validation.
     225         [ +  + ]:     1583467 :         if (tx.vin[i].scriptWitness.IsNull())
     226                 :       84221 :             continue;
     227                 :             : 
     228                 :     1499246 :         const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
     229                 :             : 
     230                 :             :         // get the scriptPubKey corresponding to this input:
     231                 :     1499246 :         CScript prevScript = prev.scriptPubKey;
     232                 :             : 
     233                 :             :         // witness stuffing detected
     234   [ +  -  +  + ]:     1499246 :         if (prevScript.IsPayToAnchor()) {
     235                 :             :             return false;
     236                 :             :         }
     237                 :             : 
     238                 :     1499244 :         bool p2sh = false;
     239   [ +  -  +  + ]:     1499244 :         if (prevScript.IsPayToScriptHash()) {
     240                 :          57 :             std::vector <std::vector<unsigned char> > stack;
     241                 :             :             // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
     242                 :             :             // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
     243                 :             :             // If the check fails at this stage, we know that this txid must be a bad one.
     244   [ +  -  +  + ]:          57 :             if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
     245                 :             :                 return false;
     246         [ +  + ]:          42 :             if (stack.empty())
     247                 :             :                 return false;
     248                 :          40 :             prevScript = CScript(stack.back().begin(), stack.back().end());
     249                 :          40 :             p2sh = true;
     250                 :          57 :         }
     251                 :             : 
     252                 :     1499227 :         int witnessversion = 0;
     253                 :     1499227 :         std::vector<unsigned char> witnessprogram;
     254                 :             : 
     255                 :             :         // Non-witness program must not be associated with any witness
     256   [ +  -  +  + ]:     1499227 :         if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
     257                 :             :             return false;
     258                 :             : 
     259                 :             :         // Check P2WSH standard limits
     260   [ +  +  +  + ]:     1498476 :         if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
     261         [ +  + ]:     1494215 :             if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
     262                 :             :                 return false;
     263         [ +  + ]:     1494100 :             size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
     264         [ +  + ]:     1494100 :             if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
     265                 :             :                 return false;
     266         [ +  + ]:     2120053 :             for (unsigned int j = 0; j < sizeWitnessStack; j++) {
     267         [ +  + ]:      626410 :                 if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
     268                 :             :                     return false;
     269                 :             :             }
     270                 :             :         }
     271                 :             : 
     272                 :             :         // Check policy limits for Taproot spends:
     273                 :             :         // - MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE limit for stack item size
     274                 :             :         // - No annexes
     275   [ +  +  +  +  :     1497904 :         if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE && !p2sh) {
                   +  - ]
     276                 :             :             // Taproot spend (non-P2SH-wrapped, version 1, witness program size 32; see BIP 341)
     277         [ +  + ]:        2182 :             std::span stack{tx.vin[i].scriptWitness.stack};
     278   [ +  +  +  +  :        2182 :             if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) {
                   +  + ]
     279                 :             :                 // Annexes are nonstandard as long as no semantics are defined for them.
     280                 :             :                 return false;
     281                 :             :             }
     282         [ +  + ]:        2180 :             if (stack.size() >= 2) {
     283                 :             :                 // Script path spend (2 or more stack elements after removing optional annex)
     284                 :         893 :                 const auto& control_block = SpanPopBack(stack);
     285                 :         893 :                 SpanPopBack(stack); // Ignore script
     286         [ +  + ]:         893 :                 if (control_block.empty()) return false; // Empty control block is invalid
     287         [ +  + ]:         891 :                 if ((control_block[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) {
     288                 :             :                     // Leaf version 0xc0 (aka Tapscript, see BIP 342)
     289         [ +  + ]:         688 :                     for (const auto& item : stack) {
     290         [ +  + ]:         635 :                         if (item.size() > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE) return false;
     291                 :             :                     }
     292                 :             :                 }
     293         [ +  - ]:        1287 :             } else if (stack.size() == 1) {
     294                 :             :                 // Key path spend (1 stack element after removing optional annex)
     295                 :             :                 // (no policy rules apply)
     296                 :             :             } else {
     297                 :             :                 // 0 stack elements; this is already invalid by consensus rules
     298                 :             :                 return false;
     299                 :             :             }
     300                 :             :         }
     301                 :     1500579 :     }
     302                 :             :     return true;
     303                 :             : }
     304                 :             : 
     305                 :  1050093285 : int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
     306                 :             : {
     307         [ +  + ]:  1050093285 :     return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
     308                 :             : }
     309                 :             : 
     310                 :       30636 : int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop)
     311                 :             : {
     312                 :       30636 :     return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost, bytes_per_sigop);
     313                 :             : }
     314                 :             : 
     315                 :          66 : int64_t GetVirtualTransactionInputSize(const CTxIn& txin, int64_t nSigOpCost, unsigned int bytes_per_sigop)
     316                 :             : {
     317                 :          66 :     return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop);
     318                 :             : }
        

Generated by: LCOV version 2.0-1