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 
72  // FIXME-C++20: use spaceship and operator synthesis
74  bool
75  operator<(SHAMapNodeID const& n) const
76  {
77  return std::tie(depth_, id_) < std::tie(n.depth_, n.id_);
78  }
79 
80  bool
81  operator>(SHAMapNodeID const& n) const
82  {
83  return n < *this;
84  }
85 
86  bool
87  operator<=(SHAMapNodeID const& n) const
88  {
89  return !(n < *this);
90  }
91 
92  bool
93  operator>=(SHAMapNodeID const& n) const
94  {
95  return !(*this < n);
96  }
97 
98  bool
99  operator==(SHAMapNodeID const& n) const
100  {
101  return (depth_ == n.depth_) && (id_ == n.id_);
102  }
103 
104  bool
105  operator!=(SHAMapNodeID const& n) const
106  {
107  return !(*this == n);
108  }
109 };
110 
111 inline std::string
113 {
114  if (node.isRoot())
115  return "NodeID(root)";
116 
117  return "NodeID(" + std::to_string(node.getDepth()) + "," +
118  to_string(node.getNodeID()) + ")";
119 }
120 
121 inline std::ostream&
123 {
124  return out << to_string(node);
125 }
126 
136 [[nodiscard]] std::optional<SHAMapNodeID>
137 deserializeSHAMapNodeID(void const* data, std::size_t size);
138 
139 [[nodiscard]] inline std::optional<SHAMapNodeID>
141 {
142  return deserializeSHAMapNodeID(s.data(), s.size());
143 }
147 [[nodiscard]] unsigned int
148 selectBranch(SHAMapNodeID const& id, uint256 const& hash);
149 
150 } // namespace ripple
151 
152 #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:99
tuple
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:45
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:81
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:457
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:75
ripple::SHAMapNodeID::operator!=
bool operator!=(SHAMapNodeID const &n) const
Definition: SHAMapNodeID.h:105
ripple::QualityDirection::out
@ out
std::ostream
STL class.
ripple::SHAMapNodeID::operator>=
bool operator>=(SHAMapNodeID const &n) const
Definition: SHAMapNodeID.h:93
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:87
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::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
string