LCOV - code coverage report
Current view: top level - src/primitives - transaction.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 29 72 40.3 %
Date: 2023-11-10 23:46:46 Functions: 10 16 62.5 %
Branches: 18 132 13.6 %

           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 <hash.h>
      10                 :            : #include <script/script.h>
      11                 :            : #include <serialize.h>
      12                 :            : #include <tinyformat.h>
      13                 :            : #include <uint256.h>
      14                 :            : #include <util/strencodings.h>
      15                 :            : #include <util/transaction_identifier.h>
      16                 :            : #include <version.h>
      17                 :            : 
      18                 :            : #include <cassert>
      19                 :            : #include <stdexcept>
      20                 :            : 
      21                 :        395 : std::string COutPoint::ToString() const
      22                 :            : {
      23 [ +  - ][ -  + ]:        395 :     return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
      24                 :          0 : }
      25                 :            : 
      26         [ +  - ]:      46819 : CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
      27                 :            : {
      28                 :      46819 :     prevout = prevoutIn;
      29         [ -  + ]:      46819 :     scriptSig = scriptSigIn;
      30                 :      46819 :     nSequence = nSequenceIn;
      31                 :      46819 : }
      32                 :            : 
      33         [ #  # ]:          0 : CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
      34                 :            : {
      35         [ #  # ]:          0 :     prevout = COutPoint(hashPrevTx, nOut);
      36         [ #  # ]:          0 :     scriptSig = scriptSigIn;
      37                 :          0 :     nSequence = nSequenceIn;
      38                 :          0 : }
      39                 :            : 
      40                 :          0 : std::string CTxIn::ToString() const
      41                 :            : {
      42                 :          0 :     std::string str;
      43         [ #  # ]:          0 :     str += "CTxIn(";
      44 [ #  # ][ #  # ]:          0 :     str += prevout.ToString();
      45 [ #  # ][ #  # ]:          0 :     if (prevout.IsNull())
      46 [ #  # ][ #  # ]:          0 :         str += strprintf(", coinbase %s", HexStr(scriptSig));
         [ #  # ][ #  # ]
      47                 :            :     else
      48 [ #  # ][ #  # ]:          0 :         str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
         [ #  # ][ #  # ]
                 [ #  # ]
      49         [ #  # ]:          0 :     if (nSequence != SEQUENCE_FINAL)
      50 [ #  # ][ #  # ]:          0 :         str += strprintf(", nSequence=%u", nSequence);
      51         [ #  # ]:          0 :     str += ")";
      52                 :          0 :     return str;
      53         [ #  # ]:          0 : }
      54                 :            : 
      55                 :      91739 : CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
      56                 :            : {
      57                 :      91739 :     nValue = nValueIn;
      58         [ +  - ]:      91739 :     scriptPubKey = scriptPubKeyIn;
      59                 :      91739 : }
      60                 :            : 
      61                 :          0 : std::string CTxOut::ToString() const
      62                 :            : {
      63 [ #  # ][ #  # ]:          0 :     return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
      64                 :          0 : }
      65                 :            : 
      66                 :      11024 : CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
      67         [ +  - ]:       1025 : CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}
      68                 :            : 
      69                 :          0 : Txid CMutableTransaction::GetHash() const
      70                 :            : {
      71                 :          0 :     return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash());
      72                 :            : }
      73                 :            : 
      74                 :      13676 : Txid CTransaction::ComputeHash() const
      75                 :            : {
      76                 :      13676 :     return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash());
      77                 :            : }
      78                 :            : 
      79                 :      13676 : Wtxid CTransaction::ComputeWitnessHash() const
      80                 :            : {
      81         [ +  + ]:      13676 :     if (!HasWitness()) {
      82                 :      12736 :         return Wtxid::FromUint256(hash.ToUint256());
      83                 :            :     }
      84                 :            : 
      85                 :        940 :     return Wtxid::FromUint256((CHashWriter{0} << *this).GetHash());
      86                 :      13676 : }
      87                 :            : 
      88 [ +  - ][ +  - ]:       9344 : CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
                 [ +  - ]
      89 [ +  - ][ +  - ]:       4332 : CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
      90                 :            : 
      91                 :        300 : CAmount CTransaction::GetValueOut() const
      92                 :            : {
      93                 :        300 :     CAmount nValueOut = 0;
      94         [ +  + ]:        900 :     for (const auto& tx_out : vout) {
      95 [ -  + ][ +  - ]:        600 :         if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
      96 [ #  # ][ #  # ]:          0 :             throw std::runtime_error(std::string(__func__) + ": value out of range");
         [ #  # ][ #  # ]
                 [ #  # ]
      97                 :        600 :         nValueOut += tx_out.nValue;
      98                 :            :     }
      99         [ +  - ]:        300 :     assert(MoneyRange(nValueOut));
     100                 :        300 :     return nValueOut;
     101                 :          0 : }
     102                 :            : 
     103                 :          0 : unsigned int CTransaction::GetTotalSize() const
     104                 :            : {
     105                 :          0 :     return ::GetSerializeSize(*this, PROTOCOL_VERSION);
     106                 :            : }
     107                 :            : 
     108                 :          0 : std::string CTransaction::ToString() const
     109                 :            : {
     110                 :          0 :     std::string str;
     111 [ #  # ][ #  # ]:          0 :     str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
     112 [ #  # ][ #  # ]:          0 :         GetHash().ToString().substr(0,10),
                 [ #  # ]
     113                 :          0 :         nVersion,
     114                 :          0 :         vin.size(),
     115                 :          0 :         vout.size(),
     116                 :          0 :         nLockTime);
     117         [ #  # ]:          0 :     for (const auto& tx_in : vin)
     118 [ #  # ][ #  # ]:          0 :         str += "    " + tx_in.ToString() + "\n";
         [ #  # ][ #  # ]
     119         [ #  # ]:          0 :     for (const auto& tx_in : vin)
     120 [ #  # ][ #  # ]:          0 :         str += "    " + tx_in.scriptWitness.ToString() + "\n";
         [ #  # ][ #  # ]
     121         [ #  # ]:          0 :     for (const auto& tx_out : vout)
     122 [ #  # ][ #  # ]:          0 :         str += "    " + tx_out.ToString() + "\n";
         [ #  # ][ #  # ]
     123                 :          0 :     return str;
     124         [ #  # ]:          0 : }

Generated by: LCOV version 1.14