hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
mst_state.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_MST_STATE_HPP
7 #define IROHA_MST_STATE_HPP
8 
9 #include <chrono>
10 #include <unordered_set>
11 
12 #include <boost/bimap.hpp>
13 #include <boost/bimap/multiset_of.hpp>
14 #include <boost/bimap/unordered_multiset_of.hpp>
15 #include <boost/bimap/unordered_set_of.hpp>
16 #include <boost/optional/optional.hpp>
17 #include <boost/range/adaptor/map.hpp>
18 #include <boost/range/any_range.hpp>
19 #include "cryptography/hash.hpp"
21 #include "logger/logger_fwd.hpp"
24 
25 namespace iroha {
26 
31  class Completer {
32  public:
38  virtual bool isCompleted(const DataType &batch) const = 0;
39 
46  virtual bool isExpired(const DataType &batch,
47  const TimeType &current_time) const = 0;
48 
49  virtual ~Completer() = default;
50  };
51 
57  class DefaultCompleter : public Completer {
58  public:
63  explicit DefaultCompleter(std::chrono::minutes expiration_time);
64 
65  bool isCompleted(const DataType &batch) const override;
66 
67  bool isExpired(const DataType &tx,
68  const TimeType &current_time) const override;
69 
70  private:
71  std::chrono::minutes expiration_time_;
72  };
73 
74  using CompleterType = std::shared_ptr<const Completer>;
75 
76  class MstState {
77  public:
78  // -----------------------------| public api |------------------------------
79 
86  static MstState empty(logger::LoggerPtr log,
87  const CompleterType &completer);
88 
94  StateUpdateResult operator+=(const DataType &rhs);
95 
101  StateUpdateResult operator+=(const MstState &rhs);
102 
111  MstState operator-(const MstState &rhs) const;
112 
116  bool isEmpty() const;
117 
121  std::unordered_set<DataType,
124  getBatches() const;
125 
131  MstState extractExpired(const TimeType &current_time);
132 
137  void eraseExpired(const TimeType &current_time);
138 
142  void eraseByTransactionHash(
144 
150  bool contains(const DataType &element) const;
151 
153  template <typename Visitor>
154  inline void iterateBatches(const Visitor &visitor) const {
155  const auto batches_range = batches_.right | boost::adaptors::map_keys;
156  std::for_each(batches_range.begin(), batches_range.end(), visitor);
157  }
158 
160  template <typename Visitor>
161  inline void iterateTransactions(const Visitor &visitor) const {
162  for (const auto &batch : batches_.right | boost::adaptors::map_keys) {
163  std::for_each(batch->transactions().begin(),
164  batch->transactions().end(),
165  visitor);
166  }
167  }
168 
169  private:
170  // --------------------------| private api |------------------------------
171 
172  using BatchesForwardCollectionType = boost::
173  any_range<BatchPtr, boost::forward_traversal_tag, const BatchPtr &>;
174 
175  using BatchesToHashBimap =
176  boost::bimap<boost::bimaps::unordered_set_of<
179  boost::bimaps::unordered_multiset_of<
180  DataType,
181  iroha::model::PointerBatchHasher,
183 
184  using BatchesBimap =
185  boost::bimap<boost::bimaps::multiset_of<
187  boost::bimaps::unordered_set_of<
188  DataType,
189  iroha::model::PointerBatchHasher,
190  shared_model::interface::BatchHashEquality>>;
191 
192  MstState(CompleterType const &completer, logger::LoggerPtr log);
193 
194  MstState(CompleterType const &completer,
195  BatchesForwardCollectionType const &batches,
196  logger::LoggerPtr log);
197 
204  void insertOne(StateUpdateResult &state_update, const DataType &rhs_tx);
205 
210  void rawInsert(const DataType &rhs_tx);
211 
217  void extractExpiredImpl(const TimeType &current_time,
218  boost::optional<MstState &> extracted);
219 
220  // -----------------------------| fields |------------------------------
221 
222  CompleterType completer_;
223 
224  BatchesBimap batches_;
225  BatchesToHashBimap batches_to_hash_;
226 
227  logger::LoggerPtr log_;
228  };
229 
230 } // namespace iroha
231 
232 #endif // IROHA_MST_STATE_HPP
Definition: hash.hpp:18
bool contains(const CollectionType &haystack, const ElementType &needle)
Definition: yac.cpp:112
crypto::Hash HashType
Type of hash.
Definition: types.hpp:34
virtual ~Completer()=default
Definition: hash.hpp:16
Definition: transaction_batch.hpp:64
Definition: mst_state.hpp:76
std::shared_ptr< Logger > LoggerPtr
Definition: logger_fwd.hpp:22
Definition: mst_state.hpp:57
Definition: mst_state.hpp:31
void iterateBatches(const Visitor &visitor) const
Apply visitor to all batches.
Definition: mst_state.hpp:154
virtual bool isExpired(const DataType &batch, const TimeType &current_time) const =0
std::shared_ptr< const Completer > CompleterType
Definition: mst_state.hpp:74
Definition: block_query.hpp:15
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
shared_model::interface::types::TimestampType TimeType
Definition: mst_types.hpp:25
void iterateTransactions(const Visitor &visitor) const
Apply visitor to all transactions.
Definition: mst_state.hpp:161
Definition: mst_types.hpp:48
BatchPtr DataType
Definition: mst_types.hpp:41
Definition: hash.hpp:23
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:67
virtual bool isCompleted(const DataType &batch) const =0