Fabcoin Core  0.16.2
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 #include <consensus/consensus.h>
12 
13 std::string COutPoint::ToString() const
14 {
15  return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
16 }
17 
18 CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
19 {
20  prevout = prevoutIn;
21  scriptSig = scriptSigIn;
22  nSequence = nSequenceIn;
23 }
24 
25 CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
26 {
27  prevout = COutPoint(hashPrevTx, nOut);
28  scriptSig = scriptSigIn;
29  nSequence = nSequenceIn;
30 }
31 
32 std::string CTxIn::ToString() const
33 {
34  std::string str;
35  str += "CTxIn(";
36  str += prevout.ToString();
37  if (prevout.IsNull())
38  str += strprintf(", coinbase %s", HexStr(scriptSig));
39  else
40  str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
41  if (nSequence != SEQUENCE_FINAL)
42  str += strprintf(", nSequence=%u", nSequence);
43  str += ")";
44  return str;
45 }
46 
47 CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
48 {
49  nValue = nValueIn;
50  scriptPubKey = scriptPubKeyIn;
51 }
52 
53 std::string CTxOut::ToString() const
54 {
55  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
56 }
57 
58 CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
60 
62 {
63  return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
64 }
65 
67 {
68  return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
69 }
70 
72 {
73  if (!HasWitness()) {
74  return GetHash();
75  }
76  return SerializeHash(*this, SER_GETHASH, 0);
77 }
78 
79 /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
80 CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash() {}
83 
85 {
86  CAmount nValueOut = 0;
87  for (const auto& tx_out : vout) {
88  nValueOut += tx_out.nValue;
89  if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut))
90  throw std::runtime_error(std::string(__func__) + ": value out of range");
91  }
92  return nValueOut;
93 }
94 
95 unsigned int CTransaction::GetTotalSize() const
96 {
97  return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
98 }
99 
100 std::string CTransaction::ToString() const
101 {
102  std::string str;
103  str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
104  GetHash().ToString().substr(0,10),
105  nVersion,
106  vin.size(),
107  vout.size(),
108  nLockTime);
109  for (const auto& tx_in : vin)
110  str += " " + tx_in.ToString() + "\n";
111  for (const auto& tx_in : vin)
112  str += " " + tx_in.scriptWitness.ToString() + "\n";
113  for (const auto& tx_out : vout)
114  str += " " + tx_out.ToString() + "\n";
115  return str;
116 }
118 {
119  return ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR -1) + ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
120 }
123  for(const CTxOut& v : vout){
124  if(v.scriptPubKey.HasOpCreate() || v.scriptPubKey.HasOpCall()){
125  return true;
126  }
127  }
128  return false;
129 }
131  for(const CTxIn& i : vin){
132  if(i.scriptSig.HasOpSpend()){
133  return true;
134  }
135  }
136  return false;
137 }
139 
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:95
int64_t GetTransactionWeight(const CTransaction &tx)
uint256 ComputeHash() const
Definition: transaction.cpp:66
#define strprintf
Definition: tinyformat.h:1054
uint256 GetWitnessHash() const
Definition: transaction.cpp:71
std::vector< CTxIn > vin
Definition: transaction.h:392
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:989
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:24
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
std::hash for asio::adress
Definition: Common.h:323
const uint256 hash
Memory only.
Definition: transaction.h:299
std::string ToString() const
Definition: transaction.cpp:53
const std::vector< CTxIn > vin
Definition: transaction.h:292
std::string ToString() const
Definition: uint256.cpp:95
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
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:200
An input of a transaction.
Definition: transaction.h:61
CAmount GetValueOut() const
Definition: transaction.cpp:84
uint32_t n
Definition: transaction.h:22
const std::vector< CTxOut > vout
Definition: transaction.h:293
An output of a transaction.
Definition: transaction.h:131
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::vector< CTxOut > vout
Definition: transaction.h:393
std::string ToString() const
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:61
std::string ToString() const
Definition: transaction.cpp:32
std::string ToString() const
Definition: transaction.cpp:13
256-bit opaque blob.
Definition: uint256.h:132
const int32_t nVersion
Definition: transaction.h:294
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:417
CTxIn()
Definition: transaction.h:96
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:80
bool HasCreateOrCall() const
A mutable version of CTransaction.
Definition: transaction.h:390
const uint256 & GetHash() const
Definition: transaction.h:325
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:275
bool HasOpSpend() const
const uint32_t nLockTime
Definition: transaction.h:295
bool HasWitness() const
Definition: transaction.h:426
uint256 hash
Definition: transaction.h:21