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/SHAMapHash.h>
25 #include <ripple/basics/TaggedCache.h>
26 #include <ripple/beast/utility/Journal.h>
27 #include <ripple/protocol/Serializer.h>
28 #include <ripple/shamap/SHAMapItem.h>
29 #include <ripple/shamap/SHAMapNodeID.h>
30 
31 #include <cstdint>
32 #include <memory>
33 #include <mutex>
34 #include <string>
35 
36 namespace ripple {
37 
38 // These are wire-protocol identifiers used during serialization to encode the
39 // type of a node. They should not be arbitrarily be changed.
40 static constexpr unsigned char const wireTypeTransaction = 0;
41 static constexpr unsigned char const wireTypeAccountState = 1;
42 static constexpr unsigned char const wireTypeInner = 2;
43 static constexpr unsigned char const wireTypeCompressedInner = 3;
44 static constexpr unsigned char const wireTypeTransactionWithMeta = 4;
45 
46 enum class SHAMapNodeType {
47  tnINNER = 1,
48  tnTRANSACTION_NM = 2, // transaction, no metadata
49  tnTRANSACTION_MD = 3, // transaction, with metadata
50  tnACCOUNT_STATE = 4
51 };
52 
54 {
55 protected:
57 
65 
66 protected:
67  SHAMapTreeNode(SHAMapTreeNode const&) = delete;
69  operator=(SHAMapTreeNode const&) = delete;
70 
78  {
79  }
80 
81  explicit SHAMapTreeNode(
83  SHAMapHash const& hash) noexcept
84  : hash_(hash), cowid_(cowid)
85  {
86  }
89 public:
90  virtual ~SHAMapTreeNode() noexcept = default;
91 
115  std::uint32_t
116  cowid() const
117  {
118  return cowid_;
119  }
120 
126  void
128  {
129  cowid_ = 0;
130  }
131 
134  clone(std::uint32_t cowid) const = 0;
138  virtual void
139  updateHash() = 0;
140 
142  SHAMapHash const&
143  getHash() const
144  {
145  return hash_;
146  }
147 
149  virtual SHAMapNodeType
150  getType() const = 0;
151 
153  virtual bool
154  isLeaf() const = 0;
155 
157  virtual bool
158  isInner() const = 0;
159 
161  virtual void
162  serializeForWire(Serializer&) const = 0;
163 
165  virtual void
166  serializeWithPrefix(Serializer&) const = 0;
167 
168  virtual std::string
169  getString(SHAMapNodeID const&) const;
170 
171  virtual void
172  invariants(bool is_root = false) const = 0;
173 
175  makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
176 
178  makeFromWire(Slice rawNode);
179 
180 private:
182  makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
183 
185  makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
186 
188  makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
189 };
190 
191 } // namespace ripple
192 
193 #endif
ripple::SHAMapTreeNode::cowid
std::uint32_t cowid() const
Returns the SHAMap that owns this node.
Definition: SHAMapTreeNode.h:116
std::string
STL class.
std::shared_ptr
STL class.
ripple::SHAMapNodeType::tnINNER
@ tnINNER
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:64
ripple::SHAMapNodeType
SHAMapNodeType
Definition: SHAMapTreeNode.h:46
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::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:127
ripple::wireTypeTransaction
static constexpr unsigned const char wireTypeTransaction
Definition: SHAMapTreeNode.h:40
ripple::wireTypeInner
static constexpr unsigned const char wireTypeInner
Definition: SHAMapTreeNode.h:42
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::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::SHAMapTreeNode::hash_
SHAMapHash hash_
Definition: SHAMapTreeNode.h:56
ripple::SHAMapHash
Definition: SHAMapHash.h:32
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
ripple::wireTypeTransactionWithMeta
static constexpr unsigned const char wireTypeTransactionWithMeta
Definition: SHAMapTreeNode.h:44
ripple::wireTypeAccountState
static constexpr unsigned const char wireTypeAccountState
Definition: SHAMapTreeNode.h:41
ripple::SHAMapTreeNode::makeTransaction
static std::shared_ptr< SHAMapTreeNode > makeTransaction(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapTreeNode.cpp:40
ripple::SHAMapTreeNode::makeTransactionWithMeta
static std::shared_ptr< SHAMapTreeNode > makeTransactionWithMeta(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapTreeNode.cpp:55
ripple::SHAMapNodeType::tnTRANSACTION_MD
@ tnTRANSACTION_MD
ripple::SHAMapTreeNode
Definition: SHAMapTreeNode.h:53
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:77
std::uint32_t
ripple::SHAMapTreeNode::updateHash
virtual void updateHash()=0
Recalculate the hash of this node.
memory
ripple::SHAMapTreeNode::getHash
SHAMapHash const & getHash() const
Return the hash of this node.
Definition: SHAMapTreeNode.h:143
ripple::Serializer
Definition: Serializer.h:39
ripple::wireTypeCompressedInner
static constexpr unsigned const char wireTypeCompressedInner
Definition: SHAMapTreeNode.h:43
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.
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:81
ripple::SHAMapTreeNode::makeAccountState
static std::shared_ptr< SHAMapTreeNode > makeAccountState(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapTreeNode.cpp:84
string