-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstub.go
181 lines (147 loc) · 4.55 KB
/
stub.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
package mpt
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
// Stub represents an omitted path in a MPT. Stubs only store a
// hash commitment to that subtree. These can be used to construct proofs and
// can be swapped out for actual subtrees which match the hash
type Stub struct {
hash []byte
}
// Compile time check if Stub implements Node properly
var _ Node = &Stub{}
// NewStub creates a new stub from a hash
func NewStub(hash []byte) (*Stub, error) {
stub := &Stub{hash: make([]byte, len(hash))}
copy(stub.hash, hash)
return stub, nil
}
func (s *Stub) Dispose() {
s.hash = nil
s = nil
}
// GetHash is the implementation of Node.GetHash
func (s *Stub) GetHash() []byte {
return s.hash
}
// GetGraphHash is the implementation of Node.GetGraphHash
func (s *Stub) GetGraphHash() []byte {
return s.hash
}
// SetLeftChild is the implementation of Node.SetLeftChild
func (s *Stub) SetLeftChild(child Node) {
panic("Cannot set children of a stub")
}
// SetRightChild is the implementation of Node.SetRightChild
func (s *Stub) SetRightChild(child Node) {
panic("Cannot set children of a stub")
}
// GetLeftChild is the implementation of Node.GetLeftChild
func (s *Stub) GetLeftChild() Node {
return nil
}
// GetRightChild is the implementation of Node.GetRightChild
func (s *Stub) GetRightChild() Node {
return nil
}
// SetValue is the implementation of Node.SetValue
func (s *Stub) SetValue(value []byte) {
panic("Cannot set the value of a stub")
}
// GetValue is the implementation of Node.GetValue
func (s *Stub) GetValue() []byte {
return nil
}
// GetKey is the implementation of Node.GetKey
func (s *Stub) GetKey() []byte {
return nil
}
// IsEmpty is the implementation of Node.IsEmpty
func (s *Stub) IsEmpty() bool {
return false
}
// IsLeaf is the implementation of Node.IsLeaf
func (s *Stub) IsLeaf() bool {
return false
}
// IsStub is the implementation of Node.IsStub
func (s *Stub) IsStub() bool {
return true
}
// Changed is the implementation of Node.Changed
func (s *Stub) Changed() bool {
return false
}
// MarkChangedAll is the implementation of Node.MarkChangedAll
func (s *Stub) MarkChangedAll() {}
// MarkUnchangedAll is the implementation of Node.MarkUnchangedAll
func (s *Stub) MarkUnchangedAll() {}
// CountHashesRequiredForGetHash is the implementation of Node.CountHashesRequiredForGetHash
func (s *Stub) CountHashesRequiredForGetHash() int {
panic("Cannot count hashes for a stub")
}
// NodesInSubtree is the implementation of Node.NodesInSubtree
func (s *Stub) NodesInSubtree() int {
panic("cannot determine size of subtree rooted at a stub")
}
// InteriorNodesInSubtree is the implementation of Node.InteriorNodesInSubtree
func (s *Stub) InteriorNodesInSubtree() int {
panic("cannot determine number of interior nodes in subtree rooted at a stub")
}
// EmptyLeafNodesInSubtree is the implementation of Node.EmptyLeafNodesInSubtree
func (s *Stub) EmptyLeafNodesInSubtree() int {
panic("cannot determine number of empty leaf nodes in subtree rooted at a stub")
}
// NonEmptyLeafNodesInSubtree is the implementation of Node.NonEmptyLeafNodesInSubtree
func (s *Stub) NonEmptyLeafNodesInSubtree() int {
panic("cannot determine number of non-empty leaf nodes in subtree rooted at a stub")
}
// Equals is the implementation of Node.Equals
func (s *Stub) Equals(s2 Node) bool {
stub2, ok := s2.(*Stub)
if ok {
return bytes.Equal(stub2.GetHash(), s.GetHash())
}
return false
}
// NewStubFromBytes deserializes the passed byteslice into a Stub
func DeserializeNewStub(r io.Reader) (*Stub, error) {
var stub []byte
iLen := int32(0)
err := binary.Read(r, binary.BigEndian, &iLen)
if err != nil {
return nil, err
}
if iLen > 0 {
stub = make([]byte, iLen)
i, err := r.Read(stub)
if err != nil {
return nil, err
}
if int32(i) != iLen {
return nil, fmt.Errorf("Specified length of stub not present in buffer")
}
} else {
return nil, fmt.Errorf("Dictionary leaf node needs a stub of at least 1 byte")
}
return NewStub(stub)
}
func (s *Stub) Serialize(w io.Writer) {
w.Write([]byte{byte(NodeTypeStub)})
binary.Write(w, binary.BigEndian, int32(len(s.hash)))
w.Write(s.hash)
}
// ByteSize returns the length of Bytes() without actually serializing it
func (s *Stub) ByteSize() int {
return 5 + len(s.hash)
}
// WriteGraphNodes is the implementation of Node.WriteGraphNodes
func (s *Stub) WriteGraphNodes(w io.Writer) {
w.Write([]byte(fmt.Sprintf("\"%x\" [\n\tshape=box\n\tstyle=\"filled,dashed\"\n\ttextcolor=blue\n\tcolor=blue\n\tfillcolor=lightblue];\n", s.GetGraphHash())))
}
func (s *Stub) DeepCopy() (Node, error) {
return NewStub(s.hash)
}