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_UTIL_TRANSACTION_IDENTIFIER_H
6 : : #define BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
7 : :
8 : : #include <attributes.h>
9 : : #include <uint256.h>
10 : : #include <util/types.h>
11 : :
12 : : /** transaction_identifier represents the two canonical transaction identifier
13 : : * types (txid, wtxid).*/
14 : : template <bool has_witness>
15 : : class transaction_identifier
16 : : {
17 : : uint256 m_wrapped;
18 : :
19 : : // Note: Use FromUint256 externally instead.
20 : 3147673 : transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}
21 : :
22 : : // TODO: Comparisons with uint256 should be disallowed once we have
23 : : // converted most of the code to using the new txid types.
24 : 5817 : constexpr int Compare(const uint256& other) const { return m_wrapped.Compare(other); }
25 : 775530892 : constexpr int Compare(const transaction_identifier<has_witness>& other) const { return m_wrapped.Compare(other.m_wrapped); }
26 : : template <typename Other>
27 : : constexpr int Compare(const Other& other) const
28 : : {
29 : : static_assert(ALWAYS_FALSE<Other>, "Forbidden comparison type");
30 : : return 0;
31 : : }
32 : :
33 : : public:
34 [ + - + - : 1867222 : transaction_identifier() : m_wrapped{} {}
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + +
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + + + -
+ - ][ + +
+ - + - ]
35 : :
36 : : template <typename Other>
37 [ + + + + ]: 106420815 : bool operator==(const Other& other) const { return Compare(other) == 0; }
[ + + + +
- - - - -
- + + +
+ ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
[ + + + -
+ - ][ + -
+ + + + +
- - - # #
# # # # #
# # # #
# ][ - - -
- - - - -
- - + - -
- ][ + - +
+ - - -
- ]
38 : : template <typename Other>
39 [ + + # # ]: 81331 : bool operator!=(const Other& other) const { return Compare(other) != 0; }
[ + + + +
+ + ][ + +
+ - + - +
- + + +
- ][ + - -
+ + - # #
# # # # ]
[ - - + -
+ - + - -
- ]
40 : : template <typename Other>
41 [ + + + + : 390782592 : bool operator<(const Other& other) const { return Compare(other) < 0; }
- + ][ + +
+ + + + +
+ + + ][ -
+ + - + -
+ - - - -
- + + - -
- - + + -
+ + - - -
- - ][ + -
- + - + +
- + - + -
- - - - +
+ - - - -
- - + + +
+ + + + +
+ - - - -
- + - + +
+ + + - -
- + - ][ -
- - - - -
+ - + - -
- - - - -
- - - - ]
[ + + # #
# # # # ]
[ + + + +
+ + + + +
+ + - + -
- - - - +
+ + - ][ -
- - - + +
- + + - +
- + - - -
- - + + -
- + + - -
- - - - -
- - + - -
- - - + +
- - - -
- ][ - + +
- + - + -
- - - - +
+ - - - -
- - - - +
- - - -
- ][ + + +
+ + + + +
+ + + - +
- - - + +
+ - + + -
- + + - +
+ + + + +
- + + - -
+ + + + +
+ + + - -
+ - ][ - -
- - - - +
+ + + + -
- - - - -
- - - ][ -
+ - - - -
+ + + + +
- + - - -
- - + + +
- ][ - - -
- + + + +
+ + - + -
- - - + +
- + - - +
+ # # #
# ]
42 : :
43 [ + - + - : 1008343 : const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }
+ - + - +
- + - + -
+ - + - +
- ][ + - +
- + - ][ +
- + - + -
+ - + - +
- + - + -
- - ]
44 [ + - ][ + - : 1428560 : static transaction_identifier FromUint256(const uint256& id) { return {id}; }
+ - + - ]
[ + - + -
+ - ]
45 : :
46 : : /** Wrapped `uint256` methods. */
47 [ + + ]: 25606215 : constexpr bool IsNull() const { return m_wrapped.IsNull(); }
48 [ + - ][ + - : 94772 : constexpr void SetNull() { m_wrapped.SetNull(); }
+ - + - +
- ]
49 : 5073 : static std::optional<transaction_identifier> FromHex(std::string_view hex)
50 : : {
51 [ + + ]: 5073 : auto u{uint256::FromHex(hex)};
52 [ + + ]: 5073 : if (!u) return std::nullopt;
53 : 4901 : return FromUint256(*u);
54 : : }
55 [ + - + - : 231596 : std::string GetHex() const { return m_wrapped.GetHex(); }
+ - + - +
- + - ][ +
- + - + -
# # # # #
# ][ + - +
- + - # #
# # ][ + -
+ - + - +
- ][ + - #
# # # # #
# # ][ + -
- - + - +
- + - +
- ]
56 [ + - + - ]: 509157 : std::string ToString() const { return m_wrapped.ToString(); }
[ + - + -
+ - + - +
- + - ][ +
- + - + -
+ - + - +
- + - ][ #
# # # # #
# # # # #
# # # ][ +
- + - + -
+ - + - +
- + - + -
- - + - +
- + - + -
- - - - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - -
- - + - +
- + - + -
+ - + - +
- + - ][ +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - # # #
# # # # #
# # ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
[ + - + -
+ - + - +
- + - +
- ][ + - +
- + - + -
- - # # #
# # # # #
# # # # #
# # # #
# ][ + - +
- + - # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ + - +
- + - + -
+ - - - +
- + - + -
+ - + - +
- + - - -
- - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - - + -
+ - + - +
- ]
57 : : static constexpr auto size() { return decltype(m_wrapped)::size(); }
58 [ + - + - : 6 : constexpr const std::byte* data() const { return reinterpret_cast<const std::byte*>(m_wrapped.data()); }
+ - + - +
- + - ]
59 [ + - ]: 259125 : constexpr const std::byte* begin() const { return reinterpret_cast<const std::byte*>(m_wrapped.begin()); }
60 : 311 : constexpr const std::byte* end() const { return reinterpret_cast<const std::byte*>(m_wrapped.end()); }
61 : 31679491 : template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); }
62 : 690462 : template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); }
63 : :
64 : : /** Conversion function to `uint256`.
65 : : *
66 : : * Note: new code should use `ToUint256`.
67 : : *
68 : : * TODO: This should be removed once the majority of the code has switched
69 : : * to using the Txid and Wtxid types. Until then it makes for a smoother
70 : : * transition to allow this conversion. */
71 [ + - + - : 295275781 : operator const uint256&() const LIFETIMEBOUND { return m_wrapped; }
+ - + - +
- + - + -
+ - + - +
- ][ + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
- - + - -
- - - ][ +
- + - + +
+ - + - +
- + - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - + -
+ - + - +
- + - # #
# # # # ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + -
+ - + - +
- + - +
- ][ + - -
- - - ][ +
- - - + -
+ - + - +
- - - - -
# # # # #
# # # # #
# # # # ]
72 : : };
73 : :
74 : : /** Txid commits to all transaction fields except the witness. */
75 : : using Txid = transaction_identifier<false>;
76 : : /** Wtxid commits to all transaction fields including the witness. */
77 : : using Wtxid = transaction_identifier<true>;
78 : :
79 : : #endif // BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
|