forked from ferranbt/fastssz
/
unmarshal.go
360 lines (308 loc) · 8.58 KB
/
unmarshal.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
359
360
package main
import (
"fmt"
"strconv"
"strings"
)
// unmarshal creates a function that decodes the structs with the input byte in SSZ format.
func (e *env) unmarshal(name string, v *Value) string {
tmpl := `// UnmarshalSSZ ssz unmarshals the {{.name}} object
func (:: *{{.name}}) UnmarshalSSZ(buf []byte) error {
var err error
{{.unmarshal}}
return err
}`
str := execTmpl(tmpl, map[string]interface{}{
"name": name,
"unmarshal": v.umarshalContainer(true, "buf"),
})
return appendObjSignature(str, v)
}
func (v *Value) unmarshal(dst string) string {
// we use dst as the input buffer where the SSZ data to decode the value is.
switch v.t {
case TypeContainer, TypeReference:
return v.umarshalContainer(false, dst)
case TypeBytes:
if v.c {
return fmt.Sprintf("copy(::.%s[:], %s)", v.name, dst)
}
validate := ""
if v.s == 0 {
// dynamic bytes, we need to validate the size of the buffer
validate = fmt.Sprintf("if len(%s) > %d { return ssz.ErrBytesLength }\n", dst, v.m)
}
// both fixed and dynamic are decoded equally
tmpl := `{{.validate}}if cap(::.{{.name}}) == 0 {
::.{{.name}} = make([]byte, 0, len({{.dst}}))
}
::.{{.name}} = append(::.{{.name}}, {{.dst}}...)`
return execTmpl(tmpl, map[string]interface{}{
"validate": validate,
"name": v.name,
"dst": dst,
"size": v.m,
})
case TypeUint:
if v.ref != "" {
// alias, we need to cast the value
return fmt.Sprintf("::.%s = %s.%s(ssz.Unmarshall%s(%s))", v.name, v.ref, v.obj, uintVToName(v), dst)
}
if v.obj != "" {
// alias to a type on the same package
return fmt.Sprintf("::.%s = %s(ssz.Unmarshall%s(%s))", v.name, v.obj, uintVToName(v), dst)
}
return fmt.Sprintf("::.%s = ssz.Unmarshall%s(%s)", v.name, uintVToName(v), dst)
case TypeBitList:
tmpl := `if err = ssz.ValidateBitlist({{.dst}}, {{.size}}); err != nil {
return err
}
if cap(::.{{.name}}) == 0 {
::.{{.name}} = make([]byte, 0, len({{.dst}}))
}
::.{{.name}} = append(::.{{.name}}, {{.dst}}...)`
return execTmpl(tmpl, map[string]interface{}{
"name": v.name,
"dst": dst,
"size": v.m,
})
case TypeVector:
if v.e.isFixed() {
dst = fmt.Sprintf("%s[ii*%d: (ii+1)*%d]", dst, v.e.n, v.e.n)
tmpl := `{{.create}}
for ii := 0; ii < {{.size}}; ii++ {
{{.unmarshal}}
}`
return execTmpl(tmpl, map[string]interface{}{
"create": v.createSlice(),
"size": v.s,
"unmarshal": v.e.unmarshal(dst),
})
}
fallthrough
case TypeList:
return v.unmarshalList()
case TypeBool:
return fmt.Sprintf("::.%s = ssz.UnmarshalBool(%s)", v.name, dst)
default:
panic(fmt.Errorf("unmarshal not implemented for type %d", v.t))
}
}
func (v *Value) unmarshalList() string {
// The Go field must have a 'ssz-max' tag to set the maximum number of items
maxSize := v.s
// In order to use createSlice with a dynamic list we need to set v.s to 0
v.s = 0
if v.e.isFixed() {
dst := fmt.Sprintf("buf[ii*%d: (ii+1)*%d]", v.e.n, v.e.n)
tmpl := `num, err := ssz.DivideInt2(len(buf), {{.size}}, {{.max}})
if err != nil {
return err
}
{{.create}}
for ii := 0; ii < num; ii++ {
{{.unmarshal}}
}`
return execTmpl(tmpl, map[string]interface{}{
"size": v.e.n,
"max": maxSize,
"create": v.createSlice(),
"unmarshal": v.e.unmarshal(dst),
})
}
if v.t == TypeVector {
panic("it cannot happen")
}
// Decode list with a dynamic element. 'ssz.DecodeDynamicLength' ensures
// that the number of elements do not surpass the 'ssz-max' tag.
tmpl := `num, err := ssz.DecodeDynamicLength(buf, {{.size}})
if err != nil {
return err
}
{{.create}}
err = ssz.UnmarshalDynamic(buf, num, func(indx int, buf []byte) (err error) {
{{.unmarshal}}
return nil
})
if err != nil {
return err
}`
v.e.name = v.name + "[indx]"
data := map[string]interface{}{
"size": maxSize,
"create": v.createSlice(),
"unmarshal": v.e.unmarshal("buf"),
}
return execTmpl(tmpl, data)
}
func (v *Value) umarshalContainer(start bool, dst string) (str string) {
if !start {
tmpl := `{{ if .check }}if ::.{{.name}} == nil {
::.{{.name}} = new({{.obj}})
}
{{ end }}if err = ::.{{.name}}.UnmarshalSSZ({{.dst}}); err != nil {
return err
}`
check := true
if v.noPtr {
check = false
}
return execTmpl(tmpl, map[string]interface{}{
"name": v.name,
"obj": v.objRef(),
"dst": dst,
"check": check,
})
}
var offsets []string
offsetsMatch := map[string]string{}
for indx, i := range v.o {
if !i.isFixed() {
name := "o" + strconv.Itoa(indx)
if len(offsets) != 0 {
offsetsMatch[name] = offsets[len(offsets)-1]
}
offsets = append(offsets, name)
}
}
// safe check for the size. Two cases:
// 1. Struct is fixed: The size of the input buffer must be the same as the struct.
// 2. Struct is dynamic. The size of the input buffer must be higher than the fixed part of the struct.
var cmp string
if v.isFixed() {
cmp = "!="
} else {
cmp = "<"
}
// If the struct is dynamic we create a set of offset variables that will be readed later.
tmpl := `size := uint64(len(buf))
if size {{.cmp}} {{.size}} {
return ssz.ErrSize
}
{{if .offsets}}
tail := buf
var {{.offsets}} uint64
{{end}}
`
str += execTmpl(tmpl, map[string]interface{}{
"cmp": cmp,
"size": v.n,
"offsets": strings.Join(offsets, ", "),
})
var o0 uint64
// Marshal the fixed part and offsets
// used for bounds checking of variable length offsets.
// for the first offset, use the size of the fixed-length data
// as the minimum boundary. subsequent offsets will replace this
// value with the name of the previous offset variable.
firstOffsetCheck := fmt.Sprintf("%d", v.n)
outs := []string{}
for indx, i := range v.o {
// How much it increases on every item
var incr uint64
if i.isFixed() {
incr = i.n
} else {
incr = 4
}
dst = fmt.Sprintf("%s[%d:%d]", "buf", o0, o0+incr)
o0 += incr
var res string
if i.isFixed() {
res = fmt.Sprintf("// Field (%d) '%s'\n%s\n\n", indx, i.name, i.unmarshal(dst))
} else {
// read the offset
offset := "o" + strconv.Itoa(indx)
data := map[string]interface{}{
"indx": indx,
"name": i.name,
"offset": offset,
"dst": dst,
"firstOffsetCheck": firstOffsetCheck,
}
// We need to do two validations for the offset:
// 1. The offset is lower than the total size of the input buffer
// 2. The offset i needs to be higher than the offset i-1 (Only if the offset is not the first).
if prev, ok := offsetsMatch[offset]; ok {
data["more"] = fmt.Sprintf(" || %s > %s", prev, offset)
} else {
data["more"] = ""
}
tmpl := `// Offset ({{.indx}}) '{{.name}}'
if {{.offset}} = ssz.ReadOffset({{.dst}}); {{.offset}} > size {{.more}} {
return ssz.ErrOffset
}
{{ if .firstOffsetCheck }}
if {{.offset}} < {{.firstOffsetCheck}} {
return ssz.ErrInvalidVariableOffset
}
{{ end }}
`
res = execTmpl(tmpl, data)
firstOffsetCheck = ""
}
outs = append(outs, res)
}
// Marshal the dynamic parts
c := 0
for indx, i := range v.o {
if !i.isFixed() {
from := offsets[c]
var to string
if c == len(offsets)-1 {
to = ""
} else {
to = offsets[c+1]
}
tmpl := `// Field ({{.indx}}) '{{.name}}'
{
buf = tail[{{.from}}:{{.to}}]
{{.unmarshal}}
}`
res := execTmpl(tmpl, map[string]interface{}{
"indx": indx,
"name": i.name,
"from": from,
"to": to,
"unmarshal": i.unmarshal("buf"),
})
outs = append(outs, res)
c++
}
}
str += strings.Join(outs, "\n\n")
return
}
// createItem is used to initialize slices of objects
func (v *Value) createSlice() string {
if v.t != TypeVector && v.t != TypeList {
panic("BUG: create item is only intended to be used with vectors and lists")
}
// If v.s is set (fixed slice) we use that value, otherwise (variable size)
// we assume there is a 'num' variable generated beforehand with the expected size.
var size string
if v.s == 0 {
size = "num"
} else {
size = strconv.Itoa(int(v.s))
}
switch v.e.t {
case TypeUint:
// []int uses the Extend functions in the fastssz package
return fmt.Sprintf("::.%s = ssz.Extend%s(::.%s, %s)", v.name, uintVToName(v.e), v.name, size)
case TypeContainer:
// []*(ref.)Struct{}
return fmt.Sprintf("::.%s = make([]*%s, %s)", v.name, v.e.objRef(), size)
case TypeBytes:
// [][]byte
if v.c {
return ""
}
if v.e.c {
return fmt.Sprintf("::.%s = make([][%d]byte, %s)", v.name, v.e.s, size)
}
return fmt.Sprintf("::.%s = make([][]byte, %s)", v.name, size)
default:
panic(fmt.Sprintf("create not implemented for type %s", v.e.t.String()))
}
}