rippled
Loading...
Searching...
No Matches
SHAMapNodeID.h
1#ifndef XRPL_SHAMAP_SHAMAPNODEID_H_INCLUDED
2#define XRPL_SHAMAP_SHAMAPNODEID_H_INCLUDED
3
4#include <xrpl/basics/CountedObject.h>
5#include <xrpl/basics/base_uint.h>
6
7#include <optional>
8#include <string>
9#include <tuple>
10
11namespace ripple {
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 bool
29 isRoot() const
30 {
31 return depth_ == 0;
32 }
33
34 // Get the wire format (256-bit nodeID, 1-byte depth)
36 getRawString() const;
37
38 unsigned int
39 getDepth() const
40 {
41 return depth_;
42 }
43
44 uint256 const&
45 getNodeID() const
46 {
47 return id_;
48 }
49
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()) + "," +
110 to_string(node.getNodeID()) + ")";
111}
112
115{
116 return out << to_string(node);
117}
118
128[[nodiscard]] std::optional<SHAMapNodeID>
129deserializeSHAMapNodeID(void const* data, std::size_t size);
130
131[[nodiscard]] inline std::optional<SHAMapNodeID>
133{
134 return deserializeSHAMapNodeID(s.data(), s.size());
135}
139[[nodiscard]] unsigned int
140selectBranch(SHAMapNodeID const& id, uint256 const& hash);
141
142} // namespace ripple
143
144#endif
Tracks the number of instances of an object.
Identifies a node inside a SHAMap.
bool operator<(SHAMapNodeID const &n) const
Comparison operators.
SHAMapNodeID(SHAMapNodeID const &other)=default
unsigned int getDepth() const
uint256 const & getNodeID() const
bool isRoot() const
bool operator>=(SHAMapNodeID const &n) const
SHAMapNodeID getChildNodeID(unsigned int m) const
std::string getRawString() 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.
bool operator<=(SHAMapNodeID const &n) const
bool operator==(SHAMapNodeID const &n) const
SHAMapNodeID & operator=(SHAMapNodeID const &other)=default
unsigned int depth_
bool operator>(SHAMapNodeID const &n) const
T data(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::optional< SHAMapNodeID > deserializeSHAMapNodeID(void const *data, std::size_t size)
Return an object representing a serialized SHAMap Node ID.
base_uint< 256 > uint256
Definition base_uint.h:539
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:628
unsigned int selectBranch(SHAMapNodeID const &id, uint256 const &hash)
Returns the branch that would contain the given hash.
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
T size(T... args)
T tie(T... args)
T to_string(T... args)