mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-03 01:33:20 +02:00
Merge bitcoin/bitcoin#33116: refactor: Convert uint256 to Txid
de0675f9derefactor: Move `transaction_identifier.h` to primitives (marcofleon)6f068f65deRemove implicit uint256 conversion and comparison (marcofleon)9c24cda72erefactor: Convert remaining instances from uint256 to Txid (marcofleon)d2ecd6815dpolicy, refactor: Convert uint256 to Txid (marcofleon)f6c0d1d231mempool, refactor: Convert uint256 to Txid (marcofleon)aeb0f78330refactor: Convert `mini_miner` from uint256 to Txid (marcofleon)326f244724refactor: Convert RPCs and `merkleblock` from uint256 to Txid (marcofleon)49b3d3a92aClean up `FindTxForGetData` (marcofleon) Pull request description: This is the final leg of the [type safety refactor](https://github.com/bitcoin/bitcoin/pull/32189). All of these changes are straightforward `uint256` --> `Txid` along with any necessary explicit conversions. Also, `transaction_identifier.h` is moved to primitives in the last commit, as `Txid` and `Wtxid` become fundamental types after this PR. ACKs for top commit: stickies-v: re-ACKde0675f9de, no changes since a20724d926d5844168c6a13fa8293df8c8927efe except address review nits. janb84: re ACKde0675f9dedergoegge: re-ACKde0675f9detheStack: Code-review ACKde0675f9deTree-SHA512: 2413160fca7ab146a8d79d18ce3afcf7384cacc73c513d41928904aa453b4dd7a350064cee71e9c5d015da5904c7c81ac17603e50a47441ebc5b0c653235dd08
This commit is contained in:
@@ -7,10 +7,18 @@
|
||||
#include <span.h>
|
||||
#include <util/hasher.h>
|
||||
|
||||
SaltedUint256Hasher::SaltedUint256Hasher() :
|
||||
k0{FastRandomContext().rand64()},
|
||||
k1{FastRandomContext().rand64()} {}
|
||||
|
||||
SaltedTxidHasher::SaltedTxidHasher() :
|
||||
k0{FastRandomContext().rand64()},
|
||||
k1{FastRandomContext().rand64()} {}
|
||||
|
||||
SaltedWtxidHasher::SaltedWtxidHasher() :
|
||||
k0{FastRandomContext().rand64()},
|
||||
k1{FastRandomContext().rand64()} {}
|
||||
|
||||
SaltedOutpointHasher::SaltedOutpointHasher(bool deterministic) :
|
||||
k0{deterministic ? 0x8e819f2607a18de6 : FastRandomContext().rand64()},
|
||||
k1{deterministic ? 0xf4020d2e3983b0eb : FastRandomContext().rand64()}
|
||||
|
||||
@@ -11,9 +11,24 @@
|
||||
#include <span.h>
|
||||
#include <uint256.h>
|
||||
|
||||
#include <concepts>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
class SaltedUint256Hasher
|
||||
{
|
||||
private:
|
||||
/** Salt */
|
||||
const uint64_t k0, k1;
|
||||
|
||||
public:
|
||||
SaltedUint256Hasher();
|
||||
|
||||
size_t operator()(const uint256& hash) const {
|
||||
return SipHashUint256(k0, k1, hash);
|
||||
}
|
||||
};
|
||||
|
||||
class SaltedTxidHasher
|
||||
{
|
||||
private:
|
||||
@@ -23,11 +38,26 @@ private:
|
||||
public:
|
||||
SaltedTxidHasher();
|
||||
|
||||
size_t operator()(const uint256& txid) const {
|
||||
return SipHashUint256(k0, k1, txid);
|
||||
size_t operator()(const Txid& txid) const {
|
||||
return SipHashUint256(k0, k1, txid.ToUint256());
|
||||
}
|
||||
};
|
||||
|
||||
class SaltedWtxidHasher
|
||||
{
|
||||
private:
|
||||
/** Salt */
|
||||
const uint64_t k0, k1;
|
||||
|
||||
public:
|
||||
SaltedWtxidHasher();
|
||||
|
||||
size_t operator()(const Wtxid& wtxid) const {
|
||||
return SipHashUint256(k0, k1, wtxid.ToUint256());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class SaltedOutpointHasher
|
||||
{
|
||||
private:
|
||||
@@ -47,7 +77,7 @@ public:
|
||||
* @see https://gcc.gnu.org/onlinedocs/gcc-13.2.0/libstdc++/manual/manual/unordered_associative.html
|
||||
*/
|
||||
size_t operator()(const COutPoint& id) const noexcept {
|
||||
return SipHashUint256Extra(k0, k1, id.hash, id.n);
|
||||
return SipHashUint256Extra(k0, k1, id.hash.ToUint256(), id.n);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
// Copyright (c) 2023-present The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or https://opensource.org/license/mit.
|
||||
|
||||
#ifndef BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
|
||||
#define BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
|
||||
|
||||
#include <attributes.h>
|
||||
#include <uint256.h>
|
||||
#include <util/types.h>
|
||||
|
||||
#include <compare>
|
||||
#include <concepts>
|
||||
#include <tuple>
|
||||
#include <variant>
|
||||
|
||||
/** transaction_identifier represents the two canonical transaction identifier
|
||||
* types (txid, wtxid).*/
|
||||
template <bool has_witness>
|
||||
class transaction_identifier
|
||||
{
|
||||
uint256 m_wrapped;
|
||||
|
||||
// Note: Use FromUint256 externally instead.
|
||||
transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}
|
||||
|
||||
// TODO: Comparisons with uint256 should be disallowed once we have
|
||||
// converted most of the code to using the new txid types.
|
||||
constexpr int Compare(const uint256& other) const { return m_wrapped.Compare(other); }
|
||||
constexpr int Compare(const transaction_identifier<has_witness>& other) const { return m_wrapped.Compare(other.m_wrapped); }
|
||||
template <typename Other>
|
||||
constexpr int Compare(const Other& other) const
|
||||
{
|
||||
static_assert(ALWAYS_FALSE<Other>, "Forbidden comparison type");
|
||||
return 0;
|
||||
}
|
||||
|
||||
public:
|
||||
transaction_identifier() : m_wrapped{} {}
|
||||
|
||||
template <typename Other>
|
||||
bool operator==(const Other& other) const { return Compare(other) == 0; }
|
||||
template <typename Other>
|
||||
bool operator!=(const Other& other) const { return Compare(other) != 0; }
|
||||
template <typename Other>
|
||||
bool operator<(const Other& other) const { return Compare(other) < 0; }
|
||||
|
||||
const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }
|
||||
static transaction_identifier FromUint256(const uint256& id) { return {id}; }
|
||||
|
||||
/** Wrapped `uint256` methods. */
|
||||
constexpr bool IsNull() const { return m_wrapped.IsNull(); }
|
||||
constexpr void SetNull() { m_wrapped.SetNull(); }
|
||||
static std::optional<transaction_identifier> FromHex(std::string_view hex)
|
||||
{
|
||||
auto u{uint256::FromHex(hex)};
|
||||
if (!u) return std::nullopt;
|
||||
return FromUint256(*u);
|
||||
}
|
||||
std::string GetHex() const { return m_wrapped.GetHex(); }
|
||||
std::string ToString() const { return m_wrapped.ToString(); }
|
||||
static constexpr auto size() { return decltype(m_wrapped)::size(); }
|
||||
constexpr const std::byte* data() const { return reinterpret_cast<const std::byte*>(m_wrapped.data()); }
|
||||
constexpr const std::byte* begin() const { return reinterpret_cast<const std::byte*>(m_wrapped.begin()); }
|
||||
constexpr const std::byte* end() const { return reinterpret_cast<const std::byte*>(m_wrapped.end()); }
|
||||
template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); }
|
||||
template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); }
|
||||
|
||||
/** Conversion function to `uint256`.
|
||||
*
|
||||
* Note: new code should use `ToUint256`.
|
||||
*
|
||||
* TODO: This should be removed once the majority of the code has switched
|
||||
* to using the Txid and Wtxid types. Until then it makes for a smoother
|
||||
* transition to allow this conversion. */
|
||||
operator const uint256&() const LIFETIMEBOUND { return m_wrapped; }
|
||||
};
|
||||
|
||||
/** Txid commits to all transaction fields except the witness. */
|
||||
using Txid = transaction_identifier<false>;
|
||||
/** Wtxid commits to all transaction fields including the witness. */
|
||||
using Wtxid = transaction_identifier<true>;
|
||||
|
||||
template <typename T>
|
||||
concept TxidOrWtxid = std::is_same_v<T, Txid> || std::is_same_v<T, Wtxid>;
|
||||
|
||||
class GenTxid : public std::variant<Txid, Wtxid>
|
||||
{
|
||||
public:
|
||||
using variant::variant;
|
||||
|
||||
bool IsWtxid() const { return std::holds_alternative<Wtxid>(*this); }
|
||||
|
||||
const uint256& ToUint256() const LIFETIMEBOUND
|
||||
{
|
||||
return std::visit([](const auto& id) -> const uint256& { return id.ToUint256(); }, *this);
|
||||
}
|
||||
|
||||
friend auto operator<=>(const GenTxid& a, const GenTxid& b)
|
||||
{
|
||||
// Use a reference for read-only access to the hash, avoiding a copy that might not be optimized away.
|
||||
return std::tuple<bool, const uint256&>(a.IsWtxid(), a.ToUint256()) <=> std::tuple<bool, const uint256&>(b.IsWtxid(), b.ToUint256());
|
||||
}
|
||||
};
|
||||
|
||||
#endif // BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
|
||||
Reference in New Issue
Block a user