hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
storage_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_STORAGE_IMPL_HPP
7 #define IROHA_STORAGE_IMPL_HPP
8 
9 #include "ametsuchi/storage.hpp"
10 
11 #include <atomic>
12 #include <shared_mutex>
13 
14 #include <soci/soci.h>
15 #include <boost/optional.hpp>
16 #include <rxcpp/rx-lite.hpp>
23 #include "logger/logger_fwd.hpp"
25 
26 namespace shared_model {
27  namespace interface {
28  class QueryResponseFactory;
29  } // namespace interface
30 } // namespace shared_model
31 namespace iroha {
32 
33  class PendingTransactionStorage;
34 
35  namespace ametsuchi {
36 
37  class AmetsuchiTest;
38  class PostgresOptions;
39  class VmCaller;
40 
41  class StorageImpl : public Storage {
42  public:
43  static expected::Result<std::shared_ptr<StorageImpl>, std::string> create(
44  const PostgresOptions &postgres_options,
45  std::shared_ptr<PoolWrapper> pool_wrapper,
46  std::shared_ptr<shared_model::interface::PermissionToString>
47  perm_converter,
48  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
49  std::shared_ptr<shared_model::interface::QueryResponseFactory>
50  query_response_factory,
51  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory,
52  std::shared_ptr<BlockStorage> persistent_block_storage,
53  std::optional<std::reference_wrapper<const VmCaller>> vm_caller_ref,
54  logger::LoggerManagerTreePtr log_manager,
55  size_t pool_size = 10);
56 
58  createCommandExecutor() override;
59 
60  std::unique_ptr<TemporaryWsv> createTemporaryWsv(
61  std::shared_ptr<CommandExecutor> command_executor) override;
62 
64  createMutableStorage(
65  std::shared_ptr<CommandExecutor> command_executor) override;
66 
67  boost::optional<std::shared_ptr<PeerQuery>> createPeerQuery()
68  const override;
69 
70  boost::optional<std::shared_ptr<BlockQuery>> createBlockQuery()
71  const override;
72 
73  boost::optional<std::unique_ptr<SettingQuery>> createSettingQuery()
74  const override;
75 
77  createQueryExecutor(
78  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
79  std::shared_ptr<shared_model::interface::QueryResponseFactory>
80  response_factory) const override;
81 
83  std::shared_ptr<const shared_model::interface::Block> block) override;
84 
86  const shared_model::interface::Peer &peer) override;
87 
89  createMutableStorage(std::shared_ptr<CommandExecutor> command_executor,
90  BlockStorageFactory &storage_factory) override;
91 
92  void resetPeers() override;
93 
94  expected::Result<void, std::string> dropBlockStorage() override;
95 
96  boost::optional<std::shared_ptr<const iroha::LedgerState>>
97  getLedgerState() const override;
98 
99  void freeConnections() override;
100 
101  CommitResult commit(
102  std::unique_ptr<MutableStorage> mutable_storage) override;
103 
104  bool preparedCommitEnabled() const override;
105 
106  CommitResult commitPrepared(
107  std::shared_ptr<const shared_model::interface::Block> block) override;
108 
109  std::shared_ptr<WsvQuery> getWsvQuery() const override;
110 
111  std::shared_ptr<BlockQuery> getBlockQuery() const override;
112 
113  rxcpp::observable<std::shared_ptr<const shared_model::interface::Block>>
114  on_commit() override;
115 
116  void prepareBlock(std::unique_ptr<TemporaryWsv> wsv) override;
117 
118  ~StorageImpl() override;
119 
120  protected:
121  StorageImpl(
122  boost::optional<std::shared_ptr<const iroha::LedgerState>>
123  ledger_state,
124  const PostgresOptions &postgres_options,
125  std::shared_ptr<BlockStorage> block_store,
126  std::shared_ptr<PoolWrapper> pool_wrapper,
127  std::shared_ptr<shared_model::interface::PermissionToString>
128  perm_converter,
129  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
130  std::shared_ptr<shared_model::interface::QueryResponseFactory>
131  query_response_factory,
132  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory,
133  size_t pool_size,
134  std::optional<std::reference_wrapper<const VmCaller>> vm_caller,
135  logger::LoggerManagerTreePtr log_manager);
136 
137  private:
139 
140  friend class ::iroha::ametsuchi::AmetsuchiTest;
141 
145  StoreBlockResult storeBlock(
146  std::shared_ptr<const shared_model::interface::Block> block);
147 
151  void tryRollback(soci::session &session);
152 
153  std::shared_ptr<BlockStorage> block_store_;
154 
155  std::shared_ptr<PoolWrapper> pool_wrapper_;
156 
158  std::shared_ptr<soci::connection_pool> &connection_;
159 
160  rxcpp::composite_subscription notifier_lifetime_;
161  rxcpp::subjects::subject<
162  std::shared_ptr<const shared_model::interface::Block>>
163  notifier_;
164 
165  std::shared_ptr<shared_model::interface::PermissionToString>
166  perm_converter_;
167 
168  std::shared_ptr<PendingTransactionStorage> pending_txs_storage_;
169 
170  std::shared_ptr<shared_model::interface::QueryResponseFactory>
171  query_response_factory_;
172 
173  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory_;
174 
175  std::optional<std::reference_wrapper<const VmCaller>> vm_caller_ref_;
176 
177  logger::LoggerManagerTreePtr log_manager_;
178  logger::LoggerPtr log_;
179 
180  mutable std::shared_timed_mutex drop_mutex_;
181 
182  const size_t pool_size_;
183 
184  bool prepared_blocks_enabled_;
185 
186  std::atomic<bool> block_is_prepared_;
187 
188  std::string prepared_block_name_;
189 
190  boost::optional<std::shared_ptr<const iroha::LedgerState>> ledger_state_;
191  };
192  } // namespace ametsuchi
193 } // namespace iroha
194 
195 #endif // IROHA_STORAGE_IMPL_HPP
Definition: postgres_options.hpp:19
Definition: peer.hpp:20
Definition: result_fwd.hpp:27
std::shared_ptr< Logger > LoggerPtr
Definition: logger_fwd.hpp:22
Definition: storage.hpp:38
Definition: block_query.hpp:15
std::shared_ptr< LoggerManagerTree > LoggerManagerTreePtr
Definition: logger_manager_fwd.hpp:14
Definition: block_storage_factory.hpp:19
Definition: command_executor.hpp:12
Definition: storage_impl.hpp:41