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 
63  std::unique_ptr<MutableStorage> createMutableStorage(
64  std::shared_ptr<CommandExecutor> command_executor) override;
65 
66  boost::optional<std::shared_ptr<PeerQuery>> createPeerQuery()
67  const override;
68 
69  boost::optional<std::shared_ptr<BlockQuery>> createBlockQuery()
70  const override;
71 
72  boost::optional<std::unique_ptr<SettingQuery>> createSettingQuery()
73  const override;
74 
76  createQueryExecutor(
77  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
78  std::shared_ptr<shared_model::interface::QueryResponseFactory>
79  response_factory) const override;
80 
81  bool insertBlock(
82  std::shared_ptr<const shared_model::interface::Block> block) override;
83 
85  const shared_model::interface::Peer &peer) override;
86 
87  std::unique_ptr<MutableStorage> createMutableStorage(
88  std::shared_ptr<CommandExecutor> command_executor,
89  BlockStorageFactory &storage_factory) override;
90 
91  void resetPeers() override;
92 
93  expected::Result<void, std::string> dropBlockStorage() override;
94 
95  boost::optional<std::shared_ptr<const iroha::LedgerState>>
96  getLedgerState() const override;
97 
98  void freeConnections() override;
99 
100  CommitResult commit(
101  std::unique_ptr<MutableStorage> mutable_storage) override;
102 
103  bool preparedCommitEnabled() const override;
104 
105  CommitResult commitPrepared(
106  std::shared_ptr<const shared_model::interface::Block> block) override;
107 
108  std::shared_ptr<WsvQuery> getWsvQuery() const override;
109 
110  std::shared_ptr<BlockQuery> getBlockQuery() const override;
111 
112  rxcpp::observable<std::shared_ptr<const shared_model::interface::Block>>
113  on_commit() override;
114 
115  void prepareBlock(std::unique_ptr<TemporaryWsv> wsv) override;
116 
117  ~StorageImpl() override;
118 
119  protected:
120  StorageImpl(
121  boost::optional<std::shared_ptr<const iroha::LedgerState>>
122  ledger_state,
123  const PostgresOptions &postgres_options,
124  std::shared_ptr<BlockStorage> block_store,
125  std::shared_ptr<PoolWrapper> pool_wrapper,
126  std::shared_ptr<shared_model::interface::PermissionToString>
127  perm_converter,
128  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
129  std::shared_ptr<shared_model::interface::QueryResponseFactory>
130  query_response_factory,
131  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory,
132  size_t pool_size,
133  std::optional<std::reference_wrapper<const VmCaller>> vm_caller,
134  logger::LoggerManagerTreePtr log_manager);
135 
136  private:
138 
139  friend class ::iroha::ametsuchi::AmetsuchiTest;
140 
144  StoreBlockResult storeBlock(
145  std::shared_ptr<const shared_model::interface::Block> block);
146 
150  void tryRollback(soci::session &session);
151 
152  std::shared_ptr<BlockStorage> block_store_;
153 
154  std::shared_ptr<PoolWrapper> pool_wrapper_;
155 
157  std::shared_ptr<soci::connection_pool> &connection_;
158 
159  rxcpp::composite_subscription notifier_lifetime_;
160  rxcpp::subjects::subject<
161  std::shared_ptr<const shared_model::interface::Block>>
162  notifier_;
163 
164  std::shared_ptr<shared_model::interface::PermissionToString>
165  perm_converter_;
166 
167  std::shared_ptr<PendingTransactionStorage> pending_txs_storage_;
168 
169  std::shared_ptr<shared_model::interface::QueryResponseFactory>
170  query_response_factory_;
171 
172  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory_;
173 
174  std::optional<std::reference_wrapper<const VmCaller>> vm_caller_ref_;
175 
176  logger::LoggerManagerTreePtr log_manager_;
177  logger::LoggerPtr log_;
178 
179  mutable std::shared_timed_mutex drop_mutex_;
180 
181  const size_t pool_size_;
182 
183  bool prepared_blocks_enabled_;
184 
185  std::atomic<bool> block_is_prepared_;
186 
187  std::string prepared_block_name_;
188 
189  boost::optional<std::shared_ptr<const iroha::LedgerState>> ledger_state_;
190  };
191  } // namespace ametsuchi
192 } // namespace iroha
193 
194 #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:18
Definition: command_executor.hpp:12
Definition: storage_impl.hpp:41