-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension_node.go
66 lines (57 loc) · 1.26 KB
/
extension_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
package PromethoniXTrie
import "io"
type ExtensionNode struct {
NodeDetails
Path Hash
NextHash Hash
}
func (node *ExtensionNode) Type() NodeType {
return Extension
}
func (node *ExtensionNode) Encode(writer io.Writer) error {
var err error
l := len(node.Path)
err = writeInt32(writer, int32(l))
if err == nil && l > 0 {
_, err = writer.Write(node.Path)
}
if err == nil {
l = len(node.NextHash)
err = writeInt32(writer, int32(l))
}
if err == nil && l > 0 {
_, err = writer.Write(node.NextHash)
}
return err
}
func (node *ExtensionNode) Decode(reader io.Reader) error {
length, err := readInt32(reader)
if err != nil {
return err
}
node.Path, err = readBytes(reader, length)
if err != nil {
return err
}
length, err = readInt32(reader)
if err != nil {
return err
}
node.NextHash, err = readBytes(reader, length)
return nil
}
func (node *ExtensionNode) NextRoute(route Route) (Hash, Route, error) {
matchLen := prefixLen(node.Path, route)
if matchLen != len(node.Path) {
return node.NextHash, nil, ErrNotFound
}
return node.NextHash, route[matchLen:], nil
}
func AsExtension(node Node) *ExtensionNode {
return node.(*ExtensionNode)
}
func NewExtensionNode() *ExtensionNode {
node := new(ExtensionNode)
node._node = node
return node
}