CatapultServer  v0.5.0.1 (Elephant)
PatriciaTreeCacheMixins.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "PatriciaTreeUtils.h"
24 #include "catapult/exceptions.h"
25 
26 namespace catapult { namespace cache {
27 
29  template<typename TTree>
31  public:
33  explicit PatriciaTreeMixin(const TTree* pTree) : m_pTree(pTree)
34  {}
35 
36  public:
38  bool supportsMerkleRoot() const {
39  return !!m_pTree;
40  }
41 
43  std::pair<Hash256, bool> tryGetMerkleRoot() const {
44  return m_pTree
45  ? std::make_pair(m_pTree->root(), true)
46  : std::make_pair(Hash256(), false);
47  }
48 
50  std::pair<Hash256, bool> tryLookup(const typename TTree::KeyType& key, std::vector<tree::TreeNode>& nodePath) const {
51  return m_pTree
52  ? m_pTree->lookup(key, nodePath)
53  : std::make_pair(Hash256(), false);
54  }
55 
56  private:
57  const TTree* m_pTree;
58  };
59 
61  template<typename TSet, typename TTree>
63  public:
65  PatriciaTreeDeltaMixin(TSet& set, const std::shared_ptr<TTree>& pTree)
66  : m_set(set)
67  , m_pTree(pTree)
68  , m_nextGenerationId(m_set.generationId())
69  {}
70 
71  public:
73  bool supportsMerkleRoot() const {
74  return !!m_pTree;
75  }
76 
78  std::pair<Hash256, bool> tryGetMerkleRoot() const {
79  return m_pTree
80  ? std::make_pair(m_pTree->root(), true)
81  : std::make_pair(Hash256(), false);
82  }
83 
85  void updateMerkleRoot(Height height) {
86  if (!m_pTree)
87  return;
88 
91  }
92 
95  void setMerkleRoot(const Hash256& merkleRoot) {
96  if (!m_pTree)
97  CATAPULT_THROW_RUNTIME_ERROR_1("cannot set merkle root", merkleRoot);
98 
99  m_pTree->reset(merkleRoot);
101  }
102 
103  private:
105  m_set.incrementGenerationId();
106  m_nextGenerationId = m_set.generationId();
107  m_pTree->setCheckpoint();
108  }
109 
110  private:
111  TSet& m_set;
112  std::shared_ptr<TTree> m_pTree;
114  };
115 }}
catapult::cache::PatriciaTreeMixin::PatriciaTreeMixin
PatriciaTreeMixin(const TTree *pTree)
Creates a mixin around pTree.
Definition: PatriciaTreeCacheMixins.h:33
exceptions.h
catapult::Hash256
utils::ByteArray< Hash256_Size, Hash256_tag > Hash256
Definition: src/catapult/types.h:47
catapult::cache::PatriciaTreeMixin::supportsMerkleRoot
bool supportsMerkleRoot() const
Returns true if merkle root is supported.
Definition: PatriciaTreeCacheMixins.h:38
catapult::cache::PatriciaTreeMixin
A mixin for adding patricia tree support to a cache.
Definition: PatriciaTreeCacheMixins.h:30
catapult::utils::BaseValue< uint64_t, Height_tag >
CATAPULT_THROW_RUNTIME_ERROR_1
#define CATAPULT_THROW_RUNTIME_ERROR_1(MESSAGE, PARAM1)
Macro used to throw a catapult runtime error with a single parameter.
Definition: exceptions.h:171
catapult::cache::PatriciaTreeDeltaMixin::PatriciaTreeDeltaMixin
PatriciaTreeDeltaMixin(TSet &set, const std::shared_ptr< TTree > &pTree)
Creates a mixin around delta set and pTree.
Definition: PatriciaTreeCacheMixins.h:65
catapult::cache::PatriciaTreeDeltaMixin::supportsMerkleRoot
bool supportsMerkleRoot() const
Returns true if merkle root is supported.
Definition: PatriciaTreeCacheMixins.h:73
HexFormatter.h
catapult::cache::PatriciaTreeMixin::tryLookup
std::pair< Hash256, bool > tryLookup(const typename TTree::KeyType &key, std::vector< tree::TreeNode > &nodePath) const
Tries to find the value associated with key in the tree and stores proof of existence or not in nodeP...
Definition: PatriciaTreeCacheMixins.h:50
catapult::cache::PatriciaTreeDeltaMixin::m_pTree
std::shared_ptr< TTree > m_pTree
Definition: PatriciaTreeCacheMixins.h:112
catapult::cache::PatriciaTreeDeltaMixin::m_nextGenerationId
uint32_t m_nextGenerationId
Definition: PatriciaTreeCacheMixins.h:113
catapult::cache::PatriciaTreeDeltaMixin
A mixin for adding patricia tree support to a delta cache.
Definition: PatriciaTreeCacheMixins.h:62
catapult::cache::PatriciaTreeDeltaMixin::updateMerkleRoot
void updateMerkleRoot(Height height)
Recalculates the merkle root given the specified chain height if supported.
Definition: PatriciaTreeCacheMixins.h:85
catapult
Definition: AddressExtractionExtension.cpp:28
PatriciaTreeUtils.h
catapult::cache::PatriciaTreeDeltaMixin::m_set
TSet & m_set
Definition: PatriciaTreeCacheMixins.h:111
catapult::cache::PatriciaTreeDeltaMixin::setApplyCheckpoint
void setApplyCheckpoint()
Definition: PatriciaTreeCacheMixins.h:104
catapult::cache::PatriciaTreeMixin::tryGetMerkleRoot
std::pair< Hash256, bool > tryGetMerkleRoot() const
Tries to get the merkle root if supported.
Definition: PatriciaTreeCacheMixins.h:43
catapult::utils::ByteArray< Hash256_Size, Hash256_tag >
catapult::cache::ApplyDeltasToTree
void ApplyDeltasToTree(TTree &tree, const TSet &set, uint32_t minGenerationId, Height height)
Definition: PatriciaTreeUtils.h:71
catapult::cache::PatriciaTreeDeltaMixin::setMerkleRoot
void setMerkleRoot(const Hash256 &merkleRoot)
Definition: PatriciaTreeCacheMixins.h:95
catapult::cache::PatriciaTreeMixin::m_pTree
const TTree * m_pTree
Definition: PatriciaTreeCacheMixins.h:57
catapult::cache::PatriciaTreeDeltaMixin::tryGetMerkleRoot
std::pair< Hash256, bool > tryGetMerkleRoot() const
Tries to get the merkle root if supported.
Definition: PatriciaTreeCacheMixins.h:78