rippled
SHAMapTreeNode.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_SHAMAPTREENODE_H_INCLUDED
21 #define RIPPLE_SHAMAP_SHAMAPTREENODE_H_INCLUDED
22 
23 #include <ripple/basics/CountedObject.h>
24 #include <ripple/basics/TaggedCache.h>
25 #include <ripple/beast/utility/Journal.h>
26 #include <ripple/shamap/SHAMapItem.h>
27 #include <ripple/shamap/SHAMapNodeID.h>
28 
29 #include <cstdint>
30 #include <memory>
31 #include <mutex>
32 #include <string>
33 
34 namespace ripple {
35 
36 // These are wire-protocol identifiers used during serialization to encode the
37 // type of a node. They should not be arbitrarily be changed.
38 static constexpr unsigned char const wireTypeTransaction = 0;
39 static constexpr unsigned char const wireTypeAccountState = 1;
40 static constexpr unsigned char const wireTypeInner = 2;
41 static constexpr unsigned char const wireTypeCompressedInner = 3;
42 static constexpr unsigned char const wireTypeTransactionWithMeta = 4;
43 
44 // A SHAMapHash is the hash of a node in a SHAMap, and also the
45 // type of the hash of the entire SHAMap.
46 
48 {
50 
51 public:
52  SHAMapHash() = default;
53  explicit SHAMapHash(uint256 const& hash) : hash_(hash)
54  {
55  }
56 
57  uint256 const&
58  as_uint256() const
59  {
60  return hash_;
61  }
62  uint256&
64  {
65  return hash_;
66  }
67  bool
68  isZero() const
69  {
70  return hash_.isZero();
71  }
72  bool
73  isNonZero() const
74  {
75  return hash_.isNonZero();
76  }
77  int
78  signum() const
79  {
80  return hash_.signum();
81  }
82  void
83  zero()
84  {
85  hash_.zero();
86  }
87 
88  friend bool
89  operator==(SHAMapHash const& x, SHAMapHash const& y)
90  {
91  return x.hash_ == y.hash_;
92  }
93 
94  friend bool
95  operator<(SHAMapHash const& x, SHAMapHash const& y)
96  {
97  return x.hash_ < y.hash_;
98  }
99 
100  friend std::ostream&
102  {
103  return os << x.hash_;
104  }
105 
106  friend std::string
108  {
109  return to_string(x.hash_);
110  }
111 
112  template <class H>
113  friend void
114  hash_append(H& h, SHAMapHash const& x)
115  {
116  hash_append(h, x.hash_);
117  }
118 };
119 
120 inline bool
121 operator!=(SHAMapHash const& x, SHAMapHash const& y)
122 {
123  return !(x == y);
124 }
125 
126 enum class SHAMapNodeType {
127  tnINNER = 1,
128  tnTRANSACTION_NM = 2, // transaction, no metadata
129  tnTRANSACTION_MD = 3, // transaction, with metadata
130  tnACCOUNT_STATE = 4
131 };
132 
134 {
135 protected:
137 
145 
146 protected:
147  SHAMapTreeNode(SHAMapTreeNode const&) = delete;
149  operator=(SHAMapTreeNode const&) = delete;
150 
158  {
159  }
160 
161  explicit SHAMapTreeNode(
163  SHAMapHash const& hash) noexcept
164  : hash_(hash), cowid_(cowid)
165  {
166  }
169 public:
170  virtual ~SHAMapTreeNode() noexcept = default;
171 
195  std::uint32_t
196  cowid() const
197  {
198  return cowid_;
199  }
200 
206  void
208  {
209  cowid_ = 0;
210  }
211 
214  clone(std::uint32_t cowid) const = 0;
218  virtual void
219  updateHash() = 0;
220 
222  SHAMapHash const&
223  getHash() const
224  {
225  return hash_;
226  }
227 
229  virtual SHAMapNodeType
230  getType() const = 0;
231 
233  virtual bool
234  isLeaf() const = 0;
235 
237  virtual bool
238  isInner() const = 0;
239 
241  virtual void
242  serializeForWire(Serializer&) const = 0;
243 
245  virtual void
246  serializeWithPrefix(Serializer&) const = 0;
247 
248  virtual std::string
249  getString(SHAMapNodeID const&) const;
250 
251  virtual void
252  invariants(bool is_root = false) const = 0;
253 
255  makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
256 
258  makeFromWire(Slice rawNode);
259 
260 private:
262  makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
263 
265  makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
266 
268  makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
269 };
270 
271 } // namespace ripple
272 
273 #endif
ripple::SHAMapTreeNode::cowid
std::uint32_t cowid() const
Returns the SHAMap that owns this node.
Definition: SHAMapTreeNode.h:196
ripple::base_uint::signum
int signum() const
Definition: base_uint.h:229
std::string
STL class.
std::shared_ptr
STL class.
ripple::SHAMapNodeType::tnINNER
@ tnINNER
ripple::base_uint::isNonZero
bool isNonZero() const
Definition: base_uint.h:444
ripple::SHAMapHash::operator<
friend bool operator<(SHAMapHash const &x, SHAMapHash const &y)
Definition: SHAMapTreeNode.h:95
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::SHAMapNodeType::tnACCOUNT_STATE
@ tnACCOUNT_STATE
ripple::SHAMapTreeNode::cowid_
std::uint32_t cowid_
Determines the owning SHAMap, if any.
Definition: SHAMapTreeNode.h:144
ripple::SHAMapNodeType
SHAMapNodeType
Definition: SHAMapTreeNode.h:126
ripple::SHAMapTreeNode::serializeWithPrefix
virtual void serializeWithPrefix(Serializer &) const =0
Serialize the node in a format appropriate for hashing.
ripple::SHAMapTreeNode::operator=
SHAMapTreeNode & operator=(SHAMapTreeNode const &)=delete
ripple::SHAMapHash::as_uint256
uint256 & as_uint256()
Definition: SHAMapTreeNode.h:63
ripple::SHAMapTreeNode::invariants
virtual void invariants(bool is_root=false) const =0
ripple::SHAMapTreeNode::unshare
void unshare()
If this node is shared with another map, mark it as no longer shared.
Definition: SHAMapTreeNode.h:207
ripple::wireTypeTransaction
static constexpr unsigned const char wireTypeTransaction
Definition: SHAMapTreeNode.h:38
ripple::SHAMapHash::isZero
bool isZero() const
Definition: SHAMapTreeNode.h:68
ripple::wireTypeInner
static constexpr unsigned const char wireTypeInner
Definition: SHAMapTreeNode.h:40
ripple::SHAMapTreeNode::makeFromWire
static std::shared_ptr< SHAMapTreeNode > makeFromWire(Slice rawNode)
Definition: SHAMapTreeNode.cpp:119
ripple::SHAMapNodeID
Identifies a node inside a SHAMap.
Definition: SHAMapNodeID.h:33
ripple::SHAMapHash::isNonZero
bool isNonZero() const
Definition: SHAMapTreeNode.h:73
ripple::SHAMapTreeNode::clone
virtual std::shared_ptr< SHAMapTreeNode > clone(std::uint32_t cowid) const =0
Make a copy of this node, setting the owner.
ripple::SHAMapTreeNode::getString
virtual std::string getString(SHAMapNodeID const &) const
Definition: SHAMapTreeNode.cpp:187
ripple::SHAMapTreeNode::isInner
virtual bool isInner() const =0
Determines if this is an inner node.
ripple::SHAMapHash::SHAMapHash
SHAMapHash()=default
ripple::SHAMapTreeNode::hash_
SHAMapHash hash_
Definition: SHAMapTreeNode.h:136
ripple::SHAMapHash
Definition: SHAMapTreeNode.h:47
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
ripple::wireTypeTransactionWithMeta
static constexpr unsigned const char wireTypeTransactionWithMeta
Definition: SHAMapTreeNode.h:42
ripple::wireTypeAccountState
static constexpr unsigned const char wireTypeAccountState
Definition: SHAMapTreeNode.h:39
ripple::SHAMapHash::to_string
friend std::string to_string(SHAMapHash const &x)
Definition: SHAMapTreeNode.h:107
ripple::base_uint< 256 >
ripple::SHAMapTreeNode::makeTransaction
static std::shared_ptr< SHAMapTreeNode > makeTransaction(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapTreeNode.cpp:40
ripple::base_uint::isZero
bool isZero() const
Definition: base_uint.h:439
std::ostream
STL class.
ripple::SHAMapTreeNode::makeTransactionWithMeta
static std::shared_ptr< SHAMapTreeNode > makeTransactionWithMeta(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapTreeNode.cpp:58
ripple::SHAMapHash::hash_
uint256 hash_
Definition: SHAMapTreeNode.h:49
ripple::operator!=
bool operator!=(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:165
ripple::SHAMapNodeType::tnTRANSACTION_MD
@ tnTRANSACTION_MD
ripple::SHAMapTreeNode
Definition: SHAMapTreeNode.h:133
ripple::SHAMapHash::operator<<
friend std::ostream & operator<<(std::ostream &os, SHAMapHash const &x)
Definition: SHAMapTreeNode.h:101
ripple::SHAMapTreeNode::isLeaf
virtual bool isLeaf() const =0
Determines if this is a leaf node.
ripple::SHAMapTreeNode::SHAMapTreeNode
SHAMapTreeNode(SHAMapTreeNode const &)=delete
cstdint
ripple::SHAMapTreeNode::SHAMapTreeNode
SHAMapTreeNode(std::uint32_t cowid) noexcept
Construct a node.
Definition: SHAMapTreeNode.h:157
std::uint32_t
ripple::SHAMapTreeNode::updateHash
virtual void updateHash()=0
Recalculate the hash of this node.
memory
ripple::SHAMapHash::operator==
friend bool operator==(SHAMapHash const &x, SHAMapHash const &y)
Definition: SHAMapTreeNode.h:89
ripple::SHAMapTreeNode::getHash
SHAMapHash const & getHash() const
Return the hash of this node.
Definition: SHAMapTreeNode.h:223
ripple::Serializer
Definition: Serializer.h:39
ripple::wireTypeCompressedInner
static constexpr unsigned const char wireTypeCompressedInner
Definition: SHAMapTreeNode.h:41
ripple::SHAMapTreeNode::makeFromPrefix
static std::shared_ptr< SHAMapTreeNode > makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
Definition: SHAMapTreeNode.cpp:151
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SHAMapTreeNode::getType
virtual SHAMapNodeType getType() const =0
Determines the type of node.
ripple::SHAMapHash::SHAMapHash
SHAMapHash(uint256 const &hash)
Definition: SHAMapTreeNode.h:53
ripple::base_uint::zero
void zero()
Definition: base_uint.h:449
ripple::SHAMapHash::zero
void zero()
Definition: SHAMapTreeNode.h:83
std
STL namespace.
ripple::SHAMapTreeNode::~SHAMapTreeNode
virtual ~SHAMapTreeNode() noexcept=default
mutex
ripple::SHAMapTreeNode::serializeForWire
virtual void serializeForWire(Serializer &) const =0
Serialize the node in a format appropriate for sending over the wire.
ripple::SHAMapTreeNode::SHAMapTreeNode
SHAMapTreeNode(std::uint32_t cowid, SHAMapHash const &hash) noexcept
Definition: SHAMapTreeNode.h:161
ripple::SHAMapHash::as_uint256
uint256 const & as_uint256() const
Definition: SHAMapTreeNode.h:58
ripple::SHAMapHash::signum
int signum() const
Definition: SHAMapTreeNode.h:78
ripple::SHAMapHash::hash_append
friend void hash_append(H &h, SHAMapHash const &x)
Definition: SHAMapTreeNode.h:114
ripple::SHAMapTreeNode::makeAccountState
static std::shared_ptr< SHAMapTreeNode > makeAccountState(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapTreeNode.cpp:87
string