-
Notifications
You must be signed in to change notification settings - Fork 178
/
encoding.go
187 lines (145 loc) · 5.37 KB
/
encoding.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package flattener
import (
"fmt"
"io"
"github.com/onflow/flow-go/ledger/common/utils"
)
const encodingDecodingVersion = uint16(0)
// EncodeStorableNode encodes StorableNode
func EncodeStorableNode(storableNode *StorableNode) []byte {
length := 2 + 2 + 8 + 8 + 2 + 8 + 2 + len(storableNode.Path) + 4 + len(storableNode.EncPayload) + 2 + len(storableNode.HashValue)
buf := make([]byte, 0, length)
// 2-bytes encoding version
buf = utils.AppendUint16(buf, encodingDecodingVersion)
// 2-bytes Big Endian uint16 height
buf = utils.AppendUint16(buf, storableNode.Height)
// 8-bytes Big Endian uint64 LIndex
buf = utils.AppendUint64(buf, storableNode.LIndex)
// 8-bytes Big Endian uint64 RIndex
buf = utils.AppendUint64(buf, storableNode.RIndex)
// 2-bytes Big Endian maxDepth
buf = utils.AppendUint16(buf, storableNode.MaxDepth)
// 8-bytes Big Endian regCount
buf = utils.AppendUint64(buf, storableNode.RegCount)
// 2-bytes Big Endian uint16 encoded path length and n-bytes encoded path
buf = utils.AppendShortData(buf, storableNode.Path)
// 4-bytes Big Endian uint32 encoded payload length and n-bytes encoded payload
buf = utils.AppendLongData(buf, storableNode.EncPayload)
// 2-bytes Big Endian uint16 hashValue length and n-bytes hashValue
buf = utils.AppendShortData(buf, storableNode.HashValue)
return buf
}
// ReadStorableNode reads a storable node from io
func ReadStorableNode(reader io.Reader) (*StorableNode, error) {
// reading version
buf := make([]byte, 2)
read, err := io.ReadFull(reader, buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node, cannot read version part: %w", err)
}
if read != len(buf) {
return nil, fmt.Errorf("not enough bytes read %d expected %d", read, len(buf))
}
version, _, err := utils.ReadUint16(buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node: %w", err)
}
if version > encodingDecodingVersion {
return nil, fmt.Errorf("error reading storable node: unsuported version %d > %d", version, encodingDecodingVersion)
}
// reading fixed-length part
buf = make([]byte, 2+8+8+2+8)
read, err = io.ReadFull(reader, buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node, cannot read fixed-length part: %w", err)
}
if read != len(buf) {
return nil, fmt.Errorf("not enough bytes read %d expected %d", read, len(buf))
}
storableNode := &StorableNode{}
storableNode.Height, buf, err = utils.ReadUint16(buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node: %w", err)
}
storableNode.LIndex, buf, err = utils.ReadUint64(buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node: %w", err)
}
storableNode.RIndex, buf, err = utils.ReadUint64(buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node: %w", err)
}
storableNode.MaxDepth, buf, err = utils.ReadUint16(buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node: %w", err)
}
storableNode.RegCount, _, err = utils.ReadUint64(buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node: %w", err)
}
storableNode.Path, err = utils.ReadShortDataFromReader(reader)
if err != nil {
return nil, fmt.Errorf("cannot read key data: %w", err)
}
storableNode.EncPayload, err = utils.ReadLongDataFromReader(reader)
if err != nil {
return nil, fmt.Errorf("cannot read value data: %w", err)
}
storableNode.HashValue, err = utils.ReadShortDataFromReader(reader)
if err != nil {
return nil, fmt.Errorf("cannot read hashValue data: %w", err)
}
return storableNode, nil
}
// EncodeStorableTrie encodes StorableTrie
func EncodeStorableTrie(storableTrie *StorableTrie) []byte {
length := 8 + 2 + len(storableTrie.RootHash)
buf := make([]byte, 0, length)
// 2-bytes encoding version
buf = utils.AppendUint16(buf, encodingDecodingVersion)
// 8-bytes Big Endian uint64 RootIndex
buf = utils.AppendUint64(buf, storableTrie.RootIndex)
// 2-bytes Big Endian uint16 RootHash length and n-bytes RootHash
buf = utils.AppendShortData(buf, storableTrie.RootHash)
return buf
}
// ReadStorableTrie reads a storable trie from io
func ReadStorableTrie(reader io.Reader) (*StorableTrie, error) {
storableTrie := &StorableTrie{}
// reading version
buf := make([]byte, 2)
read, err := io.ReadFull(reader, buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node, cannot read version part: %w", err)
}
if read != len(buf) {
return nil, fmt.Errorf("not enough bytes read %d expected %d", read, len(buf))
}
version, _, err := utils.ReadUint16(buf)
if err != nil {
return nil, fmt.Errorf("error reading storable node: %w", err)
}
if version > encodingDecodingVersion {
return nil, fmt.Errorf("error reading storable node: unsuported version %d > %d", version, encodingDecodingVersion)
}
// read root uint64 RootIndex
buf = make([]byte, 8)
read, err = io.ReadFull(reader, buf)
if err != nil {
return nil, fmt.Errorf("cannot read fixed-legth part: %w", err)
}
if read != len(buf) {
return nil, fmt.Errorf("not enough bytes read %d expected %d", read, len(buf))
}
rootIndex, _, err := utils.ReadUint64(buf)
if err != nil {
return nil, fmt.Errorf("cannot read root index data: %w", err)
}
storableTrie.RootIndex = rootIndex
roothash, err := utils.ReadShortDataFromReader(reader)
if err != nil {
return nil, fmt.Errorf("cannot read roothash data: %w", err)
}
storableTrie.RootHash = roothash
return storableTrie, nil
}