LCOV - code coverage report
Current view: top level - src/consensus - tx_verify.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.4 % 78 76
Test Date: 2024-11-04 05:10:19 Functions: 100.0 % 8 8
Branches: 72.1 % 104 75

             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_verify.h>
       6                 :             : 
       7                 :             : #include <chain.h>
       8                 :             : #include <coins.h>
       9                 :             : #include <consensus/amount.h>
      10                 :             : #include <consensus/consensus.h>
      11                 :             : #include <consensus/validation.h>
      12                 :             : #include <primitives/transaction.h>
      13                 :             : #include <script/interpreter.h>
      14                 :             : #include <util/check.h>
      15                 :             : #include <util/moneystr.h>
      16                 :             : 
      17                 :      267960 : bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
      18                 :             : {
      19         [ +  + ]:      267960 :     if (tx.nLockTime == 0)
      20                 :             :         return true;
      21   [ +  +  +  + ]:       14684 :     if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
      22                 :             :         return true;
      23                 :             : 
      24                 :             :     // Even if tx.nLockTime isn't satisfied by nBlockHeight/nBlockTime, a
      25                 :             :     // transaction is still considered final if all inputs' nSequence ==
      26                 :             :     // SEQUENCE_FINAL (0xffffffff), in which case nLockTime is ignored.
      27                 :             :     //
      28                 :             :     // Because of this behavior OP_CHECKLOCKTIMEVERIFY/CheckLockTime() will
      29                 :             :     // also check that the spending input's nSequence != SEQUENCE_FINAL,
      30                 :             :     // ensuring that an unsatisfied nLockTime value will actually cause
      31                 :             :     // IsFinalTx() to return false here:
      32         [ +  + ]:          61 :     for (const auto& txin : tx.vin) {
      33         [ +  + ]:          56 :         if (!(txin.nSequence == CTxIn::SEQUENCE_FINAL))
      34                 :             :             return false;
      35                 :             :     }
      36                 :             :     return true;
      37                 :             : }
      38                 :             : 
      39                 :       87522 : std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block)
      40                 :             : {
      41         [ -  + ]:       87522 :     assert(prevHeights.size() == tx.vin.size());
      42                 :             : 
      43                 :             :     // Will be set to the equivalent height- and time-based nLockTime
      44                 :             :     // values that would be necessary to satisfy all relative lock-
      45                 :             :     // time constraints given our view of block chain history.
      46                 :             :     // The semantics of nLockTime are the last invalid height/time, so
      47                 :             :     // use -1 to have the effect of any height or time being valid.
      48                 :       87522 :     int nMinHeight = -1;
      49                 :       87522 :     int64_t nMinTime = -1;
      50                 :             : 
      51   [ +  +  +  + ]:       87522 :     bool fEnforceBIP68 = tx.version >= 2 && flags & LOCKTIME_VERIFY_SEQUENCE;
      52                 :             : 
      53                 :             :     // Do not enforce sequence numbers as a relative lock time
      54                 :             :     // unless we have been instructed to
      55                 :        4285 :     if (!fEnforceBIP68) {
      56                 :        4285 :         return std::make_pair(nMinHeight, nMinTime);
      57                 :             :     }
      58                 :             : 
      59         [ +  + ]:      235006 :     for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
      60         [ +  + ]:      151769 :         const CTxIn& txin = tx.vin[txinIndex];
      61                 :             : 
      62                 :             :         // Sequence numbers with the most significant bit set are not
      63                 :             :         // treated as relative lock-times, nor are they given any
      64                 :             :         // consensus-enforced meaning at this point.
      65         [ +  + ]:      151769 :         if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) {
      66                 :             :             // The height of this input is not relevant for sequence locks
      67                 :       45897 :             prevHeights[txinIndex] = 0;
      68                 :       45897 :             continue;
      69                 :             :         }
      70                 :             : 
      71         [ +  + ]:      105872 :         int nCoinHeight = prevHeights[txinIndex];
      72                 :             : 
      73         [ +  + ]:      105872 :         if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
      74         [ +  - ]:         314 :             const int64_t nCoinTime{Assert(block.GetAncestor(std::max(nCoinHeight - 1, 0)))->GetMedianTimePast()};
      75                 :             :             // NOTE: Subtract 1 to maintain nLockTime semantics
      76                 :             :             // BIP 68 relative lock times have the semantics of calculating
      77                 :             :             // the first block or time at which the transaction would be
      78                 :             :             // valid. When calculating the effective block time or height
      79                 :             :             // for the entire transaction, we switch to using the
      80                 :             :             // semantics of nLockTime which is the last invalid block
      81                 :             :             // time or height.  Thus we subtract 1 from the calculated
      82                 :             :             // time or height.
      83                 :             : 
      84                 :             :             // Time-based relative lock-times are measured from the
      85                 :             :             // smallest allowed timestamp of the block containing the
      86                 :             :             // txout being spent, which is the median time past of the
      87                 :             :             // block prior.
      88         [ +  + ]:         166 :             nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
      89                 :             :         } else {
      90         [ +  + ]:      152043 :             nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
      91                 :             :         }
      92                 :             :     }
      93                 :             : 
      94                 :       83237 :     return std::make_pair(nMinHeight, nMinTime);
      95                 :             : }
      96                 :             : 
      97                 :       88240 : bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair)
      98                 :             : {
      99         [ -  + ]:       88240 :     assert(block.pprev);
     100                 :       88240 :     int64_t nBlockTime = block.pprev->GetMedianTimePast();
     101   [ +  +  +  + ]:       88240 :     if (lockPair.first >= block.nHeight || lockPair.second >= nBlockTime)
     102                 :         395 :         return false;
     103                 :             : 
     104                 :             :     return true;
     105                 :             : }
     106                 :             : 
     107                 :       57079 : bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block)
     108                 :             : {
     109                 :       57079 :     return EvaluateSequenceLocks(block, CalculateSequenceLocks(tx, flags, prevHeights, block));
     110                 :             : }
     111                 :             : 
     112                 :      609780 : unsigned int GetLegacySigOpCount(const CTransaction& tx)
     113                 :             : {
     114                 :      609780 :     unsigned int nSigOps = 0;
     115         [ +  + ]:     1322209 :     for (const auto& txin : tx.vin)
     116                 :             :     {
     117                 :      712429 :         nSigOps += txin.scriptSig.GetSigOpCount(false);
     118                 :             :     }
     119         [ +  + ]:     2059923 :     for (const auto& txout : tx.vout)
     120                 :             :     {
     121                 :     1450143 :         nSigOps += txout.scriptPubKey.GetSigOpCount(false);
     122                 :             :     }
     123                 :      609780 :     return nSigOps;
     124                 :             : }
     125                 :             : 
     126                 :       86613 : unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs)
     127                 :             : {
     128         [ +  - ]:       86613 :     if (tx.IsCoinBase())
     129                 :             :         return 0;
     130                 :             : 
     131                 :             :     unsigned int nSigOps = 0;
     132         [ +  + ]:      232204 :     for (unsigned int i = 0; i < tx.vin.size(); i++)
     133                 :             :     {
     134                 :      145591 :         const Coin& coin = inputs.AccessCoin(tx.vin[i].prevout);
     135         [ -  + ]:      145591 :         assert(!coin.IsSpent());
     136                 :      145591 :         const CTxOut &prevout = coin.out;
     137         [ +  + ]:      145591 :         if (prevout.scriptPubKey.IsPayToScriptHash())
     138                 :       10090 :             nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
     139                 :             :     }
     140                 :             :     return nSigOps;
     141                 :             : }
     142                 :             : 
     143                 :      258236 : int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, uint32_t flags)
     144                 :             : {
     145                 :      258236 :     int64_t nSigOps = GetLegacySigOpCount(tx) * WITNESS_SCALE_FACTOR;
     146                 :             : 
     147         [ +  + ]:      258236 :     if (tx.IsCoinBase())
     148                 :             :         return nSigOps;
     149                 :             : 
     150         [ +  - ]:       86610 :     if (flags & SCRIPT_VERIFY_P2SH) {
     151                 :       86610 :         nSigOps += GetP2SHSigOpCount(tx, inputs) * WITNESS_SCALE_FACTOR;
     152                 :             :     }
     153                 :             : 
     154         [ +  + ]:      232194 :     for (unsigned int i = 0; i < tx.vin.size(); i++)
     155                 :             :     {
     156                 :      145584 :         const Coin& coin = inputs.AccessCoin(tx.vin[i].prevout);
     157         [ -  + ]:      145584 :         assert(!coin.IsSpent());
     158                 :      145584 :         const CTxOut &prevout = coin.out;
     159                 :      145584 :         nSigOps += CountWitnessSigOps(tx.vin[i].scriptSig, prevout.scriptPubKey, &tx.vin[i].scriptWitness, flags);
     160                 :             :     }
     161                 :             :     return nSigOps;
     162                 :             : }
     163                 :             : 
     164                 :     6898507 : bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee)
     165                 :             : {
     166                 :             :     // are the actual inputs available?
     167         [ +  + ]:     6898507 :     if (!inputs.HaveInputs(tx)) {
     168   [ +  -  +  - ]:          20 :         return state.Invalid(TxValidationResult::TX_MISSING_INPUTS, "bad-txns-inputs-missingorspent",
     169         [ +  - ]:          40 :                          strprintf("%s: inputs missing/spent", __func__));
     170                 :             :     }
     171                 :             : 
     172                 :             :     CAmount nValueIn = 0;
     173         [ +  + ]:    16018665 :     for (unsigned int i = 0; i < tx.vin.size(); ++i) {
     174                 :     9120184 :         const COutPoint &prevout = tx.vin[i].prevout;
     175                 :     9120184 :         const Coin& coin = inputs.AccessCoin(prevout);
     176         [ -  + ]:     9120184 :         assert(!coin.IsSpent());
     177                 :             : 
     178                 :             :         // If prev is coinbase, check that it's matured
     179   [ +  +  +  + ]:     9120184 :         if (coin.IsCoinBase() && nSpendHeight - coin.nHeight < COINBASE_MATURITY) {
     180   [ +  -  +  - ]:           6 :             return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "bad-txns-premature-spend-of-coinbase",
     181         [ +  - ]:          12 :                 strprintf("tried to spend coinbase at depth %d", nSpendHeight - coin.nHeight));
     182                 :             :         }
     183                 :             : 
     184                 :             :         // Check for negative or overflow input values
     185                 :     9120178 :         nValueIn += coin.out.nValue;
     186   [ +  -  +  - ]:     9120178 :         if (!MoneyRange(coin.out.nValue) || !MoneyRange(nValueIn)) {
     187   [ #  #  #  # ]:           0 :             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputvalues-outofrange");
     188                 :             :         }
     189                 :             :     }
     190                 :             : 
     191                 :     6898481 :     const CAmount value_out = tx.GetValueOut();
     192         [ +  + ]:     6898481 :     if (nValueIn < value_out) {
     193   [ +  -  +  - ]:           6 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-in-belowout",
     194   [ +  -  +  - ]:          12 :             strprintf("value in (%s) < value out (%s)", FormatMoney(nValueIn), FormatMoney(value_out)));
     195                 :             :     }
     196                 :             : 
     197                 :             :     // Tally transaction fees
     198                 :     6898475 :     const CAmount txfee_aux = nValueIn - value_out;
     199         [ -  + ]:     6898475 :     if (!MoneyRange(txfee_aux)) {
     200   [ #  #  #  # ]:           0 :         return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-fee-outofrange");
     201                 :             :     }
     202                 :             : 
     203                 :     6898475 :     txfee = txfee_aux;
     204                 :     6898475 :     return true;
     205                 :             : }
        

Generated by: LCOV version 2.0-1