Skip to content

Commit

Permalink
Merge branch 'feature/no-block-tree-impl' into feature/import_manifes…
Browse files Browse the repository at this point in the history
…t_tests_check_block
  • Loading branch information
kamilsa committed Oct 25, 2024
2 parents 54b8374 + b2c54ea commit a3fcf58
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 150 deletions.
1 change: 0 additions & 1 deletion core/blockchain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ add_library(blockchain
impl/block_storage_error.cpp
impl/justification_storage_policy.cpp
impl/block_storage_impl.cpp
impl/block_header_repository_impl.cpp
genesis_block_hash.cpp
)
target_link_libraries(blockchain
Expand Down
7 changes: 0 additions & 7 deletions core/blockchain/block_header_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ namespace kagome::blockchain {
virtual outcome::result<primitives::BlockHeader> 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<BlockStatus> 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
Expand Down
11 changes: 2 additions & 9 deletions core/blockchain/block_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <optional>
#include <vector>

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

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

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

/**
* Get a body (extrinsics) of the block (if present)
* @param block_hash hash of the block to get body for
Expand Down
66 changes: 0 additions & 66 deletions core/blockchain/impl/block_header_repository_impl.cpp

This file was deleted.

40 changes: 0 additions & 40 deletions core/blockchain/impl/block_header_repository_impl.hpp

This file was deleted.

55 changes: 37 additions & 18 deletions core/blockchain/impl/block_tree_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,11 +36,8 @@ namespace kagome::blockchain {
namespace {
/// Function-helper for loading (and repair if it needed) of leaves
outcome::result<std::set<primitives::BlockInfo>> loadLeaves(
const std::shared_ptr<BlockStorage> &storage,
const std::shared_ptr<BlockHeaderRepository> &header_repo,
const log::Logger &log) {
const std::shared_ptr<BlockStorage> &storage, const log::Logger &log) {
BOOST_ASSERT(storage != nullptr);
BOOST_ASSERT(header_repo != nullptr);

std::set<primitives::BlockInfo> block_tree_leaves;
{
Expand All @@ -49,16 +47,23 @@ 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
auto header_opt_res = storage->getBlockHeader(hash);
if (header_opt_res.has_error()) {
if (header_opt_res
== 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_opt_res.error());
return header_opt_res.as_failure();
}
auto number = res.value();
if (not header_opt_res.value().has_value()) {
SL_TRACE(log, "Leaf {} not found", hash);
continue;
}
auto number = header_opt_res.value()->number;
SL_TRACE(log, "Leaf {} found", primitives::BlockInfo(number, hash));
block_tree_leaves.emplace(number, hash);
}
Expand Down Expand Up @@ -95,7 +100,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()) {
Expand All @@ -114,7 +120,6 @@ namespace kagome::blockchain {

outcome::result<std::shared_ptr<BlockTreeImpl>> BlockTreeImpl::create(
const application::AppConfiguration &app_config,
std::shared_ptr<BlockHeaderRepository> header_repo,
std::shared_ptr<BlockStorage> storage,
std::shared_ptr<network::ExtrinsicObserver> extrinsic_observer,
std::shared_ptr<crypto::Hasher> hasher,
Expand All @@ -128,7 +133,6 @@ namespace kagome::blockchain {
std::shared_ptr<storage::trie_pruner::TriePruner> state_pruner,
common::MainThreadPool &main_thread_pool) {
BOOST_ASSERT(storage != nullptr);
BOOST_ASSERT(header_repo != nullptr);

log::Logger log = log::createLogger("BlockTree", "block_tree");

Expand All @@ -147,7 +151,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");

Expand Down Expand Up @@ -275,7 +279,6 @@ namespace kagome::blockchain {

std::shared_ptr<BlockTreeImpl> block_tree(
new BlockTreeImpl(app_config,
std::move(header_repo),
std::move(storage),
last_finalized_block_info,
std::move(extrinsic_observer),
Expand Down Expand Up @@ -320,7 +323,7 @@ namespace kagome::blockchain {

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");
Expand Down Expand Up @@ -410,7 +413,6 @@ namespace kagome::blockchain {

BlockTreeImpl::BlockTreeImpl(
const application::AppConfiguration &app_config,
std::shared_ptr<BlockHeaderRepository> header_repo,
std::shared_ptr<BlockStorage> storage,
const primitives::BlockInfo &finalized,
std::shared_ptr<network::ExtrinsicObserver> extrinsic_observer,
Expand All @@ -425,7 +427,7 @@ namespace kagome::blockchain {
std::shared_ptr<storage::trie_pruner::TriePruner> state_pruner,
common::MainThreadPool &main_thread_pool)
: block_tree_data_{BlockTreeData{
.header_repo_ = std::move(header_repo),
.header_repo_ = nullptr, // Initialize with BlockTreeImpl in body
.storage_ = std::move(storage),
.state_pruner_ = std::move(state_pruner),
.tree_ = std::make_unique<CachedTree>(finalized),
Expand All @@ -441,6 +443,7 @@ namespace kagome::blockchain {
main_pool_handler_{main_thread_pool.handlerStarted()},
extrinsic_events_engine_{std::move(extrinsic_events_engine)} {
block_tree_data_.sharedAccess([&](const BlockTreeData &p) {
p.header_repo_ = shared_from_this();
BOOST_ASSERT(p.header_repo_ != nullptr);
BOOST_ASSERT(p.storage_ != nullptr);
BOOST_ASSERT(p.tree_ != nullptr);
Expand Down Expand Up @@ -1441,6 +1444,22 @@ namespace kagome::blockchain {
});
}

// BlockHeaderRepository methods
outcome::result<primitives::BlockNumber> BlockTreeImpl::getNumberByHash(
const primitives::BlockHash &hash) const {
OUTCOME_TRY(header, getBlockHeader(hash));
return header.number;
}

outcome::result<primitives::BlockHash> 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<uint32_t> keep,
primitives::BlockNumber finalized)
: keep_{keep}, next_{max(finalized)} {}
Expand Down
13 changes: 9 additions & 4 deletions core/blockchain/impl/block_tree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#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"
Expand All @@ -40,7 +41,6 @@ namespace kagome {
namespace kagome::blockchain {
struct ReorgAndPrune;
class TreeNode;
class CachedTree;
} // namespace kagome::blockchain

namespace kagome::common {
Expand All @@ -59,7 +59,6 @@ namespace kagome::blockchain {
/// Create an instance of block tree
static outcome::result<std::shared_ptr<BlockTreeImpl>> create(
const application::AppConfiguration &app_config,
std::shared_ptr<BlockHeaderRepository> header_repo,
std::shared_ptr<BlockStorage> storage,
std::shared_ptr<network::ExtrinsicObserver> extrinsic_observer,
std::shared_ptr<crypto::Hasher> hasher,
Expand Down Expand Up @@ -158,6 +157,13 @@ namespace kagome::blockchain {

void removeUnfinalized() override;

// BlockHeaderRepository methods
outcome::result<primitives::BlockNumber> getNumberByHash(
const primitives::BlockHash &block_hash) const override;

outcome::result<primitives::BlockHash> getHashByNumber(
primitives::BlockNumber block_number) const override;

private:
struct BlocksPruning {
BlocksPruning(std::optional<uint32_t> keep,
Expand All @@ -170,7 +176,7 @@ namespace kagome::blockchain {
};

struct BlockTreeData {
std::shared_ptr<BlockHeaderRepository> header_repo_;
mutable std::shared_ptr<BlockHeaderRepository> header_repo_;
std::shared_ptr<BlockStorage> storage_;
std::shared_ptr<storage::trie_pruner::TriePruner> state_pruner_;
std::unique_ptr<CachedTree> tree_;
Expand All @@ -190,7 +196,6 @@ namespace kagome::blockchain {
*/
BlockTreeImpl(
const application::AppConfiguration &app_config,
std::shared_ptr<BlockHeaderRepository> header_repo,
std::shared_ptr<BlockStorage> storage,
const primitives::BlockInfo &finalized,
std::shared_ptr<network::ExtrinsicObserver> extrinsic_observer,
Expand Down
Loading

0 comments on commit a3fcf58

Please sign in to comment.