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 <compare>
7#include <cstddef>
8#include <optional>
9#include <ostream>
10#include <string>
11#include <string_view>
12#include <tuple>
13
14namespace xrpl {
15
19class SHAMapNodeID : public CountedObject<SHAMapNodeID>
20{
21private:
23 unsigned int depth_ = 0;
24
25public:
26 SHAMapNodeID() = default;
27 SHAMapNodeID(SHAMapNodeID const& other) = default;
28 SHAMapNodeID(unsigned int depth, UInt256 const& hash);
29
31 operator=(SHAMapNodeID const& other) = default;
32
33 [[nodiscard]] bool
34 isRoot() const
35 {
36 return depth_ == 0;
37 }
38
39 // Get the wire format (256-bit nodeID, 1-byte depth)
40 [[nodiscard]] std::string
41 getRawString() const;
42
43 [[nodiscard]] unsigned int
44 getDepth() const
45 {
46 return depth_;
47 }
48
49 [[nodiscard]] UInt256 const&
50 getNodeID() const
51 {
52 return id_;
53 }
54
55 [[nodiscard]] SHAMapNodeID
56 getChildNodeID(unsigned int branch) const;
57
69 [[nodiscard]] bool
70 isPrefixOf(UInt256 const& key) const;
71
80 static SHAMapNodeID
81 createID(unsigned int depth, UInt256 const& key);
82
91 operator<=>(SHAMapNodeID const& n) const
92 {
93 return std::tie(depth_, id_) <=> std::tie(n.depth_, n.id_);
94 }
95
104 bool
105 operator==(SHAMapNodeID const& n) const
106 {
107 return (depth_ == n.depth_) && (id_ == n.id_);
108 }
109};
110
111inline std::string
113{
114 if (node.isRoot())
115 return "NodeID(root)";
116
117 return "NodeID(" + std::to_string(node.getDepth()) + "," + to_string(node.getNodeID()) + ")";
118}
119
121operator<<(std::ostream& out, SHAMapNodeID const& node)
122{
123 return out << to_string(node);
124}
125
136[[nodiscard]] std::optional<SHAMapNodeID>
137deserializeSHAMapNodeID(void const* data, std::size_t size);
138
139[[nodiscard]] inline std::optional<SHAMapNodeID>
144
145
149[[nodiscard]] unsigned int
150selectBranch(SHAMapNodeID const& id, UInt256 const& hash);
151
152} // namespace xrpl
Identifies a node inside a SHAMap.
bool isPrefixOf(UInt256 const &key) const
Test whether this node ID lies on the path to the given leaf key.
SHAMapNodeID getChildNodeID(unsigned int branch) const
SHAMapNodeID()=default
SHAMapNodeID(SHAMapNodeID const &other)=default
unsigned int depth_
SHAMapNodeID & operator=(SHAMapNodeID const &other)=default
UInt256 const & getNodeID() const
bool operator==(SHAMapNodeID const &n) const
Equality, which the spaceship above does not provide.
static SHAMapNodeID createID(unsigned int depth, UInt256 const &key)
Create a SHAMapNodeID of a node with the depth of the node and the key of a leaf.
std::strong_ordering operator<=>(SHAMapNodeID const &n) const
Comparison operators.
unsigned int getDepth() 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:672
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:657
BaseUInt< 256 > UInt256
Definition base_uint.h:580
std::optional< SHAMapNodeID > deserializeSHAMapNodeID(void const *data, std::size_t size)
Return an object representing a serialized SHAMap Node ID.
unsigned int selectBranch(SHAMapNodeID const &id, UInt256 const &hash)
Returns the branch that would contain the given hash.
T size(T... args)
T tie(T... args)
T to_string(T... args)