/root/bitcoin/src/primitives/transaction_identifier.h
Line | Count | Source |
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 | | |
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 | 29.4M | transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}_ZN22transaction_identifierILb0EEC2ERK7uint256 Line | Count | Source | 28 | 15.5M | transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {} |
_ZN22transaction_identifierILb1EEC2ERK7uint256 Line | Count | Source | 28 | 13.8M | transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {} |
|
29 | | |
30 | | public: |
31 | 51.9M | transaction_identifier() : m_wrapped{} {}_ZN22transaction_identifierILb0EEC2Ev Line | Count | Source | 31 | 51.4M | transaction_identifier() : m_wrapped{} {} |
_ZN22transaction_identifierILb1EEC2Ev Line | Count | Source | 31 | 444k | transaction_identifier() : m_wrapped{} {} |
|
32 | | consteval explicit transaction_identifier(std::string_view hex_str) : m_wrapped{uint256{hex_str}} {} |
33 | | |
34 | 362M | constexpr bool operator==(const transaction_identifier&) const = default; _ZNK22transaction_identifierILb0EEeqERKS0_ Line | Count | Source | 34 | 353M | constexpr bool operator==(const transaction_identifier&) const = default; |
_ZNK22transaction_identifierILb1EEeqERKS0_ Line | Count | Source | 34 | 9.49M | constexpr bool operator==(const transaction_identifier&) const = default; |
|
35 | 2.12G | constexpr auto operator<=>(const transaction_identifier&) const = default; _ZNK22transaction_identifierILb0EEssERKS0_ Line | Count | Source | 35 | 1.47G | constexpr auto operator<=>(const transaction_identifier&) const = default; |
_ZNK22transaction_identifierILb1EEssERKS0_ Line | Count | Source | 35 | 641M | constexpr auto operator<=>(const transaction_identifier&) const = default; |
|
36 | | |
37 | 532M | const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }_ZNK22transaction_identifierILb0EE9ToUint256Ev Line | Count | Source | 37 | 503M | const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; } |
_ZNK22transaction_identifierILb1EE9ToUint256Ev Line | Count | Source | 37 | 29.0M | const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; } |
|
38 | 29.4M | static transaction_identifier FromUint256(const uint256& id) { return {id}; }_ZN22transaction_identifierILb0EE11FromUint256ERK7uint256 Line | Count | Source | 38 | 15.5M | static transaction_identifier FromUint256(const uint256& id) { return {id}; } |
_ZN22transaction_identifierILb1EE11FromUint256ERK7uint256 Line | Count | Source | 38 | 13.8M | static transaction_identifier FromUint256(const uint256& id) { return {id}; } |
|
39 | | |
40 | | /** Wrapped `uint256` methods. */ |
41 | 30.0M | constexpr bool IsNull() const { return m_wrapped.IsNull(); } |
42 | 415k | constexpr void SetNull() { m_wrapped.SetNull(); } |
43 | | static std::optional<transaction_identifier> FromHex(std::string_view hex) |
44 | 24 | { |
45 | 24 | auto u{uint256::FromHex(hex)}; |
46 | 24 | if (!u) return std::nullopt; Branch (46:13): [True: 0, False: 12]
Branch (46:13): [True: 0, False: 12]
|
47 | 24 | return FromUint256(*u); |
48 | 24 | } _ZN22transaction_identifierILb0EE7FromHexESt17basic_string_viewIcSt11char_traitsIcEE Line | Count | Source | 44 | 12 | { | 45 | 12 | auto u{uint256::FromHex(hex)}; | 46 | 12 | if (!u) return std::nullopt; Branch (46:13): [True: 0, False: 12]
| 47 | 12 | return FromUint256(*u); | 48 | 12 | } |
_ZN22transaction_identifierILb1EE7FromHexESt17basic_string_viewIcSt11char_traitsIcEE Line | Count | Source | 44 | 12 | { | 45 | 12 | auto u{uint256::FromHex(hex)}; | 46 | 12 | if (!u) return std::nullopt; Branch (46:13): [True: 0, False: 12]
| 47 | 12 | return FromUint256(*u); | 48 | 12 | } |
|
49 | 103k | std::string GetHex() const { return m_wrapped.GetHex(); }_ZNK22transaction_identifierILb0EE6GetHexB5cxx11Ev Line | Count | Source | 49 | 101k | std::string GetHex() const { return m_wrapped.GetHex(); } |
_ZNK22transaction_identifierILb1EE6GetHexB5cxx11Ev Line | Count | Source | 49 | 2.50k | std::string GetHex() const { return m_wrapped.GetHex(); } |
|
50 | 4.57M | std::string ToString() const { return m_wrapped.ToString(); }_ZNK22transaction_identifierILb0EE8ToStringB5cxx11Ev Line | Count | Source | 50 | 4.28M | std::string ToString() const { return m_wrapped.ToString(); } |
_ZNK22transaction_identifierILb1EE8ToStringB5cxx11Ev Line | Count | Source | 50 | 297k | 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 | 1.02M | constexpr const std::byte* begin() const { return reinterpret_cast<const std::byte*>(m_wrapped.begin()); }_ZNK22transaction_identifierILb1EE5beginEv Line | Count | Source | 53 | 1.02M | constexpr const std::byte* begin() const { return reinterpret_cast<const std::byte*>(m_wrapped.begin()); } |
Unexecuted instantiation: _ZNK22transaction_identifierILb0EE5beginEv |
54 | 109k | constexpr const std::byte* end() const { return reinterpret_cast<const std::byte*>(m_wrapped.end()); } |
55 | 3.90G | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); }_ZNK22transaction_identifierILb0EE9SerializeI12ParamsStreamIR12SizeComputer20TransactionSerParamsEEEvRT_ Line | Count | Source | 55 | 3.82G | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
_ZNK22transaction_identifierILb0EE9SerializeI12ParamsStreamIR12VectorWriter20TransactionSerParamsEEEvRT_ Line | Count | Source | 55 | 270k | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
_ZNK22transaction_identifierILb0EE9SerializeI12ParamsStreamIR10DataStream20TransactionSerParamsEEEvRT_ Line | Count | Source | 55 | 480k | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
_ZNK22transaction_identifierILb0EE9SerializeI10DataStreamEEvRT_ Line | Count | Source | 55 | 3.89M | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
_ZNK22transaction_identifierILb0EE9SerializeI12SizeComputerEEvRT_ Line | Count | Source | 55 | 760 | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
_ZNK22transaction_identifierILb0EE9SerializeI12ParamsStreamIRS2_IR12VectorWriter20TransactionSerParamsES5_EEEvRT_ Line | Count | Source | 55 | 4.22k | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
Unexecuted instantiation: _ZNK22transaction_identifierILb0EE9SerializeI12VectorWriterEEvRT_ _ZNK22transaction_identifierILb0EE9SerializeI8AutoFileEEvRT_ Line | Count | Source | 55 | 67.2k | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
Unexecuted instantiation: _ZNK22transaction_identifierILb1EE9SerializeI10DataStreamEEvRT_ _ZNK22transaction_identifierILb0EE9SerializeI10HashWriterEEvRT_ Line | Count | Source | 55 | 23.3M | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
_ZNK22transaction_identifierILb0EE9SerializeI12ParamsStreamIR14BufferedWriterI8AutoFileE20TransactionSerParamsEEEvRT_ Line | Count | Source | 55 | 370k | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
Unexecuted instantiation: _ZNK22transaction_identifierILb0EE9SerializeI12ParamsStreamIR8AutoFile20TransactionSerParamsEEEvRT_ _ZNK22transaction_identifierILb1EE9SerializeI10HashWriterEEvRT_ Line | Count | Source | 55 | 73.2k | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
_ZNK22transaction_identifierILb0EE9SerializeI12ParamsStreamIR10HashWriter20TransactionSerParamsEEEvRT_ Line | Count | Source | 55 | 54.8M | template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); } |
|
56 | 12.5M | template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); }_ZN22transaction_identifierILb0EE11UnserializeI12ParamsStreamIR10SpanReader20TransactionSerParamsEEEvRT_ Line | Count | Source | 56 | 3.93M | template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); } |
_ZN22transaction_identifierILb0EE11UnserializeI10SpanReaderEEvRT_ Line | Count | Source | 56 | 7.14M | template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); } |
_ZN22transaction_identifierILb0EE11UnserializeI10DataStreamEEvRT_ Line | Count | Source | 56 | 31 | template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); } |
_ZN22transaction_identifierILb0EE11UnserializeI12ParamsStreamIR10DataStream20TransactionSerParamsEEEvRT_ Line | Count | Source | 56 | 361k | template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); } |
_ZN22transaction_identifierILb0EE11UnserializeI12ParamsStreamIR8AutoFile20TransactionSerParamsEEEvRT_ Line | Count | Source | 56 | 685k | template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); } |
_ZN22transaction_identifierILb0EE11UnserializeI8AutoFileEEvRT_ Line | Count | Source | 56 | 369k | template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); } |
_ZN22transaction_identifierILb0EE11UnserializeI12ParamsStreamIR12BufferedFile20TransactionSerParamsEEEvRT_ Line | Count | Source | 56 | 72.9k | template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); } |
|
57 | | }; |
58 | | |
59 | | /** Txid commits to all transaction fields except the witness. */ |
60 | | using Txid = transaction_identifier<false>; |
61 | | /** Wtxid commits to all transaction fields including the witness. */ |
62 | | using Wtxid = transaction_identifier<true>; |
63 | | |
64 | | template <typename T> |
65 | | concept TxidOrWtxid = std::is_same_v<T, Txid> || std::is_same_v<T, Wtxid>; |
66 | | |
67 | | class GenTxid : public std::variant<Txid, Wtxid> |
68 | | { |
69 | | public: |
70 | | using variant::variant; |
71 | | |
72 | 171k | bool IsWtxid() const { return std::holds_alternative<Wtxid>(*this); } |
73 | | |
74 | | const uint256& ToUint256() const LIFETIMEBOUND |
75 | 30.8M | { |
76 | 30.8M | return std::visit([](const auto& id) -> const uint256& { return id.ToUint256(); }, *this);_ZZNK7GenTxid9ToUint256EvENKUlRKT_E_clI22transaction_identifierILb0EEEERK7uint256S2_ Line | Count | Source | 76 | 20.0M | return std::visit([](const auto& id) -> const uint256& { return id.ToUint256(); }, *this); |
_ZZNK7GenTxid9ToUint256EvENKUlRKT_E_clI22transaction_identifierILb1EEEERK7uint256S2_ Line | Count | Source | 76 | 10.8M | return std::visit([](const auto& id) -> const uint256& { return id.ToUint256(); }, *this); |
|
77 | 30.8M | } |
78 | | |
79 | | friend auto operator<=>(const GenTxid& a, const GenTxid& b) |
80 | 26.7k | { |
81 | | // Use a reference for read-only access to the hash, avoiding a copy that might not be optimized away. |
82 | 26.7k | return std::tuple<bool, const uint256&>(a.IsWtxid(), a.ToUint256()) <=> std::tuple<bool, const uint256&>(b.IsWtxid(), b.ToUint256()); |
83 | 26.7k | } |
84 | | }; |
85 | | |
86 | | #endif // BITCOIN_PRIMITIVES_TRANSACTION_IDENTIFIER_H |