xrpld
Loading...
Searching...
No Matches
SHAMapNodeID.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/base_uint.h>
5
6#include <optional>
7#include <string>
8#include <string_view>
9#include <tuple>
10
11namespace xrpl {
12
14class SHAMapNodeID : public CountedObject<SHAMapNodeID>
15{
16private:
18 unsigned int depth_ = 0;
19
20public:
21 SHAMapNodeID() = default;
22 SHAMapNodeID(SHAMapNodeID const& other) = default;
23 SHAMapNodeID(unsigned int depth, uint256 const& hash);
24
26 operator=(SHAMapNodeID const& other) = default;
27
28 [[nodiscard]] bool
29 isRoot() const
30 {
31 return depth_ == 0;
32 }
33
34 // Get the wire format (256-bit nodeID, 1-byte depth)
35 [[nodiscard]] std::string
36 getRawString() const;
37
38 [[nodiscard]] unsigned int
39 getDepth() const
40 {
41 return depth_;
42 }
43
44 [[nodiscard]] uint256 const&
45 getNodeID() const
46 {
47 return id_;
48 }
49
50 [[nodiscard]] SHAMapNodeID
51 getChildNodeID(unsigned int m) const;
52
61 static SHAMapNodeID
62 createID(int depth, uint256 const& key);
63
64 // FIXME-C++20: use spaceship and operator synthesis
66 bool
67 operator<(SHAMapNodeID const& n) const
68 {
69 return std::tie(depth_, id_) < std::tie(n.depth_, n.id_);
70 }
71
72 bool
73 operator>(SHAMapNodeID const& n) const
74 {
75 return n < *this;
76 }
77
78 bool
79 operator<=(SHAMapNodeID const& n) const
80 {
81 return !(n < *this);
82 }
83
84 bool
85 operator>=(SHAMapNodeID const& n) const
86 {
87 return !(*this < n);
88 }
89
90 bool
91 operator==(SHAMapNodeID const& n) const
92 {
93 return (depth_ == n.depth_) && (id_ == n.id_);
94 }
95
96 bool
97 operator!=(SHAMapNodeID const& n) const
98 {
99 return !(*this == n);
100 }
101};
102
103inline std::string
105{
106 if (node.isRoot())
107 return "NodeID(root)";
108
109 return "NodeID(" + std::to_string(node.getDepth()) + "," + to_string(node.getNodeID()) + ")";
110}
111
113operator<<(std::ostream& out, SHAMapNodeID const& node)
114{
115 return out << to_string(node);
116}
117
127[[nodiscard]] std::optional<SHAMapNodeID>
128deserializeSHAMapNodeID(void const* data, std::size_t size);
129
130[[nodiscard]] inline std::optional<SHAMapNodeID>
135
136
138[[nodiscard]] unsigned int
139selectBranch(SHAMapNodeID const& id, uint256 const& hash);
140
141} // namespace xrpl
Identifies a node inside a SHAMap.
uint256 const & getNodeID() const
bool operator!=(SHAMapNodeID const &n) const
bool operator>(SHAMapNodeID const &n) const
static SHAMapNodeID createID(int depth, uint256 const &key)
Create a SHAMapNodeID of a node with the depth of the node and the key of a leaf.
SHAMapNodeID getChildNodeID(unsigned int m) const
bool operator<(SHAMapNodeID const &n) const
Comparison operators.
SHAMapNodeID()=default
SHAMapNodeID(SHAMapNodeID const &other)=default
unsigned int depth_
SHAMapNodeID & operator=(SHAMapNodeID const &other)=default
bool operator==(SHAMapNodeID const &n) const
bool operator<=(SHAMapNodeID const &n) const
unsigned int getDepth() const
std::string getRawString() const
bool operator>=(SHAMapNodeID const &n) const
bool isRoot() const
T data(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition base_uint.h:648
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
unsigned int selectBranch(SHAMapNodeID const &id, uint256 const &hash)
Returns the branch that would contain the given hash.
std::optional< SHAMapNodeID > deserializeSHAMapNodeID(void const *data, std::size_t size)
Return an object representing a serialized SHAMap Node ID.
BaseUInt< 256 > uint256
Definition base_uint.h:562
T size(T... args)
T tie(T... args)
T to_string(T... args)