LCOV - code coverage report
Current view: top level - src/consensus - tx_check.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 18 29 62.1 %
Date: 2023-11-10 23:46:46 Functions: 1 1 100.0 %
Branches: 18 92 19.6 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2017-2021 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #include <consensus/tx_check.h>
       6                 :            : 
       7                 :            : #include <consensus/amount.h>
       8                 :            : #include <primitives/transaction.h>
       9                 :            : #include <consensus/validation.h>
      10                 :            : 
      11                 :        451 : bool CheckTransaction(const CTransaction& tx, TxValidationState& state)
      12                 :            : {
      13                 :            :     // Basic checks that don't depend on any context
      14         [ +  - ]:        451 :     if (tx.vin.empty())
      15 [ #  # ][ #  # ]:          0 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty");
                 [ #  # ]
      16         [ -  + ]:        451 :     if (tx.vout.empty())
      17 [ #  # ][ #  # ]:          0 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty");
                 [ #  # ]
      18                 :            :     // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
      19         [ -  + ]:        451 :     if (::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
      20 [ #  # ][ #  # ]:          0 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");
                 [ #  # ]
      21                 :            : 
      22                 :            :     // Check for negative or overflow output values (see CVE-2010-5139)
      23                 :        451 :     CAmount nValueOut = 0;
      24         [ +  + ]:       1352 :     for (const auto& txout : tx.vout)
      25                 :            :     {
      26         [ +  - ]:        901 :         if (txout.nValue < 0)
      27 [ #  # ][ #  # ]:          0 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative");
                 [ #  # ]
      28         [ +  - ]:        901 :         if (txout.nValue > MAX_MONEY)
      29 [ #  # ][ #  # ]:          0 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge");
                 [ #  # ]
      30                 :        901 :         nValueOut += txout.nValue;
      31         [ +  - ]:        901 :         if (!MoneyRange(nValueOut))
      32 [ #  # ][ #  # ]:          0 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge");
                 [ #  # ]
      33                 :            :     }
      34                 :            : 
      35                 :            :     // Check for duplicate inputs (see CVE-2018-17144)
      36                 :            :     // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
      37                 :            :     // of a tx as spent, it does not check if the tx has duplicate inputs.
      38                 :            :     // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
      39                 :            :     // the underlying coins database.
      40                 :        451 :     std::set<COutPoint> vInOutPoints;
      41         [ +  + ]:        902 :     for (const auto& txin : tx.vin) {
      42 [ +  - ][ +  - ]:        451 :         if (!vInOutPoints.insert(txin.prevout).second)
      43 [ #  # ][ #  # ]:          0 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
                 [ #  # ]
      44                 :            :     }
      45                 :            : 
      46 [ +  - ][ +  - ]:        451 :     if (tx.IsCoinBase())
      47                 :            :     {
      48 [ +  - ][ +  - ]:        451 :         if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
         [ +  - ][ -  + ]
      49 [ #  # ][ #  # ]:          0 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
                 [ #  # ]
      50                 :        451 :     }
      51                 :            :     else
      52                 :            :     {
      53         [ #  # ]:          0 :         for (const auto& txin : tx.vin)
      54 [ #  # ][ #  # ]:          0 :             if (txin.prevout.IsNull())
      55 [ #  # ][ #  # ]:          0 :                 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
                 [ #  # ]
      56                 :            :     }
      57                 :            : 
      58                 :        451 :     return true;
      59                 :        451 : }

Generated by: LCOV version 1.14