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/protocol/Serializer.h>
27 #include <ripple/shamap/SHAMapItem.h>
28 #include <ripple/shamap/SHAMapNodeID.h>
29 
30 #include <cstdint>
31 #include <memory>
32 #include <mutex>
33 #include <string>
34 
35 namespace ripple {
36 
37 // These are wire-protocol identifiers used during serialization to encode the
38 // type of a node. They should not be arbitrarily be changed.
39 static constexpr unsigned char const wireTypeTransaction = 0;
40 static constexpr unsigned char const wireTypeAccountState = 1;
41 static constexpr unsigned char const wireTypeInner = 2;
42 static constexpr unsigned char const wireTypeCompressedInner = 3;
43 static constexpr unsigned char const wireTypeTransactionWithMeta = 4;
44 
45 // A SHAMapHash is the hash of a node in a SHAMap, and also the
46 // type of the hash of the entire SHAMap.
47 
49 {
51 
52 public:
53  SHAMapHash() = default;
54  explicit SHAMapHash(uint256 const& hash) : hash_(hash)
55  {
56  }
57 
58  uint256 const&
59  as_uint256() const
60  {
61  return hash_;
62  }
63  uint256&
65  {
66  return hash_;
67  }
68  bool
69  isZero() const
70  {
71  return hash_.isZero();
72  }
73  bool
74  isNonZero() const
75  {
76  return hash_.isNonZero();
77  }
78  int
79  signum() const
80  {
81  return hash_.signum();
82  }
83  void
84  zero()
85  {
86  hash_.zero();
87  }
88 
89  friend bool
90  operator==(SHAMapHash const& x, SHAMapHash const& y)
91  {
92  return x.hash_ == y.hash_;
93  }
94 
95  friend bool
96  operator<(SHAMapHash const& x, SHAMapHash const& y)
97  {
98  return x.hash_ < y.hash_;
99  }
100 
101  friend std::ostream&
103  {
104  return os << x.hash_;
105  }
106 
107  friend std::string
109  {
110  return to_string(x.hash_);
111  }
112 
113  template <class H>
114  friend void
115  hash_append(H& h, SHAMapHash const& x)
116  {
117  hash_append(h, x.hash_);
118  }
119 };
120 
121 inline bool
122 operator!=(SHAMapHash const& x, SHAMapHash const& y)
123 {
124  return !(x == y);
125 }
126 
127 enum class SHAMapNodeType {
128  tnINNER = 1,
129  tnTRANSACTION_NM = 2, // transaction, no metadata
130  tnTRANSACTION_MD = 3, // transaction, with metadata
131  tnACCOUNT_STATE = 4
132 };
133 
135 {
136 protected:
138 
146 
147 protected:
148  SHAMapTreeNode(SHAMapTreeNode const&) = delete;
150  operator=(SHAMapTreeNode const&) = delete;
151 
159  {
160  }
161 
162  explicit SHAMapTreeNode(
164  SHAMapHash const& hash) noexcept
165  : hash_(hash), cowid_(cowid)
166  {
167  }
170 public:
171  virtual ~SHAMapTreeNode() noexcept = default;
172 
196  std::uint32_t
197  cowid() const
198  {
199  return cowid_;
200  }
201 
207  void
209  {
210  cowid_ = 0;
211  }
212 
215  clone(std::uint32_t cowid) const = 0;
219  virtual void
220  updateHash() = 0;
221 
223  SHAMapHash const&
224  getHash() const
225  {
226  return hash_;
227  }
228 
230  virtual SHAMapNodeType
231  getType() const = 0;
232 
234  virtual bool
235  isLeaf() const = 0;
236 
238  virtual bool
239  isInner() const = 0;
240 
242  virtual void
243  serializeForWire(Serializer&) const = 0;
244 
246  virtual void
247  serializeWithPrefix(Serializer&) const = 0;
248 
249  virtual std::string
250  getString(SHAMapNodeID const&) const;
251 
252  virtual void
253  invariants(bool is_root = false) const = 0;
254 
256  makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
257 
259  makeFromWire(Slice rawNode);
260 
261 private:
263  makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
264 
266  makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
267 
269  makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
270 };
271 
272 } // namespace ripple
273 
274 #endif
ripple::SHAMapTreeNode::cowid
std::uint32_t cowid() const
Returns the SHAMap that owns this node.
Definition: SHAMapTreeNode.h:197
std::string
STL class.
std::shared_ptr
STL class.
ripple::SHAMapNodeType::tnINNER
@ tnINNER
ripple::base_uint::isNonZero
bool isNonZero() const
Definition: base_uint.h:516
ripple::SHAMapHash::operator<
friend bool operator<(SHAMapHash const &x, SHAMapHash const &y)
Definition: SHAMapTreeNode.h:96
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:145
ripple::SHAMapNodeType
SHAMapNodeType
Definition: SHAMapTreeNode.h:127
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:64
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:208
ripple::wireTypeTransaction
static constexpr unsigned const char wireTypeTransaction
Definition: SHAMapTreeNode.h:39
ripple::SHAMapHash::isZero
bool isZero() const
Definition: SHAMapTreeNode.h:69
ripple::wireTypeInner
static constexpr unsigned const char wireTypeInner
Definition: SHAMapTreeNode.h:41
ripple::SHAMapTreeNode::makeFromWire
static std::shared_ptr< SHAMapTreeNode > makeFromWire(Slice rawNode)
Definition: SHAMapTreeNode.cpp:116
ripple::SHAMapNodeID
Identifies a node inside a SHAMap.
Definition: SHAMapNodeID.h:33
ripple::SHAMapHash::isNonZero
bool isNonZero() const
Definition: SHAMapTreeNode.h:74
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:184
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:137
ripple::SHAMapHash
Definition: SHAMapTreeNode.h:48
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
ripple::wireTypeTransactionWithMeta
static constexpr unsigned const char wireTypeTransactionWithMeta
Definition: SHAMapTreeNode.h:43
ripple::wireTypeAccountState
static constexpr unsigned const char wireTypeAccountState
Definition: SHAMapTreeNode.h:40
ripple::SHAMapHash::to_string
friend std::string to_string(SHAMapHash const &x)
Definition: SHAMapTreeNode.h:108
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:511
std::ostream
STL class.
ripple::SHAMapTreeNode::makeTransactionWithMeta
static std::shared_ptr< SHAMapTreeNode > makeTransactionWithMeta(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapTreeNode.cpp:55
ripple::SHAMapHash::hash_
uint256 hash_
Definition: SHAMapTreeNode.h:50
ripple::operator!=
bool operator!=(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:169
ripple::SHAMapNodeType::tnTRANSACTION_MD
@ tnTRANSACTION_MD
ripple::SHAMapTreeNode
Definition: SHAMapTreeNode.h:134
ripple::SHAMapHash::operator<<
friend std::ostream & operator<<(std::ostream &os, SHAMapHash const &x)
Definition: SHAMapTreeNode.h:102
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:158
std::uint32_t
ripple::base_uint::signum
constexpr int signum() const
Definition: base_uint.h:320
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:90
ripple::SHAMapTreeNode::getHash
SHAMapHash const & getHash() const
Return the hash of this node.
Definition: SHAMapTreeNode.h:224
ripple::Serializer
Definition: Serializer.h:39
ripple::wireTypeCompressedInner
static constexpr unsigned const char wireTypeCompressedInner
Definition: SHAMapTreeNode.h:42
ripple::SHAMapTreeNode::makeFromPrefix
static std::shared_ptr< SHAMapTreeNode > makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
Definition: SHAMapTreeNode.cpp:148
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:54
ripple::base_uint::zero
void zero()
Definition: base_uint.h:521
ripple::SHAMapHash::zero
void zero()
Definition: SHAMapTreeNode.h:84
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:162
ripple::SHAMapHash::as_uint256
uint256 const & as_uint256() const
Definition: SHAMapTreeNode.h:59
ripple::SHAMapHash::signum
int signum() const
Definition: SHAMapTreeNode.h:79
ripple::SHAMapHash::hash_append
friend void hash_append(H &h, SHAMapHash const &x)
Definition: SHAMapTreeNode.h:115
ripple::SHAMapTreeNode::makeAccountState
static std::shared_ptr< SHAMapTreeNode > makeAccountState(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapTreeNode.cpp:84
string