Skip to content

Commit

Permalink
tryGetHeader for header_repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ErakhtinB committed Dec 11, 2024
1 parent 2c35681 commit 84c57ea
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 28 deletions.
8 changes: 8 additions & 0 deletions core/blockchain/block_header_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ namespace kagome::blockchain {
virtual outcome::result<primitives::BlockHeader> getBlockHeader(
const primitives::BlockHash &block_hash) const = 0;

/**
* @return block header with corresponding {@param block_hash} or a none
* optional if the corresponding block header is not in storage or a storage
* error
*/
virtual outcome::result<std::optional<primitives::BlockHeader>>
tryGetBlockHeader(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
17 changes: 17 additions & 0 deletions core/blockchain/impl/block_tree_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,23 @@ namespace kagome::blockchain {
});
}

outcome::result<std::optional<primitives::BlockHeader>>
BlockTreeImpl::tryGetBlockHeader(
const primitives::BlockHash &block_hash) const {
return block_tree_data_.sharedAccess(
[&](const BlockTreeData &p)
-> outcome::result<std::optional<primitives::BlockHeader>> {
if (auto header = getBlockHeaderNoLock(p, block_hash);
header.has_value()) {
return header.value();
} else if (header.error() == BlockTreeError::HEADER_NOT_FOUND) {
return std::nullopt;
} else {
return header.error();
}
});
}

outcome::result<primitives::BlockBody> BlockTreeImpl::getBlockBody(
const primitives::BlockHash &block_hash) const {
return block_tree_data_.sharedAccess(
Expand Down
3 changes: 3 additions & 0 deletions core/blockchain/impl/block_tree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ namespace kagome::blockchain {
outcome::result<primitives::BlockHeader> getBlockHeader(
const primitives::BlockHash &block_hash) const override;

outcome::result<std::optional<primitives::BlockHeader>> tryGetBlockHeader(
const primitives::BlockHash &block_hash) const override;

outcome::result<primitives::BlockBody> getBlockBody(
const primitives::BlockHash &block_hash) const override;

Expand Down
9 changes: 7 additions & 2 deletions core/consensus/babe/impl/babe_config_repository_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace kagome::consensus::babe {
primitives::events::ChainSubscriptionEnginePtr chain_events_engine,
LazySPtr<SlotsUtil> slots_util)
: persistent_storage_(
persistent_storage->getSpace(storage::Space::kDefault)),
persistent_storage->getSpace(storage::Space::kDefault)),
config_warp_sync_{app_config.syncMethod()
== application::SyncMethod::Warp},
timings_(timings),
Expand Down Expand Up @@ -103,7 +103,12 @@ namespace kagome::consensus::babe {
}

auto finalized = block_tree_->getLastFinalized();
auto finalized_header = block_tree_->getBlockHeader(finalized.hash).value();
auto block_header_res = block_tree_->getBlockHeader(finalized.hash);
if (not block_header_res) {
SL_ERROR(logger_, "Can't get block header: {}", block_header_res.error());
return false;
}
auto finalized_header = std::move(block_header_res.value());

SAFE_UNIQUE(indexer_) {
if (finalized.number - indexer_.last_finalized_indexed_.number
Expand Down
8 changes: 6 additions & 2 deletions core/consensus/timeline/impl/block_executor_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ namespace kagome::consensus {
previous_best_block]() mutable {
auto timer = metric_block_execution_time.manual();

auto parent =
block_tree_->getBlockHeader(block.header.parent_hash).value();
auto parent_res = block_tree_->getBlockHeader(block.header.parent_hash);
if (not parent_res) {
callback(parent_res.as_failure());
return;
}
auto parent = std::move(parent_res.value());

SL_DEBUG(logger_,
"Execute block {}, state {}, a child of block {}, state {}",
Expand Down
11 changes: 9 additions & 2 deletions core/consensus/timeline/impl/timeline_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,15 @@ namespace kagome::consensus {
state_sub_engine_->notify(
primitives::events::SyncStateEventType::kSyncState, current_state_);

auto best_block =
block_tree_->getBlockHeader(block_tree_->bestBlock().hash).value();
auto best_block_res = block_tree_->getBlockHeader(best_block_.hash);
if (not best_block_res) {
SL_ERROR(log_,
"Can't get header of best block ({}): {}",
best_block_,
best_block_res.error());
return;
}
auto best_block = std::move(best_block_res.value());
if (trie_storage_->getEphemeralBatchAt(best_block.state_root)) {
current_state_ = SyncState::CATCHING_UP;
return;
Expand Down
12 changes: 4 additions & 8 deletions core/parachain/validator/impl/parachain_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,15 +1421,11 @@ namespace kagome::parachain {
ParachainProcessorImpl::get_block_number_under_construction(
const RelayHash &relay_parent) const {
BOOST_ASSERT(main_pool_handler_->isInCurrentThread());

auto res_header = block_tree_->getBlockHeader(relay_parent);
if (res_header.has_error()) {
if (res_header.error() == blockchain::BlockTreeError::HEADER_NOT_FOUND) {
return 0;
}
return res_header.error();
OUTCOME_TRY(header, block_tree_->tryGetBlockHeader(relay_parent));
if (not header) {
return 0;
}
return res_header.value().number + 1;
return header->number + 1;
}

bool ParachainProcessorImpl::bitfields_indicate_availability(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,17 @@ namespace kagome::parachain {
ProspectiveParachains::fetchBlockInfo(const RelayHash &relay_hash) {
/// TODO(iceseer): do https://github.com/qdrvm/kagome/issues/1888
/// cache for block header request and calculations
auto res_header = block_tree_->getBlockHeader(relay_hash);
if (res_header.has_error()) {
if (res_header.error() == blockchain::BlockTreeError::HEADER_NOT_FOUND) {
return outcome::success(std::nullopt);
}
return res_header.error();

OUTCOME_TRY(header, block_tree_->tryGetBlockHeader(relay_hash));
if (not header) {
return outcome::success(std::nullopt);
}

return fragment::BlockInfoProspectiveParachains{
.hash = relay_hash,
.parent_hash = res_header.value().parent_hash,
.number = res_header.value().number,
.storage_root = res_header.value().state_root,
.parent_hash = header->parent_hash,
.number = header->number,
.storage_root = header->state_root,
};
}

Expand Down
7 changes: 2 additions & 5 deletions core/storage/migrations/migrations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ namespace kagome::storage::migrations {
while (!pending.empty()) {
primitives::BlockHash current = pending.front();
pending.pop_front();
auto header = block_tree.getBlockHeader(current);
if (!header) {
if (header.error() != blockchain::BlockTreeError::HEADER_NOT_FOUND) {
return header.error();
}
OUTCOME_TRY(header, block_tree.tryGetBlockHeader(current));
if (not header) {
continue;
}
OUTCOME_TRY(children, block_tree.getChildren(current));
Expand Down
5 changes: 5 additions & 0 deletions test/mock/core/blockchain/block_header_repository_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ namespace kagome::blockchain {
(const primitives::BlockHash &),
(const, override));

MOCK_METHOD(outcome::result<std::optional<primitives::BlockHeader>>,
tryGetBlockHeader,
(const primitives::BlockHash &),
(const, override));

MOCK_METHOD(outcome::result<primitives::BlockHash>,
getHashById,
(const primitives::BlockId &),
Expand Down
5 changes: 5 additions & 0 deletions test/mock/core/blockchain/block_tree_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ namespace kagome::blockchain {
(const primitives::BlockHash &),
(const, override));

MOCK_METHOD(outcome::result<std::optional<primitives::BlockHeader>>,
tryGetBlockHeader,
(const primitives::BlockHash &),
(const, override));

MOCK_METHOD(outcome::result<primitives::Justification>,
getBlockJustification,
(const primitives::BlockHash &),
Expand Down

0 comments on commit 84c57ea

Please sign in to comment.