-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
91 lines (78 loc) · 1.44 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
package PromethoniXTrie
import (
"bytes"
"io"
)
type Data []byte
type Hash []byte
type Route Hash
type NodeType int8
const (
_ NodeType = iota
Extension
Leaf
Branch
)
type Node interface {
Type() NodeType
Encode(io.Writer) error
Decode(io.Reader) error
NextRoute(Route) (Hash, Route, error)
Details() *NodeDetails
EncodeAndHash() error
}
type NodeDetails struct {
_node Node
Hash Hash
EncodedData Data
}
func EncodeNode(node Node) (Data, error) {
buf := bytes.Buffer{}
err := writeNodeType(&buf, node.Type())
if err == nil {
err = node.Encode(&buf)
}
return buf.Bytes(), err
}
func DecodeNode(raw Data) (Node, error) {
reader := bytes.NewReader(raw)
typ, err := readNodeType(reader)
if err != nil {
return nil, err
}
var node Node = nil
switch typ {
case Branch:
node = NewBranchNode()
err = node.Decode(reader)
case Leaf:
node = NewLeafNode()
err = node.Decode(reader)
case Extension:
node = NewExtensionNode()
err = node.Decode(reader)
default:
err = ErrInvalidNodeType
}
if err == nil {
node.Details().EncodedData = raw
node.Details().Hash, err = sha3Hash(raw)
}
return node, err
}
func (details *NodeDetails) Details() *NodeDetails {
return details
}
func (details *NodeDetails) EncodeAndHash() error {
b, err := EncodeNode(details._node)
if err != nil {
return err
}
details.EncodedData = b
h, err := sha3Hash(b)
if err != nil {
return err
}
details.Hash = h
return nil
}