2014-10-18 02:34:06 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2009-2020 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.
|
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <primitives/transaction.h>
|
2014-10-18 02:34:06 +02:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <hash.h>
|
|
|
|
|
#include <tinyformat.h>
|
2018-10-22 15:51:11 -07:00
|
|
|
#include <util/strencodings.h>
|
2014-10-18 02:34:06 +02:00
|
|
|
|
2020-03-19 14:57:02 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
2019-03-20 11:50:26 +00:00
|
|
|
bool g_con_elementsmode = false;
|
2019-01-29 12:24:13 +00:00
|
|
|
|
2014-10-18 02:34:06 +02:00
|
|
|
std::string COutPoint::ToString() const
|
|
|
|
|
{
|
|
|
|
|
return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-30 19:56:00 -04:00
|
|
|
str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
|
2015-12-07 15:44:16 -05:00
|
|
|
if (nSequence != SEQUENCE_FINAL)
|
2014-10-18 02:34:06 +02:00
|
|
|
str += strprintf(", nSequence=%u", nSequence);
|
2019-03-19 19:50:34 +00:00
|
|
|
if (!assetIssuance.IsNull())
|
|
|
|
|
str += strprintf(", %s", assetIssuance.ToString());
|
2014-10-18 02:34:06 +02:00
|
|
|
str += ")";
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-19 20:26:39 +00:00
|
|
|
CTxOut::CTxOut(const CConfidentialAsset& nAssetIn, const CConfidentialValue& nValueIn, CScript scriptPubKeyIn)
|
2014-10-18 02:34:06 +02:00
|
|
|
{
|
2019-03-19 20:26:39 +00:00
|
|
|
nAsset = nAssetIn;
|
2014-10-18 02:34:06 +02:00
|
|
|
nValue = nValueIn;
|
|
|
|
|
scriptPubKey = scriptPubKeyIn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CTxOut::ToString() const
|
|
|
|
|
{
|
2019-03-19 20:26:39 +00:00
|
|
|
std::string strAsset;
|
|
|
|
|
if (nAsset.IsExplicit())
|
|
|
|
|
strAsset = strprintf("nAsset=%s, ", nAsset.GetAsset().GetHex());
|
|
|
|
|
if (nAsset.IsCommitment())
|
|
|
|
|
strAsset = std::string("nAsset=CONFIDENTIAL, ");
|
|
|
|
|
return strprintf("CTxOut(%snValue=%s, scriptPubKey=%s)", strAsset, (nValue.IsExplicit() ? strprintf("%d.%08d", nValue.GetAmount() / COIN, nValue.GetAmount() % COIN) : std::string("CONFIDENTIAL")), HexStr(scriptPubKey).substr(0, 30));
|
2014-10-18 02:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
|
2019-01-29 12:39:33 +00:00
|
|
|
CMutableTransaction::CMutableTransaction(const CTransaction& tx) :
|
|
|
|
|
vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), witness(tx.witness) {}
|
2014-10-18 02:34:06 +02:00
|
|
|
|
|
|
|
|
uint256 CMutableTransaction::GetHash() const
|
|
|
|
|
{
|
2015-11-06 01:32:04 +01:00
|
|
|
return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
|
2014-10-18 02:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-11 16:23:17 -08:00
|
|
|
uint256 CTransaction::ComputeHash() const
|
2014-10-18 02:34:06 +02:00
|
|
|
{
|
2016-11-11 16:23:17 -08:00
|
|
|
return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
|
2015-11-06 01:32:04 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-09 19:16:24 -04:00
|
|
|
uint256 CTransaction::ComputeWitnessHash() const
|
2015-11-06 01:32:04 +01:00
|
|
|
{
|
2017-03-03 13:33:32 -05:00
|
|
|
if (!HasWitness()) {
|
2018-04-09 19:16:24 -04:00
|
|
|
return hash;
|
2017-03-03 13:33:32 -05:00
|
|
|
}
|
2015-11-06 01:32:04 +01:00
|
|
|
return SerializeHash(*this, SER_GETHASH, 0);
|
2014-10-18 02:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-29 12:39:33 +00:00
|
|
|
// ELEMENTS ONLY
|
|
|
|
|
uint256 CTransaction::GetWitnessOnlyHash() const
|
|
|
|
|
{
|
2019-03-20 11:50:26 +00:00
|
|
|
assert(g_con_elementsmode);
|
2019-01-29 12:39:33 +00:00
|
|
|
|
|
|
|
|
std::vector<uint256> leaves;
|
|
|
|
|
leaves.reserve(std::max(vin.size(), vout.size()));
|
|
|
|
|
/* Inputs */
|
|
|
|
|
for (size_t i = 0; i < vin.size(); ++i) {
|
2019-03-19 20:26:39 +00:00
|
|
|
// Input has no witness OR is null input(coinbase)
|
|
|
|
|
const CTxInWitness& txinwit = (witness.vtxinwit.size() <= i || vin[i].prevout.IsNull()) ? CTxInWitness() : witness.vtxinwit[i];
|
2019-01-29 12:39:33 +00:00
|
|
|
leaves.push_back(txinwit.GetHash());
|
|
|
|
|
}
|
|
|
|
|
uint256 hashIn = ComputeFastMerkleRoot(leaves);
|
|
|
|
|
leaves.clear();
|
|
|
|
|
/* Outputs */
|
|
|
|
|
for (size_t i = 0; i < vout.size(); ++i) {
|
|
|
|
|
const CTxOutWitness& txoutwit = witness.vtxoutwit.size() <= i ? CTxOutWitness() : witness.vtxoutwit[i];
|
|
|
|
|
leaves.push_back(txoutwit.GetHash());
|
|
|
|
|
}
|
|
|
|
|
uint256 hashOut = ComputeFastMerkleRoot(leaves);
|
|
|
|
|
leaves.clear();
|
|
|
|
|
/* Combined */
|
|
|
|
|
leaves.push_back(hashIn);
|
|
|
|
|
leaves.push_back(hashOut);
|
|
|
|
|
return ComputeFastMerkleRoot(leaves);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CTransaction::CTransaction(const CMutableTransaction& tx) :
|
|
|
|
|
vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), witness(tx.witness), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
|
|
|
|
|
CTransaction::CTransaction(CMutableTransaction&& tx) :
|
|
|
|
|
vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), witness(std::move(tx.witness)),hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
|
2014-10-18 02:34:06 +02:00
|
|
|
|
2019-03-19 20:26:39 +00:00
|
|
|
CAmountMap CTransaction::GetValueOutMap() const {
|
|
|
|
|
|
|
|
|
|
CAmountMap values;
|
2017-07-21 12:27:23 +02:00
|
|
|
for (const auto& tx_out : vout) {
|
2019-03-19 20:26:39 +00:00
|
|
|
if (tx_out.nValue.IsExplicit() && tx_out.nAsset.IsExplicit()) {
|
|
|
|
|
CAmountMap m;
|
|
|
|
|
m[tx_out.nAsset.GetAsset()] = tx_out.nValue.GetAmount();
|
2020-11-26 01:08:08 +00:00
|
|
|
if (!MoneyRange(tx_out.nValue.GetAmount()) || !MoneyRange(values[tx_out.nAsset.GetAsset()] + tx_out.nValue.GetAmount()))
|
2019-03-19 20:26:39 +00:00
|
|
|
throw std::runtime_error(std::string(__func__) + ": value out of range");
|
2020-11-26 01:08:08 +00:00
|
|
|
values += m;
|
2019-03-19 20:26:39 +00:00
|
|
|
}
|
2014-10-18 02:34:06 +02:00
|
|
|
}
|
2020-11-26 01:08:08 +00:00
|
|
|
assert(MoneyRange(values));
|
2019-03-19 20:26:39 +00:00
|
|
|
return values;
|
2014-10-18 02:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 22:30:30 +02:00
|
|
|
unsigned int CTransaction::GetTotalSize() const
|
|
|
|
|
{
|
2018-06-22 18:27:18 +00:00
|
|
|
return ::GetSerializeSize(*this, PROTOCOL_VERSION);
|
2016-09-06 22:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-18 02:34:06 +02:00
|
|
|
std::string CTransaction::ToString() const
|
|
|
|
|
{
|
|
|
|
|
std::string str;
|
|
|
|
|
str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
|
|
|
|
|
GetHash().ToString().substr(0,10),
|
|
|
|
|
nVersion,
|
|
|
|
|
vin.size(),
|
|
|
|
|
vout.size(),
|
|
|
|
|
nLockTime);
|
2017-07-21 12:27:23 +02:00
|
|
|
for (const auto& tx_in : vin)
|
|
|
|
|
str += " " + tx_in.ToString() + "\n";
|
2019-01-29 12:39:33 +00:00
|
|
|
for (const auto& tx_in : witness.vtxinwit)
|
2017-07-21 12:27:23 +02:00
|
|
|
str += " " + tx_in.scriptWitness.ToString() + "\n";
|
|
|
|
|
for (const auto& tx_out : vout)
|
|
|
|
|
str += " " + tx_out.ToString() + "\n";
|
2014-10-18 02:34:06 +02:00
|
|
|
return str;
|
|
|
|
|
}
|