diff --git a/core/api/service/chain/impl/chain_api_impl.cpp b/core/api/service/chain/impl/chain_api_impl.cpp index 4f20b83b9c..019acfb623 100644 --- a/core/api/service/chain/impl/chain_api_impl.cpp +++ b/core/api/service/chain/impl/chain_api_impl.cpp @@ -28,16 +28,12 @@ namespace kagome::api { using primitives::BlockNumber; ChainApiImpl::ChainApiImpl( - std::shared_ptr block_repo, std::shared_ptr block_tree, std::shared_ptr block_storage, LazySPtr 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_); } @@ -48,7 +44,7 @@ namespace kagome::api { } outcome::result ChainApiImpl::getBlockHash( BlockNumber value) const { - return header_repo_->getHashByNumber(value); + return block_tree_->getHashByNumber(value); } outcome::result ChainApiImpl::getBlockHash( diff --git a/core/api/service/chain/impl/chain_api_impl.hpp b/core/api/service/chain/impl/chain_api_impl.hpp index e6128e7c7c..804bc34acc 100644 --- a/core/api/service/chain/impl/chain_api_impl.hpp +++ b/core/api/service/chain/impl/chain_api_impl.hpp @@ -26,8 +26,7 @@ namespace kagome::api { ~ChainApiImpl() override = default; - ChainApiImpl(std::shared_ptr block_repo, - std::shared_ptr block_tree, + ChainApiImpl(std::shared_ptr block_tree, std::shared_ptr block_storage, LazySPtr api_service); @@ -44,12 +43,12 @@ namespace kagome::api { outcome::result 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 getHeader() override { auto last = block_tree_->getLastFinalized(); - return header_repo_->getBlockHeader(last.hash); + return block_tree_->getBlockHeader(last.hash); } outcome::result getBlock( @@ -67,7 +66,6 @@ namespace kagome::api { uint32_t subscription_id) override; private: - std::shared_ptr header_repo_; std::shared_ptr block_tree_; LazySPtr api_service_; std::shared_ptr block_storage_; diff --git a/core/api/service/child_state/impl/child_state_api_impl.cpp b/core/api/service/child_state/impl/child_state_api_impl.cpp index 1777fc68c6..579d16ca4e 100644 --- a/core/api/service/child_state/impl/child_state_api_impl.cpp +++ b/core/api/service/child_state/impl/child_state_api_impl.cpp @@ -18,17 +18,14 @@ namespace kagome::api { ChildStateApiImpl::ChildStateApiImpl( - std::shared_ptr block_repo, std::shared_ptr trie_storage, std::shared_ptr block_tree, std::shared_ptr runtime_core, std::shared_ptr 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_); @@ -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)); @@ -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)); @@ -123,7 +120,7 @@ namespace kagome::api { const std::optional &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)); @@ -154,7 +151,7 @@ namespace kagome::api { const std::optional &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)); diff --git a/core/api/service/child_state/impl/child_state_api_impl.hpp b/core/api/service/child_state/impl/child_state_api_impl.hpp index 0d591312f7..85cfdbe447 100644 --- a/core/api/service/child_state/impl/child_state_api_impl.hpp +++ b/core/api/service/child_state/impl/child_state_api_impl.hpp @@ -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" @@ -20,7 +19,6 @@ namespace kagome::api { class ChildStateApiImpl final : public ChildStateApi { public: ChildStateApiImpl( - std::shared_ptr block_repo, std::shared_ptr trie_storage, std::shared_ptr block_tree, std::shared_ptr runtime_core, @@ -59,7 +57,6 @@ namespace kagome::api { const override; private: - std::shared_ptr header_repo_; std::shared_ptr storage_; std::shared_ptr block_tree_; std::shared_ptr runtime_core_; diff --git a/core/api/service/impl/api_service_impl.cpp b/core/api/service/impl/api_service_impl.cpp index 690a2efbbc..38cf51eb04 100644 --- a/core/api/service/impl/api_service_impl.cpp +++ b/core/api/service/impl/api_service_impl.cpp @@ -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 " diff --git a/core/api/service/state/impl/state_api_impl.cpp b/core/api/service/state/impl/state_api_impl.cpp index 1999ae4d19..bb94f1d3f7 100644 --- a/core/api/service/state/impl/state_api_impl.cpp +++ b/core/api/service/state/impl/state_api_impl.cpp @@ -39,21 +39,18 @@ OUTCOME_CPP_DEFINE_CATEGORY(kagome::api, StateApiImpl::Error, e) { namespace kagome::api { StateApiImpl::StateApiImpl( - std::shared_ptr block_repo, std::shared_ptr trie_storage, std::shared_ptr block_tree, std::shared_ptr runtime_core, std::shared_ptr metadata, std::shared_ptr executor, LazySPtr 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_); @@ -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(); @@ -121,7 +118,7 @@ namespace kagome::api { outcome::result> 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( @@ -134,7 +131,7 @@ namespace kagome::api { const std::optional &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; @@ -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; } @@ -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) { @@ -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) { diff --git a/core/api/service/state/impl/state_api_impl.hpp b/core/api/service/state/impl/state_api_impl.hpp index dc301e3741..dce2da9fc5 100644 --- a/core/api/service/state/impl/state_api_impl.hpp +++ b/core/api/service/state/impl/state_api_impl.hpp @@ -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" @@ -32,8 +31,7 @@ namespace kagome::api { static constexpr size_t kMaxBlockRange = 256; static constexpr size_t kMaxKeySetSize = 64; - StateApiImpl(std::shared_ptr block_repo, - std::shared_ptr trie_storage, + StateApiImpl(std::shared_ptr trie_storage, std::shared_ptr block_tree, std::shared_ptr runtime_core, std::shared_ptr metadata, @@ -94,7 +92,6 @@ namespace kagome::api { std::string_view hex_block_hash) override; private: - std::shared_ptr header_repo_; std::shared_ptr storage_; std::shared_ptr block_tree_; std::shared_ptr runtime_core_; diff --git a/core/application/modes/recovery_mode.cpp b/core/application/modes/recovery_mode.cpp index 179d293ba2..6b86e291d6 100644 --- a/core/application/modes/recovery_mode.cpp +++ b/core/application/modes/recovery_mode.cpp @@ -19,21 +19,18 @@ namespace kagome::application::mode { const AppConfiguration &app_config, std::shared_ptr spaced_storage, std::shared_ptr storage, - std::shared_ptr header_repo, std::shared_ptr trie_storage, std::shared_ptr authority_manager, std::shared_ptr 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); @@ -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()) { diff --git a/core/application/modes/recovery_mode.hpp b/core/application/modes/recovery_mode.hpp index ff00f4b84c..a479475989 100644 --- a/core/application/modes/recovery_mode.hpp +++ b/core/application/modes/recovery_mode.hpp @@ -18,7 +18,6 @@ namespace kagome::application { namespace kagome::blockchain { class BlockStorage; - class BlockHeaderRepository; class BlockTree; } // namespace kagome::blockchain @@ -43,7 +42,6 @@ namespace kagome::application::mode { const application::AppConfiguration &app_config, std::shared_ptr spaced_storage, std::shared_ptr storage, - std::shared_ptr header_repo, std::shared_ptr trie_storage, std::shared_ptr authority_manager, std::shared_ptr block_tree); @@ -54,7 +52,6 @@ namespace kagome::application::mode { const application::AppConfiguration &app_config_; std::shared_ptr spaced_storage_; std::shared_ptr storage_; - std::shared_ptr header_repo_; std::shared_ptr trie_storage_; std::shared_ptr authority_manager_; std::shared_ptr block_tree_; diff --git a/core/blockchain/CMakeLists.txt b/core/blockchain/CMakeLists.txt index 572ca9875e..eb2afb2da3 100644 --- a/core/blockchain/CMakeLists.txt +++ b/core/blockchain/CMakeLists.txt @@ -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 diff --git a/core/blockchain/block_header_repository.hpp b/core/blockchain/block_header_repository.hpp index a18e6c73cf..5d1bb7afe3 100644 --- a/core/blockchain/block_header_repository.hpp +++ b/core/blockchain/block_header_repository.hpp @@ -54,13 +54,6 @@ namespace kagome::blockchain { virtual outcome::result getBlockHeader( const primitives::BlockHash &block_hash) const = 0; - /** - * @return status of a block with corresponding {@param block_hash} or a - * storage error - */ - virtual outcome::result getBlockStatus( - const primitives::BlockHash &block_hash) const = 0; - /** * @param id of a block which number is returned * @return block number or a none optional if the corresponding block header diff --git a/core/blockchain/block_storage.hpp b/core/blockchain/block_storage.hpp index e60754f460..975636eee5 100644 --- a/core/blockchain/block_storage.hpp +++ b/core/blockchain/block_storage.hpp @@ -93,7 +93,7 @@ namespace kagome::blockchain { * Tries to get block header by {@param block_hash} * @returns block header or error */ - virtual outcome::result> + virtual outcome::result getBlockHeader(const primitives::BlockHash &block_hash) const = 0; // -- body -- diff --git a/core/blockchain/block_tree.hpp b/core/blockchain/block_tree.hpp index 8ff72eb36a..fa14b403c3 100644 --- a/core/blockchain/block_tree.hpp +++ b/core/blockchain/block_tree.hpp @@ -10,6 +10,7 @@ #include #include +#include "blockchain/block_header_repository.hpp" #include "consensus/timeline/types.hpp" #include "outcome/outcome.hpp" #include "primitives/block.hpp" @@ -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>; @@ -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 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 diff --git a/core/blockchain/impl/block_header_repository_impl.cpp b/core/blockchain/impl/block_header_repository_impl.cpp deleted file mode 100644 index 11afe95ed0..0000000000 --- a/core/blockchain/impl/block_header_repository_impl.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright Quadrivium LLC - * All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "blockchain/impl/block_header_repository_impl.hpp" - -#include - -#include "blockchain/block_tree_error.hpp" -#include "blockchain/impl/storage_util.hpp" -#include "scale/scale.hpp" - -using kagome::primitives::BlockHash; -using kagome::primitives::BlockNumber; -using kagome::storage::Space; - -namespace kagome::blockchain { - - BlockHeaderRepositoryImpl::BlockHeaderRepositoryImpl( - std::shared_ptr storage, - std::shared_ptr hasher) - : storage_{std::move(storage)}, hasher_{std::move(hasher)} { - BOOST_ASSERT(hasher_); - } - - outcome::result BlockHeaderRepositoryImpl::getNumberByHash( - const primitives::BlockHash &hash) const { - OUTCOME_TRY(header, getBlockHeader(hash)); - return header.number; - } - - outcome::result - BlockHeaderRepositoryImpl::getHashByNumber( - primitives::BlockNumber number) const { - auto num_to_idx_key = blockNumberToKey(number); - auto key_space = storage_->getSpace(Space::kLookupKey); - auto res = key_space->get(num_to_idx_key); - if (not res.has_value()) { - return BlockTreeError::HEADER_NOT_FOUND; - } - return primitives::BlockHash::fromSpan(res.value()); - } - - outcome::result - BlockHeaderRepositoryImpl::getBlockHeader( - const primitives::BlockHash &block_hash) const { - OUTCOME_TRY(header_opt, - getFromSpace(*storage_, Space::kHeader, block_hash)); - if (header_opt.has_value()) { - OUTCOME_TRY(header, - scale::decode(header_opt.value())); - header.hash_opt.emplace(block_hash); - return header; - } - return BlockTreeError::HEADER_NOT_FOUND; - } - - outcome::result BlockHeaderRepositoryImpl::getBlockStatus( - const primitives::BlockHash &block_hash) const { - return getBlockHeader(block_hash).has_value() ? BlockStatus::InChain - : BlockStatus::Unknown; - } - -} // namespace kagome::blockchain diff --git a/core/blockchain/impl/block_header_repository_impl.hpp b/core/blockchain/impl/block_header_repository_impl.hpp deleted file mode 100644 index a0b2d8eb06..0000000000 --- a/core/blockchain/impl/block_header_repository_impl.hpp +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright Quadrivium LLC - * All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include "blockchain/block_header_repository.hpp" - -#include "crypto/hasher.hpp" -#include "storage/spaced_storage.hpp" - -namespace kagome::blockchain { - - class BlockHeaderRepositoryImpl : public BlockHeaderRepository { - public: - BlockHeaderRepositoryImpl(std::shared_ptr storage, - std::shared_ptr hasher); - - ~BlockHeaderRepositoryImpl() override = default; - - outcome::result getNumberByHash( - const primitives::BlockHash &block_hash) const override; - - outcome::result getHashByNumber( - primitives::BlockNumber block_number) const override; - - outcome::result getBlockHeader( - const primitives::BlockHash &block_hash) const override; - - outcome::result getBlockStatus( - const primitives::BlockHash &block_hash) const override; - - private: - std::shared_ptr storage_; - std::shared_ptr hasher_; - }; - -} // namespace kagome::blockchain diff --git a/core/blockchain/impl/block_storage_impl.cpp b/core/blockchain/impl/block_storage_impl.cpp index 12c00ff4ce..666e553bb3 100644 --- a/core/blockchain/impl/block_storage_impl.cpp +++ b/core/blockchain/impl/block_storage_impl.cpp @@ -118,26 +118,19 @@ namespace kagome::blockchain { if (j_opt.has_value()) { break; } - OUTCOME_TRY(header_opt, getBlockHeader(current_hash)); - if (header_opt.has_value()) { - auto header = header_opt.value(); - if (header.number == 0) { - SL_TRACE(logger_, - "Not found block with justification. " - "Genesis block will be used as last finalized ({})", - current_hash); - return {0, current_hash}; // genesis - } - current_hash = header.parent_hash; - } else { - SL_ERROR( - logger_, "Failed to fetch header for block ({})", current_hash); - return BlockStorageError::HEADER_NOT_FOUND; + OUTCOME_TRY(header, getBlockHeader(current_hash)); + if (header.number == 0) { + SL_TRACE(logger_, + "Not found block with justification. " + "Genesis block will be used as last finalized ({})", + current_hash); + return {0, current_hash}; // genesis } + current_hash = header.parent_hash; } OUTCOME_TRY(header, getBlockHeader(current_hash)); - primitives::BlockInfo found_block{header.value().number, current_hash}; + primitives::BlockInfo found_block{header.number, current_hash}; SL_TRACE(logger_, "Justification is found in block {}. " "This block will be used as last finalized", @@ -206,8 +199,7 @@ namespace kagome::blockchain { return block_hash; } - outcome::result> - BlockStorageImpl::getBlockHeader( + outcome::result BlockStorageImpl::getBlockHeader( const primitives::BlockHash &block_hash) const { OUTCOME_TRY(encoded_header_opt, getFromSpace(*storage_, Space::kHeader, block_hash)); @@ -218,7 +210,7 @@ namespace kagome::blockchain { header.hash_opt.emplace(block_hash); return header; } - return std::nullopt; + return BlockStorageError::HEADER_NOT_FOUND; } outcome::result BlockStorageImpl::putBlockBody( @@ -309,11 +301,7 @@ namespace kagome::blockchain { primitives::BlockData block_data{.hash = block_hash}; // Block header - OUTCOME_TRY(header_opt, getBlockHeader(block_hash)); - if (not header_opt.has_value()) { - return std::nullopt; - } - auto &header = header_opt.value(); + OUTCOME_TRY(header, getBlockHeader(block_hash)); block_data.header = std::move(header); // Block body @@ -332,11 +320,11 @@ namespace kagome::blockchain { outcome::result BlockStorageImpl::removeBlock( const primitives::BlockHash &block_hash) { // Check if block still in storage - OUTCOME_TRY(header_opt, getBlockHeader(block_hash)); - if (not header_opt.has_value()) { + const auto header_res = getBlockHeader(block_hash); + if (not header_res) { return outcome::success(); } - const auto &header = header_opt.value(); + const auto &header = header_res.value(); primitives::BlockInfo block_info(header.number, block_hash); diff --git a/core/blockchain/impl/block_storage_impl.hpp b/core/blockchain/impl/block_storage_impl.hpp index 927aabeadd..43445c7035 100644 --- a/core/blockchain/impl/block_storage_impl.hpp +++ b/core/blockchain/impl/block_storage_impl.hpp @@ -60,7 +60,7 @@ namespace kagome::blockchain { outcome::result putBlockHeader( const primitives::BlockHeader &header) override; - outcome::result> getBlockHeader( + outcome::result getBlockHeader( const primitives::BlockHash &block_hash) const override; // -- body -- diff --git a/core/blockchain/impl/block_tree_impl.cpp b/core/blockchain/impl/block_tree_impl.cpp index aa056ba2b5..9104f1f358 100644 --- a/core/blockchain/impl/block_tree_impl.cpp +++ b/core/blockchain/impl/block_tree_impl.cpp @@ -13,6 +13,7 @@ #include "blockchain/block_tree_error.hpp" #include "blockchain/impl/cached_tree.hpp" #include "blockchain/impl/justification_storage_policy.hpp" +#include "blockchain/impl/storage_util.hpp" #include "common/main_thread_pool.hpp" #include "consensus/babe/impl/babe_digests_util.hpp" #include "consensus/babe/is_primary.hpp" @@ -35,11 +36,8 @@ namespace kagome::blockchain { namespace { /// Function-helper for loading (and repair if it needed) of leaves outcome::result> loadLeaves( - const std::shared_ptr &storage, - const std::shared_ptr &header_repo, - const log::Logger &log) { + const std::shared_ptr &storage, const log::Logger &log) { BOOST_ASSERT(storage != nullptr); - BOOST_ASSERT(header_repo != nullptr); std::set block_tree_leaves; { @@ -49,16 +47,17 @@ namespace kagome::blockchain { block_tree_unordered_leaves.size()); for (auto &hash : block_tree_unordered_leaves) { - auto res = header_repo->getNumberById(hash); - if (res.has_error()) { - if (res == outcome::failure(BlockTreeError::HEADER_NOT_FOUND)) { + // get block nuber by hash + const auto header = storage->getBlockHeader(hash); + if (not header) { + if (header == outcome::failure(BlockTreeError::HEADER_NOT_FOUND)) { SL_TRACE(log, "Leaf {} not found", hash); continue; } - SL_ERROR(log, "Leaf {} is corrupted: {}", hash, res.error()); - return res.as_failure(); + SL_ERROR(log, "Leaf {} is corrupted: {}", hash, header.error()); + return header.as_failure(); } - auto number = res.value(); + auto number = header.value().number; SL_TRACE(log, "Leaf {} found", primitives::BlockInfo(number, hash)); block_tree_leaves.emplace(number, hash); } @@ -95,7 +94,8 @@ namespace kagome::blockchain { } } - OUTCOME_TRY(hash, header_repo->getHashById(number)); + OUTCOME_TRY(hash_opt_res, storage->getBlockHash(number)); + primitives::BlockHash hash = hash_opt_res.value(); block_tree_leaves.emplace(number, hash); if (auto res = storage->setBlockTreeLeaves({hash}); res.has_error()) { @@ -114,9 +114,7 @@ namespace kagome::blockchain { outcome::result> BlockTreeImpl::create( const application::AppConfiguration &app_config, - std::shared_ptr header_repo, std::shared_ptr storage, - std::shared_ptr extrinsic_observer, std::shared_ptr hasher, primitives::events::ChainSubscriptionEnginePtr chain_events_engine, primitives::events::ExtrinsicSubscriptionEnginePtr @@ -128,7 +126,6 @@ namespace kagome::blockchain { std::shared_ptr state_pruner, common::MainThreadPool &main_thread_pool) { BOOST_ASSERT(storage != nullptr); - BOOST_ASSERT(header_repo != nullptr); log::Logger log = log::createLogger("BlockTree", "block_tree"); @@ -136,10 +133,9 @@ namespace kagome::blockchain { auto finalized_block_header_res = storage->getBlockHeader(last_finalized_block_info.hash); - BOOST_ASSERT_MSG(finalized_block_header_res.has_value() - and finalized_block_header_res.value().has_value(), + BOOST_ASSERT_MSG(finalized_block_header_res.has_value(), "Initialized block tree must be have finalized block"); - auto &finalized_block_header = finalized_block_header_res.value().value(); + auto &finalized_block_header = finalized_block_header_res.value(); // call chain_events_engine->notify to init babe_config_repo preventive chain_events_engine->notify( primitives::events::ChainEventType::kFinalizedHeads, @@ -147,7 +143,7 @@ namespace kagome::blockchain { OUTCOME_TRY(storage->getJustification(last_finalized_block_info.hash)); - OUTCOME_TRY(block_tree_leaves, loadLeaves(storage, header_repo, log)); + OUTCOME_TRY(block_tree_leaves, loadLeaves(storage, log)); BOOST_ASSERT_MSG(not block_tree_leaves.empty(), "Must be known or calculated at least one leaf"); @@ -196,16 +192,16 @@ namespace kagome::blockchain { dead.emplace(fork); auto f_res = storage->getBlockHeader(fork.hash); - if (f_res.has_error() or not f_res.value().has_value()) { + if (f_res.has_error()) { break; } - const auto &fork_header = f_res.value().value(); + const auto &fork_header = f_res.value(); auto m_res = storage->getBlockHeader(main.hash); - if (m_res.has_error() or not m_res.value().has_value()) { + if (m_res.has_error()) { break; } - const auto &main_header = m_res.value().value(); + const auto &main_header = m_res.value(); BOOST_ASSERT(fork_header.number == main_header.number); if (fork_header.parent_hash == main_header.parent_hash) { @@ -230,19 +226,9 @@ namespace kagome::blockchain { return header_res.as_failure(); } - auto &header_opt = header_res.value(); - if (not header_opt.has_value()) { - SL_WARN(log, - "Can't get header of existing block {}: " - "not found in block storage", - block); - dead.insert(subchain.begin(), subchain.end()); - break; - } - observed.emplace(block.hash); - auto &header = header_opt.value(); + auto &header = header_res.value(); if (header.number < last_finalized_block_info.number) { SL_WARN( log, @@ -275,10 +261,8 @@ namespace kagome::blockchain { std::shared_ptr block_tree( new BlockTreeImpl(app_config, - std::move(header_repo), std::move(storage), last_finalized_block_info, - std::move(extrinsic_observer), std::move(hasher), std::move(chain_events_engine), std::move(extrinsic_events_engine), @@ -311,16 +295,14 @@ namespace kagome::blockchain { outcome::result BlockTreeImpl::recover( const primitives::BlockId &target_block_id, std::shared_ptr storage, - std::shared_ptr header_repo, std::shared_ptr trie_storage, std::shared_ptr block_tree) { BOOST_ASSERT(storage != nullptr); - BOOST_ASSERT(header_repo != nullptr); BOOST_ASSERT(trie_storage != nullptr); log::Logger log = log::createLogger("BlockTree", "block_tree"); - OUTCOME_TRY(block_tree_leaves, loadLeaves(storage, header_repo, log)); + OUTCOME_TRY(block_tree_leaves, loadLeaves(storage, log)); BOOST_ASSERT_MSG(not block_tree_leaves.empty(), "Must be known or calculated at least one leaf"); @@ -339,20 +321,15 @@ namespace kagome::blockchain { const auto &target_block_hash = target_block_hash_opt_res.value().value(); // Check if target block exists - auto target_block_header_opt_res = - storage->getBlockHeader(target_block_hash); - if (target_block_header_opt_res.has_error()) { + auto target_block_header_res = storage->getBlockHeader(target_block_hash); + if (target_block_header_res.has_error()) { SL_CRITICAL(log, "Can't get header of target block: {}", - target_block_header_opt_res.error()); - return target_block_header_opt_res.as_failure(); - } - const auto &target_block_header_opt = target_block_header_opt_res.value(); - if (not target_block_header_opt.has_value()) { - return BlockTreeError::HEADER_NOT_FOUND; + target_block_header_res.error()); + return target_block_header_res.as_failure(); } - const auto &target_block_header = target_block_header_opt.value(); + const auto &target_block_header = target_block_header_res.value(); const auto &state_root = target_block_header.state_root; // Check if target block has state @@ -371,19 +348,15 @@ namespace kagome::blockchain { break; } - auto header_opt_res = storage->getBlockHeader(block.hash); - if (header_opt_res.has_error()) { + auto header_res = storage->getBlockHeader(block.hash); + if (header_res.has_error()) { SL_CRITICAL(log, "Can't get header of one of removing block: {}", - header_opt_res.error()); - return header_opt_res.as_failure(); - } - const auto &header_opt = header_opt_res.value(); - if (not header_opt.has_value()) { - return BlockTreeError::HEADER_NOT_FOUND; + header_res.error()); + return header_res.as_failure(); } - const auto &header = header_opt.value(); + const auto &header = header_res.value(); block_tree_leaves.emplace(*header.parentInfo()); block_tree_leaves.erase(block); @@ -410,10 +383,8 @@ namespace kagome::blockchain { BlockTreeImpl::BlockTreeImpl( const application::AppConfiguration &app_config, - std::shared_ptr header_repo, std::shared_ptr storage, const primitives::BlockInfo &finalized, - std::shared_ptr extrinsic_observer, std::shared_ptr hasher, primitives::events::ChainSubscriptionEnginePtr chain_events_engine, primitives::events::ExtrinsicSubscriptionEnginePtr @@ -425,26 +396,22 @@ namespace kagome::blockchain { std::shared_ptr state_pruner, common::MainThreadPool &main_thread_pool) : block_tree_data_{BlockTreeData{ - .header_repo_ = std::move(header_repo), - .storage_ = std::move(storage), - .state_pruner_ = std::move(state_pruner), - .tree_ = std::make_unique(finalized), - .extrinsic_observer_ = std::move(extrinsic_observer), - .hasher_ = std::move(hasher), - .extrinsic_event_key_repo_ = std::move(extrinsic_event_key_repo), - .justification_storage_policy_ = - std::move(justification_storage_policy), - .genesis_block_hash_ = {}, - .blocks_pruning_ = {app_config.blocksPruning(), finalized.number}, - }}, + .storage_ = std::move(storage), + .state_pruner_ = std::move(state_pruner), + .tree_ = std::make_unique(finalized), + .hasher_ = std::move(hasher), + .extrinsic_event_key_repo_ = std::move(extrinsic_event_key_repo), + .justification_storage_policy_ = + std::move(justification_storage_policy), + .genesis_block_hash_ = {}, + .blocks_pruning_ = {app_config.blocksPruning(), finalized.number}, + }}, chain_events_engine_{std::move(chain_events_engine)}, main_pool_handler_{main_thread_pool.handlerStarted()}, extrinsic_events_engine_{std::move(extrinsic_events_engine)} { block_tree_data_.sharedAccess([&](const BlockTreeData &p) { - BOOST_ASSERT(p.header_repo_ != nullptr); BOOST_ASSERT(p.storage_ != nullptr); BOOST_ASSERT(p.tree_ != nullptr); - BOOST_ASSERT(p.extrinsic_observer_ != nullptr); BOOST_ASSERT(p.hasher_ != nullptr); BOOST_ASSERT(p.extrinsic_event_key_repo_ != nullptr); BOOST_ASSERT(p.justification_storage_policy_ != nullptr); @@ -493,7 +460,7 @@ namespace kagome::blockchain { return p.genesis_block_hash_.value(); } - auto res = p.header_repo_->getHashByNumber(0); + auto res = p.storage_->getBlockHash(0); BOOST_ASSERT_MSG( res.has_value(), "Block tree must contain at least genesis block"); @@ -501,7 +468,7 @@ namespace kagome::blockchain { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) const_cast &>( p.genesis_block_hash_) - .emplace(res.value()); + .emplace(res.value().value()); return p.genesis_block_hash_.value(); }) .get(); @@ -713,12 +680,7 @@ namespace kagome::blockchain { auto finalized = getLastFinalizedNoLock(p).number; for (auto hash = block_header.parent_hash;;) { - OUTCOME_TRY(header_opt, p.storage_->getBlockHeader(hash)); - if (not header_opt.has_value()) { - return BlockTreeError::NO_PARENT; - } - - auto &header = header_opt.value(); + OUTCOME_TRY(header, p.storage_->getBlockHeader(hash)); SL_TRACE(log_, "Block {} has found in storage and enqueued to add", primitives::BlockInfo(header.number, hash)); @@ -806,11 +768,7 @@ namespace kagome::blockchain { if (node) { SL_DEBUG(log_, "Finalizing block {}", node->info); - OUTCOME_TRY(header_opt, p.storage_->getBlockHeader(block_hash)); - if (not header_opt.has_value()) { - return BlockTreeError::HEADER_NOT_FOUND; - } - auto &header = header_opt.value(); + OUTCOME_TRY(header, p.storage_->getBlockHeader(block_hash)); OUTCOME_TRY(p.storage_->putJustification(justification, block_hash)); @@ -870,9 +828,8 @@ namespace kagome::blockchain { // we store justification for last finalized block only as long as it is // last finalized (if it doesn't meet other justification storage rules, // e.g. its number a multiple of 512) - OUTCOME_TRY( - last_finalized_header, - p.header_repo_->getBlockHeader(last_finalized_block_info.hash)); + OUTCOME_TRY(last_finalized_header, + p.storage_->getBlockHeader(last_finalized_block_info.hash)); OUTCOME_TRY( shouldStoreLastFinalized, p.justification_storage_policy_->shouldStoreFor( @@ -903,11 +860,12 @@ namespace kagome::blockchain { OUTCOME_TRY(p.storage_->removeBlockBody(*hash)); } } else { - OUTCOME_TRY(header, p.header_repo_->getBlockHeader(block_hash)); - if (header.number >= last_finalized_block_info.number) { + OUTCOME_TRY(header, p.storage_->getBlockHeader(block_hash)); + const auto header_number = header.number; + if (header_number >= last_finalized_block_info.number) { return BlockTreeError::NON_FINALIZED_BLOCK_NOT_FOUND; } - OUTCOME_TRY(canon_hash, p.header_repo_->getHashByNumber(header.number)); + OUTCOME_TRY(canon_hash, p.storage_->getBlockHash(header_number)); if (block_hash != canon_hash) { return BlockTreeError::BLOCK_ON_DEAD_END; } @@ -946,11 +904,7 @@ namespace kagome::blockchain { outcome::result BlockTreeImpl::getBlockHeaderNoLock( const BlockTreeData &p, const primitives::BlockHash &block_hash) const { - OUTCOME_TRY(header, p.storage_->getBlockHeader(block_hash)); - if (header.has_value()) { - return header.value(); - } - return BlockTreeError::HEADER_NOT_FOUND; + return p.storage_->getBlockHeader(block_hash); } outcome::result BlockTreeImpl::getBlockHeader( @@ -992,13 +946,13 @@ namespace kagome::blockchain { const primitives::BlockHash &block, uint64_t maximum) const { return block_tree_data_.sharedAccess([&](const BlockTreeData &p) -> BlockTree::BlockHashVecRes { - auto block_number_res = p.header_repo_->getNumberByHash(block); - if (block_number_res.has_error()) { + auto block_header_res = p.storage_->getBlockHeader(block); + if (block_header_res.has_error()) { log_->error( - "cannot retrieve block {}: {}", block, block_number_res.error()); + "cannot retrieve block {}: {}", block, block_header_res.error()); return BlockTreeError::HEADER_NOT_FOUND; } - auto start_block_number = block_number_res.value(); + auto start_block_number = block_header_res.value().number; if (maximum == 1) { return std::vector{block}; @@ -1017,14 +971,14 @@ namespace kagome::blockchain { start_block_number + count - 1; auto finish_block_hash_res = - p.header_repo_->getHashByNumber(finish_block_number); + p.storage_->getBlockHash(finish_block_number); if (finish_block_hash_res.has_error()) { log_->error("cannot retrieve block with number {}: {}", finish_block_number, finish_block_hash_res.error()); return BlockTreeError::HEADER_NOT_FOUND; } - const auto &finish_block_hash = finish_block_hash_res.value(); + const auto &finish_block_hash = finish_block_hash_res.value().value(); OUTCOME_TRY(chain, getDescendingChainToBlockNoLock(p, finish_block_hash, count)); @@ -1059,7 +1013,7 @@ namespace kagome::blockchain { } while (maximum > chain.size()) { - auto header_res = p.header_repo_->getBlockHeader(hash); + auto header_res = p.storage_->getBlockHeader(hash); if (header_res.has_error()) { if (chain.empty()) { log_->error("Cannot retrieve block with hash {}: {}", @@ -1092,8 +1046,10 @@ namespace kagome::blockchain { const primitives::BlockHash &descendant) const { return block_tree_data_.sharedAccess( [&](const BlockTreeData &p) -> BlockTreeImpl::BlockHashVecRes { - OUTCOME_TRY(from, p.header_repo_->getNumberByHash(ancestor)); - OUTCOME_TRY(to, p.header_repo_->getNumberByHash(descendant)); + OUTCOME_TRY(from_header, p.storage_->getBlockHeader(ancestor)); + auto from = from_header.number; + OUTCOME_TRY(to_header, p.storage_->getBlockHeader(descendant)); + auto to = to_header.number; if (to < from) { return BlockTreeError::TARGET_IS_PAST_MAX; } @@ -1136,20 +1092,20 @@ namespace kagome::blockchain { if (ancestor_node_ptr) { ancestor_depth = ancestor_node_ptr->info.number; } else { - auto number_res = p.header_repo_->getNumberByHash(ancestor); - if (!number_res) { + auto header_res = p.storage_->getBlockHeader(ancestor); + if (!header_res) { return false; } - ancestor_depth = number_res.value(); + ancestor_depth = header_res.value().number; } if (descendant_node_ptr) { descendant_depth = descendant_node_ptr->info.number; } else { - auto number_res = p.header_repo_->getNumberByHash(descendant); - if (!number_res) { + auto header_res = p.storage_->getBlockHeader(descendant); + if (!header_res) { return false; } - descendant_depth = number_res.value(); + descendant_depth = header_res.value().number; } if (descendant_depth < ancestor_depth) { SL_DEBUG(log_, @@ -1164,7 +1120,9 @@ namespace kagome::blockchain { auto finalized = [&](const primitives::BlockHash &hash, primitives::BlockNumber number) { return number <= getLastFinalizedNoLock(p).number - and p.header_repo_->getHashByNumber(number) == outcome::success(hash); + and p.storage_->getBlockHash(number) + == outcome::success( + std::optional(hash)); }; if (descendant_node_ptr or finalized(descendant, descendant_depth)) { return finalized(ancestor, ancestor_depth); @@ -1173,7 +1131,7 @@ namespace kagome::blockchain { auto current_hash = descendant; KAGOME_PROFILE_START(search_finalized_chain) while (current_hash != ancestor) { - auto current_header_res = p.header_repo_->getBlockHeader(current_hash); + auto current_header_res = p.storage_->getBlockHeader(current_hash); if (!current_header_res) { return false; } @@ -1197,8 +1155,9 @@ namespace kagome::blockchain { bool BlockTreeImpl::isFinalized(const primitives::BlockInfo &block) const { return block_tree_data_.sharedAccess([&](const BlockTreeData &p) { return block.number <= getLastFinalizedNoLock(p).number - and p.header_repo_->getHashByNumber(block.number) - == outcome::success(block.hash); + and p.storage_->getBlockHash(block.number) + == outcome::success( + std::optional(block.hash)); }); } @@ -1225,11 +1184,12 @@ namespace kagome::blockchain { // If target has not found in block tree (in memory), // it means block finalized or discarded if (not target) { - OUTCOME_TRY(target_number, - p.header_repo_->getNumberByHash(target_hash)); + OUTCOME_TRY(target_header, p.storage_->getBlockHeader(target_hash)); + auto target_number = target_header.number; - OUTCOME_TRY(canon_hash, - p.header_repo_->getHashByNumber(target_number)); + OUTCOME_TRY(canon_hash_res, + p.storage_->getBlockHash(target_number)); + auto canon_hash = canon_hash_res.value(); if (canon_hash != target_hash) { return BlockTreeError::BLOCK_ON_DEAD_END; @@ -1254,26 +1214,23 @@ namespace kagome::blockchain { BlockTreeImpl::BlockHashVecRes BlockTreeImpl::getChildren( const primitives::BlockHash &block) const { - return block_tree_data_.sharedAccess([&](const BlockTreeData &p) - -> BlockTreeImpl::BlockHashVecRes { - if (auto node = p.tree_->find(block); node != nullptr) { - std::vector result; - result.reserve(node->children.size()); - for (const auto &child : node->children) { - result.push_back(child->info.hash); - } - return result; - } - OUTCOME_TRY(header, p.storage_->getBlockHeader(block)); - if (!header.has_value()) { - return BlockTreeError::HEADER_NOT_FOUND; - } - // if node is not in tree_ it must be finalized and thus have only one - // child - OUTCOME_TRY(child_hash, - p.header_repo_->getHashByNumber(header.value().number + 1)); - return outcome::success(std::vector{child_hash}); - }); + return block_tree_data_.sharedAccess( + [&](const BlockTreeData &p) -> BlockTreeImpl::BlockHashVecRes { + if (auto node = p.tree_->find(block); node != nullptr) { + std::vector result; + result.reserve(node->children.size()); + for (const auto &child : node->children) { + result.push_back(child->info.hash); + } + return result; + } + OUTCOME_TRY(header, p.storage_->getBlockHeader(block)); + // if node is not in tree_ it must be finalized and thus have only one + // child + OUTCOME_TRY(child_hash, p.storage_->getBlockHash(header.number + 1)); + return outcome::success( + std::vector{child_hash.value()}); + }); } primitives::BlockInfo BlockTreeImpl::getLastFinalizedNoLock( @@ -1314,7 +1271,7 @@ namespace kagome::blockchain { // remove from storage retired_hashes.reserve(changes.prune.size()); for (const auto &block : changes.prune) { - OUTCOME_TRY(block_header_opt, p.storage_->getBlockHeader(block.hash)); + OUTCOME_TRY(block_header, p.storage_->getBlockHeader(block.hash)); OUTCOME_TRY(block_body_opt, p.storage_->getBlockBody(block.hash)); if (block_body_opt.has_value()) { extrinsics.reserve(extrinsics.size() + block_body_opt.value().size()); @@ -1334,8 +1291,7 @@ namespace kagome::blockchain { } extrinsics.emplace_back(std::move(ext)); } - BOOST_ASSERT(block_header_opt.has_value()); - OUTCOME_TRY(p.state_pruner_->pruneDiscarded(block_header_opt.value())); + OUTCOME_TRY(p.state_pruner_->pruneDiscarded(block_header)); } retired_hashes.emplace_back( primitives::events::RemoveAfterFinalizationParams::HeaderInfo{ @@ -1343,34 +1299,6 @@ namespace kagome::blockchain { OUTCOME_TRY(p.storage_->removeBlock(block.hash)); } - // trying to return extrinsics back to transaction pool - main_pool_handler_->execute( - [extrinsics{std::move(extrinsics)}, - wself{weak_from_this()}, - retired{primitives::events::RemoveAfterFinalizationParams{ - .removed = std::move(retired_hashes), - .finalized = getLastFinalizedNoLock(p).number}}]() mutable { - if (auto self = wself.lock()) { - auto eo = self->block_tree_data_.sharedAccess( - [&](const BlockTreeData &p) { return p.extrinsic_observer_; }); - - for (auto &&extrinsic : extrinsics) { - auto result = eo->onTxMessage(extrinsic); - if (result) { - SL_DEBUG( - self->log_, "Tx {} was reapplied", result.value().toHex()); - } else { - SL_DEBUG(self->log_, "Tx was skipped: {}", result.error()); - } - } - - self->chain_events_engine_->notify( - primitives::events::ChainEventType:: - kDeactivateAfterFinalization, - retired); - } - }); - return outcome::success(); } @@ -1441,6 +1369,22 @@ namespace kagome::blockchain { }); } + // BlockHeaderRepository methods + outcome::result BlockTreeImpl::getNumberByHash( + const primitives::BlockHash &hash) const { + OUTCOME_TRY(header, getBlockHeader(hash)); + return header.number; + } + + outcome::result BlockTreeImpl::getHashByNumber( + primitives::BlockNumber number) const { + OUTCOME_TRY(block_hash_opt, getBlockHash(number)); + if (block_hash_opt.has_value()) { + return block_hash_opt.value(); + } + return BlockTreeError::HEADER_NOT_FOUND; + } + BlockTreeImpl::BlocksPruning::BlocksPruning(std::optional keep, primitives::BlockNumber finalized) : keep_{keep}, next_{max(finalized)} {} diff --git a/core/blockchain/impl/block_tree_impl.hpp b/core/blockchain/impl/block_tree_impl.hpp index c2e2ee74a5..aed8384ad1 100644 --- a/core/blockchain/impl/block_tree_impl.hpp +++ b/core/blockchain/impl/block_tree_impl.hpp @@ -18,15 +18,14 @@ #include #include "application/app_configuration.hpp" -#include "blockchain/block_header_repository.hpp" #include "blockchain/block_storage.hpp" #include "blockchain/block_tree_error.hpp" +#include "blockchain/impl/cached_tree.hpp" #include "consensus/babe/types/babe_configuration.hpp" #include "consensus/timeline/types.hpp" #include "crypto/hasher.hpp" #include "log/logger.hpp" #include "metrics/metrics.hpp" -#include "network/extrinsic_observer.hpp" #include "primitives/event_types.hpp" #include "storage/trie/trie_storage.hpp" #include "subscription/extrinsic_event_key_repository.hpp" @@ -40,7 +39,6 @@ namespace kagome { namespace kagome::blockchain { struct ReorgAndPrune; class TreeNode; - class CachedTree; } // namespace kagome::blockchain namespace kagome::common { @@ -59,9 +57,7 @@ namespace kagome::blockchain { /// Create an instance of block tree static outcome::result> create( const application::AppConfiguration &app_config, - std::shared_ptr header_repo, std::shared_ptr storage, - std::shared_ptr extrinsic_observer, std::shared_ptr hasher, primitives::events::ChainSubscriptionEnginePtr chain_events_engine, primitives::events::ExtrinsicSubscriptionEnginePtr @@ -77,7 +73,6 @@ namespace kagome::blockchain { static outcome::result recover( const primitives::BlockId &target_block_id, std::shared_ptr storage, - std::shared_ptr header_repo, std::shared_ptr trie_storage, std::shared_ptr block_tree); @@ -158,6 +153,13 @@ namespace kagome::blockchain { void removeUnfinalized() override; + // BlockHeaderRepository methods + outcome::result getNumberByHash( + const primitives::BlockHash &block_hash) const override; + + outcome::result getHashByNumber( + primitives::BlockNumber block_number) const override; + private: struct BlocksPruning { BlocksPruning(std::optional keep, @@ -170,11 +172,9 @@ namespace kagome::blockchain { }; struct BlockTreeData { - std::shared_ptr header_repo_; std::shared_ptr storage_; std::shared_ptr state_pruner_; std::unique_ptr tree_; - std::shared_ptr extrinsic_observer_; std::shared_ptr hasher_; std::shared_ptr extrinsic_event_key_repo_; @@ -190,10 +190,8 @@ namespace kagome::blockchain { */ BlockTreeImpl( const application::AppConfiguration &app_config, - std::shared_ptr header_repo, std::shared_ptr storage, const primitives::BlockInfo &finalized, - std::shared_ptr extrinsic_observer, std::shared_ptr hasher, primitives::events::ChainSubscriptionEnginePtr chain_events_engine, primitives::events::ExtrinsicSubscriptionEnginePtr diff --git a/core/consensus/babe/impl/babe_config_repository_impl.cpp b/core/consensus/babe/impl/babe_config_repository_impl.cpp index 786753be9f..9abb36ed44 100644 --- a/core/consensus/babe/impl/babe_config_repository_impl.cpp +++ b/core/consensus/babe/impl/babe_config_repository_impl.cpp @@ -10,7 +10,6 @@ #include "application/app_state_manager.hpp" #include "babe.hpp" #include "babe_digests_util.hpp" -#include "blockchain/block_header_repository.hpp" #include "blockchain/block_tree.hpp" #include "consensus/consensus_selector.hpp" #include "consensus/timeline/slots_util.hpp" @@ -52,7 +51,6 @@ namespace kagome::consensus::babe { const application::AppConfiguration &app_config, EpochTimings &timings, std::shared_ptr block_tree, - std::shared_ptr header_repo, LazySPtr consensus_selector, std::shared_ptr babe_api, std::shared_ptr trie_storage, @@ -70,7 +68,6 @@ namespace kagome::consensus::babe { persistent_storage_), block_tree_, }, - header_repo_(std::move(header_repo)), consensus_selector_(consensus_selector), babe_api_(std::move(babe_api)), trie_storage_(std::move(trie_storage)), @@ -79,7 +76,6 @@ namespace kagome::consensus::babe { logger_(log::createLogger("BabeConfigRepo", "babe_config_repo")) { BOOST_ASSERT(persistent_storage_ != nullptr); BOOST_ASSERT(block_tree_ != nullptr); - BOOST_ASSERT(header_repo_ != nullptr); BOOST_ASSERT(babe_api_ != nullptr); SAFE_UNIQUE(indexer_) { diff --git a/core/consensus/babe/impl/babe_config_repository_impl.hpp b/core/consensus/babe/impl/babe_config_repository_impl.hpp index ec1d2bc1b9..95aad199b5 100644 --- a/core/consensus/babe/impl/babe_config_repository_impl.hpp +++ b/core/consensus/babe/impl/babe_config_repository_impl.hpp @@ -25,7 +25,6 @@ namespace kagome::application { namespace kagome::blockchain { class BlockTree; - class BlockHeaderRepository; } // namespace kagome::blockchain namespace kagome::consensus { @@ -80,7 +79,6 @@ namespace kagome::consensus::babe { const application::AppConfiguration &app_config, EpochTimings &timings, std::shared_ptr block_tree, - std::shared_ptr header_repo, LazySPtr consensus_selector, std::shared_ptr babe_api, std::shared_ptr trie_storage, @@ -127,7 +125,6 @@ namespace kagome::consensus::babe { EpochTimings &timings_; std::shared_ptr block_tree_; mutable SafeObject indexer_; - std::shared_ptr header_repo_; LazySPtr consensus_selector_; std::shared_ptr babe_api_; std::shared_ptr trie_storage_; diff --git a/core/consensus/grandpa/impl/environment_impl.cpp b/core/consensus/grandpa/impl/environment_impl.cpp index 3bb65b9bff..7674f0ea89 100644 --- a/core/consensus/grandpa/impl/environment_impl.cpp +++ b/core/consensus/grandpa/impl/environment_impl.cpp @@ -13,7 +13,6 @@ #include #include "application/app_state_manager.hpp" -#include "blockchain/block_header_repository.hpp" #include "blockchain/block_tree.hpp" #include "common/main_thread_pool.hpp" #include "consensus/grandpa/authority_manager.hpp" @@ -46,7 +45,6 @@ namespace kagome::consensus::grandpa { EnvironmentImpl::EnvironmentImpl( application::AppStateManager &app_state_manager, std::shared_ptr block_tree, - std::shared_ptr header_repository, std::shared_ptr authority_manager, std::shared_ptr transmitter, std::shared_ptr approved_ancestor, @@ -61,7 +59,6 @@ namespace kagome::consensus::grandpa { std::shared_ptr offchain_worker_pool, common::MainThreadPool &main_thread_pool) : block_tree_{std::move(block_tree)}, - header_repository_{std::move(header_repository)}, authority_manager_{std::move(authority_manager)}, transmitter_{std::move(transmitter)}, approved_ancestor_(std::move(approved_ancestor)), @@ -77,7 +74,6 @@ namespace kagome::consensus::grandpa { main_pool_handler_{main_thread_pool.handler(app_state_manager)}, logger_{log::createLogger("GrandpaEnvironment", "grandpa")} { BOOST_ASSERT(block_tree_ != nullptr); - BOOST_ASSERT(header_repository_ != nullptr); BOOST_ASSERT(authority_manager_ != nullptr); BOOST_ASSERT(transmitter_ != nullptr); BOOST_ASSERT(grandpa_api_ != nullptr); @@ -214,7 +210,7 @@ namespace kagome::consensus::grandpa { best_block = best_undisputed_block; auto block = best_block; while (block.number > finalized.number) { - OUTCOME_TRY(header, header_repository_->getBlockHeader(block.hash)); + OUTCOME_TRY(header, block_tree_->getBlockHeader(block.hash)); if (HasAuthoritySetChange{header}) { best_block = block; } @@ -225,7 +221,7 @@ namespace kagome::consensus::grandpa { if (voter_set_id.has_value()) { while (best_block.number > finalized.number) { OUTCOME_TRY(header, - header_repository_->getBlockHeader(best_block.hash)); + block_tree_->getBlockHeader(best_block.hash)); auto parent_block = *header.parentInfo(); auto voter_set = authority_manager_->authorities( diff --git a/core/consensus/grandpa/impl/environment_impl.hpp b/core/consensus/grandpa/impl/environment_impl.hpp index 20b2c731d2..2780fc6ddc 100644 --- a/core/consensus/grandpa/impl/environment_impl.hpp +++ b/core/consensus/grandpa/impl/environment_impl.hpp @@ -22,7 +22,6 @@ namespace kagome::application { } // namespace kagome::application namespace kagome::blockchain { - class BlockHeaderRepository; class BlockTree; } // namespace kagome::blockchain @@ -65,7 +64,6 @@ namespace kagome::consensus::grandpa { EnvironmentImpl( application::AppStateManager &app_state_manager, std::shared_ptr block_tree, - std::shared_ptr header_repository, std::shared_ptr authority_manager, std::shared_ptr transmitter, std::shared_ptr approved_ancestor, @@ -150,7 +148,6 @@ namespace kagome::consensus::grandpa { private: std::shared_ptr block_tree_; - std::shared_ptr header_repository_; std::shared_ptr authority_manager_; std::shared_ptr transmitter_; std::shared_ptr approved_ancestor_; diff --git a/core/dispute_coordinator/impl/dispute_coordinator_impl.cpp b/core/dispute_coordinator/impl/dispute_coordinator_impl.cpp index 9ce629399d..bc4e6c781f 100644 --- a/core/dispute_coordinator/impl/dispute_coordinator_impl.cpp +++ b/core/dispute_coordinator/impl/dispute_coordinator_impl.cpp @@ -17,7 +17,6 @@ #include "application/app_state_manager.hpp" #include "application/chain_spec.hpp" #include "authority_discovery/query/query.hpp" -#include "blockchain/block_header_repository.hpp" #include "common/main_thread_pool.hpp" #include "common/visitor.hpp" #include "consensus/timeline/timeline.hpp" @@ -139,8 +138,6 @@ namespace kagome::dispute { std::shared_ptr session_keys, std::shared_ptr storage, std::shared_ptr sr25519_crypto_provider, - std::shared_ptr - block_header_repository, std::shared_ptr hasher, std::shared_ptr block_tree, std::shared_ptr core_api, @@ -161,7 +158,6 @@ namespace kagome::dispute { session_keys_(std::move(session_keys)), storage_(std::move(storage)), sr25519_crypto_provider_(std::move(sr25519_crypto_provider)), - block_header_repository_(std::move(block_header_repository)), hasher_(std::move(hasher)), block_tree_(std::move(block_tree)), core_api_(std::move(core_api)), @@ -186,7 +182,6 @@ namespace kagome::dispute { BOOST_ASSERT(session_keys_ != nullptr); BOOST_ASSERT(storage_ != nullptr); BOOST_ASSERT(sr25519_crypto_provider_ != nullptr); - BOOST_ASSERT(block_header_repository_ != nullptr); BOOST_ASSERT(hasher_ != nullptr); BOOST_ASSERT(block_tree_ != nullptr); BOOST_ASSERT(core_api_ != nullptr); @@ -478,7 +473,7 @@ namespace kagome::dispute { } participation_ = - std::make_shared(block_header_repository_, + std::make_shared(block_tree_, hasher_, api_, runtime_info_, @@ -2202,7 +2197,7 @@ namespace kagome::dispute { // Update finality lag if possible if (not block_descriptions.empty()) { - if (auto number_res = block_header_repository_->getNumberByHash( + if (auto number_res = block_tree_->getNumberByHash( block_descriptions.back().block_hash); number_res.has_value()) { if (number_res.value() > undisputed_chain.number) { diff --git a/core/dispute_coordinator/impl/dispute_coordinator_impl.hpp b/core/dispute_coordinator/impl/dispute_coordinator_impl.hpp index 744a37b3c8..38743185f8 100644 --- a/core/dispute_coordinator/impl/dispute_coordinator_impl.hpp +++ b/core/dispute_coordinator/impl/dispute_coordinator_impl.hpp @@ -47,7 +47,6 @@ namespace kagome::authority_discovery { namespace kagome::blockchain { class BlockTree; - class BlockHeaderRepository; } // namespace kagome::blockchain namespace kagome::common { @@ -116,8 +115,6 @@ namespace kagome::dispute { std::shared_ptr session_keys, std::shared_ptr storage, std::shared_ptr sr25519_crypto_provider, - std::shared_ptr - block_header_repository, std::shared_ptr hasher, std::shared_ptr block_tree, std::shared_ptr core_api, @@ -286,7 +283,6 @@ namespace kagome::dispute { std::shared_ptr session_keys_; std::shared_ptr storage_; std::shared_ptr sr25519_crypto_provider_; - std::shared_ptr block_header_repository_; std::shared_ptr hasher_; std::shared_ptr block_tree_; std::shared_ptr core_api_; diff --git a/core/injector/CMakeLists.txt b/core/injector/CMakeLists.txt index ee180e700b..2efe21f1cd 100644 --- a/core/injector/CMakeLists.txt +++ b/core/injector/CMakeLists.txt @@ -84,5 +84,6 @@ target_link_libraries(application_injector vrf_provider waitable_timer wasm_compiler + zstd::libzstd_static ) kagome_clear_objects(application_injector) diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index 1ea840fb79..fc7cfb8596 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -56,7 +56,6 @@ #include "authorship/impl/block_builder_impl.hpp" #include "authorship/impl/proposer_impl.hpp" #include "benchmark/block_execution_benchmark.hpp" -#include "blockchain/impl/block_header_repository_impl.hpp" #include "blockchain/impl/block_storage_impl.hpp" #include "blockchain/impl/block_tree_impl.hpp" #include "blockchain/impl/justification_storage_policy.hpp" @@ -349,7 +348,7 @@ namespace { } template - sptr get_block_tree(const Injector &injector) { + sptr get_block_tree(const Injector &injector) { auto chain_events_engine = injector .template create(); @@ -357,9 +356,7 @@ namespace { // clang-format off auto block_tree_res = blockchain::BlockTreeImpl::create( injector.template create(), - injector.template create>(), injector.template create>(), - injector.template create>(), injector.template create>(), chain_events_engine, injector.template create(), @@ -374,12 +371,6 @@ namespace { } auto &block_tree = block_tree_res.value(); - auto runtime_upgrade_tracker = - injector.template create>(); - - runtime_upgrade_tracker->subscribeToBlockchainEvents(chain_events_engine, - block_tree); - return block_tree; } @@ -474,20 +465,17 @@ namespace { template std::shared_ptr get_runtime_upgrade_tracker(const Injector &injector) { - auto header_repo = - injector - .template create>(); auto storage = injector.template create>(); auto substitutes = injector .template create>(); - auto block_storage = - injector.template create>(); - auto res = - runtime::RuntimeUpgradeTrackerImpl::create(std::move(header_repo), - std::move(storage), - std::move(substitutes), - std::move(block_storage)); + auto block_tree = injector.template create>(); + auto res = runtime::RuntimeUpgradeTrackerImpl::create( + std::move(storage), std::move(substitutes), std::move(block_tree)); + auto chain_events_engine = + injector + .template create(); + res.value()->subscribeToBlockchainEvents(chain_events_engine); return std::shared_ptr( std::move(res.value())); } @@ -748,8 +736,10 @@ namespace { }), di::bind.template to(), bind_by_lambda( - [](const auto &injector) { return get_block_tree(injector); }), - di::bind.template to(), + [](const auto &injector) { + return get_block_tree(injector); + }), + di::bind.template to([](const auto &injector) { return injector.template create>(); }), di::bind.template to(), di::bind.template to(), di::bind.template to(), @@ -939,7 +929,7 @@ namespace kagome::injector { KagomeNodeInjector::KagomeNodeInjector( sptr app_config) : pimpl_{std::make_unique( - makeKagomeNodeInjector(std::move(app_config)))} {} + makeKagomeNodeInjector(std::move(app_config)))} {} sptr KagomeNodeInjector::injectAppConfig() { return pimpl_->injector_ diff --git a/core/runtime/common/runtime_upgrade_tracker_impl.cpp b/core/runtime/common/runtime_upgrade_tracker_impl.cpp index 02d13a97a1..f5f4dbb727 100644 --- a/core/runtime/common/runtime_upgrade_tracker_impl.cpp +++ b/core/runtime/common/runtime_upgrade_tracker_impl.cpp @@ -8,8 +8,6 @@ #include -#include "blockchain/block_header_repository.hpp" -#include "blockchain/block_storage.hpp" #include "blockchain/block_tree.hpp" #include "log/profiling_logger.hpp" #include "log/trace_macros.hpp" @@ -19,15 +17,13 @@ namespace kagome::runtime { outcome::result> RuntimeUpgradeTrackerImpl::create( - std::shared_ptr header_repo, std::shared_ptr storage, std::shared_ptr code_substitutes, - std::shared_ptr block_storage) { - BOOST_ASSERT(header_repo); + std::shared_ptr block_tree) { BOOST_ASSERT(storage); BOOST_ASSERT(code_substitutes); - BOOST_ASSERT(block_storage); + BOOST_ASSERT(block_tree); OUTCOME_TRY(encoded_opt, storage->getSpace(storage::Space::kDefault) @@ -41,25 +37,22 @@ namespace kagome::runtime { saved_data = std::move(decoded); } return std::unique_ptr{ - new RuntimeUpgradeTrackerImpl(std::move(header_repo), - std::move(storage), + new RuntimeUpgradeTrackerImpl(std::move(storage), std::move(code_substitutes), std::move(saved_data), - std::move(block_storage))}; + std::move(block_tree))}; } RuntimeUpgradeTrackerImpl::RuntimeUpgradeTrackerImpl( - std::shared_ptr header_repo, std::shared_ptr storage, std::shared_ptr code_substitutes, std::vector &&saved_data, - std::shared_ptr block_storage) + std::shared_ptr block_tree) : runtime_upgrades_{std::move(saved_data)}, - header_repo_{std::move(header_repo)}, storage_{storage->getSpace(storage::Space::kDefault)}, known_code_substitutes_{std::move(code_substitutes)}, - block_storage_{std::move(block_storage)}, + block_tree_{std::move(block_tree)}, logger_{log::createLogger("StorageCodeProvider", "runtime")} {} bool RuntimeUpgradeTrackerImpl::hasCodeSubstitute( @@ -72,23 +65,15 @@ namespace kagome::runtime { const primitives::BlockInfo &chain_end) const { // if the found state is finalized, it is guaranteed to not belong to a // different fork - primitives::BlockInfo last_finalized; - auto block_tree = block_tree_.lock(); - if (block_tree) { - last_finalized = block_tree->getLastFinalized(); // less expensive - } else { - OUTCOME_TRY(block_info, block_storage_->getLastFinalized()); - last_finalized = block_info; - } + primitives::BlockInfo last_finalized = block_tree_->getLastFinalized(); if (last_finalized.number >= state.number) { return true; } // a non-finalized state may belong to a different fork, need to check // explicitly (can be expensive if blocks are far apart) KAGOME_PROFILE_START(has_direct_chain) - BOOST_ASSERT(block_tree); bool has_direct_chain = - block_tree->hasDirectChain(state.hash, chain_end.hash); + block_tree_->hasDirectChain(state.hash, chain_end.hash); KAGOME_PROFILE_END(has_direct_chain) return has_direct_chain; } @@ -145,7 +130,7 @@ namespace kagome::runtime { if (latest_upgrade == runtime_upgrades_.begin()) { // if we have no info on updates before this block, we just return its // state - OUTCOME_TRY(block_header, header_repo_->getBlockHeader(block.hash)); + OUTCOME_TRY(block_header, block_tree_->getBlockHeader(block.hash)); SL_DEBUG( logger_, "Pick runtime state at block {} for the same block", block); return block_header.state_root; @@ -166,7 +151,7 @@ namespace kagome::runtime { } // if this is an orphan block for some reason, just return its state_root // (there is no other choice) - OUTCOME_TRY(block_header, header_repo_->getBlockHeader(block.hash)); + OUTCOME_TRY(block_header, block_tree_->getBlockHeader(block.hash)); logger_->warn("Block {}, a child of block {} is orphan", block, primitives::BlockInfo(block_header.number - 1, @@ -188,11 +173,7 @@ namespace kagome::runtime { void RuntimeUpgradeTrackerImpl::subscribeToBlockchainEvents( std::shared_ptr - chain_sub_engine, - std::shared_ptr block_tree) { - BOOST_ASSERT(block_tree != nullptr); - block_tree_ = block_tree; - + chain_sub_engine) { chain_subscription_ = primitives::events::subscribe( chain_sub_engine, primitives::events::ChainEventType::kNewRuntime, @@ -203,9 +184,9 @@ namespace kagome::runtime { .get(); auto res = push(block_hash); if (res.has_value() and res.value().second) { - auto header_res = header_repo_->getBlockHeader(block_hash); + auto header_res = block_tree_->getBlockHeader(block_hash); if (header_res.has_value()) { - auto &header = header_res.value(); + const auto &header = header_res.value(); primitives::BlockInfo block_info{header.number, block_hash}; SL_INFO(logger_, "Runtime upgrade at block {}", block_info); } @@ -215,7 +196,7 @@ namespace kagome::runtime { outcome::result> RuntimeUpgradeTrackerImpl::push(const primitives::BlockHash &hash) { - OUTCOME_TRY(header, header_repo_->getBlockHeader(hash)); + OUTCOME_TRY(header, block_tree_->getBlockHeader(hash)); primitives::BlockInfo block_info{header.number, hash}; bool is_new_upgrade = diff --git a/core/runtime/common/runtime_upgrade_tracker_impl.hpp b/core/runtime/common/runtime_upgrade_tracker_impl.hpp index c1c0a2d14a..557e0bc96f 100644 --- a/core/runtime/common/runtime_upgrade_tracker_impl.hpp +++ b/core/runtime/common/runtime_upgrade_tracker_impl.hpp @@ -19,9 +19,7 @@ #include "storage/trie/types.hpp" namespace kagome::blockchain { - class BlockHeaderRepository; class BlockTree; - class BlockStorage; } // namespace kagome::blockchain namespace kagome::runtime { @@ -33,11 +31,10 @@ namespace kagome::runtime { * which may fail, thus construction only from a factory method */ static outcome::result> create( - std::shared_ptr header_repo, std::shared_ptr storage, std::shared_ptr code_substitutes, - std::shared_ptr block_storage); + std::shared_ptr block_tree); struct RuntimeUpgradeData { SCALE_TIE(2); @@ -55,8 +52,7 @@ namespace kagome::runtime { void subscribeToBlockchainEvents( std::shared_ptr - chain_sub_engine, - std::shared_ptr block_tree); + chain_sub_engine); outcome::result getLastCodeUpdateState( const primitives::BlockInfo &block) override; @@ -66,12 +62,11 @@ namespace kagome::runtime { private: RuntimeUpgradeTrackerImpl( - std::shared_ptr header_repo, std::shared_ptr storage, std::shared_ptr code_substitutes, std::vector &&saved_data, - std::shared_ptr block_storage); + std::shared_ptr block_tree); outcome::result isStateInChain( const primitives::BlockInfo &state, @@ -103,12 +98,10 @@ namespace kagome::runtime { std::shared_ptr chain_subscription_; - std::weak_ptr block_tree_; - std::shared_ptr header_repo_; std::shared_ptr storage_; std::shared_ptr known_code_substitutes_; - std::shared_ptr block_storage_; + std::shared_ptr block_tree_; log::Logger logger_; }; diff --git a/core/utils/kagome_db_editor.cpp b/core/utils/kagome_db_editor.cpp index eb3ad29561..0d406f0b35 100644 --- a/core/utils/kagome_db_editor.cpp +++ b/core/utils/kagome_db_editor.cpp @@ -15,7 +15,6 @@ #include #include "blockchain/block_storage_error.hpp" -#include "blockchain/impl/block_header_repository_impl.hpp" #include "blockchain/impl/block_storage_impl.hpp" #include "blockchain/impl/block_tree_impl.hpp" #include "blockchain/impl/storage_util.hpp" @@ -268,7 +267,7 @@ int db_editor_main(int argc, const char **argv) { }), di::bind.to(factory), di::bind.template to(), - di::bind.template to(), + di::bind.template to(), di::bind.template to()); auto hasher = injector.template create>(); @@ -290,9 +289,8 @@ int db_editor_main(int argc, const char **argv) { primitives::BlockInfo best_leaf( std::numeric_limits::min(), {}); for (auto hash : block_tree_leaf_hashes) { - auto number = check(check(block_storage->getBlockHeader(hash)).value()) - .value() - .number; + auto number = + check(check(block_storage->getBlockHeader(hash))).value().number; const auto &leaf = *leafs.emplace(number, hash).first; SL_TRACE(log, "Leaf {} found", leaf); if (leaf.number <= least_leaf.number) { @@ -317,8 +315,7 @@ int db_editor_main(int argc, const char **argv) { auto &block = node.value(); auto header = - check(check(block_storage->getBlockHeader(block.hash)).value()) - .value(); + check(check(block_storage->getBlockHeader(block.hash))).value(); if (header.number == 0) { last_finalized_block = block; last_finalized_block_header = header; diff --git a/core/utils/storage_explorer.cpp b/core/utils/storage_explorer.cpp index c9941a7e02..e37e27d47b 100644 --- a/core/utils/storage_explorer.cpp +++ b/core/utils/storage_explorer.cpp @@ -203,14 +203,11 @@ class InspectBlockCommand : public Command { } const auto &hash = hash_opt_res.value().value(); - auto header_opt_res = block_storage->getBlockHeader(hash); - if (header_opt_res.has_error()) { - throwError("Internal error: {}}", header_opt_res.error()); + auto header_res = block_storage->getBlockHeader(hash); + if (header_res.has_error()) { + throwError("Internal error: {}}", header_res.error()); } - if (header_opt_res.value().has_value()) { - throwError("Block header not found for '{}'", args[1]); - } - const auto &header = header_opt_res.value().value(); + const auto &header = header_res.value(); std::cout << "#: " << header.number << "\n"; std::cout << "Parent hash: " << header.parent_hash.toHex() << "\n"; @@ -376,12 +373,8 @@ class SearchChainCommand : public Command { } const auto &hash = hash_opt_res.value().value(); - auto start_header_opt = unwrapResult("Getting 'start' block header", - block_storage->getBlockHeader(hash)); - if (!start_header_opt) { - throwError("Start block header {} not found", start); - } - auto &start_header = start_header_opt.value(); + auto start_header = unwrapResult("Getting 'start' block header", + block_storage->getBlockHeader(hash)); auto end_hash_opt = unwrapResult("Getting 'end' block header", block_storage->getBlockHash(end)); @@ -390,12 +383,8 @@ class SearchChainCommand : public Command { } const auto &end_hash = end_hash_opt.value(); - auto end_header_opt = unwrapResult("Getting 'end' block header", - block_storage->getBlockHeader(end_hash)); - if (!end_header_opt) { - throwError("'End block header {} not found", end); - } - auto &end_header = end_header_opt.value(); + auto end_header = unwrapResult("Getting 'end' block header", + block_storage->getBlockHeader(end_hash)); for (int64_t current = start_header.number, stop = end_header.number; current <= stop; @@ -408,13 +397,10 @@ class SearchChainCommand : public Command { } const auto ¤t_hash = current_hash_opt.value(); - auto current_header_opt = + auto current_header = unwrapResult(fmt::format("Getting header of block #{}", current), block_storage->getBlockHeader(current_hash)); - if (!current_header_opt) { - throwError("Block header #{} not found", current); - } - searchBlock(out, current_header_opt.value(), target); + searchBlock(out, current_header, target); } } diff --git a/test/core/api/service/chain/chain_api_test.cpp b/test/core/api/service/chain/chain_api_test.cpp index bb080bc47d..9801ff2dc0 100644 --- a/test/core/api/service/chain/chain_api_test.cpp +++ b/test/core/api/service/chain/chain_api_test.cpp @@ -11,7 +11,6 @@ #include "api/service/chain/requests/subscribe_finalized_heads.hpp" #include "mock/core/api/service/api_service_mock.hpp" #include "mock/core/api/service/chain/chain_api_mock.hpp" -#include "mock/core/blockchain/block_header_repository_mock.hpp" #include "mock/core/blockchain/block_storage_mock.hpp" #include "mock/core/blockchain/block_tree_mock.hpp" #include "primitives/block.hpp" @@ -28,7 +27,6 @@ using kagome::api::ChainApi; using kagome::api::ChainApiImpl; using kagome::api::ChainApiMock; using kagome::api::chain::request::SubscribeFinalizedHeads; -using kagome::blockchain::BlockHeaderRepositoryMock; using kagome::blockchain::BlockStorageMock; using kagome::blockchain::BlockTreeMock; using kagome::common::Buffer; @@ -44,13 +42,11 @@ using testing::Return; struct ChainApiTest : public ::testing::Test { void SetUp() override { - header_repo = std::make_shared(); block_tree = std::make_shared(); block_storage = std::make_shared(); api_service = std::make_shared(); api = std::make_shared( - header_repo, block_tree, block_storage, testutil::sptr_to_lazy(api_service)); @@ -62,7 +58,6 @@ struct ChainApiTest : public ::testing::Test { "0f82403bcd4f7d4d23ce04775d112cd5dede13633924de6cb048d2676e322950"_hash256; } - std::shared_ptr header_repo; std::shared_ptr block_tree; std::shared_ptr api_service; std::shared_ptr api; @@ -108,7 +103,7 @@ TEST_F(ChainApiTest, GetBlockHashNoParam) { */ TEST_F(ChainApiTest, GetBlockHashByNumber) { // kagome::primitives::BlockId did = "D"_hash256; - EXPECT_CALL(*header_repo, getHashByNumber(42)) + EXPECT_CALL(*block_tree, getHashByNumber(42)) .WillOnce(Return("CDE"_hash256)); EXPECT_OUTCOME_TRUE(r, api->getBlockHash(42)); @@ -121,7 +116,7 @@ TEST_F(ChainApiTest, GetBlockHashByNumber) { * @then the correct hash value is returned */ TEST_F(ChainApiTest, GetBlockHashByHexNumber) { - EXPECT_CALL(*header_repo, getHashByNumber(42)) + EXPECT_CALL(*block_tree, getHashByNumber(42)) .WillOnce(Return("CDE"_hash256)); EXPECT_OUTCOME_TRUE(r, api->getBlockHash("0x2a")); @@ -134,9 +129,9 @@ TEST_F(ChainApiTest, GetBlockHashByHexNumber) { * @then the correct vector of hash values is returned */ TEST_F(ChainApiTest, GetBlockHashArray) { - EXPECT_CALL(*header_repo, getHashByNumber(50)).WillOnce(Return(hash1)); - EXPECT_CALL(*header_repo, getHashByNumber(100)).WillOnce(Return(hash2)); - EXPECT_CALL(*header_repo, getHashByNumber(200)).WillOnce(Return(hash3)); + EXPECT_CALL(*block_tree, getHashByNumber(50)).WillOnce(Return(hash1)); + EXPECT_CALL(*block_tree, getHashByNumber(100)).WillOnce(Return(hash2)); + EXPECT_CALL(*block_tree, getHashByNumber(200)).WillOnce(Return(hash3)); std::vector> request_data = { 50, "0x64", 200}; EXPECT_OUTCOME_TRUE( @@ -153,7 +148,7 @@ TEST_F(ChainApiTest, GetBlockHashArray) { */ TEST_F(ChainApiTest, GetHeader) { BlockHash a = hash1; - EXPECT_CALL(*header_repo, getBlockHeader(a)).WillOnce(Return(*data.header)); + EXPECT_CALL(*block_tree, getBlockHeader(a)).WillOnce(Return(*data.header)); EXPECT_OUTCOME_TRUE(r, api->getHeader(std::string("0x") + hash1.toHex())); ASSERT_EQ(r, *data.header); @@ -169,7 +164,7 @@ TEST_F(ChainApiTest, GetHeaderLats) { EXPECT_CALL(*block_tree, getLastFinalized()) .WillOnce(Return(BlockInfo(42, hash1))); - EXPECT_CALL(*header_repo, getBlockHeader(a)).WillOnce(Return(*data.header)); + EXPECT_CALL(*block_tree, getBlockHeader(a)).WillOnce(Return(*data.header)); EXPECT_OUTCOME_TRUE(r, api->getHeader()); ASSERT_EQ(r, *data.header); diff --git a/test/core/api/service/child_state/child_state_api_test.cpp b/test/core/api/service/child_state/child_state_api_test.cpp index 400929040d..a6d598b3d4 100644 --- a/test/core/api/service/child_state/child_state_api_test.cpp +++ b/test/core/api/service/child_state/child_state_api_test.cpp @@ -9,7 +9,6 @@ #include "api/service/child_state/impl/child_state_api_impl.hpp" #include "core/storage/trie/polkadot_trie_cursor_dummy.hpp" #include "mock/core/api/service/child_state/child_state_api_mock.hpp" -#include "mock/core/blockchain/block_header_repository_mock.hpp" #include "mock/core/blockchain/block_tree_mock.hpp" #include "mock/core/runtime/core_mock.hpp" #include "mock/core/runtime/metadata_mock.hpp" @@ -22,7 +21,6 @@ #include "testutil/outcome.hpp" using kagome::api::ChildStateApiMock; -using kagome::blockchain::BlockHeaderRepositoryMock; using kagome::blockchain::BlockTreeMock; using kagome::common::Buffer; using kagome::primitives::BlockHash; @@ -44,14 +42,12 @@ namespace kagome::api { public: void SetUp() override { api_ = std::make_unique( - block_header_repo_, storage_, block_tree_, runtime_core_, metadata_); + storage_, block_tree_, runtime_core_, metadata_); } protected: std::shared_ptr storage_ = std::make_shared(); - std::shared_ptr block_header_repo_ = - std::make_shared(); std::shared_ptr block_tree_ = std::make_shared(); std::shared_ptr runtime_core_ = std::make_shared(); @@ -80,7 +76,7 @@ namespace kagome::api { EXPECT_CALL(*block_tree_, getLastFinalized()) .WillOnce(testing::Return(BlockInfo(42, "D"_hash256))); - EXPECT_CALL(*block_header_repo_, getBlockHeader("D"_hash256)) + EXPECT_CALL(*block_tree_, getBlockHeader("D"_hash256)) .WillOnce(testing::Return(makeBlockHeaderOfStateRoot("CDE"_hash256))); EXPECT_CALL(*storage_, getEphemeralBatchAt("CDE"_hash256)) .WillOnce(testing::Invoke([](auto &root) { @@ -107,7 +103,7 @@ namespace kagome::api { } TEST_F(ChildStateApiTest, GetStorageAt) { - EXPECT_CALL(*block_header_repo_, getBlockHeader("B"_hash256)) + EXPECT_CALL(*block_tree_, getBlockHeader("B"_hash256)) .WillOnce(testing::Return(makeBlockHeaderOfStateRoot("ABC"_hash256))); EXPECT_CALL(*storage_, getEphemeralBatchAt("ABC"_hash256)) .WillOnce(testing::Invoke([](auto &root) { @@ -151,7 +147,7 @@ namespace kagome::api { EXPECT_CALL(*block_tree_, getLastFinalized()) .WillOnce(Return(BlockInfo(10, block_hash))); - EXPECT_CALL(*block_header_repo_, getBlockHeader(block_hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_hash)) .WillOnce(Return(makeBlockHeaderOfStateRoot("6789"_hash256))); EXPECT_CALL(*storage_, getEphemeralBatchAt("6789"_hash256)) .WillOnce(testing::Invoke([&](auto &root) { @@ -206,7 +202,7 @@ namespace kagome::api { EXPECT_CALL(*block_tree_, getLastFinalized()) .WillOnce(Return(BlockInfo{10, block_hash})); - EXPECT_CALL(*block_header_repo_, getBlockHeader(block_hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_hash)) .WillOnce(Return(makeBlockHeaderOfStateRoot("6789"_hash256))); EXPECT_CALL(*storage_, getEphemeralBatchAt("6789"_hash256)) .WillOnce(testing::Invoke([&](auto &root) { @@ -258,7 +254,7 @@ namespace kagome::api { auto block_hash_opt = std::make_optional(block_hash); auto expected_result = "3030"_buf; - EXPECT_CALL(*block_header_repo_, getBlockHeader(block_hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_hash)) .WillOnce(Return(makeBlockHeaderOfStateRoot("6789"_hash256))); auto batch = std::make_unique(); EXPECT_CALL(*storage_, getEphemeralBatchAt("6789"_hash256)) diff --git a/test/core/api/service/state/state_api_test.cpp b/test/core/api/service/state/state_api_test.cpp index 1acb19b02d..68a29aed47 100644 --- a/test/core/api/service/state/state_api_test.cpp +++ b/test/core/api/service/state/state_api_test.cpp @@ -13,7 +13,6 @@ #include "core/storage/trie/polkadot_trie_cursor_dummy.hpp" #include "mock/core/api/service/api_service_mock.hpp" #include "mock/core/api/service/state/state_api_mock.hpp" -#include "mock/core/blockchain/block_header_repository_mock.hpp" #include "mock/core/blockchain/block_tree_mock.hpp" #include "mock/core/runtime/core_mock.hpp" #include "mock/core/runtime/metadata_mock.hpp" @@ -29,7 +28,6 @@ using kagome::api::ApiServiceMock; using kagome::api::StateApiMock; -using kagome::blockchain::BlockHeaderRepositoryMock; using kagome::blockchain::BlockTreeMock; using kagome::common::Buffer; using kagome::primitives::BlockHash; @@ -63,7 +61,6 @@ namespace kagome::api { executor_ = std::make_shared( std::make_shared()); api_ = std::make_unique( - block_header_repo_, storage_, block_tree_, runtime_core_, @@ -75,8 +72,6 @@ namespace kagome::api { protected: std::shared_ptr storage_ = std::make_shared(); - std::shared_ptr block_header_repo_ = - std::make_shared(); std::shared_ptr block_tree_ = std::make_shared(); std::shared_ptr runtime_core_ = std::make_shared(); @@ -96,7 +91,7 @@ namespace kagome::api { TEST_F(StateApiTest, GetStorage) { EXPECT_CALL(*block_tree_, getLastFinalized()) .WillOnce(testing::Return(BlockInfo(42, "D"_hash256))); - EXPECT_CALL(*block_header_repo_, getBlockHeader("D"_hash256)) + EXPECT_CALL(*block_tree_, getBlockHeader("D"_hash256)) .WillOnce(testing::Return(makeBlockHeaderOfStateRoot("CDE"_hash256))); auto in_buf = "a"_buf; auto out_buf = "1"_buf; @@ -112,7 +107,7 @@ namespace kagome::api { EXPECT_OUTCOME_TRUE(r, api_->getStorage(key.view())) ASSERT_EQ(r.value(), "1"_buf); - EXPECT_CALL(*block_header_repo_, getBlockHeader("B"_hash256)) + EXPECT_CALL(*block_tree_, getBlockHeader("B"_hash256)) .WillOnce(testing::Return(makeBlockHeaderOfStateRoot("ABC"_hash256))); EXPECT_OUTCOME_TRUE(r1, api_->getStorageAt(key.view(), "B"_hash256)); @@ -123,7 +118,6 @@ namespace kagome::api { public: void SetUp() override { auto storage = std::make_shared(); - block_header_repo_ = std::make_shared(); block_tree_ = std::make_shared(); api_service_ = std::make_shared(); @@ -133,7 +127,6 @@ namespace kagome::api { std::make_shared()); api_ = std::make_shared( - block_header_repo_, storage, block_tree_, runtime_core, @@ -144,7 +137,7 @@ namespace kagome::api { EXPECT_CALL(*block_tree_, getLastFinalized()) .WillOnce(testing::Return(BlockInfo(42, "D"_hash256))); - EXPECT_CALL(*block_header_repo_, getBlockHeader("D"_hash256)) + EXPECT_CALL(*block_tree_, getBlockHeader("D"_hash256)) .WillOnce(testing::Return(makeBlockHeaderOfStateRoot("CDE"_hash256))); EXPECT_CALL(*storage, getEphemeralBatchAt(_)) @@ -160,7 +153,6 @@ namespace kagome::api { } protected: - std::shared_ptr block_header_repo_; std::shared_ptr block_tree_; std::shared_ptr api_service_; @@ -387,9 +379,9 @@ namespace kagome::api { std::vector block_range{from, "block2"_hash256, "block3"_hash256, to}; EXPECT_CALL(*block_tree_, getChainByBlocks(from, to)) .WillOnce(testing::Return(block_range)); - EXPECT_CALL(*block_header_repo_, getNumberByHash(from)) + EXPECT_CALL(*block_tree_, getNumberByHash(from)) .WillOnce(testing::Return(1)); - EXPECT_CALL(*block_header_repo_, getNumberByHash(to)) + EXPECT_CALL(*block_tree_, getNumberByHash(to)) .WillOnce(testing::Return(4)); for (auto &block_hash : block_range) { primitives::BlockHash state_root; @@ -397,7 +389,7 @@ namespace kagome::api { std::copy_if(s.begin(), s.end(), state_root.begin(), [](auto b) { return b != 0; }); - EXPECT_CALL(*block_header_repo_, getBlockHeader(block_hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_hash)) .WillOnce(testing::Return(makeBlockHeaderOfStateRoot(state_root))); EXPECT_CALL(*storage_, getEphemeralBatchAt(state_root)) .WillOnce(testing::Invoke([&keys](auto &root) { @@ -431,9 +423,9 @@ namespace kagome::api { */ TEST_F(StateApiTest, HitsBlockRangeLimits) { primitives::BlockHash from{"from"_hash256}, to{"to"_hash256}; - EXPECT_CALL(*block_header_repo_, getNumberByHash(from)) + EXPECT_CALL(*block_tree_, getNumberByHash(from)) .WillOnce(Return(42)); - EXPECT_CALL(*block_header_repo_, getNumberByHash(to)) + EXPECT_CALL(*block_tree_, getNumberByHash(to)) .WillOnce(Return(42 + StateApiImpl::kMaxBlockRange + 1)); EXPECT_OUTCOME_FALSE( error, api_->queryStorage(std::vector({"some_key"_buf}), from, to)); @@ -467,7 +459,7 @@ namespace kagome::api { .WillOnce(testing::Return(block_range)); primitives::BlockHash state_root = "at_state"_hash256; - EXPECT_CALL(*block_header_repo_, getBlockHeader(at)) + EXPECT_CALL(*block_tree_, getBlockHeader(at)) .WillOnce(testing::Return(makeBlockHeaderOfStateRoot(state_root))); EXPECT_CALL(*storage_, getEphemeralBatchAt(state_root)) .WillOnce(testing::Invoke([&keys](auto &root) { diff --git a/test/core/blockchain/CMakeLists.txt b/test/core/blockchain/CMakeLists.txt index a35257b67a..c28190d6b6 100644 --- a/test/core/blockchain/CMakeLists.txt +++ b/test/core/blockchain/CMakeLists.txt @@ -4,15 +4,6 @@ # SPDX-License-Identifier: Apache-2.0 # -addtest(block_header_repository_test - block_header_repository_test.cpp - ) -target_link_libraries(block_header_repository_test - blockchain - base_rocksdb_test - hasher - ) - addtest(block_tree_test block_tree_test.cpp ) diff --git a/test/core/blockchain/block_header_repository_test.cpp b/test/core/blockchain/block_header_repository_test.cpp deleted file mode 100644 index 1eee6d2ca6..0000000000 --- a/test/core/blockchain/block_header_repository_test.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/** - * Copyright Quadrivium LLC - * All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "blockchain/block_header_repository.hpp" - -#include -#include - -#include - -#include "blockchain/impl/block_header_repository_impl.hpp" -#include "blockchain/impl/storage_util.hpp" -#include "crypto/hasher/hasher_impl.hpp" -#include "scale/scale.hpp" -#include "testutil/literals.hpp" -#include "testutil/outcome.hpp" -#include "testutil/prepare_loggers.hpp" -#include "testutil/storage/base_rocksdb_test.hpp" - -using kagome::blockchain::BlockHeaderRepository; -using kagome::blockchain::BlockHeaderRepositoryImpl; -using kagome::blockchain::putToSpace; -using kagome::common::Buffer; -using kagome::common::Hash256; -using kagome::primitives::BlockHeader; -using kagome::primitives::BlockInfo; -using kagome::primitives::BlockNumber; -using kagome::storage::Space; - -class BlockHeaderRepository_Test : public test::BaseRocksDB_Test { - public: - static void SetUpTestCase() { - testutil::prepareLoggers(); - } - - BlockHeaderRepository_Test() - : BaseRocksDB_Test(fs::path("/tmp/blockheaderrepotest.rcksdb")) {} - - void SetUp() override { - open(); - - hasher_ = std::make_shared(); - header_repo_ = std::make_shared(rocks_, hasher_); - } - - outcome::result storeHeader(BlockNumber num, BlockHeader h) { - BlockHeader header = std::move(h); - header.number = num; - OUTCOME_TRY(enc_header, scale::encode(header)); - auto hash = hasher_->blake2b_256(enc_header); - OUTCOME_TRY(putToSpace(*rocks_, Space::kHeader, hash, Buffer{enc_header})); - - auto num_to_hash_key = kagome::blockchain::blockNumberToKey(num); - auto key_space = rocks_->getSpace(Space::kLookupKey); - OUTCOME_TRY(key_space->put(num_to_hash_key, hash)); - - return hash; - } - - BlockHeader getDefaultHeader() { - BlockHeader h{}; - h.number = 42; - h.extrinsics_root = "DEADBEEF"_hash256; - h.parent_hash = "ABCDEF"_hash256; - h.state_root = "010203"_hash256; - return h; - } - - std::shared_ptr hasher_; - std::shared_ptr header_repo_; -}; - -class BlockHeaderRepository_NumberParametrized_Test - : public BlockHeaderRepository_Test, - public testing::WithParamInterface {}; - -const std::vector ParamValues = {1, 42, 12345, 0, 0xFFFFFFFF}; - -/** - * @given HeaderBackend instance with several headers in the storage - * @when accessing a header that wasn't put into storage - * @then result is error - */ -TEST_F(BlockHeaderRepository_Test, UnexistingHeader) { - auto chosen_number = ParamValues[0]; - for (auto &c : ParamValues) { - if (c != chosen_number) { - EXPECT_OUTCOME_TRUE_1(storeHeader(c, getDefaultHeader())) - } - } - BlockHeader not_in_storage = getDefaultHeader(); - not_in_storage.number = chosen_number; - EXPECT_OUTCOME_TRUE(enc_header, scale::encode(not_in_storage)) - auto hash = hasher_->blake2b_256(enc_header); - EXPECT_OUTCOME_FALSE_1(header_repo_->getBlockHeader(hash)) - EXPECT_OUTCOME_FALSE_1(header_repo_->getHashById(chosen_number)) - EXPECT_OUTCOME_FALSE_1(header_repo_->getNumberById(hash)) - - // doesn't require access to storage, as it basically returns its argument - EXPECT_OUTCOME_TRUE_1(header_repo_->getHashById(hash)) - EXPECT_OUTCOME_TRUE_1(header_repo_->getNumberById(chosen_number)) -} - -/** - * @given HeaderBackend instance - * @when learning block hash by its number through HeaderBackend - * @then resulting hash is equal to the original hash of the block for both - * retrieval through getHashByNumber and getHashById - */ -TEST_P(BlockHeaderRepository_NumberParametrized_Test, GetHashByNumber) { - EXPECT_OUTCOME_TRUE(hash, storeHeader(GetParam(), getDefaultHeader())) - EXPECT_OUTCOME_TRUE(maybe_hash, header_repo_->getHashByNumber(GetParam())) - ASSERT_THAT(hash, testing::ContainerEq(maybe_hash)); - EXPECT_OUTCOME_TRUE(maybe_another_hash, header_repo_->getHashById(GetParam())) - ASSERT_THAT(hash, testing::ContainerEq(maybe_another_hash)); -} - -/** - * @given HeaderBackend instance - * @when learning block number by its hash through HeaderBackend - * @then resulting number is equal to the original block number for both - * retrieval through getNumberByHash and getNumberById - */ -TEST_P(BlockHeaderRepository_NumberParametrized_Test, GetNumberByHash) { - EXPECT_OUTCOME_TRUE(hash, storeHeader(GetParam(), getDefaultHeader())) - EXPECT_OUTCOME_TRUE(maybe_number, header_repo_->getNumberByHash(hash)) - ASSERT_EQ(GetParam(), maybe_number); - EXPECT_OUTCOME_TRUE(maybe_another_number, - header_repo_->getNumberById(GetParam())) - ASSERT_EQ(GetParam(), maybe_another_number); -} - -/** - * @given HeaderBackend instance - * @when retrieving a block header by its id - * @then the same header that was put into the storage is returned, regardless - * of whether the id contained a number or a hash - */ -TEST_P(BlockHeaderRepository_NumberParametrized_Test, GetHeader) { - EXPECT_OUTCOME_TRUE(hash, storeHeader(GetParam(), getDefaultHeader())) - EXPECT_OUTCOME_TRUE(header_by_hash, header_repo_->getBlockHeader(hash)) - auto header_should_be = getDefaultHeader(); - header_should_be.number = GetParam(); - ASSERT_EQ(header_by_hash, header_should_be); -} - -INSTANTIATE_TEST_SUITE_P(Numbers, - BlockHeaderRepository_NumberParametrized_Test, - testing::ValuesIn(ParamValues)); diff --git a/test/core/blockchain/block_tree_test.cpp b/test/core/blockchain/block_tree_test.cpp index e58cc1c30c..befb57539f 100644 --- a/test/core/blockchain/block_tree_test.cpp +++ b/test/core/blockchain/block_tree_test.cpp @@ -3,7 +3,7 @@ * All Rights Reserved * SPDX-License-Identifier: Apache-2.0 */ - +#include #include #include "blockchain/impl/block_tree_impl.hpp" @@ -15,7 +15,6 @@ #include "crypto/hasher/hasher_impl.hpp" #include "mock/core/application/app_configuration_mock.hpp" #include "mock/core/application/app_state_manager_mock.hpp" -#include "mock/core/blockchain/block_header_repository_mock.hpp" #include "mock/core/blockchain/block_storage_mock.hpp" #include "mock/core/blockchain/justification_storage_policy.hpp" #include "mock/core/consensus/babe/babe_config_repository_mock.hpp" @@ -30,7 +29,6 @@ using namespace kagome; using application::AppConfigurationMock; using application::AppStateManagerMock; -using blockchain::BlockHeaderRepositoryMock; using blockchain::BlockStorageMock; using blockchain::BlockTreeError; using blockchain::BlockTreeImpl; @@ -114,12 +112,10 @@ struct BlockTreeTest : public testing::Test { return outcome::success(); })); - EXPECT_CALL(*header_repo_, getNumberByHash(kFinalizedBlockInfo.hash)) - .WillRepeatedly(Return(kFinalizedBlockInfo.number)); - - EXPECT_CALL(*header_repo_, getHashByNumber(_)) - .WillRepeatedly( - Invoke([&](const BlockNumber &n) -> outcome::result { + EXPECT_CALL(*storage_, getBlockHash(testing::Matcher(_))) + .WillRepeatedly(Invoke( + [&](BlockNumber n) + -> outcome::result> { auto it = num_to_hash_.find(n); if (it == num_to_hash_.end()) { return BlockTreeError::HEADER_NOT_FOUND; @@ -127,12 +123,10 @@ struct BlockTreeTest : public testing::Test { return it->second; })); - EXPECT_CALL(*header_repo_, + EXPECT_CALL(*storage_, getBlockHeader({finalized_block_header_.parent_hash})) .WillRepeatedly(Return(BlockTreeError::HEADER_NOT_FOUND)); - EXPECT_CALL(*header_repo_, getBlockHeader(kFinalizedBlockInfo.hash)) - .WillRepeatedly(Return(finalized_block_header_)); EXPECT_CALL(*storage_, getBlockHeader(kFinalizedBlockInfo.hash)) .WillRepeatedly(Return(finalized_block_header_)); @@ -170,9 +164,7 @@ struct BlockTreeTest : public testing::Test { std::make_shared(); block_tree_ = BlockTreeImpl::create(*app_config_, - header_repo_, storage_, - extrinsic_observer_, hasher_, chain_events_engine, ext_events_engine, @@ -237,8 +229,6 @@ struct BlockTreeTest : public testing::Test { // hash for header repo and number for block storage just because tests // currently require so - EXPECT_CALL(*header_repo_, getBlockHeader(hash)) - .WillRepeatedly(Return(header)); EXPECT_CALL(*storage_, getBlockHeader(hash)).WillRepeatedly(Return(header)); return {hash, header}; @@ -270,9 +260,6 @@ struct BlockTreeTest : public testing::Test { std::shared_ptr app_config_ = std::make_shared(); - std::shared_ptr header_repo_ = - std::make_shared(); - std::shared_ptr storage_ = std::make_shared(); @@ -703,10 +690,7 @@ TEST_F(BlockTreeTest, GetChainByBlockDescending) { new_block = Block{header, body}; auto hash2 = addBlock(new_block); - EXPECT_CALL(*header_repo_, getNumberByHash(kFinalizedBlockInfo.hash)) - .WillRepeatedly(Return(0)); - EXPECT_CALL(*header_repo_, getNumberByHash(hash2)).WillRepeatedly(Return(2)); - EXPECT_CALL(*header_repo_, getBlockHeader({kFinalizedBlockInfo.hash})) + EXPECT_CALL(*storage_, getBlockHeader({kFinalizedBlockInfo.hash})) .WillOnce(Return(BlockTreeError::HEADER_NOT_FOUND)); std::vector expected_chain{hash2, hash1}; @@ -719,21 +703,6 @@ TEST_F(BlockTreeTest, GetChainByBlockDescending) { ASSERT_EQ(chain, expected_chain); } -/** - * @given a block tree with one block in it - * @when trying to obtain the best chain that contais a block, which is - * present in the storage, but is not connected to the base block in the tree - * @then BLOCK_NOT_FOUND error is returned - */ -TEST_F(BlockTreeTest, GetBestChain_BlockNotFound) { - BlockInfo target(1337, "TargetBlock#1337"_hash256); - EXPECT_CALL(*header_repo_, getNumberByHash(target.hash)) - .WillRepeatedly(Return(BlockTreeError::EXISTING_BLOCK_NOT_FOUND)); - - ASSERT_OUTCOME_ERROR(block_tree_->getBestContaining(target.hash), - BlockTreeError::EXISTING_BLOCK_NOT_FOUND); -} - /** * @given a block tree with one block in it * @when trying to obtain the best chain that contais a block, which is @@ -743,9 +712,7 @@ TEST_F(BlockTreeTest, GetBestChain_BlockNotFound) { TEST_F(BlockTreeTest, GetBestChain_DiscardedBlock) { BlockInfo target = kFirstBlockInfo; BlockInfo other(kFirstBlockInfo.number, "OtherBlock#1"_hash256); - EXPECT_CALL(*header_repo_, getNumberByHash(target.hash)) - .WillRepeatedly(Return(target.number)); - EXPECT_CALL(*header_repo_, getHashByNumber(target.number)) + EXPECT_CALL(*storage_, getBlockHash(target.number)) .WillRepeatedly(Return(other.hash)); ASSERT_OUTCOME_ERROR(block_tree_->getBestContaining(target.hash), diff --git a/test/core/consensus/grandpa/chain_test.cpp b/test/core/consensus/grandpa/chain_test.cpp index 076fdd1481..b96ba2ccb3 100644 --- a/test/core/consensus/grandpa/chain_test.cpp +++ b/test/core/consensus/grandpa/chain_test.cpp @@ -12,7 +12,6 @@ #include "consensus/grandpa/impl/environment_impl.hpp" #include "consensus/grandpa/justification_observer.hpp" #include "mock/core/application/app_state_manager_mock.hpp" -#include "mock/core/blockchain/block_header_repository_mock.hpp" #include "mock/core/blockchain/block_tree_mock.hpp" #include "mock/core/consensus/grandpa/authority_manager_mock.hpp" #include "mock/core/consensus/grandpa/grandpa_mock.hpp" @@ -32,8 +31,6 @@ using kagome::Watchdog; using kagome::application::StartApp; -using kagome::blockchain::BlockHeaderRepository; -using kagome::blockchain::BlockHeaderRepositoryMock; using kagome::blockchain::BlockTree; using kagome::blockchain::BlockTreeMock; using kagome::common::Blob; @@ -80,7 +77,6 @@ class ChainTest : public testing::Test { chain = std::make_shared( app_state_manager, tree, - header_repo, authority_manager, grandpa_transmitter, approved_ancestor, @@ -119,7 +115,7 @@ class ChainTest : public testing::Test { BlockHeader hh; hh.number = number; hh.parent_hash = parent; - EXPECT_CALL(*header_repo, getBlockHeader(hash)) + EXPECT_CALL(*tree, getBlockHeader(hash)) .WillRepeatedly(Return(hh)); }; @@ -138,8 +134,6 @@ class ChainTest : public testing::Test { } std::shared_ptr tree = std::make_shared(); - std::shared_ptr header_repo = - std::make_shared(); std::shared_ptr authority_manager = std::make_shared(); std::shared_ptr grandpa_transmitter = diff --git a/test/core/runtime/binaryen/metadata_test.cpp b/test/core/runtime/binaryen/metadata_test.cpp index df3a09d588..3bf9cabde9 100644 --- a/test/core/runtime/binaryen/metadata_test.cpp +++ b/test/core/runtime/binaryen/metadata_test.cpp @@ -19,6 +19,7 @@ using ::testing::_; using ::testing::Return; +using kagome::blockchain::BlockTreeMock; using kagome::primitives::BlockHeader; using kagome::primitives::BlockId; using kagome::primitives::BlockInfo; @@ -39,7 +40,7 @@ class MetadataTest : public BinaryenRuntimeTest { prepareEphemeralStorageExpects(); api_ = std::make_shared( - executor_, header_repo_, runtime_upgrade_tracker_); + executor_, block_tree_, runtime_upgrade_tracker_); } protected: @@ -55,9 +56,9 @@ class MetadataTest : public BinaryenRuntimeTest { */ TEST_F(MetadataTest, metadata) { BlockInfo info{42, "block_hash"_hash256}; - EXPECT_CALL(*header_repo_, getBlockHeader(info.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(info.hash)) .WillRepeatedly(Return(BlockHeader{info.number, {}, {}, {}, {}})); - EXPECT_CALL(*header_repo_, getNumberByHash(info.hash)) + EXPECT_CALL(*block_tree_, getNumberByHash(info.hash)) .WillOnce(Return(info.number)); EXPECT_CALL(*runtime_upgrade_tracker_, getLastCodeUpdateState(info)) .WillOnce(Return(info.hash)); diff --git a/test/core/runtime/binaryen/runtime_external_interface_test.cpp b/test/core/runtime/binaryen/runtime_external_interface_test.cpp index 5c4ceb40fc..430035b2c5 100644 --- a/test/core/runtime/binaryen/runtime_external_interface_test.cpp +++ b/test/core/runtime/binaryen/runtime_external_interface_test.cpp @@ -10,7 +10,6 @@ #include #include "crypto/key_store/key_type.hpp" -#include "mock/core/blockchain/block_header_repository_mock.hpp" #include "mock/core/host_api/host_api_factory_mock.hpp" #include "mock/core/host_api/host_api_mock.hpp" #include "mock/core/runtime/binaryen_wasm_memory_factory_mock.hpp" @@ -27,7 +26,6 @@ using ::testing::_; using ::testing::Invoke; using ::testing::Return; -using kagome::blockchain::BlockHeaderRepositoryMock; using kagome::crypto::KeyType; using kagome::crypto::KeyTypes; using kagome::host_api::HostApi; @@ -88,7 +86,6 @@ class REITest : public ::testing::Test { core_api_factory_ = std::make_shared(); memory_provider_ = std::make_shared(); auto module_repo = std::make_shared(); - auto header_repo = std::make_shared(); } void executeWasm(std::string call_code) { diff --git a/test/core/runtime/executor_test.cpp b/test/core/runtime/executor_test.cpp index 06cdacae17..bf666e2a9a 100644 --- a/test/core/runtime/executor_test.cpp +++ b/test/core/runtime/executor_test.cpp @@ -9,7 +9,7 @@ #include #include "filesystem/common.hpp" -#include "mock/core/blockchain/block_header_repository_mock.hpp" +#include "mock/core/blockchain/block_tree_mock.hpp" #include "mock/core/host_api/host_api_mock.hpp" #include "mock/core/runtime/memory_provider_mock.hpp" #include "mock/core/runtime/module_instance_mock.hpp" @@ -25,8 +25,7 @@ #include "testutil/runtime/common/basic_code_provider.hpp" #include "testutil/runtime/memory.hpp" -using kagome::blockchain::BlockHeaderRepository; -using kagome::blockchain::BlockHeaderRepositoryMock; +using kagome::blockchain::BlockTreeMock; using kagome::common::Buffer; using kagome::host_api::HostApiMock; using kagome::runtime::BasicCodeProvider; @@ -55,7 +54,7 @@ class ExecutorTest : public testing::Test { } void SetUp() override { - header_repo_ = std::make_shared(); + block_tree_ = std::make_shared(); auto code_provider = std::make_shared( kagome::filesystem::path(__FILE__).parent_path().string() @@ -73,7 +72,7 @@ class ExecutorTest : public testing::Test { storage_ = std::make_shared(); ctx_factory_ = std::make_shared( - module_repo_, header_repo_); + module_repo_, block_tree_); } enum class CallType { Persistent, Ephemeral }; @@ -84,7 +83,7 @@ class ExecutorTest : public testing::Test { CallType type, const Buffer &encoded_args, int res) { - EXPECT_CALL(*header_repo_, getBlockHeader(blockchain_state.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(blockchain_state.hash)) .WillRepeatedly(Return(kagome::primitives::BlockHeader{ blockchain_state.number, // number {}, // parent @@ -139,9 +138,9 @@ class ExecutorTest : public testing::Test { protected: TestMemory memory_; + std::shared_ptr block_tree_; std::shared_ptr ctx_factory_; std::shared_ptr cache_; - std::shared_ptr header_repo_; std::shared_ptr storage_; std::shared_ptr module_repo_; }; diff --git a/test/core/runtime/runtime_test_base.hpp b/test/core/runtime/runtime_test_base.hpp index a2429c6221..8ec1dd7fb0 100644 --- a/test/core/runtime/runtime_test_base.hpp +++ b/test/core/runtime/runtime_test_base.hpp @@ -26,8 +26,7 @@ #include "host_api/impl/host_api_factory_impl.hpp" #include "mock/core/application/app_configuration_mock.hpp" #include "mock/core/application/app_state_manager_mock.hpp" -#include "mock/core/blockchain/block_header_repository_mock.hpp" -#include "mock/core/blockchain/block_storage_mock.hpp" +#include "mock/core/blockchain/block_tree_mock.hpp" #include "mock/core/offchain/offchain_persistent_storage_mock.hpp" #include "mock/core/offchain/offchain_worker_pool_mock.hpp" #include "mock/core/runtime/runtime_properties_cache_mock.hpp" @@ -136,12 +135,12 @@ class RuntimeTestBaseImpl { offchain_storage_, offchain_worker_pool_); - header_repo_ = std::make_shared< - testing::NiceMock>(); + block_tree_ = + std::make_shared >(); - ON_CALL(*header_repo_, getHashByNumber(0)) + ON_CALL(*block_tree_, getHashByNumber(0)) .WillByDefault(testing::Return("genesis_hash"_hash256)); - EXPECT_CALL(*header_repo_, getBlockHeader("genesis_hash"_hash256)) + EXPECT_CALL(*block_tree_, getBlockHeader("genesis_hash"_hash256)) .WillRepeatedly(testing::Return(primitives::BlockHeader{ 0, // number {}, // parent_hash @@ -179,10 +178,9 @@ class RuntimeTestBaseImpl { std::shared_ptr upgrade_tracker = runtime::RuntimeUpgradeTrackerImpl::create( - header_repo_, spaced_storage, std::make_shared(), - std::make_shared()) + block_tree_) .value(); auto wasm_cache_dir = @@ -197,14 +195,14 @@ class RuntimeTestBaseImpl { auto module_repo = std::make_shared(instance_pool_, hasher_, - header_repo_, + block_tree_, upgrade_tracker, trie_storage_, module_factory, wasm_provider_); ctx_factory_ = std::make_shared( - module_repo, header_repo_); + module_repo, block_tree_); executor_ = std::make_shared(ctx_factory_, cache_); } @@ -257,14 +255,14 @@ class RuntimeTestBaseImpl { Digest digest{}; - ON_CALL(*header_repo_, getHashByNumber(number)) + ON_CALL(*block_tree_, getHashByNumber(number)) .WillByDefault(testing::Return(hash)); - ON_CALL(*header_repo_, getNumberByHash(hash)) + ON_CALL(*block_tree_, getNumberByHash(hash)) .WillByDefault(testing::Return(number)); BlockHeader header{ number, parent_hash, state_root, extrinsics_root, digest}; - EXPECT_CALL(*header_repo_, getBlockHeader(hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(hash)) .WillRepeatedly(testing::Return(header)); return header; } @@ -280,8 +278,7 @@ class RuntimeTestBaseImpl { protected: AppConfigurationMock app_config_; - std::shared_ptr> - header_repo_; + std::shared_ptr> block_tree_; std::shared_ptr wasm_provider_; std::shared_ptr trie_storage_; std::shared_ptr serializer_; diff --git a/test/core/runtime/runtime_upgrade_tracker_test.cpp b/test/core/runtime/runtime_upgrade_tracker_test.cpp index 0800720d20..acc90fe7d8 100644 --- a/test/core/runtime/runtime_upgrade_tracker_test.cpp +++ b/test/core/runtime/runtime_upgrade_tracker_test.cpp @@ -8,8 +8,6 @@ #include -#include "mock/core/blockchain/block_header_repository_mock.hpp" -#include "mock/core/blockchain/block_storage_mock.hpp" #include "mock/core/blockchain/block_tree_mock.hpp" #include "mock/core/storage/spaced_storage_mock.hpp" #include "storage/in_memory/in_memory_storage.hpp" @@ -59,8 +57,6 @@ class RuntimeUpgradeTrackerTest : public testing::Test { } void SetUp() override { - header_repo_ = - std::make_shared(); block_tree_ = std::make_shared(); buffer_storage_ = std::make_shared(); storage_ = std::make_shared(); @@ -72,16 +68,13 @@ class RuntimeUpgradeTrackerTest : public testing::Test { std::make_shared(); sub_engine_ = std::make_shared(); - block_storage_ = std::make_shared(); - tracker_ = - kagome::runtime::RuntimeUpgradeTrackerImpl::create( - header_repo_, storage_, known_code_substitutes_, block_storage_) - .value(); + tracker_ = kagome::runtime::RuntimeUpgradeTrackerImpl::create( + storage_, known_code_substitutes_, block_tree_) + .value(); } protected: std::unique_ptr tracker_; - std::shared_ptr header_repo_; std::shared_ptr block_tree_; std::shared_ptr sub_engine_; @@ -90,7 +83,6 @@ class RuntimeUpgradeTrackerTest : public testing::Test { std::shared_ptr known_code_substitutes_{}; - std::shared_ptr block_storage_; kagome::primitives::BlockInfo genesis_block{0, "block_genesis_hash"_hash256}; kagome::primitives::BlockHeader genesis_block_header{ 0, // number @@ -113,7 +105,7 @@ class RuntimeUpgradeTrackerTest : public testing::Test { * THEN first encountered state is returned */ TEST_F(RuntimeUpgradeTrackerTest, NullBlockTree) { - EXPECT_CALL(*header_repo_, getBlockHeader({block_42.hash})) + EXPECT_CALL(*block_tree_, getBlockHeader({block_42.hash})) .WillOnce(testing::Return(block_42_header)); EXPECT_OUTCOME_TRUE(state, tracker_->getLastCodeUpdateState(block_42)); ASSERT_EQ(state, block_42_header.state_root); @@ -126,10 +118,11 @@ TEST_F(RuntimeUpgradeTrackerTest, NullBlockTree) { * returned */ TEST_F(RuntimeUpgradeTrackerTest, EmptyUpdatesCache) { - tracker_->subscribeToBlockchainEvents(sub_engine_, block_tree_); + tracker_->subscribeToBlockchainEvents(sub_engine_); - EXPECT_CALL(*header_repo_, getBlockHeader(block_42.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_42.hash)) .WillOnce(testing::Return(block_42_header)); + EXPECT_OUTCOME_TRUE(state, tracker_->getLastCodeUpdateState(block_42)); ASSERT_EQ(state, block_42_header.state_root); } @@ -140,9 +133,9 @@ TEST_F(RuntimeUpgradeTrackerTest, EmptyUpdatesCache) { * THEN genesis state is returned */ TEST_F(RuntimeUpgradeTrackerTest, AutoUpgradeAfterEmpty) { - tracker_->subscribeToBlockchainEvents(sub_engine_, block_tree_); + tracker_->subscribeToBlockchainEvents(sub_engine_); - EXPECT_CALL(*header_repo_, getBlockHeader(block_2.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_2.hash)) .WillRepeatedly(testing::Return(block_2_header)); EXPECT_OUTCOME_TRUE(state, tracker_->getLastCodeUpdateState(block_2)); ASSERT_EQ(state, block_2_header.state_root); @@ -155,12 +148,12 @@ TEST_F(RuntimeUpgradeTrackerTest, AutoUpgradeAfterEmpty) { } TEST_F(RuntimeUpgradeTrackerTest, CorrectUpgradeScenario) { - tracker_->subscribeToBlockchainEvents(sub_engine_, block_tree_); + tracker_->subscribeToBlockchainEvents(sub_engine_); EXPECT_CALL(*block_tree_, getLastFinalized()) .WillRepeatedly(testing::Return(makeBlockInfo(100500))); // first we execute block #1 - EXPECT_CALL(*header_repo_, getBlockHeader(genesis_block.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(genesis_block.hash)) .WillRepeatedly(testing::Return(genesis_block_header)); EXPECT_OUTCOME_TRUE(state1, tracker_->getLastCodeUpdateState(genesis_block)); @@ -169,9 +162,9 @@ TEST_F(RuntimeUpgradeTrackerTest, CorrectUpgradeScenario) { // then we upgrade in block #42 auto block_41_header = makeBlockHeader(41); auto block_41 = makeBlockInfo(41); - EXPECT_CALL(*header_repo_, getBlockHeader(genesis_block.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(genesis_block.hash)) .WillRepeatedly(testing::Return(genesis_block_header)); - EXPECT_CALL(*header_repo_, getBlockHeader(block_42.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_42.hash)) .WillRepeatedly(testing::Return(block_42_header)); EXPECT_OUTCOME_TRUE(state42, tracker_->getLastCodeUpdateState(block_41)); @@ -187,7 +180,7 @@ TEST_F(RuntimeUpgradeTrackerTest, CorrectUpgradeScenario) { EXPECT_CALL(*block_tree_, getChildren(block_41.hash)) .WillRepeatedly(testing::Return( std::vector{block_42.hash})); - EXPECT_CALL(*header_repo_, getBlockHeader(block_42.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_42.hash)) .WillRepeatedly(testing::Return(block_42_header)); EXPECT_OUTCOME_TRUE(state43, tracker_->getLastCodeUpdateState(block_42)); @@ -208,10 +201,10 @@ TEST_F(RuntimeUpgradeTrackerTest, CodeSubstituteAndStore) { EXPECT_CALL(*block_tree_, getLastFinalized()) .WillRepeatedly(testing::Return(makeBlockInfo(5203205))); - tracker_->subscribeToBlockchainEvents(sub_engine_, block_tree_); + tracker_->subscribeToBlockchainEvents(sub_engine_); auto block1 = makeBlockInfo(5200000); // took a block before code update!!! auto block1_header = makeBlockHeader(5200000); - EXPECT_CALL(*header_repo_, getBlockHeader(block1.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block1.hash)) .WillRepeatedly(testing::Return(block1_header)); sub_engine_->notify( kagome::primitives::events::ChainEventType::kNewRuntime, @@ -219,27 +212,25 @@ TEST_F(RuntimeUpgradeTrackerTest, CodeSubstituteAndStore) { auto block2 = makeBlockInfo(5203203); auto block2_header = makeBlockHeader(5203203); - EXPECT_CALL(*header_repo_, getBlockHeader(block2.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block2.hash)) .WillRepeatedly(testing::Return(block2_header)); known_code_substitutes_.reset( new kagome::primitives::CodeSubstituteBlockIds{{block2.number}}); // reset tracker - tracker_ = - kagome::runtime::RuntimeUpgradeTrackerImpl::create( - header_repo_, storage_, known_code_substitutes_, block_storage_) - .value(); - tracker_->subscribeToBlockchainEvents(sub_engine_, block_tree_); + tracker_ = kagome::runtime::RuntimeUpgradeTrackerImpl::create( + storage_, known_code_substitutes_, block_tree_) + .value(); + tracker_->subscribeToBlockchainEvents(sub_engine_); EXPECT_OUTCOME_TRUE(state2, tracker_->getLastCodeUpdateState(block2)); ASSERT_EQ(state2, block2_header.state_root); // reset tracker - tracker_ = - kagome::runtime::RuntimeUpgradeTrackerImpl::create( - header_repo_, storage_, known_code_substitutes_, block_storage_) - .value(); - tracker_->subscribeToBlockchainEvents(sub_engine_, block_tree_); + tracker_ = kagome::runtime::RuntimeUpgradeTrackerImpl::create( + storage_, known_code_substitutes_, block_tree_) + .value(); + tracker_->subscribeToBlockchainEvents(sub_engine_); auto block3 = makeBlockInfo(5203204); EXPECT_OUTCOME_TRUE(state3, tracker_->getLastCodeUpdateState(block3)); @@ -252,25 +243,24 @@ TEST_F(RuntimeUpgradeTrackerTest, UpgradeAfterCodeSubstitute) { EXPECT_CALL(*block_tree_, hasDirectChain(testing::_, testing::_)) .WillRepeatedly(testing::Return(true)); - tracker_ = - kagome::runtime::RuntimeUpgradeTrackerImpl::create( - header_repo_, storage_, known_code_substitutes_, block_storage_) - .value(); - tracker_->subscribeToBlockchainEvents(sub_engine_, block_tree_); + tracker_ = kagome::runtime::RuntimeUpgradeTrackerImpl::create( + storage_, known_code_substitutes_, block_tree_) + .value(); + tracker_->subscribeToBlockchainEvents(sub_engine_); auto block1 = makeBlockInfo(5203203); auto block1_header = makeBlockHeader(5203203); known_code_substitutes_.reset( new kagome::primitives::CodeSubstituteBlockIds({block1.number})); - EXPECT_CALL(*header_repo_, getBlockHeader(block1.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block1.hash)) .WillOnce(testing::Return(block1_header)); EXPECT_OUTCOME_TRUE_1(tracker_->getLastCodeUpdateState(block1)); // @see https://polkadot.subscan.io/event?module=system&event=codeupdated auto block2 = makeBlockInfo(5661442); auto block2_header = makeBlockHeader(5661442); - EXPECT_CALL(*header_repo_, getBlockHeader(block2.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block2.hash)) .WillRepeatedly(testing::Return(block2_header)); sub_engine_->notify( kagome::primitives::events::ChainEventType::kNewRuntime, @@ -285,7 +275,7 @@ TEST_F(RuntimeUpgradeTrackerTest, UpgradeAfterCodeSubstitute) { } TEST_F(RuntimeUpgradeTrackerTest, OrphanBlock) { - tracker_->subscribeToBlockchainEvents(sub_engine_, block_tree_); + tracker_->subscribeToBlockchainEvents(sub_engine_); // suppose we have two forks // / - 33f2 // 32 - 33f1 - 34f1 @@ -296,7 +286,7 @@ TEST_F(RuntimeUpgradeTrackerTest, OrphanBlock) { // and then we receive 34f2 with a runtime upgrade auto block_34f2 = makeBlockInfo(34, 2); auto block_34f2_header = makeBlockHeader(34, 2); - EXPECT_CALL(*header_repo_, getBlockHeader(block_34f2.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_34f2.hash)) .WillRepeatedly(testing::Return(block_34f2_header)); sub_engine_->notify( kagome::primitives::events::ChainEventType::kNewRuntime, @@ -305,7 +295,7 @@ TEST_F(RuntimeUpgradeTrackerTest, OrphanBlock) { // and then we receive 35f1 and query the latest runtime for it auto block_35f1 = makeBlockInfo(35, 1); auto block_35f1_header = makeBlockHeader(35, 1); - EXPECT_CALL(*header_repo_, getBlockHeader(block_35f1.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_35f1.hash)) .WillRepeatedly(testing::Return(block_35f1_header)); EXPECT_CALL(*block_tree_, hasDirectChain(block_34f2.hash, block_35f1.hash)) @@ -319,7 +309,7 @@ TEST_F(RuntimeUpgradeTrackerTest, OrphanBlock) { auto block_33f1 = makeBlockInfo(33, 1); auto block_33f1_header = makeBlockHeader(33, 1); - EXPECT_CALL(*header_repo_, getBlockHeader(block_33f1.hash)) + EXPECT_CALL(*block_tree_, getBlockHeader(block_33f1.hash)) .WillRepeatedly(testing::Return(block_33f1_header)); sub_engine_->notify( kagome::primitives::events::ChainEventType::kNewRuntime, diff --git a/test/core/runtime/wavm/core_integration_test.cpp b/test/core/runtime/wavm/core_integration_test.cpp index 3466b7afad..62ee19854e 100644 --- a/test/core/runtime/wavm/core_integration_test.cpp +++ b/test/core/runtime/wavm/core_integration_test.cpp @@ -44,7 +44,7 @@ class CoreTest : public ::testing::Test, public WavmRuntimeTest { SetUpImpl(); core_ = - std::make_shared(executor_, nullptr, header_repo_, nullptr); + std::make_shared(executor_, nullptr, block_tree_, nullptr); } protected: diff --git a/test/external-project-test/src/main.cpp b/test/external-project-test/src/main.cpp index c785d9af89..28f5626ed0 100644 --- a/test/external-project-test/src/main.cpp +++ b/test/external-project-test/src/main.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -122,7 +121,7 @@ int main() { std::shared_ptr runtime_upgrade_tracker = std::move(kagome::runtime::RuntimeUpgradeTrackerImpl::create( - header_repo, database, code_substitutes, block_storage) + header_repo, database, code_substitutes, std::make_shared()) .value()); auto storage_batch = diff --git a/test/mock/core/blockchain/block_header_repository_mock.hpp b/test/mock/core/blockchain/block_header_repository_mock.hpp index cf8c059aa4..f369e17d54 100644 --- a/test/mock/core/blockchain/block_header_repository_mock.hpp +++ b/test/mock/core/blockchain/block_header_repository_mock.hpp @@ -29,11 +29,6 @@ namespace kagome::blockchain { (const primitives::BlockHash &), (const, override)); - MOCK_METHOD(outcome::result, - getBlockStatus, - (const primitives::BlockHash &), - (const, override)); - MOCK_METHOD(outcome::result, getHashById, (const primitives::BlockId &), diff --git a/test/mock/core/blockchain/block_storage_mock.hpp b/test/mock/core/blockchain/block_storage_mock.hpp index 333216dce1..7031b13390 100644 --- a/test/mock/core/blockchain/block_storage_mock.hpp +++ b/test/mock/core/blockchain/block_storage_mock.hpp @@ -57,7 +57,7 @@ namespace kagome::blockchain { (const primitives::BlockHeader &), (override)); - MOCK_METHOD(outcome::result>, + MOCK_METHOD(outcome::result, getBlockHeader, (const primitives::BlockHash &), (const, override)); diff --git a/test/mock/core/blockchain/block_tree_mock.hpp b/test/mock/core/blockchain/block_tree_mock.hpp index 19a4b68c69..306818121d 100644 --- a/test/mock/core/blockchain/block_tree_mock.hpp +++ b/test/mock/core/blockchain/block_tree_mock.hpp @@ -123,6 +123,16 @@ namespace kagome::blockchain { (const primitives::BlockHash &), (const, override)); + MOCK_METHOD(outcome::result, + getHashByNumber, + (primitives::BlockNumber), + (const, override)); + + MOCK_METHOD(outcome::result, + getNumberByHash, + (const primitives::BlockHash &), + (const, override)); + MOCK_METHOD(primitives::BlockInfo, getLastFinalized, (), (const, override)); MOCK_METHOD(void, warp, (const primitives::BlockInfo &), (override));