LCOV - code coverage report
Current view: top level - src/consensus - validation.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 45 45
Test Date: 2024-12-04 04:00:22 Functions: 100.0 % 10 10
Branches: 40.8 % 441 180

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

Generated by: LCOV version 2.0-1