-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemptyleafnode.go
161 lines (125 loc) · 4.01 KB
/
emptyleafnode.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
package mpt
import (
"fmt"
"io"
"github.com/mit-dci/go-bverify/crypto/fastsha256"
)
var emptyLeafNodeHash []byte
var sharedEmptyLeafNode *EmptyLeafNode
// EmptyLeafNode represents an empty leaf in the tree. Empty leaves
// do not have associated values and use the special marker
// hash of all 0s.
//
type EmptyLeafNode struct {
}
// Compile time check if DictionaryLeafNode implements Node properly
var _ Node = &EmptyLeafNode{}
// NewEmptyLeafNode creates a new empty leaf node
func NewEmptyLeafNode() (*EmptyLeafNode, error) {
return sharedEmptyLeafNode, nil
}
func (eln *EmptyLeafNode) Dispose() {
}
// GetHash is the implementation of Node.GetHash
func (eln *EmptyLeafNode) GetHash() []byte {
return emptyLeafNodeHash
}
// GetGraphHash is the implementation of Node.GetGraphHash
func (eln *EmptyLeafNode) GetGraphHash() []byte {
hash := fastsha256.Sum256([]byte(fmt.Sprintf("%p", eln)))
returnHash := make([]byte, len(hash))
copy(returnHash, hash[:])
return returnHash
}
// SetLeftChild is the implementation of Node.SetLeftChild
func (eln *EmptyLeafNode) SetLeftChild(child Node) {
panic("Cannot set children of an empty leaf node")
}
// SetRightChild is the implementation of Node.SetRightChild
func (eln *EmptyLeafNode) SetRightChild(child Node) {
panic("Cannot set children of an empty leaf node")
}
// GetLeftChild is the implementation of Node.GetLeftChild
func (eln *EmptyLeafNode) GetLeftChild() Node {
return nil
}
// GetRightChild is the implementation of Node.GetRightChild
func (eln *EmptyLeafNode) GetRightChild() Node {
return nil
}
// SetValue is the implementation of Node.SetValue
func (eln *EmptyLeafNode) SetValue(value []byte) {
panic("Cannot set value of an empty leaf node")
}
// GetValue is the implementation of Node.GetValue
func (eln *EmptyLeafNode) GetValue() []byte {
return nil
}
// GetKey is the implementation of Node.GetKey
func (eln *EmptyLeafNode) GetKey() []byte {
return nil
}
// IsEmpty is the implementation of Node.IsEmpty
func (eln *EmptyLeafNode) IsEmpty() bool {
return true
}
// IsLeaf is the implementation of Node.IsLeaf
func (eln *EmptyLeafNode) IsLeaf() bool {
return true
}
// IsStub is the implementation of Node.IsStub
func (eln *EmptyLeafNode) IsStub() bool {
return false
}
// Changed is the implementation of Node.Changed
func (eln *EmptyLeafNode) Changed() bool {
return false
}
// MarkChangedAll is the implementation of Node.MarkChangedAll
func (eln *EmptyLeafNode) MarkChangedAll() {
}
// MarkUnchangedAll is the implementation of Node.MarkUnchangedAll
func (eln *EmptyLeafNode) MarkUnchangedAll() {
}
// CountHashesRequiredForGetHash is the implementation of Node.CountHashesRequiredForGetHash
func (eln *EmptyLeafNode) CountHashesRequiredForGetHash() int {
return 0
}
// NodesInSubtree is the implementation of Node.NodesInSubtree
func (eln *EmptyLeafNode) NodesInSubtree() int {
return 1
}
// InteriorNodesInSubtree is the implementation of Node.InteriorNodesInSubtree
func (eln *EmptyLeafNode) InteriorNodesInSubtree() int {
return 0
}
// EmptyLeafNodesInSubtree is the implementation of Node.EmptyLeafNodesInSubtree
func (eln *EmptyLeafNode) EmptyLeafNodesInSubtree() int {
return 1
}
// NonEmptyLeafNodesInSubtree is the implementation of Node.NonEmptyLeafNodesInSubtree
func (eln *EmptyLeafNode) NonEmptyLeafNodesInSubtree() int {
return 0
}
// Equals is the implementation of Node.Equals
func (eln *EmptyLeafNode) Equals(n Node) bool {
_, ok := n.(*EmptyLeafNode)
return ok
}
func (eln *EmptyLeafNode) ByteSize() int {
return 1
}
// Bytes is the implementation of Node.Bytes
func (eln *EmptyLeafNode) Serialize(w io.Writer) {
w.Write([]byte{byte(NodeTypeEmptyLeaf)})
}
func (eln *EmptyLeafNode) WriteGraphNodes(w io.Writer) {
w.Write([]byte(fmt.Sprintf("\"%x\" [\n\tshape=box\n\tstyle=\"filled,solid\"\n\tfontcolor=gray50\n\tcolor=gray50\n\tfillcolor=white];\n", eln.GetGraphHash())))
}
func (eln *EmptyLeafNode) DeepCopy() (Node, error) {
return sharedEmptyLeafNode, nil
}
func init() {
emptyLeafNodeHash = make([]byte, 32)
sharedEmptyLeafNode = &EmptyLeafNode{}
}