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 "common/result.hpp"
25 #include "logger/logger_fwd.hpp"
27 
28 namespace shared_model {
29  namespace interface {
30  class QueryResponseFactory;
31  } // namespace interface
32 } // namespace shared_model
33 
34 namespace iroha {
35 
36  class PendingTransactionStorage;
37 
38  namespace ametsuchi {
39  class StorageImpl : public Storage {
40  public:
41  static expected::Result<std::shared_ptr<StorageImpl>, std::string> create(
42  std::unique_ptr<ametsuchi::PostgresOptions> postgres_options,
43  std::shared_ptr<PoolWrapper> pool_wrapper,
44  std::shared_ptr<shared_model::interface::PermissionToString>
45  perm_converter,
46  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
47  std::shared_ptr<shared_model::interface::QueryResponseFactory>
48  query_response_factory,
49  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory,
50  std::unique_ptr<BlockStorage> persistent_block_storage,
51  logger::LoggerManagerTreePtr log_manager,
52  size_t pool_size = 10);
53 
54  expected::Result<std::unique_ptr<CommandExecutor>, std::string>
55  createCommandExecutor() override;
56 
57  std::unique_ptr<TemporaryWsv> createTemporaryWsv(
58  std::shared_ptr<CommandExecutor> command_executor) override;
59 
60  std::unique_ptr<MutableStorage> createMutableStorage(
61  std::shared_ptr<CommandExecutor> command_executor) override;
62 
63  boost::optional<std::shared_ptr<PeerQuery>> createPeerQuery()
64  const override;
65 
66  boost::optional<std::shared_ptr<BlockQuery>> createBlockQuery()
67  const override;
68 
69  boost::optional<std::unique_ptr<SettingQuery>> createSettingQuery()
70  const override;
71 
72  boost::optional<std::shared_ptr<QueryExecutor>> createQueryExecutor(
73  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
74  std::shared_ptr<shared_model::interface::QueryResponseFactory>
75  response_factory) const override;
76 
77  bool insertBlock(
78  std::shared_ptr<const shared_model::interface::Block> block) override;
79 
80  expected::Result<void, std::string> insertPeer(
81  const shared_model::interface::Peer &peer) override;
82 
83  std::unique_ptr<MutableStorage> createMutableStorage(
84  std::shared_ptr<CommandExecutor> command_executor,
85  BlockStorageFactory &storage_factory) override;
86 
87  void reset() override;
88 
89  expected::Result<void, std::string> resetWsv() override;
90 
91  void resetPeers() override;
92 
93  void dropStorage() override;
94 
95  void freeConnections() override;
96 
97  CommitResult commit(
98  std::unique_ptr<MutableStorage> mutable_storage) override;
99 
100  bool preparedCommitEnabled() const override;
101 
102  CommitResult commitPrepared(
103  std::shared_ptr<const shared_model::interface::Block> block) override;
104 
105  std::shared_ptr<WsvQuery> getWsvQuery() const override;
106 
107  std::shared_ptr<BlockQuery> getBlockQuery() const override;
108 
109  rxcpp::observable<std::shared_ptr<const shared_model::interface::Block>>
110  on_commit() override;
111 
112  void prepareBlock(std::unique_ptr<TemporaryWsv> wsv) override;
113 
114  ~StorageImpl() override;
115 
116  protected:
117  StorageImpl(
118  boost::optional<std::shared_ptr<const iroha::LedgerState>>
119  ledger_state,
120  std::unique_ptr<ametsuchi::PostgresOptions> postgres_options,
121  std::unique_ptr<BlockStorage> block_store,
122  std::shared_ptr<PoolWrapper> pool_wrapper,
123  std::shared_ptr<shared_model::interface::PermissionToString>
124  perm_converter,
125  std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
126  std::shared_ptr<shared_model::interface::QueryResponseFactory>
127  query_response_factory,
128  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory,
129  size_t pool_size,
130  logger::LoggerManagerTreePtr log_manager);
131 
132  // db info
133  const std::unique_ptr<ametsuchi::PostgresOptions> postgres_options_;
134 
135  private:
136  using StoreBlockResult = iroha::expected::Result<void, std::string>;
137 
141  StoreBlockResult storeBlock(
142  std::shared_ptr<const shared_model::interface::Block> block);
143 
147  void tryRollback(soci::session &session);
148 
149  std::unique_ptr<BlockStorage> block_store_;
150 
151  std::shared_ptr<PoolWrapper> pool_wrapper_;
152 
154  std::shared_ptr<soci::connection_pool> &connection_;
155 
156  rxcpp::composite_subscription notifier_lifetime_;
157  rxcpp::subjects::subject<
158  std::shared_ptr<const shared_model::interface::Block>>
159  notifier_;
160 
161  std::shared_ptr<shared_model::interface::PermissionToString>
162  perm_converter_;
163 
164  std::shared_ptr<PendingTransactionStorage> pending_txs_storage_;
165 
166  std::shared_ptr<shared_model::interface::QueryResponseFactory>
167  query_response_factory_;
168 
169  std::unique_ptr<BlockStorageFactory> temporary_block_storage_factory_;
170 
171  logger::LoggerManagerTreePtr log_manager_;
172  logger::LoggerPtr log_;
173 
174  mutable std::shared_timed_mutex drop_mutex_;
175 
176  const size_t pool_size_;
177 
178  bool prepared_blocks_enabled_;
179 
180  std::atomic<bool> block_is_prepared_;
181 
182  std::string prepared_block_name_;
183 
184  boost::optional<std::shared_ptr<const iroha::LedgerState>> ledger_state_;
185  };
186  } // namespace ametsuchi
187 } // namespace iroha
188 
189 #endif // IROHA_STORAGE_IMPL_HPP
const std::unique_ptr< ametsuchi::PostgresOptions > postgres_options_
Definition: storage_impl.hpp:133
Definition: peer.hpp:20
iroha::expected::Result< std::shared_ptr< const iroha::LedgerState >, std::string > CommitResult
Definition: commit_result.hpp:20
std::shared_ptr< Logger > LoggerPtr
Definition: logger_fwd.hpp:22
Definition: storage.hpp:38
Definition: block_query.hpp:14
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:39