diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index 91725ecc1..3646d9d1d 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -1,103 +1,107 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include +#include + std::string COutPoint::ToString() const { return strprintf("COutPoint(%s, %u)", txid.ToString().substr(0, 10), n); } std::string CTxIn::ToString() const { std::string str; str += "CTxIn("; str += prevout.ToString(); if (prevout.IsNull()) { str += strprintf(", coinbase %s", HexStr(scriptSig)); } else { str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24)); } if (nSequence != SEQUENCE_FINAL) { str += strprintf(", nSequence=%u", nSequence); } str += ")"; return str; } std::string CTxOut::ToString() const { return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, (nValue % COIN) / SATOSHI, HexStr(scriptPubKey).substr(0, 30)); } CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} CMutableTransaction::CMutableTransaction(const CTransaction &tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {} static uint256 ComputeCMutableTransactionHash(const CMutableTransaction &tx) { return SerializeHash(tx, SER_GETHASH, 0); } TxId CMutableTransaction::GetId() const { return TxId(ComputeCMutableTransactionHash(*this)); } TxHash CMutableTransaction::GetHash() const { return TxHash(ComputeCMutableTransactionHash(*this)); } uint256 CTransaction::ComputeHash() const { return SerializeHash(*this, SER_GETHASH, 0); } /** * For backward compatibility, the hash is initialized to 0. * TODO: remove the need for this default constructor entirely. */ CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash() {} CTransaction::CTransaction(const CMutableTransaction &tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash(ComputeHash()) {} CTransaction::CTransaction(CMutableTransaction &&tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash(ComputeHash()) {} Amount CTransaction::GetValueOut() const { Amount nValueOut = Amount::zero(); for (const auto &tx_out : vout) { - nValueOut += tx_out.nValue; - if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut)) { + if (!MoneyRange(tx_out.nValue) || + !MoneyRange(nValueOut + tx_out.nValue)) { throw std::runtime_error(std::string(__func__) + ": value out of range"); } + nValueOut += tx_out.nValue; } + assert(MoneyRange(nValueOut)); return nValueOut; } unsigned int CTransaction::GetTotalSize() const { return ::GetSerializeSize(*this, PROTOCOL_VERSION); } std::string CTransaction::ToString() const { std::string str; str += strprintf("CTransaction(txid=%s, ver=%d, vin.size=%u, vout.size=%u, " "nLockTime=%u)\n", GetId().ToString().substr(0, 10), nVersion, vin.size(), vout.size(), nLockTime); for (const auto &nVin : vin) { str += " " + nVin.ToString() + "\n"; } for (const auto &nVout : vout) { str += " " + nVout.ToString() + "\n"; } return str; } diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 3e827371f..fb8abe5f7 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -1,887 +1,939 @@ // Copyright (c) 2011-2019 The Bitcoin Core developers // Copyright (c) 2017-2020 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include // For CChainParams #include #include #include #include #include #include #include #include #include #include #include