Kagome
Polkadot Runtime Engine in C++17
block_header_repository_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <string_view>
9 
10 #include <optional>
11 
14 #include "common/hexutil.hpp"
15 #include "scale/scale.hpp"
16 
21 
22 namespace kagome::blockchain {
23 
25  std::shared_ptr<storage::BufferStorage> map,
26  std::shared_ptr<crypto::Hasher> hasher)
27  : map_{std::move(map)}, hasher_{std::move(hasher)} {
28  BOOST_ASSERT(hasher_);
29  }
30 
31  outcome::result<BlockNumber> BlockHeaderRepositoryImpl::getNumberByHash(
32  const Hash256 &hash) const {
33  OUTCOME_TRY(key, idToLookupKey(*map_, hash));
34  if (!key.has_value()) return BlockTreeError::HEADER_NOT_FOUND;
35  auto maybe_number = lookupKeyToNumber(key.value());
36 
37  return maybe_number;
38  }
39 
40  outcome::result<common::Hash256> BlockHeaderRepositoryImpl::getHashByNumber(
41  const primitives::BlockNumber &number) const {
42  OUTCOME_TRY(header, getBlockHeader(number));
43  OUTCOME_TRY(enc_header, scale::encode(header));
44  return hasher_->blake2b_256(enc_header);
45  }
46 
47  outcome::result<primitives::BlockHeader>
49  OUTCOME_TRY(header_opt, getWithPrefix(*map_, Prefix::HEADER, id));
50  if (header_opt.has_value()) {
51  return scale::decode<primitives::BlockHeader>(header_opt.value());
52  }
54  }
55 
56  outcome::result<BlockStatus> BlockHeaderRepositoryImpl::getBlockStatus(
57  const primitives::BlockId &id) const {
58  return getBlockHeader(id).has_value() ? BlockStatus::InChain
60  }
61 
62 } // namespace kagome::blockchain
std::shared_ptr< storage::BufferStorage > map_
outcome::result< primitives::BlockNumber > lookupKeyToNumber(const common::BufferView &key)
Blob< 32 > Hash256
Definition: blob.hpp:230
outcome::result< primitives::BlockNumber > getNumberByHash(const common::Hash256 &hash) const override
outcome::result< std::optional< common::Buffer > > idToLookupKey(const ReadableBufferStorage &map, const primitives::BlockId &id)
Definition: common.cpp:15
uint32_t BlockNumber
Definition: common.hpp:18
outcome::result< std::optional< common::Buffer > > getWithPrefix(const storage::BufferStorage &map, prefix::Prefix prefix, const primitives::BlockId &block_id)
BlockHeaderRepositoryImpl(std::shared_ptr< storage::BufferStorage > map, std::shared_ptr< crypto::Hasher > hasher)
outcome::result< common::Hash256 > getHashByNumber(const primitives::BlockNumber &number) const override
boost::variant< BlockHash, BlockNumber > BlockId
Block id is the variant over BlockHash and BlockNumber.
Definition: block_id.hpp:18
outcome::result< blockchain::BlockStatus > getBlockStatus(const primitives::BlockId &id) const override
outcome::result< primitives::BlockHeader > getBlockHeader(const primitives::BlockId &id) const override