Skip to content

Commit 03ba959

Browse files
committed
Update documentation
- Remove `encode_doc.go` - Add `README.md` with Codec section - Update comments for `Encode` and `Decode` methods - Add `node` package comment
1 parent 26bb074 commit 03ba959

File tree

5 files changed

+41
-26
lines changed

5 files changed

+41
-26
lines changed

internal/trie/node/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Trie node
2+
3+
Package node defines the `Node` structure with methods to be used in the modified Merkle-Patricia Radix-16 trie.
4+
5+
## Codec
6+
7+
The following sub-sections precise the encoding of a node.
8+
This encoding is formally described in [the Polkadot specification](https://spec.polkadot.network/#sect-state-storage).
9+
10+
### Header
11+
12+
Each node encoding has a header of one or more bytes.
13+
The first byte contains the node variant and some or all of the partial key length of the node.
14+
If the partial key length cannot fit in the first byte, additional bytes are added to the header to represent the total partial key length.
15+
16+
### Partial key
17+
18+
The header is then concatenated with the partial key of the node, encoded as Little Endian bytes.
19+
20+
### Remaining bytes
21+
22+
The remaining bytes appended depend on the node variant.
23+
24+
- For leaves, the SCALE-encoded leaf value is appended.
25+
- For branches, the following elements are concatenated in this order and appended to the previous header+partial key:
26+
- Children bitmap (2 bytes)
27+
- SCALE-encoded node value
28+
- Hash(Encoding(Child[0]))
29+
- Hash(Encoding(Child[1]))
30+
- ...
31+
- Hash(Encoding(Child[15]))

internal/trie/node/decode.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ var (
1919
)
2020

2121
// Decode decodes a node from a reader.
22+
// The encoding format is documented in the README.md
23+
// of this package, and specified in the Polkadot spec at
24+
// https://spec.polkadot.network/#sect-state-storage
2225
// For branch decoding, see the comments on decodeBranch.
2326
// For leaf decoding, see the comments on decodeLeaf.
2427
func Decode(reader io.Reader) (n *Node, err error) {
@@ -47,7 +50,7 @@ func Decode(reader io.Reader) (n *Node, err error) {
4750
}
4851
}
4952

50-
// decodeBranch reads and decodes from a reader with the encoding specified in internal/trie/node/encode_doc.go.
53+
// decodeBranch reads from a reader and decodes to a node branch.
5154
// Note that since the encoded branch stores the hash of the children nodes, we are not
5255
// reconstructing the child nodes from the encoding. This function instead stubs where the
5356
// children are known to be with an empty leaf. The children nodes hashes are then used to
@@ -115,7 +118,7 @@ func decodeBranch(reader io.Reader, variant byte, partialKeyLength uint16) (
115118
return node, nil
116119
}
117120

118-
// decodeLeaf reads and decodes from a reader with the encoding specified in lib/trie/node/encode_doc.go.
121+
// decodeLeaf reads from a reader and decodes to a leaf node.
119122
func decodeLeaf(reader io.Reader, partialKeyLength uint16) (node *Node, err error) {
120123
node = &Node{
121124
Dirty: true,

internal/trie/node/encode.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
)
1313

1414
// Encode encodes the node to the buffer given.
15-
// The encoding format is documented in encode_doc.go.
15+
// The encoding format is documented in the README.md
16+
// of this package, and specified in the Polkadot spec at
17+
// https://spec.polkadot.network/#sect-state-storage
1618
func (n *Node) Encode(buffer Buffer) (err error) {
1719
if !n.Dirty && n.Encoding != nil {
1820
_, err = buffer.Write(n.Encoding)

internal/trie/node/encode_doc.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

internal/trie/node/node.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2021 ChainSafe Systems (ON)
22
// SPDX-License-Identifier: LGPL-3.0-only
33

4+
// Package node defines the `Node` structure with methods
5+
// to be used in the modified Merkle-Patricia Radix-16 trie.
46
package node
57

68
import (

0 commit comments

Comments
 (0)