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/protocol/Serializer.h>
24 #include <ripple/basics/base_uint.h>
25 #include <ripple/beast/utility/Journal.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 isValid () const;
45  bool isRoot () const;
46 
47  // Convert to/from wire format (256-bit nodeID, 1-byte depth)
48  void addIDRaw (Serializer& s) const;
49  std::string getRawString () const;
50 
51  bool operator== (const SHAMapNodeID& n) const;
52  bool operator!= (const SHAMapNodeID& n) const;
53 
54  bool operator< (const SHAMapNodeID& n) const;
55  bool operator> (const SHAMapNodeID& n) const;
56  bool operator<= (const SHAMapNodeID& n) const;
57  bool operator>= (const SHAMapNodeID& n) const;
58 
59  std::string getString () const;
60  void dump (beast::Journal journal) const;
61 
62 // Only used by SHAMap and SHAMapTreeNode
63 
64  uint256 const& getNodeID () const;
65  SHAMapNodeID getChildNodeID (int m) const;
66  int selectBranch (uint256 const& hash) const;
67  int getDepth () const;
68  bool has_common_prefix(SHAMapNodeID const& other) const;
69 
70 private:
71  static uint256 const& Masks (int depth);
72 
73  friend std::ostream& operator<< (std::ostream& out, SHAMapNodeID const& node);
74 
75 private: // Currently unused
77 };
78 
79 //------------------------------------------------------------------------------
80 
81 inline
83  : mDepth (0)
84 {
85 }
86 
87 inline
88 int
90 {
91  return mDepth;
92 }
93 
94 inline
95 uint256 const&
97 {
98  return mNodeID;
99 }
100 
101 inline
102 bool
104 {
105  return (mDepth >= 0) && (mDepth <= 64);
106 }
107 
108 inline
109 bool
111 {
112  return mDepth == 0;
113 }
114 
115 inline
118 {
119  assert (mDepth);
120  return SHAMapNodeID (mDepth - 1,
121  mNodeID & Masks (mDepth - 1));
122 }
123 
124 inline
125 bool
127 {
128  return std::tie(mDepth, mNodeID) < std::tie(n.mDepth, n.mNodeID);
129 }
130 
131 inline
132 bool
134 {
135  return n < *this;
136 }
137 
138 inline
139 bool
141 {
142  return !(n < *this);
143 }
144 
145 inline
146 bool
148 {
149  return !(*this < n);
150 }
151 
152 inline
153 bool
155 {
156  return (mDepth == n.mDepth) && (mNodeID == n.mNodeID);
157 }
158 
159 inline
160 bool
162 {
163  return !(*this == n);
164 }
165 
167 {
168  return out << node.getString ();
169 }
170 
171 } // ripple
172 
173 #endif
ripple::SHAMapNodeID::selectBranch
int selectBranch(uint256 const &hash) const
Definition: SHAMapNodeID.cpp:113
std::string
STL class.
ripple::SHAMapNodeID::operator<
bool operator<(const SHAMapNodeID &n) const
Definition: SHAMapNodeID.h:126
ripple::SHAMapNodeID::getNodeID
uint256 const & getNodeID() const
Definition: SHAMapNodeID.h:96
ripple::SHAMapNodeID::getDepth
int getDepth() const
Definition: SHAMapNodeID.h:89
ripple::SHAMapNodeID::getChildNodeID
SHAMapNodeID getChildNodeID(int m) const
Definition: SHAMapNodeID.cpp:101
ripple::SHAMapNodeID::isValid
bool isValid() const
Definition: SHAMapNodeID.h:103
tuple
ripple::SHAMapNodeID
Definition: SHAMapNodeID.h:33
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:237
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:133
std::ostream
STL class.
ripple::SHAMapNodeID::getRawString
std::string getRawString() const
Definition: SHAMapNodeID.cpp:93
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::SHAMapNodeID::operator!=
bool operator!=(const SHAMapNodeID &n) const
Definition: SHAMapNodeID.h:161
ripple::SHAMapNodeID::getParentNodeID
SHAMapNodeID getParentNodeID() const
Definition: SHAMapNodeID.h:117
ripple::Serializer
Definition: Serializer.h:43
ripple::SHAMapNodeID::addIDRaw
void addIDRaw(Serializer &s) const
Definition: SHAMapNodeID.cpp:87
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:147
ripple::SHAMapNodeID::SHAMapNodeID
SHAMapNodeID()
Definition: SHAMapNodeID.h:82
ripple::SHAMapNodeID::operator<<
friend std::ostream & operator<<(std::ostream &out, SHAMapNodeID const &node)
Definition: SHAMapNodeID.h:166
ripple::SHAMapNodeID::getString
std::string getString() const
Definition: SHAMapNodeID.cpp:78
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:140
ripple::SHAMapNodeID::isRoot
bool isRoot() const
Definition: SHAMapNodeID.h:110
ripple::SHAMapNodeID::operator==
bool operator==(const SHAMapNodeID &n) const
Definition: SHAMapNodeID.h:154
ripple::SHAMapNodeID::dump
void dump(beast::Journal journal) const
Definition: SHAMapNodeID.cpp:146
ripple::SHAMapNodeID::has_common_prefix
bool has_common_prefix(SHAMapNodeID const &other) const
Definition: SHAMapNodeID.cpp:128
string