Bitcoin Core 31.99.0
P2P Digital Currency
transaction_identifier.h
Go to the documentation of this file.
1// Copyright (c) 2023-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit.
4
5#ifndef BITCOIN_PRIMITIVES_TRANSACTION_IDENTIFIER_H
6#define BITCOIN_PRIMITIVES_TRANSACTION_IDENTIFIER_H
7
8#include <attributes.h>
9#include <uint256.h>
10
11#include <compare>
12#include <cstddef>
13#include <optional>
14#include <string>
15#include <string_view>
16#include <tuple>
17#include <type_traits>
18#include <variant>
19
22template <bool has_witness>
24{
26
27 // Note: Use FromUint256 externally instead.
28 transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}
29
30public:
32 consteval explicit transaction_identifier(std::string_view hex_str) : m_wrapped{uint256{hex_str}} {}
33
34 constexpr bool operator==(const transaction_identifier&) const = default;
35 constexpr auto operator<=>(const transaction_identifier&) const = default;
36
37 const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }
38 static transaction_identifier FromUint256(const uint256& id) { return {id}; }
39
41 constexpr bool IsNull() const { return m_wrapped.IsNull(); }
42 constexpr void SetNull() { m_wrapped.SetNull(); }
43 static std::optional<transaction_identifier> FromHex(std::string_view hex)
44 {
45 auto u{uint256::FromHex(hex)};
46 if (!u) return std::nullopt;
47 return FromUint256(*u);
48 }
49 std::string GetHex() const { return m_wrapped.GetHex(); }
50 std::string ToString() const { return m_wrapped.ToString(); }
51 static constexpr auto size() { return decltype(m_wrapped)::size(); }
52 constexpr const std::byte* data() const { return reinterpret_cast<const std::byte*>(m_wrapped.data()); }
53 constexpr const std::byte* begin() const { return reinterpret_cast<const std::byte*>(m_wrapped.begin()); }
54 constexpr const std::byte* end() const { return reinterpret_cast<const std::byte*>(m_wrapped.end()); }
55 template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); }
56 template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); }
57};
58
63
64template <typename T>
65concept TxidOrWtxid = std::is_same_v<T, Txid> || std::is_same_v<T, Wtxid>;
66
67class GenTxid : public std::variant<Txid, Wtxid>
68{
69public:
70 using variant::variant;
71
72 bool IsWtxid() const { return std::holds_alternative<Wtxid>(*this); }
73
75 {
76 return std::visit([](const auto& id) -> const uint256& { return id.ToUint256(); }, *this);
77 }
78
79 friend auto operator<=>(const GenTxid& a, const GenTxid& b)
80 {
81 // Use a reference for read-only access to the hash, avoiding a copy that might not be optimized away.
82 return std::tuple<bool, const uint256&>(a.IsWtxid(), a.ToUint256()) <=> std::tuple<bool, const uint256&>(b.IsWtxid(), b.ToUint256());
83 }
84};
85
86#endif // BITCOIN_PRIMITIVES_TRANSACTION_IDENTIFIER_H
#define LIFETIMEBOUND
Definition: attributes.h:16
bool IsWtxid() const
const uint256 & ToUint256() const LIFETIMEBOUND
friend auto operator<=>(const GenTxid &a, const GenTxid &b)
constexpr bool IsNull() const
Definition: uint256.h:50
constexpr unsigned char * end()
Definition: uint256.h:102
void Unserialize(Stream &s)
Definition: uint256.h:118
std::string ToString() const
Definition: uint256.cpp:21
constexpr unsigned char * begin()
Definition: uint256.h:101
void Serialize(Stream &s) const
Definition: uint256.h:112
constexpr void SetNull()
Definition: uint256.h:57
std::string GetHex() const
Definition: uint256.cpp:11
constexpr const unsigned char * data() const
Definition: uint256.h:98
transaction_identifier represents the two canonical transaction identifier types (txid,...
std::string ToString() const
constexpr auto operator<=>(const transaction_identifier &) const =default
static constexpr auto size()
void Serialize(Stream &s) const
constexpr const std::byte * begin() const
const uint256 & ToUint256() const LIFETIMEBOUND
constexpr bool IsNull() const
Wrapped uint256 methods.
transaction_identifier(const uint256 &wrapped)
constexpr bool operator==(const transaction_identifier &) const =default
std::string GetHex() const
constexpr const std::byte * data() const
static transaction_identifier FromUint256(const uint256 &id)
constexpr const std::byte * end() const
consteval transaction_identifier(std::string_view hex_str)
static std::optional< transaction_identifier > FromHex(std::string_view hex)
256-bit opaque blob.
Definition: uint256.h:196
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:198
SocketId Stream
Definition: util.h:30