Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 
7 
8 #include <hash.h>
9 #include <tinyformat.h>
10 #include <utilstrencodings.h>
11 
12 std::string COutPoint::ToString() const
13 {
14  return strprintf("COutPoint(%s, %u)", hash.ToString()/*.substr(0,10)*/, n);
15 }
16 
17 std::string COutPoint::ToStringShort() const
18 {
19  return strprintf("%s-%u", hash.ToString().substr(0,64), n);
20 }
21 
22 CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
23 {
24  prevout = prevoutIn;
25  scriptSig = scriptSigIn;
26  nSequence = nSequenceIn;
27 }
28 
29 CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
30 {
31  prevout = COutPoint(hashPrevTx, nOut);
32  scriptSig = scriptSigIn;
33  nSequence = nSequenceIn;
34 }
35 
36 std::string CTxIn::ToString() const
37 {
38  std::string str;
39  str += "CTxIn(";
40  str += prevout.ToString();
41  if (prevout.IsNull())
42  str += strprintf(", coinbase %s", HexStr(scriptSig));
43  else
44  str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
46  str += strprintf(", nSequence=%u", nSequence);
47  str += ")";
48  return str;
49 }
50 
51 CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn, int nRoundsIn)
52 {
53  nValue = nValueIn;
54  scriptPubKey = scriptPubKeyIn;
55  nRounds = nRoundsIn;
56 }
57 
58 std::string CTxOut::ToString() const
59 {
60  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
61 }
62 
63 CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nType(TRANSACTION_NORMAL), nLockTime(0) {}
64 CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nType(tx.nType), nLockTime(tx.nLockTime), vExtraPayload(tx.vExtraPayload) {}
65 
67 {
68  return SerializeHash(*this);
69 }
70 
71 std::string CMutableTransaction::ToString() const
72 {
73  std::string str;
74  str += strprintf("CMutableTransaction(hash=%s, ver=%d, type=%d, vin.size=%u, vout.size=%u, nLockTime=%u, vExtraPayload.size=%d)\n",
75  GetHash().ToString().substr(0,10),
76  nVersion,
77  nType,
78  vin.size(),
79  vout.size(),
80  nLockTime,
81  vExtraPayload.size());
82  for (unsigned int i = 0; i < vin.size(); i++)
83  str += " " + vin[i].ToString() + "\n";
84  for (unsigned int i = 0; i < vout.size(); i++)
85  str += " " + vout[i].ToString() + "\n";
86  return str;
87 }
88 
90 {
91  return SerializeHash(*this);
92 }
93 
94 /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
95 CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nType(TRANSACTION_NORMAL), nLockTime(0), hash() {}
96 CTransaction::CTransaction(const CMutableTransaction &tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nType(tx.nType), nLockTime(tx.nLockTime), vExtraPayload(tx.vExtraPayload), hash(ComputeHash()) {}
97 CTransaction::CTransaction(CMutableTransaction &&tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nType(tx.nType), nLockTime(tx.nLockTime), vExtraPayload(tx.vExtraPayload), hash(ComputeHash()) {}
98 
100 {
101  CAmount nValueOut = 0;
102  for (const auto& tx_out : vout) {
103  nValueOut += tx_out.nValue;
104  if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut))
105  throw std::runtime_error(std::string(__func__) + ": value out of range");
106  }
107  return nValueOut;
108 }
109 
110 unsigned int CTransaction::GetTotalSize() const
111 {
113 }
114 
115 std::string CTransaction::ToString() const
116 {
117  std::string str;
118  str += strprintf("CTransaction(hash=%s, ver=%d, type=%d, vin.size=%u, vout.size=%u, nLockTime=%u, vExtraPayload.size=%d)\n",
119  GetHash().ToString().substr(0,10),
120  nVersion,
121  nType,
122  vin.size(),
123  vout.size(),
124  nLockTime,
125  vExtraPayload.size());
126  for (const auto& tx_in : vin)
127  str += " " + tx_in.ToString() + "\n";
128  for (const auto& tx_out : vout)
129  str += " " + tx_out.ToString() + "\n";
130  return str;
131 }
CAmount nValue
Definition: transaction.h:147
CScript scriptPubKey
Definition: transaction.h:148
#define strprintf
Definition: tinyformat.h:1066
std::vector< CTxIn > vin
Definition: transaction.h:293
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:1295
static const CAmount COIN
Definition: amount.h:14
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:79
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
Definition: box.hpp:161
std::string ToString() const
Definition: transaction.cpp:36
std::string ToString() const
Definition: transaction.cpp:12
std::string ToString() const
Definition: transaction.cpp:58
const std::vector< CTxIn > vin
Definition: transaction.h:215
CAmount GetValueOut() const
Definition: transaction.cpp:99
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object&#39;s serialization.
Definition: hash.h:254
const uint256 & GetHash() const
Definition: transaction.h:256
uint32_t n
Definition: transaction.h:30
const std::vector< CTxOut > vout
Definition: transaction.h:216
std::string ToString() const
Definition: uint256.cpp:62
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
std::vector< CTxOut > vout
Definition: transaction.h:294
uint256 ComputeHash() const
Definition: transaction.cpp:89
const int16_t nVersion
Definition: transaction.h:217
CScript scriptSig
Definition: transaction.h:74
std::vector< uint8_t > vExtraPayload
Definition: transaction.h:298
256-bit opaque blob.
Definition: uint256.h:123
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:66
std::string ToString() const
Definition: transaction.cpp:71
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
uint32_t nSequence
Definition: transaction.h:75
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:14
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:95
const std::vector< uint8_t > vExtraPayload
Definition: transaction.h:220
std::string ToString() const
int nRounds
Definition: transaction.h:149
A mutable version of CTransaction.
Definition: transaction.h:291
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
bool IsNull() const
Definition: transaction.h:44
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
COutPoint prevout
Definition: transaction.h:73
const int16_t nType
Definition: transaction.h:218
const uint32_t nLockTime
Definition: transaction.h:219
uint256 hash
Definition: transaction.h:29
std::string ToStringShort() const
Definition: transaction.cpp:17
Released under the MIT license