LCOV - code coverage report
Current view: top level - src/consensus - validation.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 89.1 % 46 41
Test Date: 2026-08-07 06:33:33 Functions: 100.0 % 10 10
Branches: 29.0 % 1119 325

             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                 :             : #ifndef BITCOIN_CONSENSUS_VALIDATION_H
       7                 :             : #define BITCOIN_CONSENSUS_VALIDATION_H
       8                 :             : 
       9                 :             : #include <consensus/consensus.h>
      10                 :             : #include <primitives/block.h>
      11                 :             : #include <primitives/transaction.h>
      12                 :             : #include <script/script.h>
      13                 :             : #include <serialize.h>
      14                 :             : 
      15                 :             : #include <cstddef>
      16                 :             : #include <cstdint>
      17                 :             : #include <memory>
      18                 :             : #include <string>
      19                 :             : #include <vector>
      20                 :             : 
      21                 :             : 
      22                 :             : /** Index marker for when no witness commitment is present in a coinbase transaction. */
      23                 :             : static constexpr int NO_WITNESS_COMMITMENT{-1};
      24                 :             : 
      25                 :             : /** Minimum size of a witness commitment structure. Defined in BIP 141. **/
      26                 :             : static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
      27                 :             : 
      28                 :             : /** A "reason" why a transaction was invalid, suitable for determining whether the
      29                 :             :   * provider of the transaction should be banned/ignored/disconnected/etc.
      30                 :             :   */
      31                 :             : enum class TxValidationResult {
      32                 :             :     TX_RESULT_UNSET = 0,     //!< initial value. Tx has not yet been rejected
      33                 :             :     TX_CONSENSUS,            //!< invalid by consensus rules
      34                 :             :     TX_INPUTS_NOT_STANDARD,   //!< inputs (covered by txid) failed policy rules
      35                 :             :     TX_NOT_STANDARD,          //!< otherwise didn't meet our local policy rules
      36                 :             :     TX_MISSING_INPUTS,        //!< transaction was missing some of its inputs
      37                 :             :     TX_PREMATURE_SPEND,       //!< transaction spends a coinbase too early, or violates locktime/sequence locks
      38                 :             :     /**
      39                 :             :      * Transaction might have a witness prior to SegWit
      40                 :             :      * activation, or witness may have been malleated (which includes
      41                 :             :      * non-standard witnesses).
      42                 :             :      */
      43                 :             :     TX_WITNESS_MUTATED,
      44                 :             :     /**
      45                 :             :      * Transaction is missing a witness.
      46                 :             :      */
      47                 :             :     TX_WITNESS_STRIPPED,
      48                 :             :     /**
      49                 :             :      * Tx already in mempool or conflicts with a tx in the chain
      50                 :             :      * (if it conflicts with another tx in mempool, we use MEMPOOL_POLICY as it failed to reach the RBF threshold)
      51                 :             :      * Currently this is only used if the transaction already exists in the mempool or on chain.
      52                 :             :      */
      53                 :             :     TX_CONFLICT,
      54                 :             :     TX_MEMPOOL_POLICY,        //!< violated mempool's fee/size/descendant/RBF/etc limits
      55                 :             :     TX_NO_MEMPOOL,            //!< this node does not have a mempool so can't validate the transaction
      56                 :             :     TX_RECONSIDERABLE,        //!< fails some policy, but might be acceptable if submitted in a (different) package
      57                 :             :     TX_UNKNOWN,               //!< transaction was not validated because package failed
      58                 :             : };
      59                 :             : 
      60                 :             : /** A "reason" why a block was invalid, suitable for determining whether the
      61                 :             :   * provider of the block should be banned/ignored/disconnected/etc.
      62                 :             :   * These are much more granular than the rejection codes, which may be more
      63                 :             :   * useful for some other use-cases.
      64                 :             :   */
      65                 :             : enum class BlockValidationResult {
      66                 :             :     BLOCK_RESULT_UNSET = 0,  //!< initial value. Block has not yet been rejected
      67                 :             :     BLOCK_CONSENSUS,         //!< invalid by consensus rules (excluding any below reasons)
      68                 :             :     BLOCK_CACHED_INVALID,    //!< this block was cached as being invalid and we didn't store the reason why
      69                 :             :     BLOCK_INVALID_HEADER,    //!< invalid proof of work or time too old
      70                 :             :     BLOCK_MUTATED,           //!< the block's data didn't match the data committed to by the PoW
      71                 :             :     BLOCK_MISSING_PREV,      //!< We don't have the previous block the checked one is built on
      72                 :             :     BLOCK_INVALID_PREV,      //!< A block this one builds on is invalid
      73                 :             :     BLOCK_TIME_FUTURE,       //!< block timestamp was > 2 hours in the future (or our clock is bad)
      74                 :             :     BLOCK_HEADER_LOW_WORK    //!< the block header may be on a too-little-work chain
      75                 :             : };
      76                 :             : 
      77                 :             : 
      78                 :             : 
      79                 :             : /** Template for capturing information about block/transaction validation. This is instantiated
      80                 :             :  *  by TxValidationState and BlockValidationState for validation information on transactions
      81                 :             :  *  and blocks respectively. */
      82                 :             : template <typename Result>
      83   [ +  -  +  +  :      178274 : class ValidationState
           +  - ][ #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ -  +  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
      84                 :             : {
      85                 :             : private:
      86                 :             :     enum class ModeState {
      87                 :             :         M_VALID,   //!< everything ok
      88                 :             :         M_INVALID, //!< network rule violation (DoS value may be set)
      89                 :             :         M_ERROR,   //!< run-time error
      90                 :             :     } m_mode{ModeState::M_VALID};
      91                 :             :     Result m_result{};
      92                 :             :     std::string m_reject_reason;
      93                 :             :     std::string m_debug_message;
      94                 :             : 
      95                 :             : public:
      96                 :       35484 :     bool Invalid(Result result,
      97                 :             :                  const std::string& reject_reason = "",
      98   [ +  -  +  -  :         348 :                  const std::string& debug_message = "")
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  -  -  
          -  -  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  +  -  
          +  -  -  -  -  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  -  -  
           +  - ][ +  -  
          +  -  +  -  +  
                -  +  - ]
      99                 :             :     {
     100                 :       35484 :         m_result = result;
     101                 :       35484 :         m_reject_reason = reject_reason;
     102                 :       35484 :         m_debug_message = debug_message;
     103         [ +  - ]:       35484 :         if (m_mode != ModeState::M_ERROR) m_mode = ModeState::M_INVALID;
     104                 :       35484 :         return false;
     105                 :             :     }
     106                 :           0 :     bool Error(const std::string& reject_reason)
     107                 :             :     {
     108   [ #  #  #  # ]:           0 :         if (m_mode == ModeState::M_VALID)
     109         [ #  # ]:           0 :             m_reject_reason = reject_reason;
     110                 :           0 :         m_mode = ModeState::M_ERROR;
     111                 :             :         return false;
     112                 :             :     }
     113   [ +  +  -  +  :       66705 :     bool IsValid() const { return m_mode == ModeState::M_VALID; }
          +  +  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
           #  # ][ -  +  
          -  -  -  -  -  
          +  -  +  +  -  
          -  +  +  -  +  
           + ][ #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  +  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  -  
             +  -  -  +  
                      - ]
     114   [ +  -  #  #  :         330 :     bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
          #  #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
             +  -  +  + ]
           [ -  -  +  -  
          +  -  +  +  +  
                -  #  # ]
           [ +  +  +  - ]
           [ +  -  +  -  
             +  -  +  - ]
     115         [ +  - ]:           2 :     bool IsError() const { return m_mode == ModeState::M_ERROR; }
     116   [ +  -  #  #  :         596 :     Result GetResult() const { return m_result; }
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  +  +  
           +  +  + ][ #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  -  +  
          -  -  -  +  -  
          +  +  +  +  +  
           - ][ #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
           [ +  +  +  + ]
           [ +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          +  -  +  +  -  
             -  -  -  -  
                      + ]
     117   [ -  +  #  #  :          88 :     std::string GetRejectReason() const { return m_reject_reason; }
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  -  
          -  -  -  +  +  
          -  -  -  -  -  
             -  -  -  - ]
           [ #  #  #  #  
             #  #  #  # ]
           [ -  +  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
           +  - ][ -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     118   [ -  +  #  #  :          34 :     std::string GetDebugMessage() const { return m_debug_message; }
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
           [ #  #  #  # ]
           [ -  -  -  -  
          -  +  +  -  -  
          -  -  -  -  -  
           -  - ][ -  +  
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
           +  - ][ -  +  
          +  -  -  +  +  
                      - ]
     119                 :       14567 :     std::string ToString() const
     120                 :             :     {
     121         [ +  + ]:       14567 :         if (IsValid()) {
     122                 :       14326 :             return "Valid";
     123                 :             :         }
     124                 :             : 
     125         [ +  - ]:         241 :         if (!m_debug_message.empty()) {
     126         [ +  - ]:         482 :             return m_reject_reason + ", " + m_debug_message;
     127                 :             :         }
     128                 :             : 
     129         [ #  # ]:           0 :         return m_reject_reason;
     130                 :             :     }
     131                 :             : };
     132                 :             : 
     133   [ -  -  -  -  :      297238 : class TxValidationState : public ValidationState<TxValidationResult> {};
          +  -  +  -  +  
          +  +  -  -  -  
          -  -  -  -  -  
          -  +  -  +  -  
          -  -  -  -  +  
          +  +  -  +  -  
          -  -  -  -  -  
          -  -  -  +  -  
          +  -  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
           +  - ][ +  + ]
           [ +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
           +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     134   [ +  +  #  #  :      196729 : class BlockValidationState : public ValidationState<BlockValidationResult> {};
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
           # ][ +  -  +  
          +  +  -  -  -  
          +  -  -  -  -  
          -  +  -  -  -  
          -  +  +  -  +  
          -  +  +  +  -  
          +  -  -  -  +  
             +  +  -  +  
           - ][ +  -  +  
           -  +  - ][ +  
             -  +  -  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
             -  +  -  +  
                      - ]
     135                 :             : 
     136                 :             : // These implement the weight = (stripped_size * 4) + witness_size formula,
     137                 :             : // using only serialization with and without witness data. As witness_size
     138                 :             : // is equal to total_size - stripped_size, this formula is identical to:
     139                 :             : // weight = (stripped_size * 3) + total_size.
     140                 :      106916 : static inline int32_t GetTransactionWeight(const CTransaction& tx)
     141                 :             : {
     142                 :      106916 :     return ::GetSerializeSize(TX_NO_WITNESS(tx)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(tx));
     143                 :             : }
     144                 :       25320 : static inline int64_t GetBlockWeight(const CBlock& block)
     145                 :             : {
     146                 :       25320 :     return ::GetSerializeSize(TX_NO_WITNESS(block)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(block));
     147                 :             : }
     148                 :           6 : static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
     149                 :             : {
     150                 :             :     // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
     151                 :           6 :     return ::GetSerializeSize(TX_NO_WITNESS(txin)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(txin)) + ::GetSerializeSize(txin.scriptWitness.stack);
     152                 :             : }
     153                 :             : 
     154                 :             : /** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
     155                 :       57106 : inline int GetWitnessCommitmentIndex(const CBlock& block)
     156                 :             : {
     157                 :       57106 :     int commitpos = NO_WITNESS_COMMITMENT;
     158         [ +  - ]:       57106 :     if (!block.vtx.empty()) {
     159   [ -  +  +  + ]:      156684 :         for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
     160         [ +  + ]:       99578 :             const CTxOut& vout = block.vtx[0]->vout[o];
     161   [ +  +  +  + ]:      182174 :             if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
     162   [ +  +  +  - ]:       41037 :                 vout.scriptPubKey[0] == OP_RETURN &&
     163   [ +  -  +  - ]:       40631 :                 vout.scriptPubKey[1] == 0x24 &&
     164   [ +  -  +  - ]:       40631 :                 vout.scriptPubKey[2] == 0xaa &&
     165   [ +  -  +  - ]:       40631 :                 vout.scriptPubKey[3] == 0x21 &&
     166   [ +  +  +  -  :       82596 :                 vout.scriptPubKey[4] == 0xa9 &&
                   +  - ]
     167         [ +  - ]:       40631 :                 vout.scriptPubKey[5] == 0xed) {
     168                 :       40631 :                 commitpos = o;
     169                 :             :             }
     170                 :             :         }
     171                 :             :     }
     172                 :       57106 :     return commitpos;
     173                 :             : }
     174                 :             : 
     175                 :             : #endif // BITCOIN_CONSENSUS_VALIDATION_H
        

Generated by: LCOV version 2.0-1