LCOV - code coverage report
Current view: top level - src/primitives - transaction.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 94.4 % 72 68
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 17 17
Branches: 56.2 % 112 63

             Branch data     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                 :             : #include <primitives/transaction.h>
       7                 :             : 
       8                 :             : #include <consensus/amount.h>
       9                 :             : #include <crypto/hex_base.h>
      10                 :             : #include <hash.h>
      11                 :             : #include <script/script.h>
      12                 :             : #include <serialize.h>
      13                 :             : #include <tinyformat.h>
      14                 :             : #include <uint256.h>
      15                 :             : #include <util/transaction_identifier.h>
      16                 :             : 
      17                 :             : #include <algorithm>
      18                 :             : #include <cassert>
      19                 :             : #include <stdexcept>
      20                 :             : 
      21                 :         113 : std::string COutPoint::ToString() const
      22                 :             : {
      23   [ +  -  +  - ]:         226 :     return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
      24                 :             : }
      25                 :             : 
      26                 :         282 : CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
      27                 :             : {
      28                 :         282 :     prevout = prevoutIn;
      29                 :         282 :     scriptSig = scriptSigIn;
      30                 :         282 :     nSequence = nSequenceIn;
      31                 :         282 : }
      32                 :             : 
      33                 :          53 : CTxIn::CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
      34                 :             : {
      35                 :          53 :     prevout = COutPoint(hashPrevTx, nOut);
      36                 :          53 :     scriptSig = scriptSigIn;
      37                 :          53 :     nSequence = nSequenceIn;
      38                 :          53 : }
      39                 :             : 
      40                 :           5 : std::string CTxIn::ToString() const
      41                 :             : {
      42         [ +  - ]:           5 :     std::string str;
      43         [ +  - ]:           5 :     str += "CTxIn(";
      44         [ +  - ]:          10 :     str += prevout.ToString();
      45         [ -  + ]:           5 :     if (prevout.IsNull())
      46   [ #  #  #  #  :           0 :         str += strprintf(", coinbase %s", HexStr(scriptSig));
                   #  # ]
      47                 :             :     else
      48   [ -  +  +  -  :          15 :         str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
             +  -  +  - ]
      49         [ +  - ]:           5 :     if (nSequence != SEQUENCE_FINAL)
      50         [ +  - ]:          10 :         str += strprintf(", nSequence=%u", nSequence);
      51         [ +  - ]:           5 :     str += ")";
      52                 :           5 :     return str;
      53                 :           0 : }
      54                 :             : 
      55                 :         447 : CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
      56                 :             : {
      57                 :         447 :     nValue = nValueIn;
      58                 :         447 :     scriptPubKey = scriptPubKeyIn;
      59                 :         447 : }
      60                 :             : 
      61                 :          10 : std::string CTxOut::ToString() const
      62                 :             : {
      63   [ +  +  +  -  :          30 :     return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
                   +  - ]
      64                 :             : }
      65                 :             : 
      66                 :      390277 : CMutableTransaction::CMutableTransaction() : version{CTransaction::CURRENT_VERSION}, nLockTime{0} {}
      67         [ +  - ]:       82074 : CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), version{tx.version}, nLockTime{tx.nLockTime} {}
      68                 :             : 
      69                 :      234413 : Txid CMutableTransaction::GetHash() const
      70                 :             : {
      71                 :      234413 :     return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
      72                 :             : }
      73                 :             : 
      74                 :      557696 : bool CTransaction::ComputeHasWitness() const
      75                 :             : {
      76                 :      994328 :     return std::any_of(vin.begin(), vin.end(), [](const auto& input) {
      77   [ +  +  +  +  :      369229 :         return !input.scriptWitness.IsNull();
          +  +  -  +  +  
             +  +  +  +  
                      + ]
      78                 :      557696 :     });
      79                 :             : }
      80                 :             : 
      81                 :      557696 : Txid CTransaction::ComputeHash() const
      82                 :             : {
      83                 :      557696 :     return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
      84                 :             : }
      85                 :             : 
      86                 :      557696 : Wtxid CTransaction::ComputeWitnessHash() const
      87                 :             : {
      88         [ +  + ]:      557696 :     if (!HasWitness()) {
      89                 :      535097 :         return Wtxid::FromUint256(hash.ToUint256());
      90                 :             :     }
      91                 :             : 
      92                 :       22599 :     return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash());
      93                 :             : }
      94                 :             : 
      95   [ +  -  +  -  :      246607 : CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), version{tx.version}, nLockTime{tx.nLockTime}, m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
                   +  - ]
      96   [ +  -  +  - ]:      311089 : CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), version{tx.version}, nLockTime{tx.nLockTime}, m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
      97                 :             : 
      98                 :       16218 : CAmount CTransaction::GetValueOut() const
      99                 :             : {
     100                 :       16218 :     CAmount nValueOut = 0;
     101         [ +  + ]:       47353 :     for (const auto& tx_out : vout) {
     102   [ +  -  -  + ]:       31135 :         if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
     103   [ #  #  #  # ]:           0 :             throw std::runtime_error(std::string(__func__) + ": value out of range");
     104                 :       31135 :         nValueOut += tx_out.nValue;
     105                 :             :     }
     106         [ -  + ]:       16218 :     assert(MoneyRange(nValueOut));
     107                 :       16218 :     return nValueOut;
     108                 :             : }
     109                 :             : 
     110                 :          28 : unsigned int CTransaction::GetTotalSize() const
     111                 :             : {
     112                 :          28 :     return ::GetSerializeSize(TX_WITH_WITNESS(*this));
     113                 :             : }
     114                 :             : 
     115                 :           5 : std::string CTransaction::ToString() const
     116                 :             : {
     117         [ +  - ]:           5 :     std::string str;
     118         [ +  - ]:          10 :     str += strprintf("CTransaction(hash=%s, ver=%u, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
     119         [ +  - ]:          10 :         GetHash().ToString().substr(0,10),
     120                 :           5 :         version,
     121         [ +  - ]:           5 :         vin.size(),
     122                 :           5 :         vout.size(),
     123         [ +  - ]:          10 :         nLockTime);
     124         [ +  + ]:          10 :     for (const auto& tx_in : vin)
     125   [ +  -  +  -  :          15 :         str += "    " + tx_in.ToString() + "\n";
                   +  - ]
     126         [ +  + ]:          10 :     for (const auto& tx_in : vin)
     127   [ +  -  +  -  :          15 :         str += "    " + tx_in.scriptWitness.ToString() + "\n";
                   +  - ]
     128         [ +  + ]:          15 :     for (const auto& tx_out : vout)
     129   [ +  -  +  -  :          30 :         str += "    " + tx_out.ToString() + "\n";
                   +  - ]
     130                 :           5 :     return str;
     131                 :           0 : }
        

Generated by: LCOV version 2.0-1