Kagome
Polkadot Runtime Engine in C++17
ordered_trie_hash.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_ORDERED_TRIE_HASH_HPP
7 #define KAGOME_ORDERED_TRIE_HASH_HPP
8 
9 #include "common/buffer.hpp"
10 #include "crypto/hasher.hpp"
11 #include "scale/scale.hpp"
14 
15 namespace kagome::storage::trie {
16 
24  template <typename It>
25  outcome::result<common::Buffer> calculateOrderedTrieHash(const It &begin,
26  const It &end) {
27  PolkadotTrieImpl trie;
28  PolkadotCodec codec;
29  // empty root
30  if (begin == end) {
31  static const auto empty_root =
33  return empty_root;
34  }
35  static_assert(
36  std::is_same_v<std::decay_t<decltype(*begin)>, common::Buffer>);
37  It it = begin;
38  scale::CompactInteger key = 0;
39  while (it != end) {
40  OUTCOME_TRY(enc, scale::encode(key++));
41  OUTCOME_TRY(trie.put(common::Buffer{enc}, *it));
42  it++;
43  }
44  OUTCOME_TRY(enc, codec.encodeNode(*trie.getRoot()));
45  return common::Buffer{codec.hash256(enc)};
46  }
47 
48  template <typename ContainerType>
49  inline outcome::result<common::Buffer> calculateOrderedTrieHash(
50  const ContainerType &container) {
51  return calculateOrderedTrieHash(container.begin(), container.end());
52  }
53 
54 } // namespace kagome::storage::trie
55 
56 #endif // KAGOME_ORDERED_TRIE_HASH_HPP
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
SLBuffer & put(std::string_view view)
Put a string into byte buffer.
Definition: buffer.hpp:117
scale::CompactInteger CompactInteger
outcome::result< common::Buffer > encodeNode(const Node &node) const
Encode node to byte representation.
Definition: codec.hpp:39
common::Hash256 hash256(const BufferView &buf) const override
Get the hash of a node.
outcome::result< common::Buffer > calculateOrderedTrieHash(const It &begin, const It &end)
outcome::result< void > put(const common::BufferView &key, const common::Buffer &value) override
Store value by key.