Kagome
Polkadot Runtime Engine in C++17
polkadot_codec.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_TRIE_POLKADOT_CODEC_IMPL_HPP
7 #define KAGOME_TRIE_POLKADOT_CODEC_IMPL_HPP
8 
9 #include <memory>
10 #include <optional>
11 #include <string>
12 
13 #include "storage/trie/codec.hpp"
16 
17 namespace kagome::storage::trie {
18 
19  class PolkadotCodec : public Codec {
20  public:
23 
24  enum class Error {
25  SUCCESS = 0,
30  };
31 
32  ~PolkadotCodec() override = default;
33 
34  outcome::result<Buffer> encodeNodeAndStoreChildren(
35  const Node &node, const StoreChildren &store_children) const override;
36 
37  outcome::result<std::shared_ptr<Node>> decodeNode(
38  gsl::span<const uint8_t> encoded_data) const override;
39 
40  common::Buffer merkleValue(const BufferView &buf) const override;
41 
42  bool isMerkleHash(const common::BufferView &buf) const override;
43 
44  common::Hash256 hash256(const BufferView &buf) const override;
45 
50  outcome::result<Buffer> encodeHeader(const TrieNode &node) const;
51 
52  private:
53  outcome::result<Buffer> encodeBranch(
54  const BranchNode &node, const StoreChildren &store_children) const;
55  outcome::result<Buffer> encodeLeaf(const LeafNode &node) const;
56 
57  outcome::result<std::pair<TrieNode::Type, size_t>> decodeHeader(
58  BufferStream &stream) const;
59 
60  outcome::result<KeyNibbles> decodePartialKey(size_t nibbles_num,
61  BufferStream &stream) const;
62 
63  outcome::result<std::shared_ptr<Node>> decodeBranch(
64  TrieNode::Type type,
65  const KeyNibbles &partial_key,
66  BufferStream &stream) const;
67  };
68 
69 } // namespace kagome::storage::trie
70 
72 
73 #endif // KAGOME_TRIE_POLKADOT_CODEC_IMPL_HPP
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
common::Buffer merkleValue(const BufferView &buf) const override
Get the merkle value of a node.
outcome::result< Buffer > encodeHeader(const TrieNode &node) const
common::BufferView BufferView
outcome::result< Buffer > encodeLeaf(const LeafNode &node) const
outcome::result< Buffer > encodeBranch(const BranchNode &node, const StoreChildren &store_children) const
outcome::result< std::pair< TrieNode::Type, size_t > > decodeHeader(BufferStream &stream) const
outcome::result< KeyNibbles > decodePartialKey(size_t nibbles_num, BufferStream &stream) const
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
bool isMerkleHash(const common::BufferView &buf) const override
is this a hash of value, or value itself
common::Hash256 hash256(const BufferView &buf) const override
Get the hash of a node.
outcome::result< std::shared_ptr< Node > > decodeBranch(TrieNode::Type type, const KeyNibbles &partial_key, BufferStream &stream) const
cannot decode a node, not enough bytes on input
outcome::result< Buffer > encodeNodeAndStoreChildren(const Node &node, const StoreChildren &store_children) const override
Encode node to byte representation and store children.
outcome::result< std::shared_ptr< Node > > decodeNode(gsl::span< const uint8_t > encoded_data) const override
Decode node from bytes.
OUTCOME_HPP_DECLARE_ERROR(kagome::storage::trie, PolkadotCodec::Error)
Internal codec for nodes in the Trie. Eth and substrate have different codecs, but rest of the code s...
Definition: codec.hpp:18
std::function< outcome::result< void >(common::BufferView, common::Buffer &&)> StoreChildren
Definition: codec.hpp:21