LCOV - code coverage report
Current view: top level - src/consensus - tx_check.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 27 27
Test Date: 2024-08-28 05:13:07 Functions: 100.0 % 1 1
Branches: 71.1 % 76 54

             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                 :      390029 : bool CheckTransaction(const CTransaction& tx, TxValidationState& state)
      12                 :             : {
      13                 :             :     // Basic checks that don't depend on any context
      14         [ +  + ]:      390029 :     if (tx.vin.empty())
      15   [ +  -  +  - ]:           4 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty");
      16         [ +  + ]:      390025 :     if (tx.vout.empty())
      17   [ +  -  +  - ]:           5 :         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         [ +  + ]:      390020 :     if (::GetSerializeSize(TX_NO_WITNESS(tx)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) {
      20   [ +  -  +  - ]:           2 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");
      21                 :             :     }
      22                 :             : 
      23                 :             :     // Check for negative or overflow output values (see CVE-2010-5139)
      24                 :      390018 :     CAmount nValueOut = 0;
      25         [ +  + ]:     1462193 :     for (const auto& txout : tx.vout)
      26                 :             :     {
      27         [ +  + ]:     1072190 :         if (txout.nValue < 0)
      28   [ +  -  +  - ]:           5 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative");
      29         [ +  + ]:     1072185 :         if (txout.nValue > MAX_MONEY)
      30   [ +  -  +  - ]:           5 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge");
      31                 :     1072180 :         nValueOut += txout.nValue;
      32         [ +  + ]:     1072180 :         if (!MoneyRange(nValueOut))
      33   [ +  -  +  - ]:           5 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge");
      34                 :             :     }
      35                 :             : 
      36                 :             :     // Check for duplicate inputs (see CVE-2018-17144)
      37                 :             :     // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
      38                 :             :     // of a tx as spent, it does not check if the tx has duplicate inputs.
      39                 :             :     // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
      40                 :             :     // the underlying coins database.
      41                 :      390003 :     std::set<COutPoint> vInOutPoints;
      42         [ +  + ]:      872692 :     for (const auto& txin : tx.vin) {
      43   [ +  -  +  + ]:      483625 :         if (!vInOutPoints.insert(txin.prevout).second)
      44   [ +  -  +  -  :         936 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
                   +  - ]
      45                 :             :     }
      46                 :             : 
      47         [ +  + ]:      389067 :     if (tx.IsCoinBase())
      48                 :             :     {
      49   [ +  +  +  +  :      390562 :         if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
             +  +  +  + ]
      50   [ +  -  +  -  :           5 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
                   +  - ]
      51                 :             :     }
      52                 :             :     else
      53                 :             :     {
      54         [ +  + ]:      339157 :         for (const auto& txin : tx.vin)
      55         [ +  + ]:      215924 :             if (txin.prevout.IsNull())
      56   [ +  -  +  -  :           6 :                 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
                   +  - ]
      57                 :             :     }
      58                 :             : 
      59                 :             :     return true;
      60                 :      390003 : }
        

Generated by: LCOV version 2.0-1