1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2021 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 <tinyformat.h>
11
#include <util/strencodings.h>
12

            
13
#include <assert.h>
14

            
15
std::string COutPoint::ToString() const
16
{
17
    return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
18
}
19

            
20
CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
21
{
22
    prevout = prevoutIn;
23
    scriptSig = scriptSigIn;
24
    nSequence = nSequenceIn;
25
}
26

            
27
CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
28
{
29
    prevout = COutPoint(hashPrevTx, nOut);
30
    scriptSig = scriptSigIn;
31
    nSequence = nSequenceIn;
32
}
33

            
34
std::string CTxIn::ToString() const
35
{
36
    std::string str;
37
    str += "CTxIn(";
38
    str += prevout.ToString();
39
    if (prevout.IsNull())
40
        str += strprintf(", coinbase %s", HexStr(scriptSig));
41
    else
42
        str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
43
    if (nSequence != SEQUENCE_FINAL)
44
        str += strprintf(", nSequence=%u", nSequence);
45
    str += ")";
46
    return str;
47
}
48

            
49
CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
50
{
51
    nValue = nValueIn;
52
    scriptPubKey = scriptPubKeyIn;
53
}
54

            
55
std::string CTxOut::ToString() const
56
{
57
    return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
58
}
59

            
60
CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
61
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}
62

            
63
uint256 CMutableTransaction::GetHash() const
64
{
65
    return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
66
}
67

            
68
uint256 CTransaction::ComputeHash() const
69
{
70
    return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
71
}
72

            
73
uint256 CTransaction::ComputeWitnessHash() const
74
{
75
    if (!HasWitness()) {
76
        return hash;
77
    }
78
    return SerializeHash(*this, SER_GETHASH, 0);
79
}
80

            
81
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
82
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()} {}
83

            
84
CAmount CTransaction::GetValueOut() const
85
{
86
    CAmount nValueOut = 0;
87
    for (const auto& tx_out : vout) {
88
        if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
89
            throw std::runtime_error(std::string(__func__) + ": value out of range");
90
        nValueOut += tx_out.nValue;
91
    }
92
    assert(MoneyRange(nValueOut));
93
    return nValueOut;
94
}
95

            
96
unsigned int CTransaction::GetTotalSize() const
97
{
98
    return ::GetSerializeSize(*this, PROTOCOL_VERSION);
99
}
100

            
101
std::string CTransaction::ToString() const
102
{
103
    std::string str;
104
    str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
105
        GetHash().ToString().substr(0,10),
106
        nVersion,
107
        vin.size(),
108
        vout.size(),
109
        nLockTime);
110
    for (const auto& tx_in : vin)
111
        str += "    " + tx_in.ToString() + "\n";
112
    for (const auto& tx_in : vin)
113
        str += "    " + tx_in.scriptWitness.ToString() + "\n";
114
    for (const auto& tx_out : vout)
115
        str += "    " + tx_out.ToString() + "\n";
116
    return str;
117
}