Bitcoin Core  27.99.0
P2P Digital Currency
transaction_identifier.h
Go to the documentation of this file.
1 #ifndef BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
2 #define BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
3 
4 #include <attributes.h>
5 #include <uint256.h>
6 #include <util/types.h>
7 
10 template <bool has_witness>
12 {
14 
15  // Note: Use FromUint256 externally instead.
16  transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}
17 
18  // TODO: Comparisons with uint256 should be disallowed once we have
19  // converted most of the code to using the new txid types.
20  constexpr int Compare(const uint256& other) const { return m_wrapped.Compare(other); }
21  constexpr int Compare(const transaction_identifier<has_witness>& other) const { return m_wrapped.Compare(other.m_wrapped); }
22  template <typename Other>
23  constexpr int Compare(const Other& other) const
24  {
25  static_assert(ALWAYS_FALSE<Other>, "Forbidden comparison type");
26  return 0;
27  }
28 
29 public:
31 
32  template <typename Other>
33  bool operator==(const Other& other) const { return Compare(other) == 0; }
34  template <typename Other>
35  bool operator!=(const Other& other) const { return Compare(other) != 0; }
36  template <typename Other>
37  bool operator<(const Other& other) const { return Compare(other) < 0; }
38 
39  const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }
40  static transaction_identifier FromUint256(const uint256& id) { return {id}; }
41 
43  constexpr bool IsNull() const { return m_wrapped.IsNull(); }
44  constexpr void SetNull() { m_wrapped.SetNull(); }
45  std::string GetHex() const { return m_wrapped.GetHex(); }
46  std::string ToString() const { return m_wrapped.ToString(); }
47  static constexpr auto size() { return decltype(m_wrapped)::size(); }
48  constexpr const std::byte* data() const { return reinterpret_cast<const std::byte*>(m_wrapped.data()); }
49  constexpr const std::byte* begin() const { return reinterpret_cast<const std::byte*>(m_wrapped.begin()); }
50  constexpr const std::byte* end() const { return reinterpret_cast<const std::byte*>(m_wrapped.end()); }
51  template <typename Stream> void Serialize(Stream& s) const { m_wrapped.Serialize(s); }
52  template <typename Stream> void Unserialize(Stream& s) { m_wrapped.Unserialize(s); }
53 
61  operator const uint256&() const LIFETIMEBOUND { return m_wrapped; }
62 };
63 
68 
69 inline Txid TxidFromString(std::string_view str)
70 {
71  return Txid::FromUint256(uint256S(str.data()));
72 }
73 
74 #endif // BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H
#define LIFETIMEBOUND
Definition: attributes.h:16
constexpr bool IsNull() const
Definition: uint256.h:42
constexpr unsigned char * end()
Definition: uint256.h:69
void Unserialize(Stream &s)
Definition: uint256.h:85
std::string ToString() const
Definition: uint256.cpp:55
constexpr const unsigned char * data() const
Definition: uint256.h:65
constexpr int Compare(const base_blob &other) const
Definition: uint256.h:54
constexpr unsigned char * begin()
Definition: uint256.h:68
void Serialize(Stream &s) const
Definition: uint256.h:79
constexpr void SetNull()
Definition: uint256.h:49
std::string GetHex() const
Definition: uint256.cpp:11
transaction_identifier represents the two canonical transaction identifier types (txid,...
std::string ToString() const
constexpr const std::byte * end() const
static constexpr auto size()
void Serialize(Stream &s) const
const uint256 & ToUint256() const LIFETIMEBOUND
constexpr bool IsNull() const
Wrapped uint256 methods.
transaction_identifier(const uint256 &wrapped)
bool operator<(const Other &other) const
bool operator==(const Other &other) const
constexpr const std::byte * begin() const
constexpr int Compare(const Other &other) const
std::string GetHex() const
bool operator!=(const Other &other) const
constexpr int Compare(const uint256 &other) const
static transaction_identifier FromUint256(const uint256 &id)
constexpr const std::byte * data() const
constexpr int Compare(const transaction_identifier< has_witness > &other) const
256-bit opaque blob.
Definition: uint256.h:106
Txid TxidFromString(std::string_view str)
uint256 uint256S(const char *str)
Definition: uint256.h:119