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/base_uint.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <ripple/protocol/Serializer.h>
26 #include <ostream>
27 #include <string>
28 #include <tuple>
29 
30 namespace ripple {
31 
32 // Identifies a node in a half-SHA512 (256 bit) hash map
34 {
35 private:
37  int mDepth;
38 
39 public:
40  SHAMapNodeID();
41  SHAMapNodeID(int depth, uint256 const& hash);
42  SHAMapNodeID(void const* ptr, int len);
43 
44  bool
45  isValid() const;
46  bool
47  isRoot() const;
48 
49  // Convert to/from wire format (256-bit nodeID, 1-byte depth)
50  void
51  addIDRaw(Serializer& s) const;
53  getRawString() const;
54 
55  bool
56  operator==(const SHAMapNodeID& n) const;
57  bool
58  operator!=(const SHAMapNodeID& n) const;
59 
60  bool
61  operator<(const SHAMapNodeID& n) const;
62  bool
63  operator>(const SHAMapNodeID& n) const;
64  bool
65  operator<=(const SHAMapNodeID& n) const;
66  bool
67  operator>=(const SHAMapNodeID& n) const;
68 
70  getString() const;
71  void
72  dump(beast::Journal journal) const;
73 
74  // Only used by SHAMap and SHAMapTreeNode
75 
76  uint256 const&
77  getNodeID() const;
79  getChildNodeID(int m) const;
80  int
81  selectBranch(uint256 const& hash) const;
82  int
83  getDepth() const;
84  bool
85  has_common_prefix(SHAMapNodeID const& other) const;
86 
87 private:
88  static uint256 const&
89  Masks(int depth);
90 
91  friend std::ostream&
92  operator<<(std::ostream& out, SHAMapNodeID const& node);
93 
94 private: // Currently unused
96  getParentNodeID() const;
97 };
98 
99 //------------------------------------------------------------------------------
100 
101 inline SHAMapNodeID::SHAMapNodeID() : mDepth(0)
102 {
103 }
104 
105 inline int
107 {
108  return mDepth;
109 }
110 
111 inline uint256 const&
113 {
114  return mNodeID;
115 }
116 
117 inline bool
119 {
120  return (mDepth >= 0) && (mDepth <= 64);
121 }
122 
123 inline bool
125 {
126  return mDepth == 0;
127 }
128 
129 inline SHAMapNodeID
131 {
132  assert(mDepth);
133  return SHAMapNodeID(mDepth - 1, mNodeID & Masks(mDepth - 1));
134 }
135 
136 inline bool
138 {
139  return std::tie(mDepth, mNodeID) < std::tie(n.mDepth, n.mNodeID);
140 }
141 
142 inline bool
144 {
145  return n < *this;
146 }
147 
148 inline bool
150 {
151  return !(n < *this);
152 }
153 
154 inline bool
156 {
157  return !(*this < n);
158 }
159 
160 inline bool
162 {
163  return (mDepth == n.mDepth) && (mNodeID == n.mNodeID);
164 }
165 
166 inline bool
168 {
169  return !(*this == n);
170 }
171 
172 inline std::ostream&
174 {
175  return out << node.getString();
176 }
177 
178 } // namespace ripple
179 
180 #endif
ripple::SHAMapNodeID::selectBranch
int selectBranch(uint256 const &hash) const
Definition: SHAMapNodeID.cpp:114
std::string
STL class.
ripple::SHAMapNodeID::operator<
bool operator<(const SHAMapNodeID &n) const
Definition: SHAMapNodeID.h:137
ripple::SHAMapNodeID::getNodeID
uint256 const & getNodeID() const
Definition: SHAMapNodeID.h:112
ripple::SHAMapNodeID::getDepth
int getDepth() const
Definition: SHAMapNodeID.h:106
ripple::SHAMapNodeID::getChildNodeID
SHAMapNodeID getChildNodeID(int m) const
Definition: SHAMapNodeID.cpp:101
ripple::SHAMapNodeID::isValid
bool isValid() const
Definition: SHAMapNodeID.h:118
tuple
ripple::SHAMapNodeID
Definition: SHAMapNodeID.h:33
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:242
std::tie
T tie(T... args)
ripple::base_uint< 256 >
ripple::QualityDirection::out
@ out
ripple::SHAMapNodeID::mDepth
int mDepth
Definition: SHAMapNodeID.h:37
ripple::SHAMapNodeID::operator>
bool operator>(const SHAMapNodeID &n) const
Definition: SHAMapNodeID.h:143
std::ostream
STL class.
ripple::SHAMapNodeID::getRawString
std::string getRawString() const
Definition: SHAMapNodeID.cpp:92
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::SHAMapNodeID::operator!=
bool operator!=(const SHAMapNodeID &n) const
Definition: SHAMapNodeID.h:167
ripple::SHAMapNodeID::getParentNodeID
SHAMapNodeID getParentNodeID() const
Definition: SHAMapNodeID.h:130
ripple::Serializer
Definition: Serializer.h:39
ripple::SHAMapNodeID::addIDRaw
void addIDRaw(Serializer &s) const
Definition: SHAMapNodeID.cpp:85
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SHAMapNodeID::operator>=
bool operator>=(const SHAMapNodeID &n) const
Definition: SHAMapNodeID.h:155
ripple::SHAMapNodeID::SHAMapNodeID
SHAMapNodeID()
Definition: SHAMapNodeID.h:101
ripple::SHAMapNodeID::operator<<
friend std::ostream & operator<<(std::ostream &out, SHAMapNodeID const &node)
Definition: SHAMapNodeID.h:173
ripple::SHAMapNodeID::getString
std::string getString() const
Definition: SHAMapNodeID.cpp:76
ripple::SHAMapNodeID::Masks
static uint256 const & Masks(int depth)
Definition: SHAMapNodeID.cpp:31
ripple::SHAMapNodeID::mNodeID
uint256 mNodeID
Definition: SHAMapNodeID.h:36
ostream
ripple::SHAMapNodeID::operator<=
bool operator<=(const SHAMapNodeID &n) const
Definition: SHAMapNodeID.h:149
ripple::SHAMapNodeID::isRoot
bool isRoot() const
Definition: SHAMapNodeID.h:124
ripple::SHAMapNodeID::operator==
bool operator==(const SHAMapNodeID &n) const
Definition: SHAMapNodeID.h:161
ripple::SHAMapNodeID::dump
void dump(beast::Journal journal) const
Definition: SHAMapNodeID.cpp:149
ripple::SHAMapNodeID::has_common_prefix
bool has_common_prefix(SHAMapNodeID const &other) const
Definition: SHAMapNodeID.cpp:129
string