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 <compare>
13 : : #include <concepts>
14 : : #include <tuple>
15 : : #include <variant>
16 : :
17 : : /** transaction_identifier represents the two canonical transaction identifier
18 : : * types (txid, wtxid).*/
19 : : template <bool has_witness>
20 : : class transaction_identifier
21 : : {
22 : : uint256 m_wrapped;
23 : :
24 : : // Note: Use FromUint256 externally instead.
25 : 3270229 : transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}
26 : :
27 : 714425049 : constexpr int Compare(const transaction_identifier<has_witness>& other) const { return m_wrapped.Compare(other.m_wrapped); }
28 : : template <typename Other>
29 : : constexpr int Compare(const Other& other) const
30 : : {
31 : : static_assert(ALWAYS_FALSE<Other>, "Forbidden comparison type");
32 : : return 0;
33 : : }
34 : :
35 : : public:
36 [ + - ][ + + : 60824 : transaction_identifier() : m_wrapped{} {}
+ - + - ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
37 : :
38 : : template <typename Other>
39 [ + + + + : 137401292 : bool operator==(const Other& other) const { return Compare(other) == 0; }
# # # # #
# # # ][ -
+ - + - -
- - - - -
- + + -
- ]
[ + + # # ]
[ - + - -
- + + + +
+ + + ][ +
+ - + - +
# # # # #
# # # # #
# # # # #
# ][ + + +
- + + + -
+ + + + +
- + - + +
+ + + - ]
[ + + + +
- - + - #
# # # # #
# # # # ]
[ + - + +
+ + + + -
+ + - + +
+ + + + ]
[ + - + -
+ - + - -
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
40 : : template <typename Other>
41 [ + + ][ + + : 107948 : bool operator!=(const Other& other) const { return Compare(other) != 0; }
+ + + + ]
[ + + + -
+ - + - +
+ ][ + - +
- + - + -
+ - + - ]
[ - + - +
# # # # #
# # # ]
42 : : template <typename Other>
43 [ + + - - : 385697966 : bool operator<(const Other& other) const { return Compare(other) < 0; }
- - - - +
+ ][ + + +
+ # # # #
# # # # #
# # # # #
# # ][ + +
- + - - +
- - - + -
- + - - -
- + + + +
+ + + - +
+ + - + -
- - + + +
+ + + +
+ ][ + - +
+ + + + +
+ + + + +
+ + + + +
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# # # ][ +
+ - + + -
+ - + - -
- - - + +
+ + - - -
- - - - -
- + + - +
- + - - -
- - - + -
- - - + +
- - - - +
+ - - - -
- - - + +
+ - + + -
- + + - -
- - - ][ +
+ + + + +
- + + - +
- ][ - - +
+ + + + +
# # # # #
# # # ][ +
- - + -
+ ][ # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ + - - +
- - + + -
- + - - +
- # # # #
# # # # #
# ][ - + -
- - - + +
+ - + - +
- - - - -
+ + + - ]
[ - + - -
- - + - +
- + - + -
- - - - +
+ + + + +
+ - + + +
+ ][ + + -
- - - - -
- - - + +
- - - - -
- - + + +
+ + + + +
+ + + + -
- - - - -
- - - - -
- - - - -
- + + - -
+ - - - -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - -
- + + + +
+ + + + +
- - - - +
- + - - +
+ ][ + - -
+ + - + -
+ - - - -
- + + + -
- + + - +
- + - - -
- - - + +
- + - + -
- - - - -
+ - - - -
- - - - +
+ + + + +
+ + + + +
+ + + - -
- - - - +
+ + + - -
- - + - +
- + - - -
- - - - -
- - - - -
+ - - + -
+ + + + +
- - + - +
- + - + -
+ + - - -
- ]
44 : :
45 [ + - + - ]: 322770377 : const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ][ + - -
- + - + -
+ - ][ + - ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
46 [ + - + - ]: 1540630 : static transaction_identifier FromUint256(const uint256& id) { return {id}; }
[ + + # # ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ][ + - +
- + - + -
+ - + - ]
47 : :
48 : : /** Wrapped `uint256` methods. */
49 [ + + ]: 44968456 : constexpr bool IsNull() const { return m_wrapped.IsNull(); }
50 : 47800 : constexpr void SetNull() { m_wrapped.SetNull(); }
51 : 5202 : static std::optional<transaction_identifier> FromHex(std::string_view hex)
52 : : {
53 [ + + ]: 5202 : auto u{uint256::FromHex(hex)};
54 [ + + ]: 5202 : if (!u) return std::nullopt;
55 : 4909 : return FromUint256(*u);
56 : : }
57 [ + - + - : 248721 : std::string GetHex() const { return m_wrapped.GetHex(); }
+ - # # #
# ][ + - +
- + - +
- ][ + - +
- # # #
# ][ + - #
# # # ][ -
- - - + -
- - + - ]
[ + - + -
+ - + - +
- + - ]
58 [ + - # # : 444639 : std::string ToString() const { return m_wrapped.ToString(); }
# # # # #
# # # ]
[ + - + - ]
[ + - + -
+ - + - ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + - +
- + - + -
- - - - +
- - - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ - -
+ - + - +
- + - -
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - -
- - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - - -
+ - + - +
- + - + -
+ - + - +
- ][ + - +
- + - + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + - +
- + - + -
+ - + - +
- # # # #
# # # # #
# # # #
# ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
[ + - + -
+ - ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
59 : : static constexpr auto size() { return decltype(m_wrapped)::size(); }
60 [ + - + - : 6 : constexpr const std::byte* data() const { return reinterpret_cast<const std::byte*>(m_wrapped.data()); }
+ - + - +
- + - ]
61 [ - + ]: 259632 : constexpr const std::byte* begin() const { return reinterpret_cast<const std::byte*>(m_wrapped.begin()); }
62 : 304 : constexpr const std::byte* end() const { return reinterpret_cast<const std::byte*>(m_wrapped.end()); }
63 : 32388242 : template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); }
64 : 701677 : template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); }
65 : : };
66 : :
67 : : /** Txid commits to all transaction fields except the witness. */
68 : : using Txid = transaction_identifier<false>;
69 : : /** Wtxid commits to all transaction fields including the witness. */
70 : : using Wtxid = transaction_identifier<true>;
71 : :
72 : : template <typename T>
73 : : concept TxidOrWtxid = std::is_same_v<T, Txid> || std::is_same_v<T, Wtxid>;
74 : :
75 : : class GenTxid : public std::variant<Txid, Wtxid>
76 : : {
77 : : public:
78 [ + - + - ]: 298958 : using variant::variant;
[ + - + -
+ - + - ]
79 : :
80 [ + + + + : 72837 : bool IsWtxid() const { return std::holds_alternative<Wtxid>(*this); }
+ + ][ + + ]
81 : :
82 [ + + - ][ + : 1016195 : const uint256& ToUint256() const LIFETIMEBOUND
+ - + + -
+ + - + +
- ]
83 : : {
84 [ + + - + : 11305364 : return std::visit([](const auto& id) -> const uint256& { return id.ToUint256(); }, *this);
+ - + - +
+ - + - +
+ - + - -
+ - + - +
+ - ][ + +
- + + ][ -
- - + + -
+ + - + +
- + + - +
+ - + + -
+ + - + +
- + + - +
+ - + + -
+ + - + +
- + + - +
- + + - +
+ - + + +
+ - + + +
+ - + + -
+ + + + -
+ + - + +
+ + - + +
- + + + +
- + + - +
+ + + - +
+ - + + +
+ - + - +
+ - + +
- ][ - + -
+ - + - +
- + - + -
+ - + + -
- + - + -
+ - + -
+ ]
85 : : }
86 : :
87 : 728839 : friend auto operator<=>(const GenTxid& a, const GenTxid& b)
88 : : {
89 : : // Use a reference for read-only access to the hash, avoiding a copy that might not be optimized away.
90 [ + + - + : 2186517 : return std::tuple<bool, const uint256&>(a.IsWtxid(), a.ToUint256()) <=> std::tuple<bool, const uint256&>(b.IsWtxid(), b.ToUint256());
+ - ]
91 : : }
92 : : };
93 : :
94 : : #endif // BITCOIN_PRIMITIVES_TRANSACTION_IDENTIFIER_H
|