Raven Core  3.0.0
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include <streams.h>
9 
10 #include "hash.h"
11 #include "tinyformat.h"
12 #include "utilstrencodings.h"
13 
14 std::string COutPoint::ToString() const
15 {
16  return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
17 }
18 
19 std::string COutPoint::ToSerializedString() const
20 {
21  CDataStream stream(PROTOCOL_VERSION, SER_DISK);
22  stream << *this;
23  return stream.str();
24 }
25 
26 CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
27 {
28  prevout = prevoutIn;
29  scriptSig = scriptSigIn;
30  nSequence = nSequenceIn;
31 }
32 
33 CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
34 {
35  prevout = COutPoint(hashPrevTx, nOut);
36  scriptSig = scriptSigIn;
37  nSequence = nSequenceIn;
38 }
39 
40 std::string CTxIn::ToString() const
41 {
42  std::string str;
43  str += "CTxIn(";
44  str += prevout.ToString();
45  if (prevout.IsNull())
46  str += strprintf(", coinbase %s", HexStr(scriptSig));
47  else
48  str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
49  if (nSequence != SEQUENCE_FINAL)
50  str += strprintf(", nSequence=%u", nSequence);
51  str += ")";
52  return str;
53 }
54 
55 CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
56 {
57  nValue = nValueIn;
58  scriptPubKey = scriptPubKeyIn;
59 }
60 
61 std::string CTxOut::ToString() const
62 {
63  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
64 }
65 
66 CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
68 
70 {
71  return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
72 }
73 
75 {
76  return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
77 }
78 
80 {
81  if (!HasWitness()) {
82  return GetHash();
83  }
84  return SerializeHash(*this, SER_GETHASH, 0);
85 }
86 
87 /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
88 CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash() {}
91 
93 {
94  CAmount nValueOut = 0;
95  for (const auto& tx_out : vout) {
96  nValueOut += tx_out.nValue;
97  if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut))
98  throw std::runtime_error(std::string(__func__) + ": value out of range");
99  }
100  return nValueOut;
101 }
102 
103 unsigned int CTransaction::GetTotalSize() const
104 {
105  return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
106 }
107 
108 std::string CTransaction::ToString() const
109 {
110  std::string str;
111  str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
112  GetHash().ToString().substr(0,10),
113  nVersion,
114  vin.size(),
115  vout.size(),
116  nLockTime);
117  for (const auto& tx_in : vin)
118  str += " " + tx_in.ToString() + "\n";
119  for (const auto& tx_in : vin)
120  str += " " + tx_in.scriptWitness.ToString() + "\n";
121  for (const auto& tx_out : vout)
122  str += " " + tx_out.ToString() + "\n";
123  return str;
124 }
uint256 GetWitnessHash() const
Definition: transaction.cpp:79
#define strprintf
Definition: tinyformat.h:1054
std::vector< CTxIn > vin
Definition: transaction.h:391
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:967
std::string ToSerializedString() const
Definition: transaction.cpp:19
std::string str() const
Definition: streams.h:225
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:28
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
std::string ToString() const
Definition: transaction.cpp:40
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
std::string ToString() const
Definition: transaction.cpp:14
std::string ToString() const
Definition: transaction.cpp:61
const uint256 hash
Memory only.
Definition: transaction.h:294
const std::vector< CTxIn > vin
Definition: transaction.h:287
CAmount GetValueOut() const
Definition: transaction.cpp:92
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
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:301
const uint256 & GetHash() const
Definition: transaction.h:320
uint32_t n
Definition: transaction.h:26
const std::vector< CTxOut > vout
Definition: transaction.h:288
std::string ToString() const
Definition: uint256.cpp:63
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
std::vector< CTxOut > vout
Definition: transaction.h:392
bool HasWitness() const
Definition: transaction.h:425
uint256 ComputeHash() const
Definition: transaction.cpp:74
256-bit opaque blob.
Definition: uint256.h:123
const int32_t nVersion
Definition: transaction.h:289
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:88
std::string ToString() const
A mutable version of CTransaction.
Definition: transaction.h:389
unsigned int GetTotalSize() const
RVN END.
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
const uint32_t nLockTime
Definition: transaction.h:290
uint256 hash
Definition: transaction.h:25