2014-10-18 02:34:06 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-10-18 02:34:06 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-11-18 22:03:02 +01:00
|
|
|
#include "primitives/transaction.h"
|
2014-10-18 02:34:06 +02:00
|
|
|
|
|
|
|
#include "hash.h"
|
|
|
|
#include "tinyformat.h"
|
|
|
|
#include "utilstrencodings.h"
|
|
|
|
|
|
|
|
std::string COutPoint::ToString() const
|
|
|
|
{
|
2015-04-03 00:51:08 +02:00
|
|
|
return strprintf("COutPoint(%s, %u)", hash.ToString()/*.substr(0,10)*/, n);
|
2014-10-18 02:34:06 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 16:08:06 +02:00
|
|
|
std::string COutPoint::ToStringShort() const
|
|
|
|
{
|
|
|
|
return strprintf("%s-%u", hash.ToString().substr(0,64), n);
|
|
|
|
}
|
|
|
|
|
2014-10-18 02:34:06 +02:00
|
|
|
CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
|
|
|
|
{
|
|
|
|
prevout = prevoutIn;
|
|
|
|
scriptSig = scriptSigIn;
|
|
|
|
nSequence = nSequenceIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
|
|
|
|
{
|
|
|
|
prevout = COutPoint(hashPrevTx, nOut);
|
|
|
|
scriptSig = scriptSigIn;
|
|
|
|
nSequence = nSequenceIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CTxIn::ToString() const
|
|
|
|
{
|
|
|
|
std::string str;
|
|
|
|
str += "CTxIn(";
|
|
|
|
str += prevout.ToString();
|
|
|
|
if (prevout.IsNull())
|
|
|
|
str += strprintf(", coinbase %s", HexStr(scriptSig));
|
|
|
|
else
|
2015-07-31 01:56:00 +02:00
|
|
|
str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
|
2015-12-07 21:44:16 +01:00
|
|
|
if (nSequence != SEQUENCE_FINAL)
|
2014-10-18 02:34:06 +02:00
|
|
|
str += strprintf(", nSequence=%u", nSequence);
|
|
|
|
str += ")";
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-09-15 12:18:32 +02:00
|
|
|
CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn, int nRoundsIn)
|
2014-10-18 02:34:06 +02:00
|
|
|
{
|
|
|
|
nValue = nValueIn;
|
|
|
|
scriptPubKey = scriptPubKeyIn;
|
2018-09-15 12:18:32 +02:00
|
|
|
nRounds = nRoundsIn;
|
2014-10-18 02:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CTxOut::ToString() const
|
|
|
|
{
|
2015-07-31 01:56:00 +02:00
|
|
|
return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
|
2014-10-18 02:34:06 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 14:44:32 +01:00
|
|
|
CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nType(TRANSACTION_NORMAL), nLockTime(0) {}
|
|
|
|
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), nType(tx.nType), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime), vExtraPayload(tx.vExtraPayload) {}
|
2014-10-18 02:34:06 +02:00
|
|
|
|
|
|
|
uint256 CMutableTransaction::GetHash() const
|
|
|
|
{
|
|
|
|
return SerializeHash(*this);
|
|
|
|
}
|
|
|
|
|
2015-04-03 00:51:08 +02:00
|
|
|
std::string CMutableTransaction::ToString() const
|
|
|
|
{
|
|
|
|
std::string str;
|
2018-02-12 14:44:32 +01:00
|
|
|
str += strprintf("CMutableTransaction(hash=%s, ver=%d, type=%d, vin.size=%u, vout.size=%u, nLockTime=%u, vExtraPayload.size=%d)\n",
|
2015-11-09 01:52:22 +01:00
|
|
|
GetHash().ToString().substr(0,10),
|
2015-04-03 00:51:08 +02:00
|
|
|
nVersion,
|
2018-02-12 14:44:32 +01:00
|
|
|
nType,
|
2015-04-03 00:51:08 +02:00
|
|
|
vin.size(),
|
|
|
|
vout.size(),
|
2018-02-12 14:44:32 +01:00
|
|
|
nLockTime,
|
|
|
|
vExtraPayload.size());
|
2015-04-03 00:51:08 +02:00
|
|
|
for (unsigned int i = 0; i < vin.size(); i++)
|
|
|
|
str += " " + vin[i].ToString() + "\n";
|
|
|
|
for (unsigned int i = 0; i < vout.size(); i++)
|
|
|
|
str += " " + vout[i].ToString() + "\n";
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-12-05 08:01:20 +01:00
|
|
|
uint256 CTransaction::ComputeHash() const
|
2014-10-18 02:34:06 +02:00
|
|
|
{
|
2016-12-05 08:01:20 +01:00
|
|
|
return SerializeHash(*this);
|
2016-11-21 10:51:32 +01:00
|
|
|
}
|
|
|
|
|
2016-12-05 08:01:20 +01:00
|
|
|
/* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
|
2018-02-12 14:44:32 +01:00
|
|
|
CTransaction::CTransaction() : nVersion(CTransaction::CURRENT_VERSION), nType(TRANSACTION_NORMAL), vin(), vout(), nLockTime(0), hash() {}
|
|
|
|
CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), nType(tx.nType), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime), vExtraPayload(tx.vExtraPayload), hash(ComputeHash()) {}
|
|
|
|
CTransaction::CTransaction(CMutableTransaction &&tx) : nVersion(tx.nVersion), nType(tx.nType), vin(std::move(tx.vin)), vout(std::move(tx.vout)), nLockTime(tx.nLockTime), vExtraPayload(tx.vExtraPayload), hash(ComputeHash()) {}
|
2014-10-18 02:34:06 +02:00
|
|
|
|
|
|
|
CAmount CTransaction::GetValueOut() const
|
|
|
|
{
|
|
|
|
CAmount nValueOut = 0;
|
2017-07-27 13:59:19 +02:00
|
|
|
for (const auto& tx_out : vout) {
|
|
|
|
nValueOut += tx_out.nValue;
|
|
|
|
if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut))
|
2016-08-22 09:58:00 +02:00
|
|
|
throw std::runtime_error(std::string(__func__) + ": value out of range");
|
2014-10-18 02:34:06 +02:00
|
|
|
}
|
|
|
|
return nValueOut;
|
|
|
|
}
|
|
|
|
|
2017-09-09 09:04:02 +02:00
|
|
|
unsigned int CTransaction::GetTotalSize() const
|
|
|
|
{
|
|
|
|
return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
}
|
|
|
|
|
2014-10-18 02:34:06 +02:00
|
|
|
std::string CTransaction::ToString() const
|
|
|
|
{
|
|
|
|
std::string str;
|
2018-02-12 14:44:32 +01:00
|
|
|
str += strprintf("CTransaction(hash=%s, ver=%d, type=%d, vin.size=%u, vout.size=%u, nLockTime=%u, vExtraPayload.size=%d)\n",
|
2014-10-18 02:34:06 +02:00
|
|
|
GetHash().ToString().substr(0,10),
|
|
|
|
nVersion,
|
2018-02-12 14:44:32 +01:00
|
|
|
nType,
|
2014-10-18 02:34:06 +02:00
|
|
|
vin.size(),
|
|
|
|
vout.size(),
|
2018-02-12 14:44:32 +01:00
|
|
|
nLockTime,
|
|
|
|
vExtraPayload.size());
|
2017-07-27 13:59:19 +02:00
|
|
|
for (const auto& tx_in : vin)
|
|
|
|
str += " " + tx_in.ToString() + "\n";
|
|
|
|
for (const auto& tx_out : vout)
|
|
|
|
str += " " + tx_out.ToString() + "\n";
|
2014-10-18 02:34:06 +02:00
|
|
|
return str;
|
|
|
|
}
|