LCOV - code coverage report
Current view: top level - src/primitives - transaction.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 70 74 94.6 %
Date: 2020-09-26 01:30:44 Functions: 25 25 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2020 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 <hash.h>
       9             : #include <tinyformat.h>
      10             : #include <util/strencodings.h>
      11             : 
      12             : #include <assert.h>
      13             : 
      14        3499 : std::string COutPoint::ToString() const
      15             : {
      16        3499 :     return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
      17           0 : }
      18             : 
      19      738748 : CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
      20      369374 : {
      21      369374 :     prevout = prevoutIn;
      22      369374 :     scriptSig = scriptSigIn;
      23      369374 :     nSequence = nSequenceIn;
      24      738748 : }
      25             : 
      26       50632 : CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
      27       25316 : {
      28       25316 :     prevout = COutPoint(hashPrevTx, nOut);
      29       25316 :     scriptSig = scriptSigIn;
      30       25316 :     nSequence = nSequenceIn;
      31       50632 : }
      32             : 
      33        3499 : std::string CTxIn::ToString() const
      34             : {
      35        3499 :     std::string str;
      36        3499 :     str += "CTxIn(";
      37        3499 :     str += prevout.ToString();
      38        3499 :     if (prevout.IsNull())
      39           0 :         str += strprintf(", coinbase %s", HexStr(scriptSig));
      40             :     else
      41        3499 :         str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
      42        3499 :     if (nSequence != SEQUENCE_FINAL)
      43        3499 :         str += strprintf(", nSequence=%u", nSequence);
      44        3499 :     str += ")";
      45             :     return str;
      46        3499 : }
      47             : 
      48      166260 : CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
      49       83130 : {
      50       83130 :     nValue = nValueIn;
      51       83130 :     scriptPubKey = scriptPubKeyIn;
      52      166260 : }
      53             : 
      54        3778 : std::string CTxOut::ToString() const
      55             : {
      56        3778 :     return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
      57           0 : }
      58             : 
      59     1498424 : CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
      60      252134 : CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}
      61             : 
      62       68235 : uint256 CMutableTransaction::GetHash() const
      63             : {
      64       68235 :     return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
      65             : }
      66             : 
      67      870239 : uint256 CTransaction::ComputeHash() const
      68             : {
      69      870239 :     return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
      70             : }
      71             : 
      72      870240 : uint256 CTransaction::ComputeWitnessHash() const
      73             : {
      74      870240 :     if (!HasWitness()) {
      75      688542 :         return hash;
      76             :     }
      77      181698 :     return SerializeHash(*this, SER_GETHASH, 0);
      78      870240 : }
      79             : 
      80             : /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
      81         182 : CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash{}, m_witness_hash{} {}
      82      534036 : CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
      83     1206441 : 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()} {}
      84             : 
      85     5259739 : CAmount CTransaction::GetValueOut() const
      86             : {
      87     5259739 :     CAmount nValueOut = 0;
      88    16363136 :     for (const auto& tx_out : vout) {
      89    11103397 :         if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
      90           0 :             throw std::runtime_error(std::string(__func__) + ": value out of range");
      91    11103397 :         nValueOut += tx_out.nValue;
      92             :     }
      93     5259739 :     assert(MoneyRange(nValueOut));
      94    10519478 :     return nValueOut;
      95     5259739 : }
      96             : 
      97          34 : unsigned int CTransaction::GetTotalSize() const
      98             : {
      99          34 :     return ::GetSerializeSize(*this, PROTOCOL_VERSION);
     100             : }
     101             : 
     102        1184 : std::string CTransaction::ToString() const
     103             : {
     104        1184 :     std::string str;
     105        2368 :     str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
     106        1184 :         GetHash().ToString().substr(0,10),
     107        1184 :         nVersion,
     108        1184 :         vin.size(),
     109        1184 :         vout.size(),
     110        1184 :         nLockTime);
     111        4683 :     for (const auto& tx_in : vin)
     112        3499 :         str += "    " + tx_in.ToString() + "\n";
     113        4683 :     for (const auto& tx_in : vin)
     114        3499 :         str += "    " + tx_in.scriptWitness.ToString() + "\n";
     115        4960 :     for (const auto& tx_out : vout)
     116        3776 :         str += "    " + tx_out.ToString() + "\n";
     117             :     return str;
     118        1184 : }

Generated by: LCOV version 1.15