Skip to content

Commit

Permalink
tryGetBlockHeader for BlockStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
ErakhtinB committed Dec 5, 2024
1 parent ad8b83b commit 18554b8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
13 changes: 11 additions & 2 deletions core/blockchain/block_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,17 @@ namespace kagome::blockchain {
* Tries to get block header by {@param block_hash}
* @returns block header or error
*/
virtual outcome::result<primitives::BlockHeader>
getBlockHeader(const primitives::BlockHash &block_hash) const = 0;
virtual outcome::result<primitives::BlockHeader> getBlockHeader(
const primitives::BlockHash &block_hash) const = 0;

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

// -- body --

Expand Down
37 changes: 26 additions & 11 deletions core/blockchain/impl/block_storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,19 @@ namespace kagome::blockchain {

outcome::result<primitives::BlockHeader> BlockStorageImpl::getBlockHeader(
const primitives::BlockHash &block_hash) const {
OUTCOME_TRY(encoded_header_opt,
getFromSpace(*storage_, Space::kHeader, block_hash));
if (encoded_header_opt.has_value()) {
OUTCOME_TRY(
header,
scale::decode<primitives::BlockHeader>(encoded_header_opt.value()));
header.hash_opt.emplace(block_hash);
return header;
OUTCOME_TRY(header_opt, fetchBlockHeader(block_hash));
if (header_opt.has_value()) {
return header_opt.value();
}
return BlockStorageError::HEADER_NOT_FOUND;
}

outcome::result<std::optional<primitives::BlockHeader>>
BlockStorageImpl::tryGetBlockHeader(
const primitives::BlockHash &block_hash) const {
return fetchBlockHeader(block_hash);
}

outcome::result<void> BlockStorageImpl::putBlockBody(
const primitives::BlockHash &block_hash,
const primitives::BlockBody &block_body) {
Expand Down Expand Up @@ -320,11 +321,11 @@ namespace kagome::blockchain {
outcome::result<void> BlockStorageImpl::removeBlock(
const primitives::BlockHash &block_hash) {
// Check if block still in storage
const auto header_res = getBlockHeader(block_hash);
if (not header_res) {
OUTCOME_TRY(header_opt, fetchBlockHeader(block_hash));
if (not header_opt) {
return outcome::success();
}
const auto &header = header_res.value();
const auto &header = header_opt.value();

primitives::BlockInfo block_info(header.number, block_hash);

Expand Down Expand Up @@ -385,4 +386,18 @@ namespace kagome::blockchain {
return outcome::success();
}

outcome::result<std::optional<primitives::BlockHeader>>
BlockStorageImpl::fetchBlockHeader(
const primitives::BlockHash &block_hash) const {
OUTCOME_TRY(encoded_header_opt,
getFromSpace(*storage_, Space::kHeader, block_hash));
if (encoded_header_opt.has_value()) {
OUTCOME_TRY(
header,
scale::decode<primitives::BlockHeader>(encoded_header_opt.value()));
header.hash_opt.emplace(block_hash);
return std::make_optional(std::move(header));
}
return std::nullopt;
}
} // namespace kagome::blockchain
6 changes: 6 additions & 0 deletions core/blockchain/impl/block_storage_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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;

// -- body --

outcome::result<void> putBlockBody(
Expand Down Expand Up @@ -102,6 +105,9 @@ namespace kagome::blockchain {
BlockStorageImpl(std::shared_ptr<storage::SpacedStorage> storage,
std::shared_ptr<crypto::Hasher> hasher);

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

std::shared_ptr<storage::SpacedStorage> storage_;
std::shared_ptr<crypto::Hasher> hasher_;

Expand Down
30 changes: 30 additions & 0 deletions test/core/blockchain/block_storage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,36 @@ TEST_F(BlockStorageTest, PutBlock) {
ASSERT_OUTCOME_SUCCESS_TRY(block_storage->putBlock(block));
}

/*
* @given a block storage and a block that is not in storage yet
* @when trying to get a block from the storage
* @then an error is returned
*/
TEST_F(BlockStorageTest, GetBlockNotFound) {
ASSERT_OUTCOME_SUCCESS(
block_storage,
BlockStorageImpl::create(root_hash, spaced_storage, hasher));

EXPECT_OUTCOME_ERROR(get_res,
block_storage->getBlockHeader(genesis_block_hash),
BlockStorageError::HEADER_NOT_FOUND);
}

/*
* @given a block storage and a block that is not in storage yet
* @when trying to get a block from the storage
* @then success value containing nullopt is returned
*/
TEST_F(BlockStorageTest, TryGetBlockNotFound) {
ASSERT_OUTCOME_SUCCESS(
block_storage,
BlockStorageImpl::create(root_hash, spaced_storage, hasher));

ASSERT_OUTCOME_SUCCESS(try_get_res,
block_storage->tryGetBlockHeader(genesis_block_hash));
ASSERT_FALSE(try_get_res.has_value());
}

/**
* @given a block storage and a block that is not in storage yet
* @when putting a block in the storage and underlying storage throws an
Expand Down
5 changes: 5 additions & 0 deletions test/mock/core/blockchain/block_storage_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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<void>,
putBlockBody,
(const primitives::BlockHash &, const primitives::BlockBody &),
Expand Down

0 comments on commit 18554b8

Please sign in to comment.