LCOV - code coverage report
Current view: top level - src/consensus - validation.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 33 50 66.0 %
Date: 2023-09-26 12:08:55 Functions: 28 44 63.6 %

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

Generated by: LCOV version 1.14