Introduce types for txids & wtxids

This commit is contained in:
dergoegge
2023-07-19 15:37:29 +02:00
parent cdb14d79e8
commit ed70e65016
9 changed files with 109 additions and 33 deletions

View File

@@ -12,6 +12,7 @@
#include <tinyformat.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <util/transaction_identifier.h>
#include <version.h>
#include <cassert>
@@ -65,22 +66,23 @@ std::string CTxOut::ToString() const
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) {}
uint256 CMutableTransaction::GetHash() const
Txid CMutableTransaction::GetHash() const
{
return (CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash();
return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash());
}
uint256 CTransaction::ComputeHash() const
Txid CTransaction::ComputeHash() const
{
return (CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash();
return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash());
}
uint256 CTransaction::ComputeWitnessHash() const
Wtxid CTransaction::ComputeWitnessHash() const
{
if (!HasWitness()) {
return hash;
return Wtxid::FromUint256(hash.ToUint256());
}
return (CHashWriter{0} << *this).GetHash();
return Wtxid::FromUint256((CHashWriter{0} << *this).GetHash());
}
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}

View File

@@ -11,6 +11,7 @@
#include <script/script.h>
#include <serialize.h>
#include <uint256.h>
#include <util/transaction_identifier.h> // IWYU pragma: export
#include <cstddef>
#include <cstdint>
@@ -309,11 +310,11 @@ public:
private:
/** Memory only. */
const uint256 hash;
const uint256 m_witness_hash;
const Txid hash;
const Wtxid m_witness_hash;
uint256 ComputeHash() const;
uint256 ComputeWitnessHash() const;
Txid ComputeHash() const;
Wtxid ComputeWitnessHash() const;
public:
/** Convert a CMutableTransaction into a CTransaction. */
@@ -334,8 +335,8 @@ public:
return vin.empty() && vout.empty();
}
const uint256& GetHash() const { return hash; }
const uint256& GetWitnessHash() const { return m_witness_hash; };
const Txid& GetHash() const { return hash; }
const Wtxid& GetWitnessHash() const { return m_witness_hash; };
// Return sum of txouts.
CAmount GetValueOut() const;
@@ -405,7 +406,7 @@ struct CMutableTransaction
/** Compute the hash of this CMutableTransaction. This is computed on the
* fly, as opposed to GetHash() in CTransaction, which uses a cached result.
*/
uint256 GetHash() const;
Txid GetHash() const;
bool HasWitness() const
{