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 <algorithm> // std::for_each
10 #include <chrono>
11 #include <queue>
12 #include <unordered_set>
13 #include <vector>
14 
15 #include <boost/bimap.hpp>
16 #include <boost/bimap/multiset_of.hpp>
17 #include <boost/bimap/unordered_set_of.hpp>
18 #include <boost/optional/optional.hpp>
19 #include <boost/range/adaptor/map.hpp>
20 #include <boost/range/any_range.hpp>
22 #include "logger/logger_fwd.hpp"
25 
26 namespace iroha {
27 
32  class Completer {
33  public:
39  virtual bool isCompleted(const DataType &batch) const = 0;
40 
47  virtual bool isExpired(const DataType &batch,
48  const TimeType &current_time) const = 0;
49 
50  virtual ~Completer() = default;
51  };
52 
57  public:
62  bool operator()(const DataType &left_tx, const DataType &right_tx) const;
63  };
64 
70  class DefaultCompleter : public Completer {
71  public:
76  explicit DefaultCompleter(std::chrono::minutes expiration_time);
77 
78  bool isCompleted(const DataType &batch) const override;
79 
80  bool isExpired(const DataType &tx,
81  const TimeType &current_time) const override;
82 
83  private:
84  std::chrono::minutes expiration_time_;
85  };
86 
87  using CompleterType = std::shared_ptr<const Completer>;
88 
89  class MstState {
90  public:
91  // -----------------------------| public api |------------------------------
92 
99  static MstState empty(logger::LoggerPtr log,
100  const CompleterType &completer);
101 
107  StateUpdateResult operator+=(const DataType &rhs);
108 
114  StateUpdateResult operator+=(const MstState &rhs);
115 
124  MstState operator-(const MstState &rhs) const;
125 
129  bool isEmpty() const;
130 
134  std::unordered_set<DataType,
137  getBatches() const;
138 
144  MstState extractExpired(const TimeType &current_time);
145 
150  void eraseExpired(const TimeType &current_time);
151 
157  bool contains(const DataType &element) const;
158 
160  template <typename Visitor>
161  inline void iterateBatches(const Visitor &visitor) const {
162  const auto batches_range = batches_.right | boost::adaptors::map_keys;
163  std::for_each(batches_range.begin(), batches_range.end(), visitor);
164  }
165 
167  template <typename Visitor>
168  inline void iterateTransactions(const Visitor &visitor) const {
169  for (const auto &batch : batches_.right | boost::adaptors::map_keys) {
170  std::for_each(batch->transactions().begin(),
171  batch->transactions().end(),
172  visitor);
173  }
174  }
175 
176  private:
177  // --------------------------| private api |------------------------------
178 
179  using BatchesForwardCollectionType = boost::
180  any_range<BatchPtr, boost::forward_traversal_tag, const BatchPtr &>;
181 
182  using BatchesBimap = boost::bimap<
183  boost::bimaps::multiset_of<
185  boost::bimaps::unordered_set_of<DataType,
186  iroha::model::PointerBatchHasher,
188 
189  MstState(const CompleterType &completer, logger::LoggerPtr log);
190 
191  MstState(const CompleterType &completer,
192  const BatchesForwardCollectionType &batches,
193  logger::LoggerPtr log);
194 
201  void insertOne(StateUpdateResult &state_update, const DataType &rhs_tx);
202 
207  void rawInsert(const DataType &rhs_tx);
208 
214  void extractExpiredImpl(const TimeType &current_time,
215  boost::optional<MstState &> extracted);
216 
217  // -----------------------------| fields |------------------------------
218 
219  CompleterType completer_;
220 
221  BatchesBimap batches_;
222 
223  logger::LoggerPtr log_;
224  };
225 
226 } // namespace iroha
227 
228 #endif // IROHA_MST_STATE_HPP
bool contains(const CollectionType &haystack, const ElementType &needle)
Definition: yac.cpp:118
virtual ~Completer()=default
Definition: hash.hpp:16
Definition: mst_state.hpp:56
Definition: mst_state.hpp:89
std::shared_ptr< Logger > LoggerPtr
Definition: logger_fwd.hpp:22
Definition: mst_state.hpp:70
Definition: mst_state.hpp:32
virtual bool isExpired(const DataType &batch, const TimeType &current_time) const =0
std::shared_ptr< const Completer > CompleterType
Definition: mst_state.hpp:87
Definition: block_query.hpp:14
void iterateBatches(const Visitor &visitor) const
Apply visitor to all batches.
Definition: mst_state.hpp:161
shared_model::interface::types::TimestampType TimeType
Definition: mst_types.hpp:25
Definition: mst_types.hpp:47
BatchPtr DataType
Definition: mst_types.hpp:40
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:64
void iterateTransactions(const Visitor &visitor) const
Apply visitor to all transactions.
Definition: mst_state.hpp:168
virtual bool isCompleted(const DataType &batch) const =0