8 #include <jsonrpc-lean/fault.h> 17 case E::BLOCK_NOT_FOUND:
18 return "The requested block is not found";
19 case E::HEADER_NOT_FOUND:
20 return "The requested block header is not found";
22 return "Unknown Chain API error";
30 std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
31 std::shared_ptr<blockchain::BlockTree> block_tree,
32 std::shared_ptr<blockchain::BlockStorage> block_storage)
33 : header_repo_{std::move(block_repo)},
37 "block repo parameter is nullptr");
38 BOOST_ASSERT_MSG(
block_tree_ !=
nullptr,
"block tree parameter is nullptr");
43 auto last_finalized =
block_tree_->getLastFinalized();
44 return last_finalized.hash;
52 std::shared_ptr<api::ApiService>
const &api_service) {
53 BOOST_ASSERT(api_service !=
nullptr);
58 std::string_view value)
const {
63 OUTCOME_TRY(number, common::unhexNumber<BlockNumber>(value));
68 gsl::span<const ValueType> values)
const {
69 std::vector<BlockHash> results;
70 results.reserve(values.size());
72 for (
const auto &v : values) {
73 auto &&res = kagome::visit_in_place(
76 [
this](std::string_view hex_string) {
80 results.emplace_back(r);
87 std::string_view hash) {
90 if (block)
return block.value();
95 auto last_finalized_info =
block_tree_->getLastFinalized();
96 OUTCOME_TRY(block,
block_storage_->getBlockData(last_finalized_info.hash));
97 if (block)
return block.value();
108 return api_service->subscribeFinalizedHeads();
110 throw jsonrpc::InternalErrorFault(
111 "Internal error. Api service not initialized.");
115 uint32_t subscription_id) {
117 OUTCOME_TRY(api_service->unsubscribeFinalizedHeads(subscription_id));
119 return outcome::success();
122 throw jsonrpc::InternalErrorFault(
123 "Internal error. Api service not initialized.");
128 return api_service->subscribeNewHeads();
130 throw jsonrpc::InternalErrorFault(
131 "Internal error. Api service not initialized.");
135 uint32_t subscription_id) {
137 OUTCOME_TRY(api_service->unsubscribeNewHeads(subscription_id));
138 return outcome::success();
141 throw jsonrpc::InternalErrorFault(
142 "Internal error. Api service not initialized.");
outcome::result< primitives::BlockData > getBlock() override
outcome::result< uint32_t > subscribeNewHeads() override
OUTCOME_CPP_DEFINE_CATEGORY(kagome::api, ChainApiImpl::Error, e)
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
static outcome::result< Blob< size_ > > fromHexWithPrefix(std::string_view hex)
std::shared_ptr< blockchain::BlockTree > block_tree_
common::Hash256 BlockHash
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