Branch data Line data Source code
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 : : #include <util/types.h>
11 : :
12 : : #include <cstddef>
13 : : #include <optional>
14 : : #include <string>
15 : : #include <string_view>
16 : : #include <tuple>
17 : : #include <type_traits>
18 : : #include <variant>
19 : :
20 : : /** transaction_identifier represents the two canonical transaction identifier
21 : : * types (txid, wtxid).*/
22 : : template <bool has_witness>
23 : : class transaction_identifier
24 : : {
25 : : uint256 m_wrapped;
26 : :
27 : : // Note: Use FromUint256 externally instead.
28 : 2130921 : transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}
29 : :
30 [ - - + + : 122374016 : constexpr int Compare(const transaction_identifier<has_witness>& other) const { return m_wrapped.Compare(other.m_wrapped); }
+ + - + -
- - - - +
+ - - - +
+ ]
31 : : template <typename Other>
32 : : constexpr int Compare(const Other& other) const
33 : : {
34 : : static_assert(ALWAYS_FALSE<Other>, "Forbidden comparison type");
35 : : return 0;
36 : : }
37 : :
38 : : public:
39 [ + + + - : 338828 : transaction_identifier() : m_wrapped{} {}
+ - ]
[ + - + - ]
[ + + ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
40 : : consteval explicit transaction_identifier(std::string_view hex_str) : m_wrapped{uint256{hex_str}} {}
41 : :
42 : : template <typename Other>
43 [ + + - + : 17415409 : bool operator==(const Other& other) const { return Compare(other) == 0; }
+ + + + +
+ + + + +
+ + + + +
+ + + -
- ][ + + #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - -
+ - - + -
+ - - - -
+ - - ][ +
+ - + + -
- + ]
[ + + + + ]
[ + + - -
+ + ][ - -
+ - + - +
- - - -
- ][ + - +
- + - + -
- - ][ + -
- - - - +
- + - - +
+ - # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ - +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
44 : : template <typename Other>
45 [ + + + + ]: 70107014 : bool operator<(const Other& other) const { return Compare(other) < 0; }
[ - - - -
- - - - -
- - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - ][ + -
+ + + + +
+ - - + +
+ + + - -
- ][ - - +
+ + + +
- ][ # # #
# # # # #
# # # # #
# # # ][ +
+ + + + +
- + + - +
- ][ + + -
+ + - + -
+ - - - -
- + + + +
- - - - -
- - - - +
+ - + - +
- - - - -
- + - - -
- + + - -
- - + + -
- - - - -
- + + + -
+ + - - +
+ - - - -
- ][ # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
- + + - +
- + - - -
- - - - +
- - + + -
+ - + - -
- - - - +
+ - + - +
- - - - -
- + - - -
- - - - -
+ + + + +
+ + + + +
+ + + + -
- - - - -
+ + + + -
- - - + -
+ - + - -
- - - - -
- - - - -
- + - - +
- + + + +
+ - - + -
+ - + - +
- + + - -
- - ][ - -
- - - - -
- - - - -
- + - - -
- + + - +
- + - - +
- + - - -
- - - - -
- - - + +
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # ][ +
+ - - - -
- - - - -
+ + - - -
- - - - -
- - - + +
+ + + + +
+ - - - -
- - - - -
- - - - -
- - - + +
- - + - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - - + +
- - - - -
+ - ][ - +
- - - - +
+ + - + -
- - - - -
- + + + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - + - -
- - + - +
- + - + -
- - - - +
+ + + + +
+ - - + +
+ ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
46 : :
47 [ + - + - ]: 118429595 : const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }
[ + - # #
# # # # #
# # # ][ +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
48 [ + - + - ]: 916466 : static transaction_identifier FromUint256(const uint256& id) { return {id}; }
[ + - ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + -
+ - + - +
- + - +
- ]
49 : :
50 : : /** Wrapped `uint256` methods. */
51 [ + + ]: 835292 : constexpr bool IsNull() const { return m_wrapped.IsNull(); }
52 : 9141 : constexpr void SetNull() { m_wrapped.SetNull(); }
53 : 454 : static std::optional<transaction_identifier> FromHex(std::string_view hex)
54 : : {
55 [ + + ]: 454 : auto u{uint256::FromHex(hex)};
56 [ + + ]: 454 : if (!u) return std::nullopt;
57 : 290 : return FromUint256(*u);
58 : : }
59 [ + - ][ + - : 9 : std::string GetHex() const { return m_wrapped.GetHex(); }
+ - + - +
- + - +
- ][ # # #
# # # ]
[ # # # # ]
[ # # # #
# # # # #
# ][ # # #
# # # #
# ]
60 [ + - ][ + - : 85958 : std::string ToString() const { return m_wrapped.ToString(); }
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
- - - - +
- - - -
- ][ + - +
- + - + -
+ - + - +
- ][ - - +
- - - + -
+ - - - +
- + - + -
- - + - +
- + - +
- ][ + - +
- + - ][ +
- + - + -
+ - + - +
- + - + -
- - + - +
- + - + -
+ - - - -
- + - + -
- - - - +
- + - + -
+ - + - +
- + - + -
+ - + - -
- - - + -
+ - + - +
- + - + -
+ - + - ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + - +
- + - +
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
61 : : static constexpr auto size() { return decltype(m_wrapped)::size(); }
62 [ + - + - : 6 : constexpr const std::byte* data() const { return reinterpret_cast<const std::byte*>(m_wrapped.data()); }
+ - + - +
- + - ]
63 [ - + ]: 140482 : constexpr const std::byte* begin() const { return reinterpret_cast<const std::byte*>(m_wrapped.begin()); }
64 : 44 : constexpr const std::byte* end() const { return reinterpret_cast<const std::byte*>(m_wrapped.end()); }
65 : 26386327 : template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); }
66 : 23564 : template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); }
67 : : };
68 : :
69 : : /** Txid commits to all transaction fields except the witness. */
70 : : using Txid = transaction_identifier<false>;
71 : : /** Wtxid commits to all transaction fields including the witness. */
72 : : using Wtxid = transaction_identifier<true>;
73 : :
74 : : template <typename T>
75 : : concept TxidOrWtxid = std::is_same_v<T, Txid> || std::is_same_v<T, Wtxid>;
76 : :
77 : : class GenTxid : public std::variant<Txid, Wtxid>
78 : : {
79 : : public:
80 [ + - + - ]: 4359 : using variant::variant;
[ + - + -
+ - + - ]
81 : :
82 [ - - - - : 4188 : bool IsWtxid() const { return std::holds_alternative<Wtxid>(*this); }
+ + ][ # #
# # # # ]
83 : :
84 [ + + - + : 47095 : const uint256& ToUint256() const LIFETIMEBOUND
+ - + + -
+ + - ][ +
+ - # # #
# # # # #
# ]
85 : : {
86 [ - - - + : 7431562 : return std::visit([](const auto& id) -> const uint256& { return id.ToUint256(); }, *this);
+ - + + -
+ + - + +
- + + - +
+ - + + -
+ + - + +
- + + - +
+ - + + -
+ + - + +
- + - + +
- + + - -
+ + + - +
+ + + - +
+ - + + +
+ - + + -
+ + + + -
+ + - + +
+ + - + +
- + + + +
- + + - +
+ + + - +
- + + - +
+ - ][ - -
- - - - -
- - - - -
- - - - -
- - - - -
- + + - ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - +
- + - + -
+ - + - +
- + - + +
- - + - +
- + - + -
+ ]
87 : : }
88 : :
89 : 2080 : friend auto operator<=>(const GenTxid& a, const GenTxid& b)
90 : : {
91 : : // Use a reference for read-only access to the hash, avoiding a copy that might not be optimized away.
92 [ + + - + : 6240 : return std::tuple<bool, const uint256&>(a.IsWtxid(), a.ToUint256()) <=> std::tuple<bool, const uint256&>(b.IsWtxid(), b.ToUint256());
+ - ]
93 : : }
94 : : };
95 : :
96 : : #endif // BITCOIN_PRIMITIVES_TRANSACTION_IDENTIFIER_H
|