-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteriornode.go
358 lines (313 loc) · 8.97 KB
/
interiornode.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package mpt
import (
"encoding/binary"
"fmt"
"io"
"sync"
"github.com/mit-dci/go-bverify/crypto/fastsha256"
)
// InteriorNode represents an interior node in the MPT. An interior node has
// two children, a left child and right child. Interior nodes do not store
// keys or values. The hash of the interior node is H(left.getHash()||right.getHash())
// where left.getHash() (resp. right.getHash()) is the hash of the left (resp right)
// child.
//
// The children of the interior node may be changed. Whenever the children are changed
// the node is marked "changed" until reset() is called. Hashes are calculated
// lazily, only when getHash() is called.
type InteriorNode struct {
hash []byte
recalculateHash bool
changed bool
leftChild Node
rightChild Node
}
// Compile time check if InteriorNode implements Node properly
var _ Node = &InteriorNode{}
// NewInteriorNode creates a new interior node
func NewInteriorNode(leftChild, rightChild Node) (*InteriorNode, error) {
return &InteriorNode{leftChild: leftChild, rightChild: rightChild, changed: true, hash: make([]byte, 32), recalculateHash: true}, nil
}
// NewInteriorNodeWithCachedHash creates a new interior node with a cached hash.
// This is useful when creating proof trees - since we're just substituting parts
// of the tree with stubs, the resulting hashes are equal. Rehashing is a lot of overhead
func NewInteriorNodeWithCachedHash(leftChild, rightChild Node, hash []byte) (*InteriorNode, error) {
node := &InteriorNode{leftChild: leftChild, rightChild: rightChild, changed: true, recalculateHash: false, hash: make([]byte, 32)}
copy(node.hash, hash)
return node, nil
}
func (i *InteriorNode) Dispose() {
i.hash = nil
i.recalculateHash = false
i.changed = false
i.leftChild.Dispose()
i.rightChild.Dispose()
i.leftChild = nil
i.rightChild = nil
i = nil
}
func (i *InteriorNode) CalculateAllHashes() {
}
// GetHash is the implementation of Node.GetHash
func (i *InteriorNode) GetHash() []byte {
if i.recalculateHash {
var leftHash []byte
var rightHash []byte
var wg sync.WaitGroup
if i.leftChild != nil {
wg.Add(1)
go func() {
leftHash = i.leftChild.GetHash()
wg.Done()
}()
}
if i.rightChild != nil {
wg.Add(1)
go func() {
rightHash = i.rightChild.GetHash()
wg.Done()
}()
}
wg.Wait()
hash := fastsha256.Sum256(append(leftHash, rightHash...))
copy(i.hash, hash[:])
i.recalculateHash = false
}
return i.hash
}
// GetGraphHash is the implementation of Node.GetGraphHash
func (i *InteriorNode) GetGraphHash() []byte {
return i.GetHash()
}
// SetLeftChild is the implementation of Node.SetLeftChild
func (i *InteriorNode) SetLeftChild(child Node) {
i.leftChild = child
i.changed = true
i.recalculateHash = true
}
// SetRightChild is the implementation of Node.SetRightChild
func (i *InteriorNode) SetRightChild(child Node) {
i.rightChild = child
i.changed = true
i.recalculateHash = true
}
// GetLeftChild is the implementation of Node.GetLeftChild
func (i *InteriorNode) GetLeftChild() Node {
return i.leftChild
}
// GetRightChild is the implementation of Node.GetRightChild
func (i *InteriorNode) GetRightChild() Node {
return i.rightChild
}
// HasLeft returns true if the left child of this node is not nil
func (i *InteriorNode) HasLeft() bool {
return i.leftChild != nil
}
// HasRight returns true if the left child of this node is not nil
func (i *InteriorNode) HasRight() bool {
return i.rightChild != nil
}
// SetValue is the implementation of Node.SetValue
func (i *InteriorNode) SetValue(value []byte) {
panic("Cannot set value of an interior node")
}
// GetValue is the implementation of Node.GetValue
func (i *InteriorNode) GetValue() []byte {
return nil
}
// GetKey is the implementation of Node.GetKey
func (i *InteriorNode) GetKey() []byte {
return nil
}
// IsEmpty is the implementation of Node.IsEmpty
func (i *InteriorNode) IsEmpty() bool {
return false
}
// IsLeaf is the implementation of Node.IsLeaf
func (i *InteriorNode) IsLeaf() bool {
return false
}
// IsStub is the implementation of Node.IsStub
func (i *InteriorNode) IsStub() bool {
return false
}
// Changed is the implementation of Node.Changed
func (i *InteriorNode) Changed() bool {
return i.changed
}
// MarkChangedAll is the implementation of Node.MarkChangedAll
func (i *InteriorNode) MarkChangedAll() {
if !i.leftChild.Changed() {
i.leftChild.MarkChangedAll()
}
if !i.rightChild.Changed() {
i.rightChild.MarkChangedAll()
}
i.changed = true
}
// MarkUnchangedAll is the implementation of Node.MarkUnchangedAll
func (i *InteriorNode) MarkUnchangedAll() {
if i.leftChild.Changed() {
i.leftChild.MarkUnchangedAll()
}
if i.rightChild.Changed() {
i.rightChild.MarkUnchangedAll()
}
i.changed = false
}
// CountHashesRequiredForGetHash is the implementation of Node.CountHashesRequiredForGetHash
func (i *InteriorNode) CountHashesRequiredForGetHash() int {
if i.recalculateHash {
total := 1
total += i.leftChild.CountHashesRequiredForGetHash()
total += i.rightChild.CountHashesRequiredForGetHash()
return total
}
return 0
}
// NodesInSubtree is the implementation of Node.NodesInSubtree
func (i *InteriorNode) NodesInSubtree() int {
total := 1
if i.leftChild != nil {
total += i.leftChild.NodesInSubtree()
}
if i.rightChild != nil {
total += i.rightChild.NodesInSubtree()
}
return total
}
// InteriorNodesInSubtree is the implementation of Node.InteriorNodesInSubtree
func (i *InteriorNode) InteriorNodesInSubtree() int {
total := 1
if i.leftChild != nil {
total += i.leftChild.InteriorNodesInSubtree()
}
if i.rightChild != nil {
total += i.rightChild.InteriorNodesInSubtree()
}
return total
}
// EmptyLeafNodesInSubtree is the implementation of Node.EmptyLeafNodesInSubtree
func (i *InteriorNode) EmptyLeafNodesInSubtree() int {
total := 0
if i.leftChild != nil {
total += i.leftChild.EmptyLeafNodesInSubtree()
}
if i.rightChild != nil {
total += i.rightChild.EmptyLeafNodesInSubtree()
}
return total
}
// NonEmptyLeafNodesInSubtree is the implementation of Node.NonEmptyLeafNodesInSubtree
func (i *InteriorNode) NonEmptyLeafNodesInSubtree() int {
total := 0
if i.leftChild != nil {
total += i.leftChild.NonEmptyLeafNodesInSubtree()
}
if i.rightChild != nil {
total += i.rightChild.NonEmptyLeafNodesInSubtree()
}
return total
}
// Equals is the implementation of Node.Equals
func (i *InteriorNode) Equals(n Node) bool {
i2, ok := n.(*InteriorNode)
if ok {
if i.leftChild == nil && i2.leftChild != nil {
return false
}
if i.rightChild == nil && i2.rightChild != nil {
return false
}
if i.leftChild != nil && !i.leftChild.Equals(i2.leftChild) {
return false
}
if i.rightChild != nil && !i.rightChild.Equals(i2.rightChild) {
return false
}
return true
}
return false
}
// NewInteriorNodeFromBytes deserializes the passed byteslice into a InteriorNode
func DeserializeNewInteriorNode(r io.Reader) (*InteriorNode, error) {
var err error
var leftNode, rightNode Node
iLen := int32(0)
err = binary.Read(r, binary.BigEndian, &iLen)
if err != nil {
return nil, err
}
if iLen > 0 {
leftNode, err = DeserializeNode(r)
if err != nil {
return nil, err
}
}
err = binary.Read(r, binary.BigEndian, &iLen)
if err != nil {
return nil, err
}
if iLen > 0 {
rightNode, err = DeserializeNode(r)
if err != nil {
return nil, err
}
}
return NewInteriorNode(leftNode, rightNode)
}
func (i *InteriorNode) ByteSize() int {
// 1 (Type) + 4 (leftChild size) + leftChild size + 4 (rightChild size) + rightChildSize
size := 9
if i.leftChild != nil {
size += i.leftChild.ByteSize()
}
if i.rightChild != nil {
size += i.rightChild.ByteSize()
}
return size
}
func (i *InteriorNode) Serialize(w io.Writer) {
w.Write([]byte{byte(NodeTypeInterior)})
if i.leftChild != nil {
binary.Write(w, binary.BigEndian, int32(i.leftChild.ByteSize()))
i.leftChild.Serialize(w)
} else {
binary.Write(w, binary.BigEndian, int32(0))
}
if i.rightChild != nil {
binary.Write(w, binary.BigEndian, int32(i.rightChild.ByteSize()))
i.rightChild.Serialize(w)
} else {
binary.Write(w, binary.BigEndian, int32(0))
}
}
// WriteGraphNodes is the implementation of Node.WriteGraphNodes
func (i *InteriorNode) WriteGraphNodes(w io.Writer) {
w.Write([]byte(fmt.Sprintf("\"%x\" [\n\tshape=box\n\tstyle=\"filled,dashed\"\n\tcolor=black\n\tfillcolor=gray68];\n", i.GetGraphHash())))
if i.leftChild != nil {
i.leftChild.WriteGraphNodes(w)
w.Write([]byte(fmt.Sprintf("\"%x\" -> \"%x\";\n", i.GetGraphHash(), i.leftChild.GetGraphHash())))
}
if i.rightChild != nil {
i.rightChild.WriteGraphNodes(w)
w.Write([]byte(fmt.Sprintf("\"%x\" -> \"%x\";\n", i.GetGraphHash(), i.rightChild.GetGraphHash())))
}
}
func (i *InteriorNode) DeepCopy() (Node, error) {
var left, right Node
var err error
if i.leftChild != nil {
left, err = i.leftChild.DeepCopy()
if err != nil {
return nil, err
}
}
if i.rightChild != nil {
right, err = i.rightChild.DeepCopy()
if err != nil {
return nil, err
}
}
return NewInteriorNodeWithCachedHash(left, right, i.hash)
}