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

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2017-present 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 <consensus/consensus.h>
       9                 :             : #include <consensus/validation.h>
      10                 :             : #include <primitives/transaction.h>
      11                 :             : #include <script/script.h>
      12                 :             : #include <serialize.h>
      13                 :             : 
      14                 :             : #include <set>
      15                 :             : #include <string>
      16                 :             : #include <utility>
      17                 :             : #include <vector>
      18                 :             : 
      19                 :       27527 : bool CheckTransaction(const CTransaction& tx, TxValidationState& state)
      20                 :             : {
      21                 :             :     // Basic checks that don't depend on any context
      22         [ +  + ]:       27527 :     if (tx.vin.empty())
      23   [ +  -  +  - ]:           1 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty");
      24         [ +  + ]:       27526 :     if (tx.vout.empty())
      25   [ +  -  +  - ]:           3 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty");
      26                 :             :     // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
      27         [ +  + ]:       27523 :     if (::GetSerializeSize(TX_NO_WITNESS(tx)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) {
      28   [ +  -  +  - ]:           1 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");
      29                 :             :     }
      30                 :             : 
      31                 :             :     // Check for negative or overflow output values (see CVE-2010-5139)
      32                 :       27522 :     CAmount nValueOut = 0;
      33         [ +  + ]:       87089 :     for (const auto& txout : tx.vout)
      34                 :             :     {
      35         [ +  + ]:       59573 :         if (txout.nValue < 0)
      36   [ +  -  +  - ]:           2 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative");
      37         [ +  + ]:       59571 :         if (txout.nValue > MAX_MONEY)
      38   [ +  -  +  - ]:           2 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge");
      39                 :       59569 :         nValueOut += txout.nValue;
      40         [ +  + ]:       59569 :         if (!MoneyRange(nValueOut))
      41   [ +  -  +  - ]:           2 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge");
      42                 :             :     }
      43                 :             : 
      44                 :             :     // Check for duplicate inputs (see CVE-2018-17144)
      45                 :             :     // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
      46                 :             :     // of a tx as spent, it does not check if the tx has duplicate inputs.
      47                 :             :     // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
      48                 :             :     // the underlying coins database.
      49                 :       27516 :     std::set<COutPoint> vInOutPoints;
      50         [ +  + ]:       56884 :     for (const auto& txin : tx.vin) {
      51   [ +  -  +  + ]:       29371 :         if (!vInOutPoints.insert(txin.prevout).second)
      52   [ +  -  +  -  :           3 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
                   +  - ]
      53                 :             :     }
      54                 :             : 
      55         [ +  + ]:       27513 :     if (tx.IsCoinBase())
      56                 :             :     {
      57   [ +  +  +  +  :       27695 :         if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
             +  +  +  + ]
      58   [ +  -  +  -  :           5 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
                   +  - ]
      59                 :             :     }
      60                 :             :     else
      61                 :             :     {
      62         [ +  + ]:        4569 :         for (const auto& txin : tx.vin)
      63         [ +  + ]:        3211 :             if (txin.prevout.IsNull())
      64   [ +  -  +  -  :           3 :                 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
                   +  - ]
      65                 :             :     }
      66                 :             : 
      67                 :             :     return true;
      68                 :       27516 : }
        

Generated by: LCOV version 2.0-1