LCOV - code coverage report
Current view: top level - src/primitives - transaction.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 23 73 31.5 %
Date: 2023-09-26 12:08:55 Functions: 9 17 52.9 %

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

Generated by: LCOV version 1.14