LCOV - code coverage report
Current view: top level - src/consensus - validation.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 88.9 % 45 40
Test Date: 2024-11-04 04:45:35 Functions: 80.0 % 10 8
Branches: 30.2 % 562 170

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 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 <string>
      10                 :             : #include <consensus/consensus.h>
      11                 :             : #include <primitives/transaction.h>
      12                 :             : #include <primitives/block.h>
      13                 :             : 
      14                 :             : /** Index marker for when no witness commitment is present in a coinbase transaction. */
      15                 :             : static constexpr int NO_WITNESS_COMMITMENT{-1};
      16                 :             : 
      17                 :             : /** Minimum size of a witness commitment structure. Defined in BIP 141. **/
      18                 :             : static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};
      19                 :             : 
      20                 :             : /** A "reason" why a transaction was invalid, suitable for determining whether the
      21                 :             :   * provider of the transaction should be banned/ignored/disconnected/etc.
      22                 :             :   */
      23                 :             : enum class TxValidationResult {
      24                 :             :     TX_RESULT_UNSET = 0,     //!< initial value. Tx has not yet been rejected
      25                 :             :     TX_CONSENSUS,            //!< invalid by consensus rules
      26                 :             :     /**
      27                 :             :      * Invalid by a change to consensus rules more recent than SegWit.
      28                 :             :      * Currently unused as there are no such consensus rule changes, and any download
      29                 :             :      * sources realistically need to support SegWit in order to provide useful data,
      30                 :             :      * so differentiating between always-invalid and invalid-by-pre-SegWit-soft-fork
      31                 :             :      * is uninteresting.
      32                 :             :      */
      33                 :             :     TX_RECENT_CONSENSUS_CHANGE,
      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                 :             :     /**
      69                 :             :      * Invalid by a change to consensus rules more recent than SegWit.
      70                 :             :      * Currently unused as there are no such consensus rule changes, and any download
      71                 :             :      * sources realistically need to support SegWit in order to provide useful data,
      72                 :             :      * so differentiating between always-invalid and invalid-by-pre-SegWit-soft-fork
      73                 :             :      * is uninteresting.
      74                 :             :      */
      75                 :             :     BLOCK_RECENT_CONSENSUS_CHANGE,
      76                 :             :     BLOCK_CACHED_INVALID,    //!< this block was cached as being invalid and we didn't store the reason why
      77                 :             :     BLOCK_INVALID_HEADER,    //!< invalid proof of work or time too old
      78                 :             :     BLOCK_MUTATED,           //!< the block's data didn't match the data committed to by the PoW
      79                 :             :     BLOCK_MISSING_PREV,      //!< We don't have the previous block the checked one is built on
      80                 :             :     BLOCK_INVALID_PREV,      //!< A block this one builds on is invalid
      81                 :             :     BLOCK_TIME_FUTURE,       //!< block timestamp was > 2 hours in the future (or our clock is bad)
      82                 :             :     BLOCK_CHECKPOINT,        //!< the block failed to meet one of our checkpoints
      83                 :             :     BLOCK_HEADER_LOW_WORK    //!< the block header may be on a too-little-work chain
      84                 :             : };
      85                 :             : 
      86                 :             : 
      87                 :             : 
      88                 :             : /** Template for capturing information about block/transaction validation. This is instantiated
      89                 :             :  *  by TxValidationState and BlockValidationState for validation information on transactions
      90                 :             :  *  and blocks respectively. */
      91                 :             : template <typename Result>
      92   [ +  -  +  +  :      162025 : class ValidationState
           +  - ][ #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
      93                 :             : {
      94                 :             : private:
      95                 :             :     enum class ModeState {
      96                 :             :         M_VALID,   //!< everything ok
      97                 :             :         M_INVALID, //!< network rule violation (DoS value may be set)
      98                 :             :         M_ERROR,   //!< run-time error
      99                 :             :     } m_mode{ModeState::M_VALID};
     100                 :             :     Result m_result{};
     101                 :             :     std::string m_reject_reason;
     102                 :             :     std::string m_debug_message;
     103                 :             : 
     104                 :             : public:
     105                 :       35520 :     bool Invalid(Result result,
     106                 :             :                  const std::string& reject_reason = "",
     107                 :             :                  const std::string& debug_message = "")
     108                 :             :     {
     109                 :       35520 :         m_result = result;
     110                 :       35520 :         m_reject_reason = reject_reason;
     111                 :       35520 :         m_debug_message = debug_message;
     112         [ +  - ]:       35520 :         if (m_mode != ModeState::M_ERROR) m_mode = ModeState::M_INVALID;
     113                 :       35520 :         return false;
     114                 :             :     }
     115                 :           0 :     bool Error(const std::string& reject_reason)
     116                 :             :     {
     117         [ #  # ]:           0 :         if (m_mode == ModeState::M_VALID)
           [ #  #  #  # ]
     118         [ #  # ]:           0 :             m_reject_reason = reject_reason;
     119                 :           0 :         m_mode = ModeState::M_ERROR;
     120                 :             :         return false;
     121                 :             :     }
     122   [ +  +  +  - ]:       21712 :     bool IsValid() const { return m_mode == ModeState::M_VALID; }
           [ #  #  #  #  
             #  #  #  # ]
           [ -  +  -  -  
           +  - ][ +  +  
             #  #  #  # ]
     123   [ -  -  -  -  :         161 :     bool IsInvalid() const { return m_mode == ModeState::M_INVALID; }
          -  -  -  -  -  
           -  -  + ][ +  
          -  #  #  #  #  
           #  # ][ -  -  
          +  -  +  -  +  
             +  #  #  #  
           # ][ +  -  +  
                -  +  - ]
           [ -  +  +  - ]
     124         [ #  # ]:           0 :     bool IsError() const { return m_mode == ModeState::M_ERROR; }
     125   [ #  #  #  #  :         352 :     Result GetResult() const { return m_result; }
          #  #  #  #  #  
           #  # ][ +  +  
             +  +  +  + ]
           [ +  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ -  -  -  -  
          +  -  -  -  +  
          -  +  +  +  +  
             +  -  -  - ]
           [ #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
           [ +  -  +  + ]
     126   [ -  -  +  -  :          28 :     std::string GetRejectReason() const { return m_reject_reason; }
             -  -  -  - ]
           [ +  -  #  # ]
           [ #  #  #  # ]
           [ +  -  +  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     127   [ -  -  +  -  :           5 :     std::string GetDebugMessage() const { return m_debug_message; }
             -  -  -  - ]
     128                 :       13804 :     std::string ToString() const
     129                 :             :     {
     130         [ +  + ]:       13804 :         if (IsValid()) {
     131                 :       13400 :             return "Valid";
     132                 :             :         }
     133                 :             : 
     134         [ +  + ]:         404 :         if (!m_debug_message.empty()) {
     135         [ +  - ]:          50 :             return m_reject_reason + ", " + m_debug_message;
     136                 :             :         }
     137                 :             : 
     138                 :         379 :         return m_reject_reason;
     139                 :             :     }
     140                 :             : };
     141                 :             : 
     142   [ -  -  -  -  :      299170 : class TxValidationState : public ValidationState<TxValidationResult> {};
          +  -  +  -  +  
          -  +  -  -  -  
          -  -  -  -  -  
          -  +  -  +  -  
          -  -  -  -  +  
          +  +  -  -  -  
          -  -  +  -  +  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
         [ +  + ][ +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  +  
          -  #  #  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
                      - ]
     143   [ #  #  #  #  :      150951 : class BlockValidationState : public ValidationState<BlockValidationResult> {};
          #  #  #  #  #  
           # ][ +  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  -  -  
          +  -  +  -  +  
          +  +  -  -  -  
          +  -  +  -  +  
           - ][ #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  #  #  
           #  # ][ -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             +  -  +  - ]
           [ +  -  +  - ]
     144                 :             : 
     145                 :             : // These implement the weight = (stripped_size * 4) + witness_size formula,
     146                 :             : // using only serialization with and without witness data. As witness_size
     147                 :             : // is equal to total_size - stripped_size, this formula is identical to:
     148                 :             : // weight = (stripped_size * 3) + total_size.
     149                 :       53014 : static inline int32_t GetTransactionWeight(const CTransaction& tx)
     150                 :             : {
     151                 :       53014 :     return ::GetSerializeSize(TX_NO_WITNESS(tx)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(tx));
     152                 :             : }
     153                 :       21707 : static inline int64_t GetBlockWeight(const CBlock& block)
     154                 :             : {
     155                 :       21707 :     return ::GetSerializeSize(TX_NO_WITNESS(block)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(block));
     156                 :             : }
     157                 :           6 : static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
     158                 :             : {
     159                 :             :     // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
     160                 :           6 :     return ::GetSerializeSize(TX_NO_WITNESS(txin)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(txin)) + ::GetSerializeSize(txin.scriptWitness.stack);
     161                 :             : }
     162                 :             : 
     163                 :             : /** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
     164                 :       42486 : inline int GetWitnessCommitmentIndex(const CBlock& block)
     165                 :             : {
     166                 :       42486 :     int commitpos = NO_WITNESS_COMMITMENT;
     167         [ +  - ]:       42486 :     if (!block.vtx.empty()) {
     168         [ +  + ]:      114984 :         for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
     169         [ +  + ]:       72498 :             const CTxOut& vout = block.vtx[0]->vout[o];
     170   [ +  +  +  + ]:      167738 :             if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
     171   [ +  +  +  - ]:       28024 :                 vout.scriptPubKey[0] == OP_RETURN &&
     172         [ +  - ]:       27983 :                 vout.scriptPubKey[1] == 0x24 &&
     173   [ +  -  +  - ]:       27983 :                 vout.scriptPubKey[2] == 0xaa &&
     174   [ +  -  +  - ]:       27983 :                 vout.scriptPubKey[3] == 0x21 &&
     175   [ +  +  +  -  :       95240 :                 vout.scriptPubKey[4] == 0xa9 &&
                   +  - ]
     176         [ +  - ]:       27983 :                 vout.scriptPubKey[5] == 0xed) {
     177                 :       27983 :                 commitpos = o;
     178                 :             :             }
     179                 :             :         }
     180                 :             :     }
     181                 :       42486 :     return commitpos;
     182                 :             : }
     183                 :             : 
     184                 :             : #endif // BITCOIN_CONSENSUS_VALIDATION_H
        

Generated by: LCOV version 2.0-1