CatapultServer  v0.5.0.1 (Elephant)
TreeNode.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "TreeNodePath.h"
23 #include "catapult/types.h"
24 #include <bitset>
25 #include <memory>
26 
27 namespace catapult { namespace tree { class TreeNode; } }
28 
29 namespace catapult { namespace tree {
30 
31  // region LeafTreeNode
32 
34  class LeafTreeNode {
35  public:
37  LeafTreeNode(const TreeNodePath& path, const Hash256& value);
38 
39  public:
41  const TreeNodePath& path() const;
42 
44  const Hash256& value() const;
45 
47  const Hash256& hash() const;
48 
49  private:
53  };
54 
55  // endregion
56 
57  // region BranchTreeNode
58 
61  public:
63  static constexpr size_t Max_Links = 16;
64 
65  public:
67  explicit BranchTreeNode(const TreeNodePath& path);
68 
69  public:
71  const TreeNodePath& path() const;
72 
74  size_t numLinks() const;
75 
77  bool hasLink(size_t index) const;
78 
80  const Hash256& link(size_t index) const;
81 
83  std::unique_ptr<const TreeNode> linkedNode(size_t index) const;
84 
86  uint8_t highestLinkIndex() const;
87 
89  const Hash256& hash() const;
90 
91  public:
93  void setPath(const TreeNodePath& path);
94 
96  void setLink(const Hash256& link, size_t index);
97 
99  void setLink(const TreeNode& node, size_t index);
100 
102  void clearLink(size_t index);
103 
105  void compactLinks();
106 
107  private:
108  void setLink(size_t index);
109 
110  private:
112  std::array<Hash256, BranchTreeNode::Max_Links> m_links;
113  std::array<std::shared_ptr<const TreeNode>, BranchTreeNode::Max_Links> m_linkedNodes; // shared_ptr to allow copying
114  std::bitset<BranchTreeNode::Max_Links> m_linkSet;
115  mutable Hash256 m_hash;
116  mutable bool m_isDirty;
117  };
118 
119  // endregion
120 
121  // region TreeNode
122 
124  class TreeNode {
125  public:
127  TreeNode();
128 
130  explicit TreeNode(const LeafTreeNode& node);
131 
133  explicit TreeNode(const BranchTreeNode& node);
134 
135  public:
137  bool empty() const;
138 
140  bool isBranch() const;
141 
143  bool isLeaf() const;
144 
145  public:
147  const TreeNodePath& path() const;
148 
150  const Hash256& hash() const;
151 
152  public:
154  void setPath(const TreeNodePath& path);
155 
156  public:
158  const LeafTreeNode& asLeafNode() const;
159 
161  const BranchTreeNode& asBranchNode() const;
162 
163  public:
165  TreeNode copy() const;
166 
167  private:
170  std::unique_ptr<LeafTreeNode> m_pLeafNode;
171  std::unique_ptr<BranchTreeNode> m_pBranchNode;
172  };
173 
174  // endregion
175 }}
catapult::tree::TreeNode::copy
TreeNode copy() const
Creates a copy of this node.
Definition: TreeNode.cpp:234
catapult::tree::BranchTreeNode::path
const TreeNodePath & path() const
Gets the node path.
Definition: TreeNode.cpp:90
exceptions.h
catapult::Hash256
utils::ByteArray< Hash256_Size, Hash256_tag > Hash256
Definition: src/catapult/types.h:47
catapult::tree::TreeNode::TreeNode
TreeNode()
Creates an empty tree node.
Definition: TreeNode.cpp:172
catapult::tree::BranchTreeNode::m_linkedNodes
std::array< std::shared_ptr< const TreeNode >, BranchTreeNode::Max_Links > m_linkedNodes
Definition: TreeNode.h:113
catapult::tree::BranchTreeNode::m_path
TreeNodePath m_path
Definition: TreeNode.h:111
catapult::tree::LeafTreeNode::m_hash
Hash256 m_hash
Definition: TreeNode.h:52
catapult::tree::LeafTreeNode::LeafTreeNode
LeafTreeNode(const TreeNodePath &path, const Hash256 &value)
Creates a leaf node with path and value.
Definition: TreeNode.cpp:62
catapult::tree::LeafTreeNode::m_value
Hash256 m_value
Definition: TreeNode.h:51
catapult::crypto::KeccakBuilder
Builder for building a hash.
Definition: Hashes.h:61
catapult::tree::TreeNode::hash
const Hash256 & hash() const
Gets the hash representation of this node.
Definition: TreeNode.cpp:202
catapult::tree::TreeNode::empty
bool empty() const
Returns true if this node represents an empty node.
Definition: TreeNode.cpp:181
TreeNode.h
catapult::crypto::Sha3_256_Builder
KeccakBuilder< Sha3ModeTag, Hash256_tag > Sha3_256_Builder
Sha3_256_Builder.
Definition: Hashes.h:85
catapult::tree::TreeNode::m_emptyPath
TreeNodePath m_emptyPath
Definition: TreeNode.h:168
catapult::tree::LeafTreeNode::value
const Hash256 & value() const
Gets the node value.
Definition: TreeNode.cpp:72
IntegerMath.h
catapult::tree::BranchTreeNode::m_isDirty
bool m_isDirty
Definition: TreeNode.h:116
catapult::tree::LeafTreeNode::path
const TreeNodePath & path() const
Gets the node path.
Definition: TreeNode.cpp:68
catapult::tree::LeafTreeNode::m_path
TreeNodePath m_path
Definition: TreeNode.h:50
catapult::tree::BranchTreeNode::compactLinks
void compactLinks()
Compacts all links by replacing node links with hash links.
Definition: TreeNode.cpp:152
catapult::tree::TreeNode::isLeaf
bool isLeaf() const
Returns true if this node represents a leaf node.
Definition: TreeNode.cpp:189
catapult::tree::BranchTreeNode::Max_Links
static constexpr size_t Max_Links
Maximum number of branch links.
Definition: TreeNode.h:63
catapult::crypto::KeccakBuilder::final
void final(OutputType &output) noexcept
Finalize hash calculation. Returns result in output.
Definition: Hashes.cpp:145
Hashes.h
catapult::tree::TreeNode::setPath
void setPath(const TreeNodePath &path)
Sets the node path.
Definition: TreeNode.cpp:211
catapult::tree::BranchTreeNode::link
const Hash256 & link(size_t index) const
Gets the branch link at index.
Definition: TreeNode.cpp:102
catapult::tree::BranchTreeNode::m_linkSet
std::bitset< BranchTreeNode::Max_Links > m_linkSet
Definition: TreeNode.h:114
catapult::tree::TreeNode::asBranchNode
const BranchTreeNode & asBranchNode() const
Gets a branch node interface to this node.
Definition: TreeNode.cpp:227
catapult::tree::BranchTreeNode::m_links
std::array< Hash256, BranchTreeNode::Max_Links > m_links
Definition: TreeNode.h:112
catapult::tree::BranchTreeNode::setPath
void setPath(const TreeNodePath &path)
Sets the branch node path.
Definition: TreeNode.cpp:130
catapult::tree::BranchTreeNode::hash
const Hash256 & hash() const
Gets the hash representation of this node.
Definition: TreeNode.cpp:116
catapult::crypto::KeccakBuilder::update
void update(const RawBuffer &dataBuffer) noexcept
Updates the state of hash with data inside dataBuffer.
Definition: Hashes.cpp:134
m_path
std::vector< uint8_t > m_path
Definition: TreeNodePath.cpp:112
catapult::tree::BranchTreeNode::hasLink
bool hasLink(size_t index) const
Returns true if this branch has a link at index.
Definition: TreeNode.cpp:98
catapult::tree::LeafTreeNode
Represents a leaf tree node.
Definition: TreeNode.h:34
catapult::tree::TreeNode::path
const TreeNodePath & path() const
Gets the node path.
Definition: TreeNode.cpp:193
catapult::tree::BranchTreeNode::highestLinkIndex
uint8_t highestLinkIndex() const
Gets the index of the highest set link.
Definition: TreeNode.cpp:112
CATAPULT_THROW_RUNTIME_ERROR
#define CATAPULT_THROW_RUNTIME_ERROR(MESSAGE)
Macro used to throw a catapult runtime error.
Definition: exceptions.h:167
catapult::tree::BranchTreeNode::setLink
void setLink(const Hash256 &link, size_t index)
Sets the branch link at index.
Definition: TreeNode.cpp:135
types.h
catapult::tree::TreeNode::m_pLeafNode
std::unique_ptr< LeafTreeNode > m_pLeafNode
Definition: TreeNode.h:170
catapult::tree::TreeNode::asLeafNode
const LeafTreeNode & asLeafNode() const
Gets a leaf node interface to this node.
Definition: TreeNode.cpp:220
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::Log2
constexpr T Log2(T value)
Calculates log2(value).
Definition: IntegerMath.h:46
TreeNodePath.h
catapult::tree::TreeNode
Represents a tree node.
Definition: TreeNode.h:124
catapult::utils::ByteArray< Hash256_Size, Hash256_tag >
catapult::tree::TreeNode::isBranch
bool isBranch() const
Returns true if this node represents a branch node.
Definition: TreeNode.cpp:185
catapult::tree::TreeNode::m_emptyHash
Hash256 m_emptyHash
Definition: TreeNode.h:169
catapult::tree::BranchTreeNode::m_hash
Hash256 m_hash
Definition: TreeNode.h:115
catapult::tree::TreeNodePath
Represents a path in a tree.
Definition: TreeNodePath.h:31
catapult::tree::BranchTreeNode::BranchTreeNode
BranchTreeNode(const TreeNodePath &path)
Creates a branch node with path.
Definition: TreeNode.cpp:84
catapult::tree::TreeNode::m_pBranchNode
std::unique_ptr< BranchTreeNode > m_pBranchNode
Definition: TreeNode.h:171
catapult::tree::BranchTreeNode::clearLink
void clearLink(size_t index)
Clears the branch link at index.
Definition: TreeNode.cpp:147
catapult::tree::BranchTreeNode::linkedNode
std::unique_ptr< const TreeNode > linkedNode(size_t index) const
Gets a copy of the linked node at index or nullptr if no linked node is present.
Definition: TreeNode.cpp:107
catapult::tree::BranchTreeNode
Represents a branch tree node.
Definition: TreeNode.h:60
catapult::tree::LeafTreeNode::hash
const Hash256 & hash() const
Gets the hash representation of this node.
Definition: TreeNode.cpp:76
catapult::tree::BranchTreeNode::numLinks
size_t numLinks() const
Gets the number of links set in this node.
Definition: TreeNode.cpp:94