-
Notifications
You must be signed in to change notification settings - Fork 8
/
encoder.go
1096 lines (1021 loc) · 37.2 KB
/
encoder.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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ssz: Go Simple Serialize (SSZ) codec library
// Copyright 2024 ssz Authors
// SPDX-License-Identifier: BSD-3-Clause
package ssz
import (
"encoding/binary"
"io"
"math/big"
"reflect"
"unsafe"
"github.com/holiman/uint256"
"github.com/prysmaticlabs/go-bitfield"
)
// Some helpers to avoid occasional allocations
var (
boolFalse = []byte{0x00}
boolTrue = []byte{0x01}
uint256Zero = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
bitlistZero = bitfield.NewBitlist(0)
)
// Encoder is a wrapper around an io.Writer or a []byte buffer to implement SSZ
// encoding in a streaming or buffered way. It has the following behaviors:
//
// 1. The encoder does not buffer, simply writes to the wrapped output stream
// directly. If you need buffering (and flushing), that is up to you.
//
// 2. The encoder does not return errors that were hit during writing to the
// underlying output stream from individual encoding methods. Since there
// is no expectation (in general) for failure, user code can be denser if
// error checking is done at the end. Internally, of course, an error will
// halt all future output operations.
//
// 3. The offsets for dynamic fields are tracked internally by the encoder, so
// the caller only needs to provide the field, the offset of which should be
// included at the allotted slot.
//
// 4. The contents for dynamic fields are not appended explicitly, rather the
// caller needs to provide them once more at the end of encoding. This is a
// design choice to keep the encoder 0-alloc (vs having to stash away the
// dynamic fields internally).
//
// 5. The encoder does not enforce defined size limits on the dynamic fields.
// If the caller provided bad data to encode, it is a programming error and
// a runtime error will not fix anything.
//
// Internally there are a few implementation details that maintainers need to be
// aware of when modifying the code:
//
// 1. The encoder supports two modes of operation: streaming and buffered. Any
// high level Go code would achieve that with two encoder types implementing
// a common interface. Unfortunately, the EncodeXYZ methods are using Go's
// generic system, which is not supported on struct/interface *methods*. As
// such, `Encoder.EncodeUint64s[T ~uint64](ns []T)` style methods cannot be
// used, only `EncodeUint64s[T ~uint64](end *Encoder, ns []T)`. The latter
// form then requires each method internally to do some soft of type cast to
// handle different encoder implementations. To avoid runtime type asserts,
// we've opted for a combo encoder with 2 possible outputs and switching on
// which one is set. Elegant? No. Fast? Yes.
//
// 2. A lot of code snippets are repeated (e.g. encoding the offset, which is
// the exact same for all the different types, yet the code below has them
// copied verbatim). Unfortunately the Go compiler doesn't inline functions
// aggressively enough (neither does it allow explicitly directing it to),
// and in such tight loops, extra calls matter on performance.
type Encoder struct {
outWriter io.Writer // Underlying output stream to write into (streaming mode)
outBuffer []byte // Underlying output stream to write into (buffered mode)
err error // Any write error to halt future encoding calls
codec *Codec // Self-referencing to pass DefineSSZ calls through (API trick)
sizer *Sizer // Self-referencing to pass SizeSSZ call through (API trick)
buf [32]byte // Integer conversion buffer
bufInt uint256.Int // Big.Int conversion buffer (not pointer, alloc free)
offset uint32 // Offset tracker for dynamic fields
}
// EncodeBool serializes a boolean.
func EncodeBool[T ~bool](enc *Encoder, v T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
if !v {
_, enc.err = enc.outWriter.Write(boolFalse)
} else {
_, enc.err = enc.outWriter.Write(boolTrue)
}
} else {
if !v {
enc.outBuffer[0] = 0x00
} else {
enc.outBuffer[0] = 0x01
}
enc.outBuffer = enc.outBuffer[1:]
}
}
// EncodeBoolPointerOnFork serializes a boolean if present in a fork.
//
// Note, a nil pointer is serialized as false.
func EncodeBoolPointerOnFork[T ~bool](enc *Encoder, v *T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
if v == nil {
EncodeBool[bool](enc, false)
return
}
EncodeBool(enc, *v)
}
// EncodeUint8 serializes a uint8.
func EncodeUint8[T ~uint8](enc *Encoder, n T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
enc.buf[0] = byte(n)
_, enc.err = enc.outWriter.Write(enc.buf[:1])
} else {
enc.outBuffer[0] = byte(n)
enc.outBuffer = enc.outBuffer[1:]
}
}
// EncodeUint8PointerOnFork serializes a uint8 if present in a fork.
//
// Note, a nil pointer is serialized as zero.
func EncodeUint8PointerOnFork[T ~uint8](enc *Encoder, n *T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
if n == nil {
EncodeUint8[uint8](enc, 0)
return
}
EncodeUint8(enc, *n)
}
// EncodeUint16 serializes a uint16.
func EncodeUint16[T ~uint16](enc *Encoder, n T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint16(enc.buf[:2], (uint16)(n))
_, enc.err = enc.outWriter.Write(enc.buf[:2])
} else {
binary.LittleEndian.PutUint16(enc.outBuffer, (uint16)(n))
enc.outBuffer = enc.outBuffer[2:]
}
}
// EncodeUint16PointerOnFork serializes a uint16 if present in a fork.
//
// Note, a nil pointer is serialized as zero.
func EncodeUint16PointerOnFork[T ~uint16](enc *Encoder, n *T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
if n == nil {
EncodeUint16[uint16](enc, 0)
return
}
EncodeUint16(enc, *n)
}
// EncodeUint32 serializes a uint32.
func EncodeUint32[T ~uint32](enc *Encoder, n T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], (uint32)(n))
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, (uint32)(n))
enc.outBuffer = enc.outBuffer[4:]
}
}
// EncodeUint32PointerOnFork serializes a uint32 if present in a fork.
//
// Note, a nil pointer is serialized as zero.
func EncodeUint32PointerOnFork[T ~uint32](enc *Encoder, n *T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
if n == nil {
EncodeUint32[uint32](enc, 0)
return
}
EncodeUint32(enc, *n)
}
// EncodeUint64 serializes a uint64.
func EncodeUint64[T ~uint64](enc *Encoder, n T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint64(enc.buf[:8], (uint64)(n))
_, enc.err = enc.outWriter.Write(enc.buf[:8])
} else {
binary.LittleEndian.PutUint64(enc.outBuffer, (uint64)(n))
enc.outBuffer = enc.outBuffer[8:]
}
}
// EncodeUint64PointerOnFork serializes a uint64 if present in a fork.
//
// Note, a nil pointer is serialized as zero.
func EncodeUint64PointerOnFork[T ~uint64](enc *Encoder, n *T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
if n == nil {
EncodeUint64[uint64](enc, 0)
return
}
EncodeUint64(enc, *n)
}
// EncodeUint256 serializes a uint256.
//
// Note, a nil pointer is serialized as zero.
func EncodeUint256(enc *Encoder, n *uint256.Int) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
if n != nil {
n.MarshalSSZInto(enc.buf[:32])
_, enc.err = enc.outWriter.Write(enc.buf[:32])
} else {
_, enc.err = enc.outWriter.Write(uint256Zero)
}
} else {
if n != nil {
n.MarshalSSZInto(enc.outBuffer)
} else {
copy(enc.outBuffer, uint256Zero)
}
enc.outBuffer = enc.outBuffer[32:]
}
}
// EncodeUint256OnFork serializes a uint256 if present in a fork.
//
// Note, a nil pointer is serialized as zero.
func EncodeUint256OnFork(enc *Encoder, n *uint256.Int, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeUint256(enc, n)
}
// EncodeUint256BigInt serializes a big.Int as uint256.
//
// Note, a nil pointer is serialized as zero.
// Note, an overflow will be silently dropped.
func EncodeUint256BigInt(enc *Encoder, n *big.Int) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
if n != nil {
enc.bufInt.SetFromBig(n)
enc.bufInt.MarshalSSZInto(enc.buf[:32])
_, enc.err = enc.outWriter.Write(enc.buf[:32])
} else {
_, enc.err = enc.outWriter.Write(uint256Zero)
}
} else {
if n != nil {
enc.bufInt.SetFromBig(n)
enc.bufInt.MarshalSSZInto(enc.outBuffer)
} else {
copy(enc.outBuffer, uint256Zero)
}
enc.outBuffer = enc.outBuffer[32:]
}
}
// EncodeUint256BigIntOnFork serializes a big.Int as uint256 if present in a
// fork.
//
// Note, a nil pointer is serialized as zero.
// Note, an overflow will be silently dropped.
func EncodeUint256BigIntOnFork(enc *Encoder, n *big.Int, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeUint256BigInt(enc, n)
}
// EncodeStaticBytes serializes a static binary blob.
//
// The blob is passed by pointer to avoid high stack copy costs and a potential
// escape to the heap.
func EncodeStaticBytes[T commonBytesLengths](enc *Encoder, blob *T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
// The code below should have used `*blob[:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
_, enc.err = enc.outWriter.Write(unsafe.Slice(&(*blob)[0], len(*blob)))
} else {
// The code below should have used `blob[:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
copy(enc.outBuffer, unsafe.Slice(&(*blob)[0], len(*blob)))
enc.outBuffer = enc.outBuffer[len(*blob):]
}
}
// EncodeStaticBytesPointerOnFork serializes a static binary blob if present in
// a fork.
//
// Note, a nil pointer is serialized as a zero-value blob.
func EncodeStaticBytesPointerOnFork[T commonBytesLengths](enc *Encoder, blob *T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
if blob == nil {
enc.encodeZeroes(reflect.TypeFor[T]().Len())
return
}
EncodeStaticBytes(enc, blob)
}
// EncodeCheckedStaticBytes serializes a static binary blob.
func EncodeCheckedStaticBytes(enc *Encoder, blob []byte, size uint64) {
// If the blob is nil, write a batch of zeroes and exit
if blob == nil {
enc.encodeZeroes(int(size))
return
}
// Blob not nil, write the actual data content
if enc.outWriter != nil {
if enc.err != nil {
return
}
_, enc.err = enc.outWriter.Write(blob)
} else {
copy(enc.outBuffer, blob)
enc.outBuffer = enc.outBuffer[len(blob):]
}
}
// EncodeDynamicBytesOffset serializes a dynamic binary blob.
func EncodeDynamicBytesOffset(enc *Encoder, blob []byte) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], enc.offset)
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, enc.offset)
enc.outBuffer = enc.outBuffer[4:]
}
enc.offset += uint32(len(blob))
}
// EncodeDynamicBytesOffsetOnFork serializes a dynamic binary blob if present in
// a fork.
func EncodeDynamicBytesOffsetOnFork(enc *Encoder, blob []byte, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeDynamicBytesOffset(enc, blob)
}
// EncodeDynamicBytesContent is the lazy data writer for EncodeDynamicBytesOffset.
func EncodeDynamicBytesContent(enc *Encoder, blob []byte) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
_, enc.err = enc.outWriter.Write(blob)
} else {
copy(enc.outBuffer, blob)
enc.outBuffer = enc.outBuffer[len(blob):]
}
}
// EncodeDynamicBytesContentOnFork is the lazy data writer for EncodeDynamicBytesOffsetOnFork.
func EncodeDynamicBytesContentOnFork(enc *Encoder, blob []byte, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeDynamicBytesContent(enc, blob)
}
// EncodeStaticObject serializes a static ssz object.
//
// Note, nil will be encoded as a zero-value initialized object.
func EncodeStaticObject[T newableStaticObject[U], U any](enc *Encoder, obj T) {
if enc.err != nil {
return
}
if obj == nil {
// If the object is nil, pull up it's zero value. This will be very slow,
// but it should not happen in production, only during tests mostly.
obj = zeroValueStatic[T, U]()
}
obj.DefineSSZ(enc.codec)
}
// EncodeStaticObjectOnFork serializes a static ssz object is present in a fork.
//
// Note, nil will be encoded as a zero-value initialized object.
func EncodeStaticObjectOnFork[T newableStaticObject[U], U any](enc *Encoder, obj T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeStaticObject(enc, obj)
}
// EncodeDynamicObjectOffset serializes a dynamic ssz object.
//
// Note, nil will be encoded as a zero-value initialized object.
func EncodeDynamicObjectOffset[T newableDynamicObject[U], U any](enc *Encoder, obj T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], enc.offset)
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, enc.offset)
enc.outBuffer = enc.outBuffer[4:]
}
// If the object is nil, pull up it's zero value. This will be very slow, but
// it should not happen in production, only during tests mostly.
if obj == nil {
obj = zeroValueDynamic[T, U]()
}
enc.offset += obj.SizeSSZ(enc.sizer, false)
}
// EncodeDynamicObjectOffsetOnFork serializes a dynamic ssz object if present in
// a fork.
//
// Note, nil will be encoded as a zero-value initialized object.
func EncodeDynamicObjectOffsetOnFork[T newableDynamicObject[U], U any](enc *Encoder, obj T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeDynamicObjectOffset(enc, obj)
}
// EncodeDynamicObjectContent is the lazy data writer for EncodeDynamicObjectOffset.
//
// Note, nil will be encoded as a zero-value initialized object.
func EncodeDynamicObjectContent[T newableDynamicObject[U], U any](enc *Encoder, obj T) {
if enc.err != nil {
return
}
// If the object is nil, pull up it's zero value. This will be very slow, but
// it should not happen in production, only during tests mostly.
if obj == nil {
obj = zeroValueDynamic[T, U]()
}
enc.offsetDynamics(obj.SizeSSZ(enc.sizer, true))
obj.DefineSSZ(enc.codec)
}
// EncodeDynamicObjectContentOnFork is the lazy data writer for EncodeDynamicObjectOffsetOnFork.
//
// Note, nil will be encoded as a zero-value initialized object.
func EncodeDynamicObjectContentOnFork[T newableDynamicObject[U], U any](enc *Encoder, obj T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeDynamicObjectContent(enc, obj)
}
// EncodeArrayOfBits serializes a static array of (packed) bits.
func EncodeArrayOfBits[T commonBitsLengths](enc *Encoder, bits *T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
// The code below should have used `*bits[:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
_, enc.err = enc.outWriter.Write(unsafe.Slice(&(*bits)[0], len(*bits)))
} else {
// The code below should have used `*bits[:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
copy(enc.outBuffer, unsafe.Slice(&(*bits)[0], len(*bits)))
enc.outBuffer = enc.outBuffer[len(*bits):]
}
}
// EncodeArrayOfBitsPointerOnFork serializes a static array of (packed) bits if
// present in a fork.
//
// Note, a nil pointer is serialized as a zero-value bit array.
func EncodeArrayOfBitsPointerOnFork[T commonBitsLengths](enc *Encoder, bits *T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
if bits == nil {
enc.encodeZeroes(reflect.TypeFor[T]().Len())
return
}
EncodeArrayOfBits(enc, bits)
}
// EncodeSliceOfBitsOffset serializes a dynamic slice of (packed) bits.
//
// Note, a nil slice of bits is serialized as an empty bit list.
func EncodeSliceOfBitsOffset(enc *Encoder, bits bitfield.Bitlist) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], enc.offset)
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, enc.offset)
enc.outBuffer = enc.outBuffer[4:]
}
if bits != nil {
enc.offset += uint32(len(bits))
} else {
enc.offset += uint32(len(bitlistZero))
}
}
// EncodeSliceOfBitsOffsetOnFork serializes a dynamic slice of (packed) bits if
// present in a fork.
//
// Note, a nil slice of bits is serialized as an empty bit list.
func EncodeSliceOfBitsOffsetOnFork(enc *Encoder, bits bitfield.Bitlist, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfBitsOffset(enc, bits)
}
// EncodeSliceOfBitsContent is the lazy data writer for EncodeSliceOfBitsOffset.
//
// Note, a nil slice of bits is serialized as an empty bit list.
func EncodeSliceOfBitsContent(enc *Encoder, bits bitfield.Bitlist) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
if bits != nil {
_, enc.err = enc.outWriter.Write(bits) // bitfield.Bitlist already has the length bit set
} else {
_, enc.err = enc.outWriter.Write(bitlistZero)
}
} else {
if bits != nil {
copy(enc.outBuffer, bits)
enc.outBuffer = enc.outBuffer[len(bits):] // bitfield.Bitlist already has the length bit set
} else {
copy(enc.outBuffer, bitlistZero)
enc.outBuffer = enc.outBuffer[len(bitlistZero):]
}
}
}
// EncodeSliceOfBitsContentOnFork is the lazy data writer for EncodeSliceOfBitsOffsetOnFork.
//
// Note, a nil slice of bits is serialized as an empty bit list.
func EncodeSliceOfBitsContentOnFork(enc *Encoder, bits bitfield.Bitlist, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfBitsContent(enc, bits)
}
// EncodeArrayOfUint64s serializes a static array of uint64s.
//
// The reason the ns is passed by pointer and not by value is to prevent it from
// escaping to the heap (and incurring an allocation) when passing it to the
// output stream.
func EncodeArrayOfUint64s[T commonUint64sLengths](enc *Encoder, ns *T) {
// The code below should have used `*blob[:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
nums := unsafe.Slice(&(*ns)[0], len(*ns))
// Internally this method is essentially calling EncodeUint64 on all numbers
// in a loop. Practically, we've inlined that call to make things a *lot* faster.
if enc.outWriter != nil {
for _, n := range nums {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint64(enc.buf[:8], n)
_, enc.err = enc.outWriter.Write(enc.buf[:8])
}
} else {
for _, n := range nums {
binary.LittleEndian.PutUint64(enc.outBuffer, n)
enc.outBuffer = enc.outBuffer[8:]
}
}
}
// EncodeArrayOfUint64sPointerOnFork serializes a static array of uint64s if
// present in a fork.
//
// Note, a nil pointer is serialized as a uint64 array filled with zeroes.
func EncodeArrayOfUint64sPointerOnFork[T commonUint64sLengths](enc *Encoder, ns *T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
if ns == nil {
enc.encodeZeroes(reflect.TypeFor[T]().Len() * 8)
return
}
EncodeArrayOfUint64s(enc, ns)
}
// EncodeSliceOfUint64sOffset serializes a dynamic slice of uint64s.
func EncodeSliceOfUint64sOffset[T ~uint64](enc *Encoder, ns []T) {
// Nope, dive into actual encoding
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], enc.offset)
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, enc.offset)
enc.outBuffer = enc.outBuffer[4:]
}
if items := len(ns); items > 0 {
enc.offset += uint32(items * 8)
}
}
// EncodeSliceOfUint64sOffsetOnFork serializes a dynamic slice of uint64s if
// present in a fork.
func EncodeSliceOfUint64sOffsetOnFork[T ~uint64](enc *Encoder, ns []T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfUint64sOffset(enc, ns)
}
// EncodeSliceOfUint64sContent is the lazy data writer for EncodeSliceOfUint64sOffset.
func EncodeSliceOfUint64sContent[T ~uint64](enc *Encoder, ns []T) {
if enc.outWriter != nil {
for _, n := range ns {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint64(enc.buf[:8], (uint64)(n))
_, enc.err = enc.outWriter.Write(enc.buf[:8])
}
} else {
for _, n := range ns {
binary.LittleEndian.PutUint64(enc.outBuffer, (uint64)(n))
enc.outBuffer = enc.outBuffer[8:]
}
}
}
// EncodeSliceOfUint64sContentOnFork is the lazy data writer for EncodeSliceOfUint64sOffsetOnFork.
func EncodeSliceOfUint64sContentOnFork[T ~uint64](enc *Encoder, ns []T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfUint64sContent(enc, ns)
}
// EncodeArrayOfStaticBytes serializes a static array of static binary
// blobs.
//
// The reason the blobs is passed by pointer and not by value is to prevent it
// from escaping to the heap (and incurring an allocation) when passing it to
// the output stream.
func EncodeArrayOfStaticBytes[T commonBytesArrayLengths[U], U commonBytesLengths](enc *Encoder, blobs *T) {
// The code below should have used `(*blobs)[:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
EncodeUnsafeArrayOfStaticBytes(enc, unsafe.Slice(&(*blobs)[0], len(*blobs)))
}
// EncodeUnsafeArrayOfStaticBytes serializes a static array of static binary
// blobs.
func EncodeUnsafeArrayOfStaticBytes[T commonBytesLengths](enc *Encoder, blobs []T) {
// Internally this method is essentially calling EncodeStaticBytes on all
// the blobs in a loop. Practically, we've inlined that call to make things
// a *lot* faster.
if enc.outWriter != nil {
for i := 0; i < len(blobs); i++ { // don't range loop, T might be an array, copy is expensive
if enc.err != nil {
return
}
// The code below should have used `blobs[i][:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
_, enc.err = enc.outWriter.Write(unsafe.Slice(&blobs[i][0], len(blobs[i])))
}
} else {
for i := 0; i < len(blobs); i++ { // don't range loop, T might be an array, copy is expensive
// The code below should have used `blobs[i][:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
copy(enc.outBuffer, unsafe.Slice(&blobs[i][0], len(blobs[i])))
enc.outBuffer = enc.outBuffer[len(blobs[i]):]
}
}
}
// EncodeCheckedArrayOfStaticBytes serializes a static array of static binary
// blobs.
func EncodeCheckedArrayOfStaticBytes[T commonBytesLengths](enc *Encoder, blobs []T, size uint64) {
// If the blobs are nil, write a batch of zeroes and exit
if blobs == nil {
enc.encodeZeroes(int(size) * reflect.TypeFor[T]().Len())
return
}
// Internally this method is essentially calling EncodeStaticBytes on all
// the blobs in a loop. Practically, we've inlined that call to make things
// a *lot* faster.
if enc.outWriter != nil {
for i := 0; i < len(blobs); i++ { // don't range loop, T might be an array, copy is expensive
if enc.err != nil {
return
}
// The code below should have used `blobs[i][:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
_, enc.err = enc.outWriter.Write(unsafe.Slice(&blobs[i][0], len(blobs[i])))
}
} else {
for i := 0; i < len(blobs); i++ { // don't range loop, T might be an array, copy is expensive
// The code below should have used `blobs[i][:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
copy(enc.outBuffer, unsafe.Slice(&blobs[i][0], len(blobs[i])))
enc.outBuffer = enc.outBuffer[len(blobs[i]):]
}
}
}
// EncodeSliceOfStaticBytesOffset serializes a dynamic slice of static binary blobs.
func EncodeSliceOfStaticBytesOffset[T commonBytesLengths](enc *Encoder, blobs []T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], enc.offset)
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, enc.offset)
enc.outBuffer = enc.outBuffer[4:]
}
if items := len(blobs); items > 0 {
enc.offset += uint32(items * len(blobs[0]))
}
}
// EncodeSliceOfStaticBytesOffsetOnFork serializes a dynamic slice of static binary blobs.
func EncodeSliceOfStaticBytesOffsetOnFork[T commonBytesLengths](enc *Encoder, blobs []T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfStaticBytesOffset(enc, blobs)
}
// EncodeSliceOfStaticBytesContent is the lazy data writer for EncodeSliceOfStaticBytesOffset.
func EncodeSliceOfStaticBytesContent[T commonBytesLengths](enc *Encoder, blobs []T) {
// Internally this method is essentially calling EncodeStaticBytes on all
// the blobs in a loop. Practically, we've inlined that call to make things
// a *lot* faster.
if enc.outWriter != nil {
for i := 0; i < len(blobs); i++ { // don't range loop, T might be an array, copy is expensive
if enc.err != nil {
return
}
// The code below should have used `blobs[i][:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
_, enc.err = enc.outWriter.Write(unsafe.Slice(&blobs[i][0], len(blobs[i])))
}
} else {
for i := 0; i < len(blobs); i++ { // don't range loop, T might be an array, copy is expensive
// The code below should have used `blobs[i][:]`, alas Go's generics compiler
// is missing that (i.e. a bug): https://github.com/golang/go/issues/51740
copy(enc.outBuffer, unsafe.Slice(&blobs[i][0], len(blobs[i])))
enc.outBuffer = enc.outBuffer[len(blobs[i]):]
}
}
}
// EncodeSliceOfStaticBytesContentOnFork is the lazy data writer for EncodeSliceOfStaticBytesOffsetOnFork.
func EncodeSliceOfStaticBytesContentOnFork[T commonBytesLengths](enc *Encoder, blobs []T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfStaticBytesContent(enc, blobs)
}
// EncodeSliceOfDynamicBytesOffset serializes a dynamic slice of dynamic binary
// blobs.
func EncodeSliceOfDynamicBytesOffset(enc *Encoder, blobs [][]byte) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], enc.offset)
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, enc.offset)
enc.outBuffer = enc.outBuffer[4:]
}
for _, blob := range blobs {
enc.offset += uint32(4 + len(blob))
}
}
// EncodeSliceOfDynamicBytesOffsetOnFork serializes a dynamic slice of dynamic
// binary blob if present in a fork.
func EncodeSliceOfDynamicBytesOffsetOnFork(enc *Encoder, blobs [][]byte, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfDynamicBytesOffset(enc, blobs)
}
// EncodeSliceOfDynamicBytesContent is the lazy data writer for EncodeSliceOfDynamicBytesOffset.
func EncodeSliceOfDynamicBytesContent(enc *Encoder, blobs [][]byte) {
// Nope, dive into actual encoding
enc.offsetDynamics(uint32(4 * len(blobs)))
// Inline:
//
// for _, blob := range blobs {
// EncodeDynamicBytesOffset(enc, blob)
// }
if enc.outWriter != nil {
for _, blob := range blobs {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], enc.offset)
_, enc.err = enc.outWriter.Write(enc.buf[:4])
enc.offset += uint32(len(blob))
}
} else {
for _, blob := range blobs {
binary.LittleEndian.PutUint32(enc.outBuffer, enc.offset)
enc.outBuffer = enc.outBuffer[4:]
enc.offset += uint32(len(blob))
}
}
// Inline:
//
// for _, blob := range blobs {
// EncodeDynamicBytesContent(enc, blob)
// }
if enc.outWriter != nil {
for _, blob := range blobs {
if enc.err != nil {
return
}
_, enc.err = enc.outWriter.Write(blob)
}
} else {
for _, blob := range blobs {
copy(enc.outBuffer, blob)
enc.outBuffer = enc.outBuffer[len(blob):]
}
}
}
// EncodeSliceOfDynamicBytesContentOnFork is the lazy data writer for EncodeSliceOfDynamicBytesOffsetOnFork.
func EncodeSliceOfDynamicBytesContentOnFork(enc *Encoder, blobs [][]byte, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfDynamicBytesContent(enc, blobs)
}
// EncodeSliceOfStaticObjectsOffset serializes a dynamic slice of static ssz objects.
func EncodeSliceOfStaticObjectsOffset[T StaticObject](enc *Encoder, objects []T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], enc.offset)
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, enc.offset)
enc.outBuffer = enc.outBuffer[4:]
}
if items := len(objects); items > 0 {
enc.offset += uint32(items) * objects[0].SizeSSZ(enc.sizer)
}
}
// EncodeSliceOfStaticObjectsOffsetOnFork serializes a dynamic slice of static ssz
// objects if present in a fork.
func EncodeSliceOfStaticObjectsOffsetOnFork[T StaticObject](enc *Encoder, objects []T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfStaticObjectsOffset(enc, objects)
}
// EncodeSliceOfStaticObjectsContent is the lazy data writer for EncodeSliceOfStaticObjectsOffset.
func EncodeSliceOfStaticObjectsContent[T StaticObject](enc *Encoder, objects []T) {
for _, obj := range objects {
if enc.err != nil {
return
}
obj.DefineSSZ(enc.codec)
}
}
// EncodeSliceOfStaticObjectsContentOnFork is the lazy data writer for EncodeSliceOfStaticObjectsOffsetOnFork.
func EncodeSliceOfStaticObjectsContentOnFork[T StaticObject](enc *Encoder, objects []T, filter ForkFilter) {
// If the field is not active in the current fork, early return
if enc.codec.fork < filter.Added || (filter.Removed > ForkUnknown && enc.codec.fork >= filter.Removed) {
return
}
// Otherwise fall back to the standard encoder
EncodeSliceOfStaticObjectsContent(enc, objects)
}
// EncodeSliceOfDynamicObjectsOffset serializes a dynamic slice of dynamic ssz
// objects.
func EncodeSliceOfDynamicObjectsOffset[T DynamicObject](enc *Encoder, objects []T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], enc.offset)
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, enc.offset)
enc.outBuffer = enc.outBuffer[4:]
}
for _, obj := range objects {
enc.offset += 4 + obj.SizeSSZ(enc.sizer, false)
}
}
// EncodeSliceOfDynamicObjectsOffsetOnFork serializes a dynamic slice of dynamic