-
Notifications
You must be signed in to change notification settings - Fork 12
/
decoder_borsh.go
458 lines (414 loc) · 11.1 KB
/
decoder_borsh.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright 2021 github.com/gagliardetto
// This file has been modified by github.com/gagliardetto
//
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bin
import (
"errors"
"fmt"
"io"
"reflect"
"go.uber.org/zap"
)
func (dec *Decoder) decodeWithOptionBorsh(v interface{}, option *option) (err error) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr {
return &InvalidDecoderError{reflect.TypeOf(v)}
}
// We decode rv not rv.Elem because the Unmarshaler interface
// test must be applied at the top level of the value.
err = dec.decodeBorsh(rv, option)
if err != nil {
return err
}
return nil
}
func (dec *Decoder) decodeBorsh(rv reflect.Value, opt *option) (err error) {
if opt == nil {
opt = newDefaultOption()
}
dec.currentFieldOpt = opt
unmarshaler, rv := indirect(rv, opt.is_Optional() || opt.is_COptional())
if traceEnabled {
zlog.Debug("decode: type",
zap.Stringer("value_kind", rv.Kind()),
zap.Bool("has_unmarshaler", (unmarshaler != nil)),
zap.Reflect("options", opt),
)
}
if opt.is_Optional() {
isPresent, e := dec.ReadOption()
if e != nil {
err = fmt.Errorf("decode: %t isPresent, %s", rv.Type(), e)
return
}
if !isPresent {
if traceEnabled {
zlog.Debug("decode: skipping optional value", zap.Stringer("type", rv.Kind()))
}
rv.Set(reflect.Zero(rv.Type()))
return
}
// we have ptr here we should not go get the element
unmarshaler, rv = indirect(rv, false)
}
if opt.is_COptional() {
isPresent, e := dec.ReadCOption()
if e != nil {
err = fmt.Errorf("decode: %t isPresent, %s", rv.Type(), e)
return
}
if !isPresent {
if traceEnabled {
zlog.Debug("decode: skipping optional value", zap.Stringer("type", rv.Kind()))
}
rv.Set(reflect.Zero(rv.Type()))
return
}
// we have ptr here we should not go get the element
unmarshaler, rv = indirect(rv, false)
}
// Reset optionality so it won't propagate to child types:
opt = opt.clone().set_Optional(false).set_COptional(false)
if unmarshaler != nil {
if traceEnabled {
zlog.Debug("decode: using UnmarshalWithDecoder method to decode type")
}
return unmarshaler.UnmarshalWithDecoder(dec)
}
rt := rv.Type()
switch rv.Kind() {
// case reflect.Int:
// // TODO: check if is x32 or x64
// var n int64
// n, err = dec.ReadInt64(LE)
// rv.SetInt(n)
// return
// case reflect.Uint:
// // TODO: check if is x32 or x64
// var n uint64
// n, err = dec.ReadUint64(LE)
// rv.SetUint(n)
// return
case reflect.String:
s, e := dec.ReadString()
if e != nil {
err = e
return
}
rv.SetString(s)
return
case reflect.Uint8:
var n byte
n, err = dec.ReadByte()
rv.SetUint(uint64(n))
return
case reflect.Int8:
var n int8
n, err = dec.ReadInt8()
rv.SetInt(int64(n))
return
case reflect.Int16:
var n int16
n, err = dec.ReadInt16(LE)
rv.SetInt(int64(n))
return
case reflect.Int32:
var n int32
n, err = dec.ReadInt32(LE)
rv.SetInt(int64(n))
return
case reflect.Int64:
var n int64
n, err = dec.ReadInt64(LE)
rv.SetInt(int64(n))
return
case reflect.Uint16:
var n uint16
n, err = dec.ReadUint16(LE)
rv.SetUint(uint64(n))
return
case reflect.Uint32:
var n uint32
n, err = dec.ReadUint32(LE)
rv.SetUint(uint64(n))
return
case reflect.Uint64:
var n uint64
n, err = dec.ReadUint64(LE)
rv.SetUint(n)
return
case reflect.Float32:
var n float32
n, err = dec.ReadFloat32(LE)
rv.SetFloat(float64(n))
return
case reflect.Float64:
var n float64
n, err = dec.ReadFloat64(LE)
rv.SetFloat(n)
return
case reflect.Bool:
var r bool
r, err = dec.ReadBool()
rv.SetBool(r)
return
case reflect.Interface:
// Skip: cannot know the concrete type of the interface.
// The parent container should implement a custom decoder.
return nil
// TODO: handle reflect.Ptr ???
}
switch rt.Kind() {
case reflect.Array:
l := rt.Len()
if traceEnabled {
zlog.Debug("decoding: reading array", zap.Int("length", l))
}
switch k := rv.Type().Elem().Kind(); k {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if err := reflect_readArrayOfUint_(dec, l, k, rv, LE); err != nil {
return err
}
default:
for i := 0; i < l; i++ {
if err = dec.decodeBorsh(rv.Index(i), nil); err != nil {
return
}
}
}
return
case reflect.Slice:
var l int
if opt.hasSizeOfSlice() {
l = opt.getSizeOfSlice()
} else {
length, err := dec.ReadUint32(LE)
if err != nil {
return err
}
l = int(length)
}
if traceEnabled {
zlog.Debug("reading slice", zap.Int("len", l), typeField("type", rv))
}
if l == 0 {
// Empty slices are left nil
return
}
if l > dec.Remaining() {
return io.ErrUnexpectedEOF
}
switch k := rv.Type().Elem().Kind(); k {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if err := reflect_readArrayOfUint_(dec, l, k, rv, LE); err != nil {
return err
}
default:
rv.Set(reflect.MakeSlice(rt, 0, 0))
for i := 0; i < l; i++ {
// create new element of type rt:
element := reflect.New(rt.Elem())
// decode into element:
if err = dec.decodeBorsh(element, nil); err != nil {
return
}
// append to slice:
rv.Set(reflect.Append(rv, element.Elem()))
}
}
case reflect.Struct:
if err = dec.decodeStructBorsh(rt, rv); err != nil {
return
}
case reflect.Map:
l, err := dec.ReadUint32(LE)
if err != nil {
return err
}
if l == 0 {
// If the map has no content, keep it nil.
return nil
}
rv.Set(reflect.MakeMap(rt))
for i := 0; i < int(l); i++ {
key := reflect.New(rt.Key())
err := dec.decodeBorsh(key.Elem(), nil)
if err != nil {
return err
}
val := reflect.New(rt.Elem())
err = dec.decodeBorsh(val.Elem(), nil)
if err != nil {
return err
}
rv.SetMapIndex(key.Elem(), val.Elem())
}
return nil
default:
return fmt.Errorf("decode: unsupported type %q", rt)
}
return
}
func (dec *Decoder) deserializeComplexEnum(rv reflect.Value) error {
rt := rv.Type()
// read enum identifier
tmp, err := dec.ReadUint8()
if err != nil {
return err
}
enum := BorshEnum(tmp)
rv.Field(0).Set(reflect.ValueOf(enum).Convert(rv.Field(0).Type()))
// read enum field, if necessary
if int(enum)+1 >= rt.NumField() {
return errors.New("complex enum too large")
}
field := rv.Field(int(enum) + 1)
return dec.decodeBorsh(field, nil)
}
var borshEnumType = reflect.TypeOf(BorshEnum(0))
func isTypeBorshEnum(typ reflect.Type) bool {
return typ.Kind() == reflect.Uint8 && typ == borshEnumType
}
func (dec *Decoder) decodeStructBorsh(rt reflect.Type, rv reflect.Value) (err error) {
l := rv.NumField()
if traceEnabled {
zlog.Debug("decode: struct", zap.Int("fields", l), zap.Stringer("type", rv.Kind()))
}
// Handle complex enum:
if rt.NumField() > 0 {
// If the first field has type BorshEnum and is flagged with "borsh_enum"
// we have a complex enum:
firstField := rt.Field(0)
if isTypeBorshEnum(firstField.Type) &&
parseFieldTag(firstField.Tag).IsBorshEnum {
return dec.deserializeComplexEnum(rv)
}
}
sizeOfMap := map[string]int{}
seenBinaryExtensionField := false
for i := 0; i < l; i++ {
structField := rt.Field(i)
fieldTag := parseFieldTag(structField.Tag)
if fieldTag.Skip {
if traceEnabled {
zlog.Debug("decode: skipping struct field with skip flag",
zap.String("struct_field_name", structField.Name),
)
}
continue
}
if !fieldTag.BinaryExtension && seenBinaryExtensionField {
panic(fmt.Sprintf("the `bin:\"binary_extension\"` tags must be packed together at the end of struct fields, problematic field %q", structField.Name))
}
if fieldTag.BinaryExtension {
seenBinaryExtensionField = true
// FIXME: This works only if what is in `d.data` is the actual full data buffer that
// needs to be decoded. If there is for example two structs in the buffer, this
// will not work as we would continue into the next struct.
//
// But at the same time, does it make sense otherwise? What would be the inference
// rule in the case of extra bytes available? Continue decoding and revert if it's
// not working? But how to detect valid errors?
if len(dec.data[dec.pos:]) <= 0 {
continue
}
}
v := rv.Field(i)
if !v.CanSet() {
// This means that the field cannot be set, to fix this
// we need to create a pointer to said field
if !v.CanAddr() {
// we cannot create a point to field skipping
if traceEnabled {
zlog.Debug("skipping struct field that cannot be addressed",
zap.String("struct_field_name", structField.Name),
zap.Stringer("struct_value_type", v.Kind()),
)
}
return fmt.Errorf("unable to decode a none setup struc field %q with type %q", structField.Name, v.Kind())
}
v = v.Addr()
}
if !v.CanSet() {
if traceEnabled {
zlog.Debug("skipping struct field that cannot be addressed",
zap.String("struct_field_name", structField.Name),
zap.Stringer("struct_value_type", v.Kind()),
)
}
continue
}
option := &option{
is_OptionalField: fieldTag.Option,
is_COptionalField: fieldTag.COption,
Order: fieldTag.Order,
}
if s, ok := sizeOfMap[structField.Name]; ok {
option.setSizeOfSlice(s)
}
if traceEnabled {
zlog.Debug("decode: struct field",
zap.Stringer("struct_field_value_type", v.Kind()),
zap.String("struct_field_name", structField.Name),
zap.Reflect("struct_field_tags", fieldTag),
zap.Reflect("struct_field_option", option),
)
}
rt := v.Type()
ptrImplements := reflect.PtrTo(rt).Implements(unmarshalableType)
vImplements := rt.Implements(unmarshalableType)
if ptrImplements || vImplements {
switch {
case ptrImplements:
m := reflect.New(rt)
val := m.Interface()
err := val.(BinaryUnmarshaler).UnmarshalWithDecoder(dec)
if err != nil {
return err
}
v.Set(reflect.ValueOf(val).Elem())
continue
case vImplements:
m := reflect.New(rt.Elem())
val := m.Interface()
err := val.(BinaryUnmarshaler).UnmarshalWithDecoder(dec)
if err != nil {
return err
}
v.Set(reflect.ValueOf(val))
continue
}
}
if err = dec.decodeBorsh(v, option); err != nil {
return fmt.Errorf("error while decoding %q field: %w", structField.Name, err)
}
if fieldTag.SizeOf != "" {
size := sizeof(structField.Type, v)
if traceEnabled {
zlog.Debug("setting size of field",
zap.String("field_name", fieldTag.SizeOf),
zap.Int("size", size),
)
}
sizeOfMap[fieldTag.SizeOf] = size
}
}
return
}
var (
marshalableType = reflect.TypeOf((*BinaryMarshaler)(nil)).Elem()
unmarshalableType = reflect.TypeOf((*BinaryUnmarshaler)(nil)).Elem()
)