-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
325 lines (291 loc) · 6.24 KB
/
node.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package mpt
import (
"fmt"
"io"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/golang/protobuf/proto"
)
type node interface {
Encode() []byte
Hash() common.Hash
Capped() []byte
Cache([]byte)
}
// we use proto rather than rlp, so we need to append one flag byte to the
// encoded node, which indicate that the length of key nibbles is odd/even
// and the type of node, the low 4 bits represent the type of node, the
// high 4 bits represent if we need to remove the first padded nibble of the key
const (
leafType byte = 0x00
leafWithPad byte = 0x10
extType byte = 0x01
extWithPad byte = 0x11
// branch node have no key, so pad is unnecessary
branchType byte = 0x02
)
type (
extNode struct {
key []byte
child node
encoded []byte
hash []byte
}
branchNode struct {
children [16]node
target []byte
encoded []byte
hash []byte
}
leafNode struct {
key []byte
value []byte
encoded []byte
hash []byte
}
// use struct instead of `hashNode []byte` here because of we use pointer implement node interface
hashNode struct {
hash []byte
}
)
func branchWithTarget(target []byte) *branchNode {
return &branchNode{
target: target,
}
}
func branchWithChild(pos int, n node, target []byte) *branchNode {
b := &branchNode{
target: target,
}
b.children[pos] = n
return b
}
func branchWithChildren(children [16]node) *branchNode {
return &branchNode{
children: children,
}
}
func (n *branchNode) updateTarget(target []byte) *branchNode {
b := &branchNode{
children: n.children,
target: target,
}
return b
}
func (n *branchNode) updateChild(pos int, child node) *branchNode {
b := &branchNode{
children: n.children,
target: n.target,
}
b.children[pos] = child
return b
}
// return the index of children
func (n *branchNode) childrenIndex() []int {
index := make([]int, 0)
for i, child := range n.children {
if child != nil {
index = append(index, i)
}
}
return index
}
func (n *branchNode) hasTarget() bool {
return n.target != nil
}
func (n *branchNode) Encode() []byte {
if n.encoded != nil {
return n.encoded
}
var rawNode BranchNode
for _, n := range n.children {
if n == nil {
rawNode.Children = append(rawNode.Children, nil)
} else {
rawNode.Children = append(rawNode.Children, n.Capped())
}
}
rawNode.Target = n.target
encoded, _ := proto.Marshal(&rawNode)
encoded = append(encoded, branchType)
n.encoded = encoded
return encoded
}
func (n *branchNode) Hash() common.Hash {
if n.hash != nil {
return common.BytesToHash(n.hash)
}
hash := crypto.Keccak256Hash(n.Encode())
n.hash = hash[:]
return hash
}
func (n *branchNode) Capped() []byte {
encoded := n.Encode()
if len(encoded) < common.HashLength {
return encoded
} else {
hash := n.Hash()
return hash[:]
}
}
func (n *branchNode) Cache(bytes []byte) {
n.encoded = bytes
}
func newExtNode(key []byte, child node) *extNode {
return &extNode{
key: key,
child: child,
}
}
func (n *extNode) Encode() []byte {
if n.encoded != nil {
return n.encoded
}
capped := n.child.Capped()
keyBytes, flag := encodeKey(n.key, extType)
rawNode := &ExtNode{
Key: keyBytes,
Node: capped,
}
encoded, _ := proto.Marshal(rawNode)
encoded = append(encoded, flag)
n.encoded = encoded
return encoded
}
func (n *extNode) Hash() common.Hash {
if n.hash != nil {
return common.BytesToHash(n.hash)
}
hash := crypto.Keccak256Hash(n.Encode())
n.hash = hash[:]
return hash
}
func (n *extNode) Cache(bytes []byte) {
n.encoded = bytes
}
func (n *extNode) Capped() []byte {
encoded := n.Encode()
if len(encoded) < common.HashLength {
return encoded
} else {
hash := n.Hash()
return hash[:]
}
}
func newLeafNode(key, value []byte) *leafNode {
return &leafNode{
key: key,
value: value,
}
}
func (n *leafNode) Encode() []byte {
if n.encoded != nil {
return n.encoded
}
keyBytes, flag := encodeKey(n.key, leafType)
rawNode := &LeafNode{
Key: keyBytes,
Value: n.value,
}
encoded, _ := proto.Marshal(rawNode)
encoded = append(encoded, flag)
n.encoded = encoded
return encoded
}
func (n *leafNode) Hash() common.Hash {
if n.hash != nil {
return common.BytesToHash(n.hash)
}
hash := crypto.Keccak256Hash(n.Encode())
n.hash = hash[:]
return hash
}
func (n *leafNode) Capped() []byte {
encoded := n.Encode()
if len(encoded) < common.HashLength {
return encoded
} else {
hash := n.Hash()
return hash[:]
}
}
func (n *leafNode) Cache(bytes []byte) {
n.encoded = bytes
}
func (n *hashNode) Encode() []byte {
return n.hash
}
func (n *hashNode) Hash() common.Hash {
return common.BytesToHash(n.hash)
}
func (n *hashNode) Capped() []byte {
return n.hash
}
func (n *hashNode) Cache(bytes []byte) {
}
func decodeNode(bytes []byte) (node, error) {
if len(bytes) <= 1 {
return nil, io.ErrUnexpectedEOF
}
flag := bytes[len(bytes)-1]
raw := bytes[0 : len(bytes)-1]
switch flag & 0x0f {
case leafType:
return decodeLeafNode(raw, flag)
case extType:
return decodeExtNode(raw, flag)
case branchType:
return decodeBranchNode(raw)
default:
// this should never happen
return nil, fmt.Errorf("unknown node type: %v", flag)
}
}
func decodeLeafNode(bytes []byte, flag byte) (node, error) {
var rawNode LeafNode
err := proto.Unmarshal(bytes, &rawNode)
if err != nil {
return nil, err
}
keyNibbles, _ := decodeKey(flag, rawNode.Key)
n := &leafNode{
key: keyNibbles,
value: rawNode.Value,
}
return n, nil
}
func decodeExtNode(bytes []byte, flag byte) (node, error) {
var rawNode ExtNode
err := proto.Unmarshal(bytes, &rawNode)
if err != nil {
return nil, err
}
var n extNode
keyNibbles, _ := decodeKey(flag, rawNode.Key)
n.key = keyNibbles
if len(rawNode.Node) == common.HashLength {
n.child = &hashNode{rawNode.Node}
} else {
n.child, err = decodeNode(rawNode.Node)
}
return &n, err
}
func decodeBranchNode(bytes []byte) (node, error) {
var rawNode BranchNode
err := proto.Unmarshal(bytes, &rawNode)
if err != nil {
return nil, err
}
var n branchNode
n.target = rawNode.Target
for i, child := range rawNode.Children {
if len(child) == 0 {
n.children[i] = nil
} else if len(child) == common.HashLength {
n.children[i] = &hashNode{child}
} else {
n.children[i], err = decodeNode(child)
}
}
return &n, err
}