Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block header repo removed #2286

Merged
merged 15 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 4 additions & 5 deletions core/api/service/impl/api_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,10 @@ namespace kagome::api {
auto &session = session_context.storage_sub;
const auto id = session->generateSubscriptionSetId();
const auto &best_block_hash = block_tree_->bestBlock().hash;
const auto &header =
block_tree_->getBlockHeader(best_block_hash);
BOOST_ASSERT(header.has_value());
auto batch_res = trie_storage_->getEphemeralBatchAt(
header.value().state_root);
OUTCOME_TRY(header,
block_tree_->getBlockHeader(best_block_hash));
auto batch_res =
trie_storage_->getEphemeralBatchAt(header.state_root);
if (!batch_res.has_value()) {
SL_ERROR(logger_,
"Failed to get storage state for block {}, required "
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
1 change: 0 additions & 1 deletion core/blockchain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ add_library(blockchain
impl/block_storage_error.cpp
impl/justification_storage_policy.cpp
impl/block_storage_impl.cpp
impl/block_header_repository_impl.cpp
genesis_block_hash.cpp
)
target_link_libraries(blockchain
Expand Down
9 changes: 5 additions & 4 deletions core/blockchain/block_header_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ namespace kagome::blockchain {
const primitives::BlockHash &block_hash) const = 0;

/**
* @return status of a block with corresponding {@param block_hash} or a
* storage error
* @return block header with corresponding {@param block_hash} or a none
* optional if the corresponding block header is not in storage or a storage
* error
*/
virtual outcome::result<BlockStatus> getBlockStatus(
const primitives::BlockHash &block_hash) const = 0;
virtual outcome::result<std::optional<primitives::BlockHeader>>
tryGetBlockHeader(const primitives::BlockHash &block_hash) const = 0;

/**
* @param id of a block which number is returned
Expand Down
11 changes: 10 additions & 1 deletion core/blockchain/block_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,17 @@ namespace kagome::blockchain {
* Tries to get block header by {@param block_hash}
* @returns block header or error
*/
virtual outcome::result<primitives::BlockHeader> getBlockHeader(
const primitives::BlockHash &block_hash) const = 0;

/**
* Attempts to retrieve the block header for the given {@param block_hash}.
* @param block_hash The hash of the block whose header is to be retrieved.
* @returns An optional containing the block header if found, std::nullopt
* if not found, or an error if the operation fails.
*/
virtual outcome::result<std::optional<primitives::BlockHeader>>
getBlockHeader(const primitives::BlockHash &block_hash) const = 0;
tryGetBlockHeader(const primitives::BlockHash &block_hash) const = 0;

// -- body --

Expand Down
11 changes: 2 additions & 9 deletions core/blockchain/block_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <optional>
#include <vector>

#include "blockchain/block_header_repository.hpp"
#include "consensus/timeline/types.hpp"
#include "outcome/outcome.hpp"
#include "primitives/block.hpp"
Expand All @@ -27,7 +28,7 @@ namespace kagome::blockchain {
* production (handling forks, pruning the blocks, resolving child-parent
* relations, etc)
*/
class BlockTree {
class BlockTree : public BlockHeaderRepository {
public:
using BlockHashVecRes = outcome::result<std::vector<primitives::BlockHash>>;

Expand All @@ -53,14 +54,6 @@ namespace kagome::blockchain {
*/
virtual bool has(const primitives::BlockHash &hash) const = 0;

/**
* Get block header by provided block id
* @param block_hash hash of the block header we are looking for
* @return result containing block header if it exists, error otherwise
*/
virtual outcome::result<primitives::BlockHeader> getBlockHeader(
const primitives::BlockHash &block_hash) const = 0;

/**
* Get a body (extrinsics) of the block (if present)
* @param block_hash hash of the block to get body for
Expand Down
Loading
Loading