Kagome
Polkadot Runtime Engine in C++17
chain_api_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CHAIN_API_IMPL_HPP
7 #define KAGOME_CHAIN_API_IMPL_HPP
8 
9 #include <memory>
10 
15 
16 namespace kagome::api {
17 
18  class ChainApiImpl : public ChainApi {
19  public:
20  enum class Error {
21  BLOCK_NOT_FOUND = 1,
23  };
24 
25  ~ChainApiImpl() override = default;
26 
27  ChainApiImpl(std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
28  std::shared_ptr<blockchain::BlockTree> block_tree,
29  std::shared_ptr<blockchain::BlockStorage> block_storage);
30 
31  void setApiService(
32  std::shared_ptr<api::ApiService> const &api_service) override;
33 
34  outcome::result<BlockHash> getBlockHash() const override;
35 
36  outcome::result<BlockHash> getBlockHash(BlockNumber value) const override;
37 
38  outcome::result<BlockHash> getBlockHash(
39  std::string_view value) const override;
40 
41  outcome::result<std::vector<BlockHash>> getBlockHash(
42  gsl::span<const ValueType> values) const override;
43 
44  outcome::result<primitives::BlockHeader> getHeader(
45  std::string_view hash) override {
46  OUTCOME_TRY(h, primitives::BlockHash::fromHexWithPrefix(hash));
47  return header_repo_->getBlockHeader(h);
48  }
49 
50  outcome::result<primitives::BlockHeader> getHeader() override {
51  auto last = block_tree_->getLastFinalized();
52  return header_repo_->getBlockHeader(last.hash);
53  }
54 
55  outcome::result<primitives::BlockData> getBlock(
56  std::string_view hash) override;
57  outcome::result<primitives::BlockData> getBlock() override;
58 
59  outcome::result<primitives::BlockHash> getFinalizedHead() const override;
60 
61  outcome::result<uint32_t> subscribeFinalizedHeads() override;
62  outcome::result<void> unsubscribeFinalizedHeads(
63  uint32_t subscription_id) override;
64 
65  outcome::result<uint32_t> subscribeNewHeads() override;
66  outcome::result<void> unsubscribeNewHeads(
67  uint32_t subscription_id) override;
68 
69  private:
70  std::shared_ptr<blockchain::BlockHeaderRepository> header_repo_;
71  std::shared_ptr<blockchain::BlockTree> block_tree_;
72  std::weak_ptr<api::ApiService> api_service_;
73  std::shared_ptr<blockchain::BlockStorage> block_storage_;
74  };
75 } // namespace kagome::api
76 
78 
79 #endif // KAGOME_CHAIN_API_IMPL_HPP
outcome::result< primitives::BlockData > getBlock() override
outcome::result< uint32_t > subscribeNewHeads() override
outcome::result< primitives::BlockHeader > getHeader() override
outcome::result< void > unsubscribeFinalizedHeads(uint32_t subscription_id) override
std::shared_ptr< blockchain::BlockHeaderRepository > header_repo_
std::shared_ptr< blockchain::BlockStorage > block_storage_
primitives::BlockNumber BlockNumber
Definition: chain_api.hpp:26
static outcome::result< Blob< size_ > > fromHexWithPrefix(std::string_view hex)
Definition: blob.hpp:197
~ChainApiImpl() override=default
outcome::result< primitives::BlockHeader > getHeader(std::string_view hash) override
std::shared_ptr< blockchain::BlockTree > block_tree_
OUTCOME_HPP_DECLARE_ERROR(kagome::api, ChainApiImpl::Error)
ChainApiImpl(std::shared_ptr< blockchain::BlockHeaderRepository > block_repo, std::shared_ptr< blockchain::BlockTree > block_tree, std::shared_ptr< blockchain::BlockStorage > block_storage)
outcome::result< primitives::BlockHash > getFinalizedHead() const override
outcome::result< BlockHash > getBlockHash() const override
std::weak_ptr< api::ApiService > api_service_
void setApiService(std::shared_ptr< api::ApiService > const &api_service) override
outcome::result< uint32_t > subscribeFinalizedHeads() override
outcome::result< void > unsubscribeNewHeads(uint32_t subscription_id) override