-
Notifications
You must be signed in to change notification settings - Fork 112
/
encode.go
58 lines (49 loc) · 1.52 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package node
import (
"fmt"
"github.com/ChainSafe/gossamer/internal/trie/codec"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/scale"
)
// Encode encodes the node to the buffer given.
// The encoding format is documented in the README.md
// of this package, and specified in the Polkadot spec at
// https://spec.polkadot.network/#sect-state-storage
func (n *Node) Encode(buffer Buffer) (err error) {
err = encodeHeader(n, buffer)
if err != nil {
return fmt.Errorf("cannot encode header: %w", err)
}
keyLE := codec.NibblesToKeyLE(n.PartialKey)
_, err = buffer.Write(keyLE)
if err != nil {
return fmt.Errorf("cannot write LE key to buffer: %w", err)
}
kind := n.Kind()
nodeIsBranch := kind == Branch
if nodeIsBranch {
childrenBitmap := common.Uint16ToBytes(n.ChildrenBitmap())
_, err = buffer.Write(childrenBitmap)
if err != nil {
return fmt.Errorf("cannot write children bitmap to buffer: %w", err)
}
}
// Only encode node value if the node is a leaf or
// the node is a branch with a non empty value.
if !nodeIsBranch || (nodeIsBranch && n.SubValue != nil) {
encoder := scale.NewEncoder(buffer)
err = encoder.Encode(n.SubValue)
if err != nil {
return fmt.Errorf("scale encoding value: %w", err)
}
}
if nodeIsBranch {
err = encodeChildrenOpportunisticParallel(n.Children, buffer)
if err != nil {
return fmt.Errorf("cannot encode children of branch: %w", err)
}
}
return nil
}