rippled
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 <ripple/basics/CountedObject.h>
24 #include <ripple/basics/base_uint.h>
25 #include <optional>
26 #include <ostream>
27 #include <string>
28 #include <tuple>
29 
30 namespace ripple {
31 
33 class SHAMapNodeID : public CountedObject<SHAMapNodeID>
34 {
35 private:
37  unsigned int depth_ = 0;
38 
39 public:
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 
122 inline 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 
132 inline std::ostream&
134 {
135  return out << to_string(node);
136 }
137 
147 [[nodiscard]] std::optional<SHAMapNodeID>
148 deserializeSHAMapNodeID(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
159 selectBranch(SHAMapNodeID const& id, uint256 const& hash);
160 
161 } // namespace ripple
162 
163 #endif
ripple::SHAMapNodeID::getChildNodeID
SHAMapNodeID getChildNodeID(unsigned int m) const
Definition: SHAMapNodeID.cpp:74
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
std::string
STL class.
ripple::selectBranch
unsigned int selectBranch(SHAMapNodeID const &id, uint256 const &hash)
Returns the branch that would contain the given hash.
Definition: SHAMapNodeID.cpp:121
ripple::deserializeSHAMapNodeID
std::optional< SHAMapNodeID > deserializeSHAMapNodeID(void const *data, std::size_t size)
Return an object representing a serialized SHAMap Node ID.
Definition: SHAMapNodeID.cpp:101
std::string::size
T size(T... args)
ripple::SHAMapNodeID::id_
uint256 id_
Definition: SHAMapNodeID.h:36
ripple::SHAMapNodeID::operator==
bool operator==(SHAMapNodeID const &n) const
Definition: SHAMapNodeID.h:110
tuple
ripple::SHAMapNodeID
Identifies a node inside a SHAMap.
Definition: SHAMapNodeID.h:33
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:242
ripple::SHAMapNodeID::operator>
bool operator>(SHAMapNodeID const &n) const
Definition: SHAMapNodeID.h:92
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
std::tie
T tie(T... args)
ripple::SHAMapNodeID::SHAMapNodeID
SHAMapNodeID()=default
ripple::SHAMapNodeID::operator=
SHAMapNodeID & operator=(SHAMapNodeID const &other)=default
ripple::base_uint< 256 >
ripple::SHAMapNodeID::operator<
bool operator<(SHAMapNodeID const &n) const
Comparison operators.
Definition: SHAMapNodeID.h:86
ripple::SHAMapNodeID::operator!=
bool operator!=(SHAMapNodeID const &n) const
Definition: SHAMapNodeID.h:116
ripple::QualityDirection::out
@ out
std::ostream
STL class.
ripple::SHAMapNodeID::operator>=
bool operator>=(SHAMapNodeID const &n) const
Definition: SHAMapNodeID.h:104
ripple::SHAMapNodeID::getRawString
std::string getRawString() const
Definition: SHAMapNodeID.cpp:65
std::to_string
T to_string(T... args)
ripple::SHAMapNodeID::getDepth
unsigned int getDepth() const
Definition: SHAMapNodeID.h:58
ripple::SHAMapNodeID::operator<=
bool operator<=(SHAMapNodeID const &n) const
Definition: SHAMapNodeID.h:98
ripple::SHAMapNodeID::depth_
unsigned int depth_
Definition: SHAMapNodeID.h:37
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
optional
std::size_t
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::SHAMapNodeID::getNodeID
uint256 const & getNodeID() const
Definition: SHAMapNodeID.h:64
ostream
std::string::data
T data(T... args)
ripple::SHAMapNodeID::isRoot
bool isRoot() const
Definition: SHAMapNodeID.h:48
ripple::SHAMapNodeID::createID
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.
Definition: SHAMapNodeID.cpp:136
string