mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-14 11:26:09 -05:00
![Ava Chow](/assets/img/avatar_default.png)
c7376babd1
doc: Clarify distinction between util and common libraries in libraries.md (Ryan Ofsky)4f74c59334
util: Move util/string.h functions to util namespace (Ryan Ofsky)4d05d3f3b4
util: add TransactionError includes and namespace declarations (Ryan Ofsky)680eafdc74
util: move fees.h and error.h to common/messages.h (Ryan Ofsky)02e62c6c9a
common: Add PSBTError enum (Ryan Ofsky)0d44c44ae3
util: move error.h TransactionError enum to node/types.h (Ryan Ofsky)9bcce2608d
util: move spanparsing.h to script/parsing.h (Ryan Ofsky)6dd2ad4792
util: move spanparsing.h Split functions to string.h (Ryan Ofsky)23cc8ddff4
util: move HexStr and HexDigit from util to crypto (TheCharlatan)6861f954f8
util: move util/message to common/signmessage (Ryan Ofsky)cc5f29fbea
build: move memory_cleanse from util to crypto (Ryan Ofsky)5b9309420c
build: move chainparamsbase from util to common (Ryan Ofsky)ffa27af24d
test: Add check-deps.sh script to check for unexpected library dependencies (Ryan Ofsky) Pull request description: Remove `fees.h`, `errors.h`, and `spanparsing.h` from the util library. Specifically: - Move `Split` functions from `util/spanparsing.h` to `util/string.h`, using `util` namespace for clarity. - Move remaining spanparsing functions to `script/parsing.h` since they are used for descriptor and miniscript parsing. - Combine `util/fees.h` and `util/errors.h` into `common/messages.h` so there is a place for simple functions that generate user messages to live, and these functions are not part of the util library. Motivation for this change is that the util library is a dependency of the kernel, and we should remove functionality from util that shouldn't be called by kernel code or kernel applications. These changes should also improve code organization and make functions easier to discover. Some of these same moves are (or were) part of #28690, but did not help with code organization, or made it worse, so it is better to move them and clean them up in the same PR so code only has to change one time. ACKs for top commit: achow101: ACKc7376babd1
TheCharlatan: Re-ACKc7376babd1
hebasto: re-ACKc7376babd1
. Tree-SHA512: 5bcef16c1255463b1b69270548711e7ff78ca0dd34e300b95e3ca1ce52ceb34f83d9ddb2839e83800ba36b200de30396e504bbb04fa02c6d0c24a16d06ae523d
131 lines
4.1 KiB
C++
131 lines
4.1 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2022 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 <primitives/transaction.h>
|
|
|
|
#include <consensus/amount.h>
|
|
#include <crypto/hex_base.h>
|
|
#include <hash.h>
|
|
#include <script/script.h>
|
|
#include <serialize.h>
|
|
#include <tinyformat.h>
|
|
#include <uint256.h>
|
|
#include <util/transaction_identifier.h>
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <stdexcept>
|
|
|
|
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(Txid 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
|
|
str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
|
|
if (nSequence != SEQUENCE_FINAL)
|
|
str += strprintf(", nSequence=%u", nSequence);
|
|
str += ")";
|
|
return str;
|
|
}
|
|
|
|
CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
|
|
{
|
|
nValue = nValueIn;
|
|
scriptPubKey = scriptPubKeyIn;
|
|
}
|
|
|
|
std::string CTxOut::ToString() const
|
|
{
|
|
return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
|
|
}
|
|
|
|
CMutableTransaction::CMutableTransaction() : version{CTransaction::CURRENT_VERSION}, nLockTime{0} {}
|
|
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), version{tx.version}, nLockTime{tx.nLockTime} {}
|
|
|
|
Txid CMutableTransaction::GetHash() const
|
|
{
|
|
return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
|
|
}
|
|
|
|
bool CTransaction::ComputeHasWitness() const
|
|
{
|
|
return std::any_of(vin.begin(), vin.end(), [](const auto& input) {
|
|
return !input.scriptWitness.IsNull();
|
|
});
|
|
}
|
|
|
|
Txid CTransaction::ComputeHash() const
|
|
{
|
|
return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
|
|
}
|
|
|
|
Wtxid CTransaction::ComputeWitnessHash() const
|
|
{
|
|
if (!HasWitness()) {
|
|
return Wtxid::FromUint256(hash.ToUint256());
|
|
}
|
|
|
|
return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash());
|
|
}
|
|
|
|
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), version{tx.version}, nLockTime{tx.nLockTime}, m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
|
|
CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), version{tx.version}, nLockTime{tx.nLockTime}, m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
|
|
|
|
CAmount CTransaction::GetValueOut() const
|
|
{
|
|
CAmount nValueOut = 0;
|
|
for (const auto& tx_out : vout) {
|
|
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(TX_WITH_WITNESS(*this));
|
|
}
|
|
|
|
std::string CTransaction::ToString() const
|
|
{
|
|
std::string str;
|
|
str += strprintf("CTransaction(hash=%s, ver=%u, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
|
|
GetHash().ToString().substr(0,10),
|
|
version,
|
|
vin.size(),
|
|
vout.size(),
|
|
nLockTime);
|
|
for (const auto& tx_in : vin)
|
|
str += " " + tx_in.ToString() + "\n";
|
|
for (const auto& tx_in : vin)
|
|
str += " " + tx_in.scriptWitness.ToString() + "\n";
|
|
for (const auto& tx_out : vout)
|
|
str += " " + tx_out.ToString() + "\n";
|
|
return str;
|
|
}
|