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 m) const;
57
66 static SHAMapNodeID
67 createID(int depth, uint256 const& key);
68
77 operator<=>(SHAMapNodeID const& n) const
78 {
79 return std::tie(depth_, id_) <=> std::tie(n.depth_, n.id_);
80 }
81
90 bool
91 operator==(SHAMapNodeID const& n) const
92 {
93 return (depth_ == n.depth_) && (id_ == n.id_);
94 }
95};
96
97inline std::string
99{
100 if (node.isRoot())
101 return "NodeID(root)";
102
103 return "NodeID(" + std::to_string(node.getDepth()) + "," + to_string(node.getNodeID()) + ")";
104}
105
107operator<<(std::ostream& out, SHAMapNodeID const& node)
108{
109 return out << to_string(node);
110}
111
122[[nodiscard]] std::optional<SHAMapNodeID>
123deserializeSHAMapNodeID(void const* data, std::size_t size);
124
125[[nodiscard]] inline std::optional<SHAMapNodeID>
130
131
135[[nodiscard]] unsigned int
136selectBranch(SHAMapNodeID const& id, uint256 const& hash);
137
138} // namespace xrpl
Identifies a node inside a SHAMap.
uint256 const & getNodeID() 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
SHAMapNodeID()=default
SHAMapNodeID(SHAMapNodeID const &other)=default
unsigned int depth_
SHAMapNodeID & operator=(SHAMapNodeID const &other)=default
bool operator==(SHAMapNodeID const &n) const
Equality, which the spaceship above does not provide.
std::strong_ordering operator<=>(SHAMapNodeID const &n) const
Comparison operators.
unsigned int getDepth() const
std::string getRawString() 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:666
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
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:580
T size(T... args)
T tie(T... args)
T to_string(T... args)