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