rippled
SHAMapInnerNode.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_SHAMAPINNERNODE_H_INCLUDED
21 #define RIPPLE_SHAMAP_SHAMAPINNERNODE_H_INCLUDED
22 
23 #include <ripple/basics/TaggedCache.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <ripple/shamap/SHAMapItem.h>
26 #include <ripple/shamap/SHAMapNodeID.h>
27 #include <ripple/shamap/SHAMapTreeNode.h>
28 
29 #include <bitset>
30 #include <cstdint>
31 #include <memory>
32 #include <mutex>
33 #include <optional>
34 #include <string>
35 
36 namespace ripple {
37 
38 class SHAMapInnerNode final : public SHAMapTreeNode,
39  public CountedObject<SHAMapInnerNode>
40 {
43  int mIsBranch = 0;
45 
47 
48 public:
50 
52  clone(std::uint32_t cowid) const override;
53 
55  getType() const override
56  {
58  }
59 
60  bool
61  isLeaf() const override
62  {
63  return false;
64  }
65 
66  bool
67  isInner() const override
68  {
69  return true;
70  }
71 
72  bool
73  isEmpty() const;
74  bool
75  isEmptyBranch(int m) const;
76  int
77  getBranchCount() const;
78  SHAMapHash const&
79  getChildHash(int m) const;
80 
81  void
82  setChild(int m, std::shared_ptr<SHAMapTreeNode> const& child);
83  void
84  shareChild(int m, std::shared_ptr<SHAMapTreeNode> const& child);
86  getChildPointer(int branch);
88  getChild(int branch);
91 
92  // sync functions
93  bool
94  isFullBelow(std::uint32_t generation) const;
95  void
97 
98  void
99  updateHash() override;
100 
102  void
103  updateHashDeep();
104 
105  void
106  serializeForWire(Serializer&) const override;
107 
108  void
109  serializeWithPrefix(Serializer&) const override;
110 
112  getString(SHAMapNodeID const&) const override;
113 
114  void
115  invariants(bool is_root = false) const override;
116 
118  makeFullInner(Slice data, SHAMapHash const& hash, bool hashValid);
119 
122 };
123 
125  : SHAMapTreeNode(cowid)
126 {
127 }
128 
129 inline bool
131 {
132  return (mIsBranch & (1 << m)) == 0;
133 }
134 
135 inline SHAMapHash const&
137 {
138  assert(m >= 0 && m < 16);
139  return mHashes[m];
140 }
141 
142 inline bool
144 {
145  return mFullBelowGen == generation;
146 }
147 
148 inline void
150 {
151  mFullBelowGen = gen;
152 }
153 
154 } // namespace ripple
155 #endif
ripple::SHAMapTreeNode::cowid
std::uint32_t cowid() const
Returns the SHAMap that owns this node.
Definition: SHAMapTreeNode.h:196
ripple::SHAMapInnerNode::serializeWithPrefix
void serializeWithPrefix(Serializer &) const override
Serialize the node in a format appropriate for hashing.
Definition: SHAMapInnerNode.cpp:174
ripple::SHAMapInnerNode::isInner
bool isInner() const override
Determines if this is an inner node.
Definition: SHAMapInnerNode.h:67
bitset
ripple::SHAMapInnerNode::setChild
void setChild(int m, std::shared_ptr< SHAMapTreeNode > const &child)
Definition: SHAMapInnerNode.cpp:220
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::SHAMapInnerNode::clone
std::shared_ptr< SHAMapTreeNode > clone(std::uint32_t cowid) const override
Make a copy of this node, setting the owner.
Definition: SHAMapInnerNode.cpp:46
std::string
STL class.
std::shared_ptr
STL class.
ripple::SHAMapNodeType::tnINNER
@ tnINNER
ripple::SHAMapInnerNode::mHashes
std::array< SHAMapHash, 16 > mHashes
Definition: SHAMapInnerNode.h:41
ripple::SHAMapInnerNode::getChild
std::shared_ptr< SHAMapTreeNode > getChild(int branch)
Definition: SHAMapInnerNode.cpp:256
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::SHAMapInnerNode::makeFullInner
static std::shared_ptr< SHAMapTreeNode > makeFullInner(Slice data, SHAMapHash const &hash, bool hashValid)
Definition: SHAMapInnerNode.cpp:60
ripple::SHAMapInnerNode::canonicalizeChild
virtual std::shared_ptr< SHAMapTreeNode > canonicalizeChild(int branch, std::shared_ptr< SHAMapTreeNode > node)
Definition: SHAMapInnerNode.cpp:265
ripple::SHAMapInnerNode::mFullBelowGen
std::uint32_t mFullBelowGen
Definition: SHAMapInnerNode.h:44
ripple::SHAMapNodeType
SHAMapNodeType
Definition: SHAMapTreeNode.h:126
ripple::SHAMapInnerNode::updateHash
void updateHash() override
Recalculate the hash of this node.
Definition: SHAMapInnerNode.cpp:118
ripple::SHAMapInnerNode::shareChild
void shareChild(int m, std::shared_ptr< SHAMapTreeNode > const &child)
Definition: SHAMapInnerNode.cpp:236
ripple::SHAMapNodeID
Identifies a node inside a SHAMap.
Definition: SHAMapNodeID.h:33
ripple::SHAMapHash
Definition: SHAMapTreeNode.h:47
ripple::SHAMapInnerNode::SHAMapInnerNode
SHAMapInnerNode(std::uint32_t cowid)
Definition: SHAMapInnerNode.h:124
ripple::SHAMapInnerNode::isEmptyBranch
bool isEmptyBranch(int m) const
Definition: SHAMapInnerNode.h:130
ripple::SHAMapInnerNode::getString
std::string getString(SHAMapNodeID const &) const override
Definition: SHAMapInnerNode.cpp:202
ripple::SHAMapInnerNode::getChildHash
SHAMapHash const & getChildHash(int m) const
Definition: SHAMapInnerNode.h:136
ripple::SHAMapInnerNode
Definition: SHAMapInnerNode.h:38
ripple::SHAMapInnerNode::isFullBelow
bool isFullBelow(std::uint32_t generation) const
Definition: SHAMapInnerNode.h:143
ripple::SHAMapTreeNode
Definition: SHAMapTreeNode.h:133
std::array
STL class.
ripple::SHAMapInnerNode::updateHashDeep
void updateHashDeep()
Recalculate the hash of all children and this node.
Definition: SHAMapInnerNode.cpp:134
cstdint
ripple::SHAMapInnerNode::getBranchCount
int getBranchCount() const
Definition: SHAMapInnerNode.cpp:190
std::uint32_t
memory
ripple::SHAMapInnerNode::getType
SHAMapNodeType getType() const override
Determines the type of node.
Definition: SHAMapInnerNode.h:55
ripple::Serializer
Definition: Serializer.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SHAMapInnerNode::serializeForWire
void serializeForWire(Serializer &) const override
Serialize the node in a format appropriate for sending over the wire.
Definition: SHAMapInnerNode.cpp:145
ripple::SHAMapInnerNode::mIsBranch
int mIsBranch
Definition: SHAMapInnerNode.h:43
ripple::SHAMapInnerNode::childLock
static std::mutex childLock
Definition: SHAMapInnerNode.h:46
optional
mutex
ripple::SHAMapInnerNode::isLeaf
bool isLeaf() const override
Determines if this is a leaf node.
Definition: SHAMapInnerNode.h:61
ripple::SHAMapInnerNode::isEmpty
bool isEmpty() const
Definition: SHAMapInnerNode.cpp:184
ripple::SHAMapInnerNode::makeCompressedInner
static std::shared_ptr< SHAMapTreeNode > makeCompressedInner(Slice data)
Definition: SHAMapInnerNode.cpp:88
ripple::SHAMapInnerNode::getChildPointer
SHAMapTreeNode * getChildPointer(int branch)
Definition: SHAMapInnerNode.cpp:247
ripple::SHAMapInnerNode::setFullBelowGen
void setFullBelowGen(std::uint32_t gen)
Definition: SHAMapInnerNode.h:149
ripple::SHAMapInnerNode::invariants
void invariants(bool is_root=false) const override
Definition: SHAMapInnerNode.cpp:288
ripple::SHAMapInnerNode::mChildren
std::shared_ptr< SHAMapTreeNode > mChildren[16]
Definition: SHAMapInnerNode.h:42
string