-
Notifications
You must be signed in to change notification settings - Fork 665
/
type_codec.go
711 lines (659 loc) · 19.9 KB
/
type_codec.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package reflectcodec
import (
"bytes"
"errors"
"fmt"
"math"
"reflect"
"golang.org/x/exp/slices"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
// DefaultTagName that enables serialization.
const DefaultTagName = "serialize"
var (
_ codec.Codec = (*genericCodec)(nil)
errMarshalNil = errors.New("can't marshal nil pointer or interface")
errUnmarshalNil = errors.New("can't unmarshal nil")
errNeedPointer = errors.New("argument to unmarshal must be a pointer")
)
type TypeCodec interface {
// UnpackPrefix unpacks the prefix of an interface from the given packer.
// The prefix specifies the concrete type that the interface should be
// deserialized into. This function returns a new instance of that concrete
// type. The concrete type must implement the given type.
UnpackPrefix(*wrappers.Packer, reflect.Type) (reflect.Value, error)
// PackPrefix packs the prefix for the given type into the given packer.
// This identifies the bytes that follow, which are the byte representation
// of an interface, as having the given concrete type.
// When deserializing the bytes, the prefix specifies which concrete type
// to deserialize into.
PackPrefix(*wrappers.Packer, reflect.Type) error
// PrefixSize returns prefix length for the given type into the given
// packer.
PrefixSize(reflect.Type) int
}
// genericCodec handles marshaling and unmarshaling of structs with a generic
// implementation for interface encoding.
//
// A few notes:
//
// 1. We use "marshal" and "serialize" interchangeably, and "unmarshal" and
// "deserialize" interchangeably
// 2. To include a field of a struct in the serialized form, add the tag
// `{tagName}:"true"` to it. `{tagName}` defaults to `serialize`.
// 3. These typed members of a struct may be serialized:
// bool, string, uint[8,16,32,64], int[8,16,32,64],
// structs, slices, arrays, maps, interface.
// structs, slices, maps and arrays can only be serialized if their constituent
// values can be.
// 4. To marshal an interface, you must pass a pointer to the value
// 5. To unmarshal an interface, you must call
// codec.RegisterType([instance of the type that fulfills the interface]).
// 6. Serialized fields must be exported
// 7. nil slices are marshaled as empty slices
type genericCodec struct {
typer TypeCodec
maxSliceLen uint32
fielder StructFielder
}
// New returns a new, concurrency-safe codec
func New(typer TypeCodec, tagNames []string, maxSliceLen uint32) codec.Codec {
return &genericCodec{
typer: typer,
maxSliceLen: maxSliceLen,
fielder: NewStructFielder(tagNames, maxSliceLen),
}
}
func (c *genericCodec) Size(value interface{}) (int, error) {
if value == nil {
return 0, errMarshalNil // can't marshal nil
}
size, _, err := c.size(reflect.ValueOf(value))
return size, err
}
// size returns the size of the value along with whether the value is constant sized.
func (c *genericCodec) size(value reflect.Value) (int, bool, error) {
switch valueKind := value.Kind(); valueKind {
case reflect.Uint8:
return wrappers.ByteLen, true, nil
case reflect.Int8:
return wrappers.ByteLen, true, nil
case reflect.Uint16:
return wrappers.ShortLen, true, nil
case reflect.Int16:
return wrappers.ShortLen, true, nil
case reflect.Uint32:
return wrappers.IntLen, true, nil
case reflect.Int32:
return wrappers.IntLen, true, nil
case reflect.Uint64:
return wrappers.LongLen, true, nil
case reflect.Int64:
return wrappers.LongLen, true, nil
case reflect.Bool:
return wrappers.BoolLen, true, nil
case reflect.String:
return wrappers.StringLen(value.String()), false, nil
case reflect.Ptr:
if value.IsNil() {
// Can't marshal nil pointers (but nil slices are fine)
return 0, false, errMarshalNil
}
return c.size(value.Elem())
case reflect.Interface:
if value.IsNil() {
// Can't marshal nil interfaces (but nil slices are fine)
return 0, false, errMarshalNil
}
underlyingValue := value.Interface()
underlyingType := reflect.TypeOf(underlyingValue)
prefixSize := c.typer.PrefixSize(underlyingType)
valueSize, _, err := c.size(value.Elem())
if err != nil {
return 0, false, err
}
return prefixSize + valueSize, false, nil
case reflect.Slice:
numElts := value.Len()
if numElts == 0 {
return wrappers.IntLen, false, nil
}
size, constSize, err := c.size(value.Index(0))
if err != nil {
return 0, false, err
}
// For fixed-size types we manually calculate lengths rather than
// processing each element separately to improve performance.
if constSize {
return wrappers.IntLen + numElts*size, false, nil
}
for i := 1; i < numElts; i++ {
innerSize, _, err := c.size(value.Index(i))
if err != nil {
return 0, false, err
}
size += innerSize
}
return wrappers.IntLen + size, false, nil
case reflect.Array:
numElts := value.Len()
if numElts == 0 {
return 0, true, nil
}
size, constSize, err := c.size(value.Index(0))
if err != nil {
return 0, false, err
}
// For fixed-size types we manually calculate lengths rather than
// processing each element separately to improve performance.
if constSize {
return numElts * size, true, nil
}
for i := 1; i < numElts; i++ {
innerSize, _, err := c.size(value.Index(i))
if err != nil {
return 0, false, err
}
size += innerSize
}
return size, false, nil
case reflect.Struct:
serializedFields, err := c.fielder.GetSerializedFields(value.Type())
if err != nil {
return 0, false, err
}
var (
size int
constSize = true
)
for _, fieldDesc := range serializedFields {
innerSize, innerConstSize, err := c.size(value.Field(fieldDesc.Index))
if err != nil {
return 0, false, err
}
size += innerSize
constSize = constSize && innerConstSize
}
return size, constSize, nil
case reflect.Map:
iter := value.MapRange()
if !iter.Next() {
return wrappers.IntLen, false, nil
}
keySize, keyConstSize, err := c.size(iter.Key())
if err != nil {
return 0, false, err
}
valueSize, valueConstSize, err := c.size(iter.Value())
if err != nil {
return 0, false, err
}
switch {
case keyConstSize && valueConstSize:
numElts := value.Len()
return wrappers.IntLen + numElts*(keySize+valueSize), false, nil
case keyConstSize:
var (
numElts = 1
totalValueSize = valueSize
)
for iter.Next() {
valueSize, _, err := c.size(iter.Value())
if err != nil {
return 0, false, err
}
totalValueSize += valueSize
numElts++
}
return wrappers.IntLen + numElts*keySize + totalValueSize, false, nil
case valueConstSize:
var (
numElts = 1
totalKeySize = keySize
)
for iter.Next() {
keySize, _, err := c.size(iter.Key())
if err != nil {
return 0, false, err
}
totalKeySize += keySize
numElts++
}
return wrappers.IntLen + totalKeySize + numElts*valueSize, false, nil
default:
totalSize := wrappers.IntLen + keySize + valueSize
for iter.Next() {
keySize, _, err := c.size(iter.Key())
if err != nil {
return 0, false, err
}
valueSize, _, err := c.size(iter.Value())
if err != nil {
return 0, false, err
}
totalSize += keySize + valueSize
}
return totalSize, false, nil
}
default:
return 0, false, fmt.Errorf("can't evaluate marshal length of unknown kind %s", valueKind)
}
}
// To marshal an interface, [value] must be a pointer to the interface
func (c *genericCodec) MarshalInto(value interface{}, p *wrappers.Packer) error {
if value == nil {
return errMarshalNil // can't marshal nil
}
return c.marshal(reflect.ValueOf(value), p, c.maxSliceLen)
}
// marshal writes the byte representation of [value] to [p]
// [value]'s underlying value must not be a nil pointer or interface
// c.lock should be held for the duration of this function
func (c *genericCodec) marshal(value reflect.Value, p *wrappers.Packer, maxSliceLen uint32) error {
switch valueKind := value.Kind(); valueKind {
case reflect.Uint8:
p.PackByte(uint8(value.Uint()))
return p.Err
case reflect.Int8:
p.PackByte(uint8(value.Int()))
return p.Err
case reflect.Uint16:
p.PackShort(uint16(value.Uint()))
return p.Err
case reflect.Int16:
p.PackShort(uint16(value.Int()))
return p.Err
case reflect.Uint32:
p.PackInt(uint32(value.Uint()))
return p.Err
case reflect.Int32:
p.PackInt(uint32(value.Int()))
return p.Err
case reflect.Uint64:
p.PackLong(value.Uint())
return p.Err
case reflect.Int64:
p.PackLong(uint64(value.Int()))
return p.Err
case reflect.String:
p.PackStr(value.String())
return p.Err
case reflect.Bool:
p.PackBool(value.Bool())
return p.Err
case reflect.Ptr:
if value.IsNil() { // Can't marshal nil (except nil slices)
return errMarshalNil
}
return c.marshal(value.Elem(), p, c.maxSliceLen)
case reflect.Interface:
if value.IsNil() { // Can't marshal nil (except nil slices)
return errMarshalNil
}
underlyingValue := value.Interface()
underlyingType := reflect.TypeOf(underlyingValue)
if err := c.typer.PackPrefix(p, underlyingType); err != nil {
return err
}
if err := c.marshal(value.Elem(), p, c.maxSliceLen); err != nil {
return err
}
return p.Err
case reflect.Slice:
numElts := value.Len() // # elements in the slice/array. 0 if this slice is nil.
if uint32(numElts) > maxSliceLen {
return fmt.Errorf("%w; slice length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts,
maxSliceLen,
)
}
p.PackInt(uint32(numElts)) // pack # elements
if p.Err != nil {
return p.Err
}
if numElts == 0 {
// Returning here prevents execution of the (expensive) reflect
// calls below which check if the slice is []byte and, if it is,
// the call of value.Bytes()
return nil
}
// If this is a slice of bytes, manually pack the bytes rather
// than calling marshal on each byte. This improves performance.
if elemKind := value.Type().Elem().Kind(); elemKind == reflect.Uint8 {
p.PackFixedBytes(value.Bytes())
return p.Err
}
for i := 0; i < numElts; i++ { // Process each element in the slice
if err := c.marshal(value.Index(i), p, c.maxSliceLen); err != nil {
return err
}
}
return nil
case reflect.Array:
numElts := value.Len()
if elemKind := value.Type().Kind(); elemKind == reflect.Uint8 {
sliceVal := value.Convert(reflect.TypeOf([]byte{}))
p.PackFixedBytes(sliceVal.Bytes())
return p.Err
}
if uint32(numElts) > c.maxSliceLen {
return fmt.Errorf("%w; array length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts,
c.maxSliceLen,
)
}
for i := 0; i < numElts; i++ { // Process each element in the array
if err := c.marshal(value.Index(i), p, c.maxSliceLen); err != nil {
return err
}
}
return nil
case reflect.Struct:
serializedFields, err := c.fielder.GetSerializedFields(value.Type())
if err != nil {
return err
}
for _, fieldDesc := range serializedFields { // Go through all fields of this struct that are serialized
if err := c.marshal(value.Field(fieldDesc.Index), p, fieldDesc.MaxSliceLen); err != nil { // Serialize the field and write to byte array
return err
}
}
return nil
case reflect.Map:
keys := value.MapKeys()
numElts := len(keys)
if uint32(numElts) > maxSliceLen {
return fmt.Errorf("%w; map length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts,
maxSliceLen,
)
}
p.PackInt(uint32(numElts)) // pack # elements
if p.Err != nil {
return p.Err
}
// pack key-value pairs sorted by increasing key
type keyTuple struct {
key reflect.Value
startIndex int
endIndex int
}
sortedKeys := make([]keyTuple, len(keys))
startOffset := p.Offset
endOffset := p.Offset
for i, key := range keys {
if err := c.marshal(key, p, c.maxSliceLen); err != nil {
return err
}
if p.Err != nil {
return fmt.Errorf("couldn't marshal map key %+v: %w ", key, p.Err)
}
sortedKeys[i] = keyTuple{
key: key,
startIndex: endOffset,
endIndex: p.Offset,
}
endOffset = p.Offset
}
slices.SortFunc(sortedKeys, func(a, b keyTuple) bool {
aBytes := p.Bytes[a.startIndex:a.endIndex]
bBytes := p.Bytes[b.startIndex:b.endIndex]
return bytes.Compare(aBytes, bBytes) < 0
})
allKeyBytes := slices.Clone(p.Bytes[startOffset:p.Offset])
p.Offset = startOffset
for _, key := range sortedKeys {
// pack key
startIndex := key.startIndex - startOffset
endIndex := key.endIndex - startOffset
keyBytes := allKeyBytes[startIndex:endIndex]
p.PackFixedBytes(keyBytes)
if p.Err != nil {
return p.Err
}
// serialize and pack value
if err := c.marshal(value.MapIndex(key.key), p, c.maxSliceLen); err != nil {
return err
}
}
return nil
default:
return fmt.Errorf("%w: %s", codec.ErrUnsupportedType, valueKind)
}
}
// Unmarshal unmarshals [bytes] into [dest], where
// [dest] must be a pointer or interface
func (c *genericCodec) Unmarshal(bytes []byte, dest interface{}) error {
if dest == nil {
return errUnmarshalNil
}
p := wrappers.Packer{
Bytes: bytes,
}
destPtr := reflect.ValueOf(dest)
if destPtr.Kind() != reflect.Ptr {
return errNeedPointer
}
if err := c.unmarshal(&p, destPtr.Elem(), c.maxSliceLen); err != nil {
return err
}
if p.Offset != len(bytes) {
return fmt.Errorf("%w: read %d provided %d",
codec.ErrExtraSpace,
p.Offset,
len(bytes),
)
}
return nil
}
// Unmarshal from p.Bytes into [value]. [value] must be addressable.
// c.lock should be held for the duration of this function
func (c *genericCodec) unmarshal(p *wrappers.Packer, value reflect.Value, maxSliceLen uint32) error {
switch value.Kind() {
case reflect.Uint8:
value.SetUint(uint64(p.UnpackByte()))
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal uint8: %w", p.Err)
}
return nil
case reflect.Int8:
value.SetInt(int64(p.UnpackByte()))
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal int8: %w", p.Err)
}
return nil
case reflect.Uint16:
value.SetUint(uint64(p.UnpackShort()))
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal uint16: %w", p.Err)
}
return nil
case reflect.Int16:
value.SetInt(int64(p.UnpackShort()))
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal int16: %w", p.Err)
}
return nil
case reflect.Uint32:
value.SetUint(uint64(p.UnpackInt()))
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal uint32: %w", p.Err)
}
return nil
case reflect.Int32:
value.SetInt(int64(p.UnpackInt()))
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal int32: %w", p.Err)
}
return nil
case reflect.Uint64:
value.SetUint(p.UnpackLong())
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal uint64: %w", p.Err)
}
return nil
case reflect.Int64:
value.SetInt(int64(p.UnpackLong()))
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal int64: %w", p.Err)
}
return nil
case reflect.Bool:
value.SetBool(p.UnpackBool())
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal bool: %w", p.Err)
}
return nil
case reflect.Slice:
numElts32 := p.UnpackInt()
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal slice: %w", p.Err)
}
if numElts32 > maxSliceLen {
return fmt.Errorf("%w; array length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts32,
maxSliceLen,
)
}
if numElts32 > math.MaxInt32 {
return fmt.Errorf("%w; array length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts32,
math.MaxInt32,
)
}
numElts := int(numElts32)
// If this is a slice of bytes, manually unpack the bytes rather
// than calling unmarshal on each byte. This improves performance.
if elemKind := value.Type().Elem().Kind(); elemKind == reflect.Uint8 {
value.SetBytes(p.UnpackFixedBytes(numElts))
return p.Err
}
// set [value] to be a slice of the appropriate type/capacity (right now it is nil)
value.Set(reflect.MakeSlice(value.Type(), numElts, numElts))
// Unmarshal each element into the appropriate index of the slice
for i := 0; i < numElts; i++ {
if err := c.unmarshal(p, value.Index(i), c.maxSliceLen); err != nil {
return fmt.Errorf("couldn't unmarshal slice element: %w", err)
}
}
return nil
case reflect.Array:
numElts := value.Len()
if elemKind := value.Type().Elem().Kind(); elemKind == reflect.Uint8 {
unpackedBytes := p.UnpackFixedBytes(numElts)
if p.Errored() {
return p.Err
}
// Get a slice to the underlying array value
underlyingSlice := value.Slice(0, numElts).Interface().([]byte)
copy(underlyingSlice, unpackedBytes)
return nil
}
for i := 0; i < numElts; i++ {
if err := c.unmarshal(p, value.Index(i), c.maxSliceLen); err != nil {
return fmt.Errorf("couldn't unmarshal array element: %w", err)
}
}
return nil
case reflect.String:
value.SetString(p.UnpackStr())
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal string: %w", p.Err)
}
return nil
case reflect.Interface:
intfImplementor, err := c.typer.UnpackPrefix(p, value.Type())
if err != nil {
return err
}
// Unmarshal into the struct
if err := c.unmarshal(p, intfImplementor, c.maxSliceLen); err != nil {
return fmt.Errorf("couldn't unmarshal interface: %w", err)
}
// And assign the filled struct to the value
value.Set(intfImplementor)
return nil
case reflect.Struct:
// Get indices of fields that will be unmarshaled into
serializedFieldIndices, err := c.fielder.GetSerializedFields(value.Type())
if err != nil {
return fmt.Errorf("couldn't unmarshal struct: %w", err)
}
// Go through the fields and umarshal into them
for _, fieldDesc := range serializedFieldIndices {
if err := c.unmarshal(p, value.Field(fieldDesc.Index), fieldDesc.MaxSliceLen); err != nil {
return fmt.Errorf("couldn't unmarshal struct: %w", err)
}
}
return nil
case reflect.Ptr:
// Get the type this pointer points to
t := value.Type().Elem()
// Create a new pointer to a new value of the underlying type
v := reflect.New(t)
// Fill the value
if err := c.unmarshal(p, v.Elem(), c.maxSliceLen); err != nil {
return fmt.Errorf("couldn't unmarshal pointer: %w", err)
}
// Assign to the top-level struct's member
value.Set(v)
return nil
case reflect.Map:
numElts32 := p.UnpackInt()
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal map: %w", p.Err)
}
if numElts32 > c.maxSliceLen {
return fmt.Errorf("%w; map length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts32,
c.maxSliceLen,
)
}
var (
numElts = int(numElts32)
mapType = value.Type()
mapKeyType = mapType.Key()
mapValueType = mapType.Elem()
prevKey []byte
)
// Set [value] to be a new map of the appropriate type.
value.Set(reflect.MakeMapWithSize(mapType, numElts))
for i := 0; i < numElts; i++ {
mapKey := reflect.New(mapKeyType).Elem()
keyStartOffset := p.Offset
if err := c.unmarshal(p, mapKey, c.maxSliceLen); err != nil {
return fmt.Errorf("couldn't unmarshal map key (%s): %w", mapKeyType, err)
}
// Get the key's byte representation and check that the new key is
// actually bigger (according to bytes.Compare) than the previous
// key.
//
// We do this to enforce that key-value pairs are sorted by
// increasing key.
keyBytes := p.Bytes[keyStartOffset:p.Offset]
if i != 0 && bytes.Compare(keyBytes, prevKey) <= 0 {
return fmt.Errorf("keys aren't sorted: (%s, %s)", prevKey, mapKey)
}
prevKey = keyBytes
// Get the value
mapValue := reflect.New(mapValueType).Elem()
if err := c.unmarshal(p, mapValue, c.maxSliceLen); err != nil {
return fmt.Errorf("couldn't unmarshal map value for key %s: %w", mapKey, err)
}
// Assign the key-value pair in the map
value.SetMapIndex(mapKey, mapValue)
}
return nil
default:
return fmt.Errorf("can't unmarshal unknown type %s", value.Kind().String())
}
}