rippled
Loading...
Searching...
No Matches
SHAMapNodeID.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_SHAMAP_SHAMAPNODEID_H_INCLUDED
21#define RIPPLE_SHAMAP_SHAMAPNODEID_H_INCLUDED
22
23#include <xrpl/basics/CountedObject.h>
24#include <xrpl/basics/base_uint.h>
25
26#include <optional>
27#include <string>
28#include <tuple>
29
30namespace ripple {
31
33class SHAMapNodeID : public CountedObject<SHAMapNodeID>
34{
35private:
37 unsigned int depth_ = 0;
38
39public:
40 SHAMapNodeID() = default;
41 SHAMapNodeID(SHAMapNodeID const& other) = default;
42 SHAMapNodeID(unsigned int depth, uint256 const& hash);
43
45 operator=(SHAMapNodeID const& other) = default;
46
47 bool
48 isRoot() const
49 {
50 return depth_ == 0;
51 }
52
53 // Get the wire format (256-bit nodeID, 1-byte depth)
55 getRawString() const;
56
57 unsigned int
58 getDepth() const
59 {
60 return depth_;
61 }
62
63 uint256 const&
64 getNodeID() const
65 {
66 return id_;
67 }
68
70 getChildNodeID(unsigned int m) const;
71
80 static SHAMapNodeID
81 createID(int depth, uint256 const& key);
82
83 // FIXME-C++20: use spaceship and operator synthesis
85 bool
86 operator<(SHAMapNodeID const& n) const
87 {
88 return std::tie(depth_, id_) < std::tie(n.depth_, n.id_);
89 }
90
91 bool
92 operator>(SHAMapNodeID const& n) const
93 {
94 return n < *this;
95 }
96
97 bool
98 operator<=(SHAMapNodeID const& n) const
99 {
100 return !(n < *this);
101 }
102
103 bool
104 operator>=(SHAMapNodeID const& n) const
105 {
106 return !(*this < n);
107 }
108
109 bool
110 operator==(SHAMapNodeID const& n) const
111 {
112 return (depth_ == n.depth_) && (id_ == n.id_);
113 }
114
115 bool
116 operator!=(SHAMapNodeID const& n) const
117 {
118 return !(*this == n);
119 }
120};
121
122inline std::string
124{
125 if (node.isRoot())
126 return "NodeID(root)";
127
128 return "NodeID(" + std::to_string(node.getDepth()) + "," +
129 to_string(node.getNodeID()) + ")";
130}
131
134{
135 return out << to_string(node);
136}
137
147[[nodiscard]] std::optional<SHAMapNodeID>
148deserializeSHAMapNodeID(void const* data, std::size_t size);
149
150[[nodiscard]] inline std::optional<SHAMapNodeID>
152{
153 return deserializeSHAMapNodeID(s.data(), s.size());
154}
158[[nodiscard]] unsigned int
159selectBranch(SHAMapNodeID const& id, uint256 const& hash);
160
161} // namespace ripple
162
163#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:25
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:558
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:647
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:630
T size(T... args)
T tie(T... args)
T to_string(T... args)