Kagome
Polkadot Runtime Engine in C++17
common.cpp
Go to the documentation of this file.
1 
8 #include "common/visitor.hpp"
12 
13 namespace kagome::blockchain {
14 
15  outcome::result<std::optional<common::Buffer>> idToLookupKey(
16  const ReadableBufferStorage &map, const primitives::BlockId &id) {
17  auto key = visit_in_place(
18  id,
19  [](const primitives::BlockNumber &n) {
22  },
23  [](const common::Hash256 &hash) {
25  });
26 
27  OUTCOME_TRY(key_opt, map.tryLoad(key));
28 
29  return std::move(key_opt);
30  }
31 
33  const std::vector<std::pair<common::Buffer, common::Buffer>> &key_vals) {
34  auto trie = storage::trie::PolkadotTrieImpl();
35  auto codec = storage::trie::PolkadotCodec();
36 
37  for (const auto &[key, val] : key_vals) {
38  [[maybe_unused]] auto res = trie.put(key, val);
39  BOOST_ASSERT_MSG(res.has_value(), "Insertion into trie failed");
40  }
41  auto root = trie.getRoot();
42  if (root == nullptr) {
43  static const auto zero_hash = codec.hash256(common::Buffer{0});
44  return zero_hash;
45  }
46  auto encode_res = codec.encodeNode(*root);
47  BOOST_ASSERT_MSG(encode_res.has_value(), "Trie encoding failed");
48  return codec.hash256(encode_res.value());
49  }
50 } // namespace kagome::blockchain
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
outcome::result< std::optional< common::Buffer > > idToLookupKey(const ReadableBufferStorage &map, const primitives::BlockId &id)
Definition: common.cpp:15
common::Buffer numberToIndexKey(primitives::BlockNumber n)
uint32_t BlockNumber
Definition: common.hpp:18
boost::variant< BlockHash, BlockNumber > BlockId
Block id is the variant over BlockHash and BlockNumber.
Definition: block_id.hpp:18
storage::trie::RootHash trieRoot(const std::vector< std::pair< common::Buffer, common::Buffer >> &key_vals)
Definition: common.cpp:32
common::Buffer prependPrefix(common::BufferView key, prefix::Prefix key_column)
virtual outcome::result< std::optional< V > > tryLoad(const Key &key) const =0
Load value by key.