Skip to content

Commit

Permalink
Remove block header repo in many places
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsa committed Oct 28, 2024
1 parent ee46df1 commit c1b0ea4
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 157 deletions.
8 changes: 2 additions & 6 deletions core/api/service/chain/impl/chain_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ namespace kagome::api {
using primitives::BlockNumber;

ChainApiImpl::ChainApiImpl(
std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<blockchain::BlockStorage> block_storage,
LazySPtr<api::ApiService> api_service)
: header_repo_{std::move(block_repo)},
block_tree_{std::move(block_tree)},
: block_tree_{std::move(block_tree)},
api_service_{api_service},
block_storage_{std::move(block_storage)} {
BOOST_ASSERT_MSG(header_repo_ != nullptr,
"block repo parameter is nullptr");
BOOST_ASSERT_MSG(block_tree_ != nullptr, "block tree parameter is nullptr");
BOOST_ASSERT(block_storage_);
}
Expand All @@ -48,7 +44,7 @@ namespace kagome::api {
}
outcome::result<common::Hash256> ChainApiImpl::getBlockHash(
BlockNumber value) const {
return header_repo_->getHashByNumber(value);
return block_tree_->getHashByNumber(value);
}

outcome::result<BlockHash> ChainApiImpl::getBlockHash(
Expand Down
8 changes: 3 additions & 5 deletions core/api/service/chain/impl/chain_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ namespace kagome::api {

~ChainApiImpl() override = default;

ChainApiImpl(std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<blockchain::BlockTree> block_tree,
ChainApiImpl(std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<blockchain::BlockStorage> block_storage,
LazySPtr<api::ApiService> api_service);

Expand All @@ -44,12 +43,12 @@ namespace kagome::api {
outcome::result<primitives::BlockHeader> getHeader(
std::string_view hash) override {
OUTCOME_TRY(h, primitives::BlockHash::fromHexWithPrefix(hash));
return header_repo_->getBlockHeader(h);
return block_tree_->getBlockHeader(h);
}

outcome::result<primitives::BlockHeader> getHeader() override {
auto last = block_tree_->getLastFinalized();
return header_repo_->getBlockHeader(last.hash);
return block_tree_->getBlockHeader(last.hash);
}

outcome::result<primitives::BlockData> getBlock(
Expand All @@ -67,7 +66,6 @@ namespace kagome::api {
uint32_t subscription_id) override;

private:
std::shared_ptr<blockchain::BlockHeaderRepository> header_repo_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
LazySPtr<api::ApiService> api_service_;
std::shared_ptr<blockchain::BlockStorage> block_storage_;
Expand Down
13 changes: 5 additions & 8 deletions core/api/service/child_state/impl/child_state_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
namespace kagome::api {

ChildStateApiImpl::ChildStateApiImpl(
std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::Core> runtime_core,
std::shared_ptr<runtime::Metadata> metadata)
: header_repo_{std::move(block_repo)},
storage_{std::move(trie_storage)},
: storage_{std::move(trie_storage)},
block_tree_{std::move(block_tree)},
runtime_core_{std::move(runtime_core)},
metadata_{std::move(metadata)} {
BOOST_ASSERT(nullptr != header_repo_);
BOOST_ASSERT(nullptr != storage_);
BOOST_ASSERT(nullptr != block_tree_);
BOOST_ASSERT(nullptr != runtime_core_);
Expand All @@ -43,7 +40,7 @@ namespace kagome::api {
const auto &block_hash =
block_hash_opt.value_or(block_tree_->getLastFinalized().hash);

OUTCOME_TRY(header, header_repo_->getBlockHeader(block_hash));
OUTCOME_TRY(header, block_tree_->getBlockHeader(block_hash));
OUTCOME_TRY(initial_trie_reader,
storage_->getEphemeralBatchAt(header.state_root));
OUTCOME_TRY(child_root, initial_trie_reader->get(child_storage_key));
Expand Down Expand Up @@ -81,7 +78,7 @@ namespace kagome::api {
const auto &block_hash =
block_hash_opt.value_or(block_tree_->getLastFinalized().hash);

OUTCOME_TRY(header, header_repo_->getBlockHeader(block_hash));
OUTCOME_TRY(header, block_tree_->getBlockHeader(block_hash));
OUTCOME_TRY(initial_trie_reader,
storage_->getEphemeralBatchAt(header.state_root));
OUTCOME_TRY(child_root, initial_trie_reader->get(child_storage_key));
Expand Down Expand Up @@ -123,7 +120,7 @@ namespace kagome::api {
const std::optional<primitives::BlockHash> &block_hash_opt) const {
auto at = block_hash_opt ? block_hash_opt.value()
: block_tree_->getLastFinalized().hash;
OUTCOME_TRY(header, header_repo_->getBlockHeader(at));
OUTCOME_TRY(header, block_tree_->getBlockHeader(at));
OUTCOME_TRY(trie_reader, storage_->getEphemeralBatchAt(header.state_root));
OUTCOME_TRY(child_root, trie_reader->get(child_storage_key));
OUTCOME_TRY(child_root_hash, common::Hash256::fromSpan(child_root));
Expand Down Expand Up @@ -154,7 +151,7 @@ namespace kagome::api {
const std::optional<primitives::BlockHash> &block_hash_opt) const {
auto at = block_hash_opt ? block_hash_opt.value()
: block_tree_->getLastFinalized().hash;
OUTCOME_TRY(header, header_repo_->getBlockHeader(at));
OUTCOME_TRY(header, block_tree_->getBlockHeader(at));
OUTCOME_TRY(trie_reader, storage_->getEphemeralBatchAt(header.state_root));
OUTCOME_TRY(child_root, trie_reader->get(child_storage_key));
OUTCOME_TRY(child_root_hash, common::Hash256::fromSpan(child_root));
Expand Down
3 changes: 0 additions & 3 deletions core/api/service/child_state/impl/child_state_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "api/service/child_state/child_state_api.hpp"

#include "blockchain/block_header_repository.hpp"
#include "blockchain/block_tree.hpp"
#include "injector/lazy.hpp"
#include "runtime/runtime_api/core.hpp"
Expand All @@ -20,7 +19,6 @@ namespace kagome::api {
class ChildStateApiImpl final : public ChildStateApi {
public:
ChildStateApiImpl(
std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::Core> runtime_core,
Expand Down Expand Up @@ -59,7 +57,6 @@ namespace kagome::api {
const override;

private:
std::shared_ptr<blockchain::BlockHeaderRepository> header_repo_;
std::shared_ptr<const storage::trie::TrieStorage> storage_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<runtime::Core> runtime_core_;
Expand Down
19 changes: 8 additions & 11 deletions core/api/service/state/impl/state_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,18 @@ OUTCOME_CPP_DEFINE_CATEGORY(kagome::api, StateApiImpl::Error, e) {
namespace kagome::api {

StateApiImpl::StateApiImpl(
std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::Core> runtime_core,
std::shared_ptr<runtime::Metadata> metadata,
std::shared_ptr<runtime::Executor> executor,
LazySPtr<api::ApiService> api_service)
: header_repo_{std::move(block_repo)},
storage_{std::move(trie_storage)},
: storage_{std::move(trie_storage)},
block_tree_{std::move(block_tree)},
runtime_core_{std::move(runtime_core)},
api_service_{api_service},
metadata_{std::move(metadata)},
executor_{std::move(executor)} {
BOOST_ASSERT(nullptr != header_repo_);
BOOST_ASSERT(nullptr != storage_);
BOOST_ASSERT(nullptr != block_tree_);
BOOST_ASSERT(nullptr != runtime_core_);
Expand Down Expand Up @@ -81,7 +78,7 @@ namespace kagome::api {
const auto &block_hash =
block_hash_opt.value_or(block_tree_->getLastFinalized().hash);

OUTCOME_TRY(header, header_repo_->getBlockHeader(block_hash));
OUTCOME_TRY(header, block_tree_->getBlockHeader(block_hash));
OUTCOME_TRY(initial_trie_reader,
storage_->getEphemeralBatchAt(header.state_root));
auto cursor = initial_trie_reader->trieCursor();
Expand Down Expand Up @@ -121,7 +118,7 @@ namespace kagome::api {

outcome::result<std::optional<common::Buffer>> StateApiImpl::getStorageAt(
common::BufferView key, const primitives::BlockHash &at) const {
OUTCOME_TRY(header, header_repo_->getBlockHeader(at));
OUTCOME_TRY(header, block_tree_->getBlockHeader(at));
OUTCOME_TRY(trie_reader, storage_->getEphemeralBatchAt(header.state_root));
auto res = trie_reader->tryGet(key);
return common::map_result_optional(
Expand All @@ -134,7 +131,7 @@ namespace kagome::api {
const std::optional<primitives::BlockHash> &block_hash_opt) const {
auto at = block_hash_opt ? block_hash_opt.value()
: block_tree_->getLastFinalized().hash;
OUTCOME_TRY(header, header_repo_->getBlockHeader(at));
OUTCOME_TRY(header, block_tree_->getBlockHeader(at));
OUTCOME_TRY(trie_reader, storage_->getEphemeralBatchAt(header.state_root));
OUTCOME_TRY(res, trie_reader->tryGet(key));
return res ? std::make_optional(res->size()) : std::nullopt;
Expand All @@ -152,8 +149,8 @@ namespace kagome::api {
}

if (from != to) {
OUTCOME_TRY(from_number, header_repo_->getNumberByHash(from));
OUTCOME_TRY(to_number, header_repo_->getNumberByHash(to));
OUTCOME_TRY(from_number, block_tree_->getNumberByHash(from));
OUTCOME_TRY(to_number, block_tree_->getNumberByHash(to));
if (to_number < from_number) {
return Error::END_BLOCK_LOWER_THAN_BEGIN_BLOCK;
}
Expand All @@ -169,7 +166,7 @@ namespace kagome::api {
// returning the whole vector with block ids
OUTCOME_TRY(range, block_tree_->getChainByBlocks(from, to));
for (auto &block : range) {
OUTCOME_TRY(header, header_repo_->getBlockHeader(block));
OUTCOME_TRY(header, block_tree_->getBlockHeader(block));
OUTCOME_TRY(batch, storage_->getEphemeralBatchAt(header.state_root));
StorageChangeSet change{.block = block};
for (auto &key : keys) {
Expand Down Expand Up @@ -206,7 +203,7 @@ namespace kagome::api {
auto at =
opt_at.has_value() ? opt_at.value() : block_tree_->bestBlock().hash;
storage::trie::OnRead db;
OUTCOME_TRY(header, header_repo_->getBlockHeader(at));
OUTCOME_TRY(header, block_tree_->getBlockHeader(at));
OUTCOME_TRY(
trie, storage_->getProofReaderBatchAt(header.state_root, db.onRead()));
for (auto &key : keys) {
Expand Down
5 changes: 1 addition & 4 deletions core/api/service/state/impl/state_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "api/service/state/state_api.hpp"

#include "blockchain/block_header_repository.hpp"
#include "blockchain/block_tree.hpp"
#include "injector/lazy.hpp"
#include "runtime/runtime_api/core.hpp"
Expand All @@ -32,8 +31,7 @@ namespace kagome::api {
static constexpr size_t kMaxBlockRange = 256;
static constexpr size_t kMaxKeySetSize = 64;

StateApiImpl(std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
StateApiImpl(std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::Core> runtime_core,
std::shared_ptr<runtime::Metadata> metadata,
Expand Down Expand Up @@ -94,7 +92,6 @@ namespace kagome::api {
std::string_view hex_block_hash) override;

private:
std::shared_ptr<blockchain::BlockHeaderRepository> header_repo_;
std::shared_ptr<const storage::trie::TrieStorage> storage_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<runtime::Core> runtime_core_;
Expand Down
4 changes: 0 additions & 4 deletions core/application/modes/recovery_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@ namespace kagome::application::mode {
const AppConfiguration &app_config,
std::shared_ptr<storage::SpacedStorage> spaced_storage,
std::shared_ptr<blockchain::BlockStorage> storage,
std::shared_ptr<blockchain::BlockHeaderRepository> header_repo,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
std::shared_ptr<consensus::grandpa::AuthorityManager> authority_manager,
std::shared_ptr<blockchain::BlockTree> block_tree)
: app_config_(app_config),
spaced_storage_(std::move(spaced_storage)),
storage_(std::move(storage)),
header_repo_(std::move(header_repo)),
trie_storage_(std::move(trie_storage)),
authority_manager_(std::move(authority_manager)),
block_tree_(std::move(block_tree)),
log_(log::createLogger("RecoveryMode", "main")) {
BOOST_ASSERT(spaced_storage_ != nullptr);
BOOST_ASSERT(storage_ != nullptr);
BOOST_ASSERT(header_repo_ != nullptr);
BOOST_ASSERT(trie_storage_ != nullptr);
BOOST_ASSERT(authority_manager_ != nullptr);
BOOST_ASSERT(block_tree_ != nullptr);
Expand All @@ -44,7 +41,6 @@ namespace kagome::application::mode {
auto res =
blockchain::BlockTreeImpl::recover(app_config_.recoverState().value(),
storage_,
header_repo_,
trie_storage_,
block_tree_);
if (res.has_error()) {
Expand Down
3 changes: 0 additions & 3 deletions core/application/modes/recovery_mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace kagome::application {

namespace kagome::blockchain {
class BlockStorage;
class BlockHeaderRepository;
class BlockTree;
} // namespace kagome::blockchain

Expand All @@ -43,7 +42,6 @@ namespace kagome::application::mode {
const application::AppConfiguration &app_config,
std::shared_ptr<storage::SpacedStorage> spaced_storage,
std::shared_ptr<blockchain::BlockStorage> storage,
std::shared_ptr<blockchain::BlockHeaderRepository> header_repo,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage,
std::shared_ptr<consensus::grandpa::AuthorityManager> authority_manager,
std::shared_ptr<blockchain::BlockTree> block_tree);
Expand All @@ -54,7 +52,6 @@ namespace kagome::application::mode {
const application::AppConfiguration &app_config_;
std::shared_ptr<storage::SpacedStorage> spaced_storage_;
std::shared_ptr<blockchain::BlockStorage> storage_;
std::shared_ptr<blockchain::BlockHeaderRepository> header_repo_;
std::shared_ptr<const storage::trie::TrieStorage> trie_storage_;
std::shared_ptr<consensus::grandpa::AuthorityManager> authority_manager_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
Expand Down
Loading

0 comments on commit c1b0ea4

Please sign in to comment.