forked from ferranbt/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
222 lines (181 loc) · 4.13 KB
/
wrapper.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
package ssz
var _ HashWalker = (*Wrapper)(nil)
// ProofTree hashes a HashRoot object with a Hasher from
// the default HasherPool
func ProofTree(v HashRoot) (*Node, error) {
w := &Wrapper{}
if err := v.HashTreeRootWith(w); err != nil {
return nil, err
}
return w.Node(), nil
}
type Wrapper struct {
nodes []*Node
buf []byte
}
/// --- wrapper implements the HashWalker interface ---
func (w *Wrapper) Index() int {
return len(w.nodes)
}
func (w *Wrapper) Append(i []byte) {
w.buf = append(w.buf, i...)
}
func (w *Wrapper) AppendUint64(i uint64) {
w.buf = MarshalUint64(w.buf, i)
}
func (w *Wrapper) AppendUint32(i uint32) {
w.buf = MarshalUint32(w.buf, i)
}
func (w *Wrapper) AppendUint8(i uint8) {
w.buf = MarshalUint8(w.buf, i)
}
func (w *Wrapper) AppendBytes32(b []byte) {
w.buf = append(w.buf, b...)
w.FillUpTo32()
}
func (w *Wrapper) FillUpTo32() {
// pad zero bytes to the left
if rest := len(w.buf) % 32; rest != 0 {
w.buf = append(w.buf, zeroBytes[:32-rest]...)
}
}
func (w *Wrapper) Merkleize(indx int) {
if len(w.buf) != 0 {
w.appendBytesAsNodes(w.buf)
w.buf = w.buf[:0]
}
w.Commit(indx)
}
func (w *Wrapper) MerkleizeWithMixin(indx int, num, limit uint64) {
if len(w.buf) != 0 {
w.appendBytesAsNodes(w.buf)
w.buf = w.buf[:0]
}
w.CommitWithMixin(indx, int(num), int(limit))
}
func (w *Wrapper) PutBitlist(bb []byte, maxSize uint64) {
b, size := parseBitlist(nil, bb)
indx := w.Index()
w.appendBytesAsNodes(b)
w.CommitWithMixin(indx, int(size), int((maxSize+255)/256))
}
func (w *Wrapper) appendBytesAsNodes(b []byte) {
// if byte list is empty, fill with zeros
if len(b) == 0 {
b = append(b, zeroBytes[:32]...)
}
// if byte list isn't filled with 32-bytes padded, pad
if rest := len(b) % 32; rest != 0 {
b = append(b, zeroBytes[:32-rest]...)
}
for i := 0; i < len(b); i += 32 {
val := append([]byte{}, b[i:min(len(b), i+32)]...)
w.nodes = append(w.nodes, LeafFromBytes(val))
}
}
func (w *Wrapper) PutBool(b bool) {
w.AddNode(LeafFromBool(b))
}
func (w *Wrapper) PutBytes(b []byte) {
w.AddBytes(b)
}
func (w *Wrapper) PutUint16(i uint16) {
w.AddUint16(i)
}
func (w *Wrapper) PutUint64(i uint64) {
w.AddUint64(i)
}
func (w *Wrapper) PutUint8(i uint8) {
w.AddUint8(i)
}
func (w *Wrapper) PutUint32(i uint32) {
w.AddUint32(i)
}
func (w *Wrapper) PutUint64Array(b []uint64, maxCapacity ...uint64) {
indx := w.Index()
for _, i := range b {
w.AppendUint64(i)
}
// pad zero bytes to the left
w.FillUpTo32()
if len(maxCapacity) == 0 {
// Array with fixed size
w.Merkleize(indx)
} else {
numItems := uint64(len(b))
limit := CalculateLimit(maxCapacity[0], numItems, 8)
w.MerkleizeWithMixin(indx, numItems, limit)
}
}
/// --- legacy ones ---
func min(i, j int) int {
if i < j {
return i
}
return j
}
func (w *Wrapper) AddBytes(b []byte) {
if len(b) <= 32 {
w.AddNode(LeafFromBytes(b))
} else {
indx := w.Index()
w.appendBytesAsNodes(b)
w.Commit(indx)
}
}
func (w *Wrapper) AddUint64(i uint64) {
w.AddNode(LeafFromUint64(i))
}
func (w *Wrapper) AddUint32(i uint32) {
w.AddNode(LeafFromUint32(i))
}
func (w *Wrapper) AddUint16(i uint16) {
w.AddNode(LeafFromUint16(i))
}
func (w *Wrapper) AddUint8(i uint8) {
w.AddNode(LeafFromUint8(i))
}
func (w *Wrapper) AddNode(n *Node) {
if w.nodes == nil {
w.nodes = []*Node{}
}
w.nodes = append(w.nodes, n)
}
func (w *Wrapper) Node() *Node {
if len(w.nodes) != 1 {
panic("BAD")
}
return w.nodes[0]
}
func (w *Wrapper) Hash() []byte {
return w.nodes[len(w.nodes)-1].Hash()
}
func (w *Wrapper) Commit(i int) {
// create tree from nodes
res, err := TreeFromNodes(w.nodes[i:], w.getLimit(i))
if err != nil {
panic(err)
}
// remove the old nodes
w.nodes = w.nodes[:i]
// add the new node
w.AddNode(res)
}
func (w *Wrapper) CommitWithMixin(i, num, limit int) {
// create tree from nodes
res, err := TreeFromNodesWithMixin(w.nodes[i:], num, limit)
if err != nil {
panic(err)
}
// remove the old nodes
w.nodes = w.nodes[:i]
// add the new node
w.AddNode(res)
}
func (w *Wrapper) AddEmpty() {
w.AddNode(EmptyLeaf())
}
func (w *Wrapper) getLimit(i int) int {
size := len(w.nodes[i:])
return int(nextPowerOfTwo(uint64(size)))
}