Changeset View
Changeset View
Standalone View
Standalone View
src/primitives/transaction.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "primitives/transaction.h" | #include "primitives/transaction.h" | ||||
#include "hash.h" | #include "hash.h" | ||||
#include "tinyformat.h" | #include "tinyformat.h" | ||||
#include "utilstrencodings.h" | #include "utilstrencodings.h" | ||||
std::string COutPoint::ToString() const { | std::string COutPoint::ToString() const { | ||||
return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0, 10), n); | return strprintf("COutPoint(%s, %u)", unspentid.ToString().substr(0, 10), | ||||
n); | |||||
} | } | ||||
CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) { | CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) { | ||||
prevout = prevoutIn; | prevout = prevoutIn; | ||||
scriptSig = scriptSigIn; | scriptSig = scriptSigIn; | ||||
nSequence = nSequenceIn; | nSequence = nSequenceIn; | ||||
} | } | ||||
CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, | CTxIn::CTxIn(unspentid_t unspentid, uint32_t nOut, CScript scriptSigIn, | ||||
uint32_t nSequenceIn) { | uint32_t nSequenceIn) { | ||||
prevout = COutPoint(hashPrevTx, nOut); | prevout = COutPoint(unspentid, nOut); | ||||
scriptSig = scriptSigIn; | scriptSig = scriptSigIn; | ||||
nSequence = nSequenceIn; | nSequence = nSequenceIn; | ||||
} | } | ||||
std::string CTxIn::ToString() const { | std::string CTxIn::ToString() const { | ||||
std::string str; | std::string str; | ||||
str += "CTxIn("; | str += "CTxIn("; | ||||
str += prevout.ToString(); | str += prevout.ToString(); | ||||
Show All 20 Lines | |||||
} | } | ||||
CMutableTransaction::CMutableTransaction() | CMutableTransaction::CMutableTransaction() | ||||
: nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} | : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} | ||||
CMutableTransaction::CMutableTransaction(const CTransaction &tx) | CMutableTransaction::CMutableTransaction(const CTransaction &tx) | ||||
: nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), | : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), | ||||
nLockTime(tx.nLockTime) {} | nLockTime(tx.nLockTime) {} | ||||
uint256 CMutableTransaction::GetId() const { | unspentid_t CMutableTransaction::GetUnspentid() const { | ||||
return SerializeHash(*this, SER_GETHASH, 0); | return unspentid_t(SerializeHash(*this, SER_GETHASH, 0)); | ||||
} | } | ||||
uint256 CTransaction::ComputeHash() const { | txhash_t CMutableTransaction::GetHash() const { | ||||
return SerializeHash(*this, SER_GETHASH, 0); | return txhash_t(SerializeHash(*this, SER_GETHASH, 0)); | ||||
} | } | ||||
uint256 CTransaction::GetHash() const { | unspentid_t CTransaction::Computeunspentid() const { | ||||
return GetId(); | // TODO Compute Immutable unspentid | ||||
return unspentid_t(SerializeHash(*this, SER_GETHASH, 0)); | |||||
} | |||||
txhash_t CTransaction::ComputeHash() const { | |||||
return txhash_t(SerializeHash(*this, SER_GETHASH, 0)); | |||||
} | } | ||||
/** | /** | ||||
* For backward compatibility, the hash is initialized to 0. | * For backward compatibility, the hash is initialized to 0. | ||||
* TODO: remove the need for this default constructor entirely. | * TODO: remove the need for this default constructor entirely. | ||||
*/ | */ | ||||
CTransaction::CTransaction() | CTransaction::CTransaction() | ||||
: nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0), | : nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0), | ||||
hash() {} | hash() {} | ||||
CTransaction::CTransaction(const CMutableTransaction &tx) | CTransaction::CTransaction(const CMutableTransaction &tx) | ||||
: nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), | : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), | ||||
nLockTime(tx.nLockTime), hash(ComputeHash()) {} | nLockTime(tx.nLockTime), hash(ComputeHash()), | ||||
unspentid(Computeunspentid()) {} | |||||
CTransaction::CTransaction(CMutableTransaction &&tx) | CTransaction::CTransaction(CMutableTransaction &&tx) | ||||
: nVersion(tx.nVersion), vin(std::move(tx.vin)), vout(std::move(tx.vout)), | : nVersion(tx.nVersion), vin(std::move(tx.vin)), vout(std::move(tx.vout)), | ||||
nLockTime(tx.nLockTime), hash(ComputeHash()) {} | nLockTime(tx.nLockTime), hash(ComputeHash()), | ||||
unspentid(Computeunspentid()) {} | |||||
Amount CTransaction::GetValueOut() const { | Amount CTransaction::GetValueOut() const { | ||||
Amount nValueOut = 0; | Amount nValueOut = 0; | ||||
for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); | for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); | ||||
++it) { | ++it) { | ||||
nValueOut += it->nValue; | nValueOut += it->nValue; | ||||
if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut)) | if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut)) | ||||
throw std::runtime_error(std::string(__func__) + | throw std::runtime_error(std::string(__func__) + | ||||
Show All 28 Lines | |||||
} | } | ||||
unsigned int CTransaction::GetTotalSize() const { | unsigned int CTransaction::GetTotalSize() const { | ||||
return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION); | return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION); | ||||
} | } | ||||
std::string CTransaction::ToString() const { | std::string CTransaction::ToString() const { | ||||
std::string str; | std::string str; | ||||
str += strprintf("CTransaction(txid=%s, ver=%d, vin.size=%u, vout.size=%u, " | str += | ||||
strprintf("CTransaction(txhash=%s, ver=%d, vin.size=%u, vout.size=%u, " | |||||
"nLockTime=%u)\n", | "nLockTime=%u)\n", | ||||
GetId().ToString().substr(0, 10), nVersion, vin.size(), | GetHash().ToString().substr(0, 10), nVersion, vin.size(), | ||||
vout.size(), nLockTime); | vout.size(), nLockTime); | ||||
for (unsigned int i = 0; i < vin.size(); i++) | for (unsigned int i = 0; i < vin.size(); i++) | ||||
str += " " + vin[i].ToString() + "\n"; | str += " " + vin[i].ToString() + "\n"; | ||||
for (unsigned int i = 0; i < vout.size(); i++) | for (unsigned int i = 0; i < vout.size(); i++) | ||||
str += " " + vout[i].ToString() + "\n"; | str += " " + vout[i].ToString() + "\n"; | ||||
return str; | return str; | ||||
} | } | ||||
int64_t GetTransactionSize(const CTransaction &tx) { | int64_t GetTransactionSize(const CTransaction &tx) { | ||||
return ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); | return ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); | ||||
} | } |