rippled
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 <tuple>
9
10namespace xrpl {
11
13class SHAMapNodeID : public CountedObject<SHAMapNodeID>
14{
15private:
17 unsigned int depth_ = 0;
18
19public:
20 SHAMapNodeID() = default;
21 SHAMapNodeID(SHAMapNodeID const& other) = default;
22 SHAMapNodeID(unsigned int depth, uint256 const& hash);
23
25 operator=(SHAMapNodeID const& other) = default;
26
27 bool
28 isRoot() const
29 {
30 return depth_ == 0;
31 }
32
33 // Get the wire format (256-bit nodeID, 1-byte depth)
35 getRawString() const;
36
37 unsigned int
38 getDepth() const
39 {
40 return depth_;
41 }
42
43 uint256 const&
44 getNodeID() const
45 {
46 return id_;
47 }
48
50 getChildNodeID(unsigned int m) const;
51
60 static SHAMapNodeID
61 createID(int depth, uint256 const& key);
62
63 // FIXME-C++20: use spaceship and operator synthesis
65 bool
66 operator<(SHAMapNodeID const& n) const
67 {
68 return std::tie(depth_, id_) < std::tie(n.depth_, n.id_);
69 }
70
71 bool
72 operator>(SHAMapNodeID const& n) const
73 {
74 return n < *this;
75 }
76
77 bool
78 operator<=(SHAMapNodeID const& n) const
79 {
80 return !(n < *this);
81 }
82
83 bool
84 operator>=(SHAMapNodeID const& n) const
85 {
86 return !(*this < n);
87 }
88
89 bool
90 operator==(SHAMapNodeID const& n) const
91 {
92 return (depth_ == n.depth_) && (id_ == n.id_);
93 }
94
95 bool
96 operator!=(SHAMapNodeID const& n) const
97 {
98 return !(*this == n);
99 }
100};
101
102inline std::string
104{
105 if (node.isRoot())
106 return "NodeID(root)";
107
108 return "NodeID(" + std::to_string(node.getDepth()) + "," + to_string(node.getNodeID()) + ")";
109}
110
113{
114 return out << to_string(node);
115}
116
126[[nodiscard]] std::optional<SHAMapNodeID>
127deserializeSHAMapNodeID(void const* data, std::size_t size);
128
129[[nodiscard]] inline std::optional<SHAMapNodeID>
131{
132 return deserializeSHAMapNodeID(s.data(), s.size());
133}
137[[nodiscard]] unsigned int
138selectBranch(SHAMapNodeID const& id, uint256 const& hash);
139
140} // namespace xrpl
Tracks the number of instances of an object.
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::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:612
base_uint< 256 > uint256
Definition base_uint.h:526
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.
T size(T... args)
T tie(T... args)
T to_string(T... args)