This repository has been archived by the owner on Jan 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
ssz_test.go
800 lines (760 loc) · 19.3 KB
/
ssz_test.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
package ssz
import (
"bytes"
"encoding/hex"
"reflect"
"strconv"
"sync"
"testing"
"github.com/pkg/errors"
"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/go-ssz/types"
)
type beaconState struct {
BlockRoots [][]byte `ssz-size:"65536,32"`
}
type fork struct {
PreviousVersion [4]byte
CurrentVersion [4]byte
Epoch uint64
}
type truncateSignatureCase struct {
Slot uint64
PreviousBlockRoot []byte
Signature []byte
}
type simpleNonProtoMessage struct {
Foo []byte
Bar uint64
}
func TestNilElementMarshal(t *testing.T) {
type ex struct{}
var item *ex
buf, err := Marshal(item)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, []byte{}) {
t.Errorf("Wanted empty byte slice, got %v", buf)
}
}
func TestNilElementDetermineSize(t *testing.T) {
type ex struct{}
var item *ex
size := types.DetermineSize(reflect.ValueOf(item))
if size != 0 {
t.Errorf("Wanted size 0, received %d", size)
}
}
// This test verifies if a nil pseudo-array is treated the same as an instantiated,
// zero-valued array when running hash tree root computations.
func TestEmptyArrayInstantiation(t *testing.T) {
type data struct {
DepositRoot []byte `ssz-size:"32"`
DepositCount uint64
BlockHash []byte `ssz-size:"32"`
}
type example struct {
Randao []byte `ssz-size:"96"`
Data *data
Graffiti []byte `ssz-size:"32"`
}
empty := &example{
Randao: make([]byte, 96),
Data: &data{
DepositRoot: make([]byte, 32),
DepositCount: 0,
BlockHash: make([]byte, 32),
},
}
withInstantiatedArray := &example{
Randao: make([]byte, 96),
Data: &data{
DepositRoot: make([]byte, 32),
DepositCount: 0,
BlockHash: make([]byte, 32),
},
Graffiti: make([]byte, 32),
}
r1, err := HashTreeRoot(empty)
if err != nil {
t.Fatal(err)
}
r2, err := HashTreeRoot(withInstantiatedArray)
if err != nil {
t.Fatal(err)
}
if r1 != r2 {
t.Errorf("Wanted nil_array_field = %#x, instiantiated_empty_array_field = %#x", r1, r2)
}
}
func TestMarshalNilArray(t *testing.T) {
type ex struct {
Slot uint64
Graffiti []byte `ssz-size:"32"`
DepositIndex uint64
}
b1 := &ex{
Slot: 5,
Graffiti: nil,
DepositIndex: 64,
}
b2 := &ex{
Slot: 5,
Graffiti: make([]byte, 32),
DepositIndex: 64,
}
enc1, err := Marshal(b1)
if err != nil {
t.Fatal(err)
}
enc2, err := Marshal(b2)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(enc1, enc2) {
t.Errorf("First item %v != second item %v", enc1, enc2)
}
}
func TestPartialDataMarshalUnmarshal(t *testing.T) {
type block struct {
Slot uint64
Transfers []*simpleProtoMessage
}
b := &block{
Slot: 5,
}
enc, err := Marshal(b)
if err != nil {
t.Fatal(err)
}
dec := &block{}
if err := Unmarshal(enc, dec); err != nil {
t.Fatal(err)
}
}
func TestMarshal(t *testing.T) {
tests := []struct {
name string
input interface{}
output []byte
err error
}{
{
name: "Nil",
err: errors.New("untyped-value nil cannot be marshaled"),
},
{
name: "Unsupported",
input: complex(1, 1),
err: errors.New("unsupported kind: complex128"),
},
{
name: "UnsupportedPointer",
input: &[]complex128{complex(1, 1), complex(1, 1)},
err: errors.New("failed to marshal for type: []complex128: unsupported kind: complex128"),
},
{
name: "UnsupportedStructElement",
input: struct{ Foo complex128 }{complex(1, 1)},
err: errors.New("failed to marshal for type: struct { Foo complex128 }: unsupported kind: complex128"),
},
{
name: "Simple",
input: struct{ Foo uint32 }{12345},
output: []byte{0x39, 0x30, 0x00, 0x00},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := Marshal(test.input)
if test.err == nil {
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if bytes.Compare(test.output, output) != 0 {
t.Errorf("incorrect output: expected %v; received %v", test.output, output)
}
} else {
if err == nil {
t.Fatalf("missing expected error %v", test.err)
}
if test.err.Error() != err.Error() {
t.Errorf("incorrect error: expected %v; received %v", test.err, err)
}
}
})
}
}
func TestUnmarshal(t *testing.T) {
tests := []struct {
name string
input []byte
output interface{}
err error
}{
{
name: "Nil",
err: errors.New("cannot unmarshal into untyped, nil value"),
},
{
name: "NotPointer",
input: []byte{0x00, 0x00, 0x00, 0x00},
output: "",
err: errors.New("can only unmarshal into a pointer target"),
},
{
name: "OutputNotSupported",
input: []byte{0x00, 0x00, 0x00, 0x00},
output: &struct{ Foo complex128 }{complex(1, 1)},
err: errors.New("could not unmarshal input into type: struct { Foo complex128 }: unsupported kind: complex128"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := Unmarshal(test.input, test.output)
if test.err == nil {
if err != nil {
t.Errorf("unexpected error %v", err)
}
} else {
if err == nil {
t.Fatalf("missing expected error %v", test.err)
}
if test.err.Error() != err.Error() {
t.Errorf("unexpected error value %v (expected %v)", err, test.err)
}
}
})
}
}
func TestHashTreeRoot(t *testing.T) {
tests := []struct {
name string
input interface{}
output [32]byte
err error
}{
{
name: "Nil",
err: errors.New("untyped nil is not supported"),
},
{
name: "UnsupportedKind",
input: complex(1, 1),
err: errors.New("could not generate tree hasher for type: complex128: unsupported kind: complex128"),
},
{
name: "NoInput",
input: &struct{ Foo complex128 }{},
err: errors.New("unsupported kind: complex128"),
},
{
name: "Valid",
input: fork{
PreviousVersion: [4]byte{0x9f, 0x41, 0xbd, 0x5b},
CurrentVersion: [4]byte{0xcb, 0xb0, 0xf1, 0xd7},
Epoch: 11971467576204192310,
},
output: [32]byte{0x3a, 0xd1, 0x26, 0x4c, 0x33, 0xbc, 0x66, 0xb4, 0x3a, 0x49, 0xb1, 0x25, 0x8b, 0x88, 0xf3, 0x4b, 0x8d, 0xbf, 0xa1, 0x64, 0x9f, 0x17, 0xe6, 0xdf, 0x55, 0x0f, 0x58, 0x96, 0x50, 0xd3, 0x49, 0x92},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := HashTreeRoot(test.input)
if test.err == nil {
if err != nil {
t.Errorf("unexpected error %v", err)
}
if bytes.Compare(test.output[:], output[:]) != 0 {
t.Errorf("incorrect output: expected %v; received %v", test.output, output)
}
} else {
if err == nil {
t.Fatalf("missing expected error %v", test.err)
}
if test.err.Error() != err.Error() {
t.Errorf("incorrect error: expected %v; received %v", test.err, err)
}
}
})
}
}
func TestHashTreeRootBitlist(t *testing.T) {
tests := []struct {
name string
input bitfield.Bitlist
maxCapacity uint64
output []byte
err error
}{
{
name: "Nil",
input: nil,
maxCapacity: 0,
// Hash([]byte{})
output: hexDecodeOrDie(t, "f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b"),
err: nil,
},
{
name: "SampleBitlist",
input: bitfield.Bitlist{1, 2, 3},
maxCapacity: 4,
// Known output hash.
output: hexDecodeOrDie(t, "835e878350f244651619cbac69de3002251be60225ba0d6ac999b5becb469281"),
err: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := HashTreeRootBitfield(test.input, test.maxCapacity)
if test.err == nil {
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if bytes.Compare(test.output[:], output[:]) != 0 {
t.Errorf("incorrect output: expected %#x; received %#x", test.output, output)
}
} else {
if err == nil {
t.Fatalf("missing expected error %v", test.err)
}
if test.err.Error() != err.Error() {
t.Errorf("incorrect error: expected %#x; received %#x", test.err, err)
}
}
})
}
}
func TestHashTreeRootWithCapacity(t *testing.T) {
tests := []struct {
name string
input interface{}
maxCapacity uint64
output [32]byte
err error
}{
{
name: "Nil",
err: errors.New("untyped nil is not supported"),
},
{
name: "NotSlice",
input: "foo",
err: errors.New("expected slice-kind input, received string"),
},
{
name: "InvalidSlice1",
input: []complex128{complex(1, 1)},
err: errors.New("unsupported kind: complex128"),
},
{
name: "InvalidSlice2",
input: []struct{ Foo complex128 }{{Foo: complex(1, 1)}},
err: errors.New("unsupported kind: complex128"),
},
{
name: "NoInput",
input: []uint32{},
output: [32]byte{0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30, 0x27, 0x98, 0xef, 0x6e, 0xd3, 0x09, 0x97, 0x9b, 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8, 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b},
},
{
name: "Valid",
input: []fork{{
PreviousVersion: [4]byte{0x9f, 0x41, 0xbd, 0x5b},
CurrentVersion: [4]byte{0xcb, 0xb0, 0xf1, 0xd7},
Epoch: 11971467576204192310,
}},
maxCapacity: 100,
output: [32]byte{0x5c, 0xa4, 0xd8, 0xbf, 0x17, 0xb9, 0x53, 0x6d, 0x69, 0x56, 0xee, 0x48, 0xfa, 0x3d, 0xc6, 0x91, 0xe3, 0x52, 0x48, 0xbd, 0x09, 0xb2, 0x9b, 0x1b, 0x5b, 0xa4, 0x5a, 0x0e, 0xd5, 0xda, 0xe0, 0xd9},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := HashTreeRootWithCapacity(test.input, test.maxCapacity)
if test.err == nil {
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if bytes.Compare(test.output[:], output[:]) != 0 {
t.Errorf("incorrect output: expected %v; received %v", test.output, output)
}
} else {
if err == nil {
t.Fatalf("missing expected error %v", test.err)
}
if test.err.Error() != err.Error() {
t.Errorf("incorrect error: expected %v; received %v", test.err, err)
}
}
})
}
}
func TestProtobufSSZFieldsIgnored(t *testing.T) {
withProto := &simpleProtoMessage{
Foo: []byte("foo"),
Bar: 9001,
}
noProto := &simpleNonProtoMessage{
Foo: []byte("foo"),
Bar: 9001,
}
enc, err := Marshal(withProto)
if err != nil {
t.Fatal(err)
}
enc2, err := Marshal(noProto)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(enc, enc2) {
t.Errorf("Wanted %v, received %v", enc, enc2)
}
withProtoDecoded := &simpleProtoMessage{}
if err := Unmarshal(enc, withProtoDecoded); err != nil {
t.Fatal(err)
}
noProtoDecoded := &simpleNonProtoMessage{}
if err := Unmarshal(enc2, noProtoDecoded); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(noProto, noProtoDecoded) {
t.Errorf("Wanted %v, received %v", noProto, noProtoDecoded)
}
if !reflect.DeepEqual(withProto, withProtoDecoded) {
t.Errorf("Wanted %v, received %v", withProto, withProtoDecoded)
}
}
func TestNilPointerHashTreeRoot(t *testing.T) {
type nilItem struct {
Field1 []*fork
Field2 uint64
}
i := &nilItem{
Field1: []*fork{nil},
Field2: 10,
}
if _, err := HashTreeRoot(i); err != nil {
t.Fatal(err)
}
}
func TestNilInstantiationMarshalEquality(t *testing.T) {
type exampleBody struct {
Epoch uint64
}
type example struct {
Slot uint64
Root [32]byte
Body *exampleBody
}
root := [32]byte{1, 2, 3, 4}
item := &example{
Slot: 5,
Root: root,
Body: nil,
}
item2 := &example{
Slot: 5,
Root: root,
Body: &exampleBody{},
}
enc, err := Marshal(item)
if err != nil {
t.Fatal(err)
}
enc2, err := Marshal(item2)
if err != nil {
t.Fatal(err)
}
dec := &example{}
if err := Unmarshal(enc, dec); err != nil {
t.Fatal(err)
}
dec2 := &example{}
if err := Unmarshal(enc2, dec2); err != nil {
t.Fatal(err)
}
if !bytes.Equal(enc, enc2) {
t.Errorf("Unequal marshalings %v != %v", enc, enc2)
}
}
func TestEmptyDataUnmarshal(t *testing.T) {
msg := &simpleProtoMessage{}
if err := Unmarshal([]byte{}, msg); err == nil {
t.Error("Expected unmarshal to fail when attempting to unmarshal from an empty byte slice")
}
}
func TestHashTreeRootWithCapacity_FailsWithNonSliceType(t *testing.T) {
forkItem := fork{
Epoch: 11971467576204192310,
}
capacity := uint64(100)
if _, err := HashTreeRootWithCapacity(forkItem, capacity); err == nil {
t.Error("Expected hash tree root to fail with non-slice type")
}
}
func TestHashTreeRootWithCapacity_HashesCorrectly(t *testing.T) {
capacity := uint64(1099511627776)
balances := make([]uint64, 512)
for i := 0; i < len(balances); i++ {
balances[i] = 32000000000
}
root, err := HashTreeRootWithCapacity(balances, capacity)
if err != nil {
t.Fatal(err)
}
// Test case taken from validator balances of the state value in:
// https://github.com/ethereum/eth2.0-spec-tests/blob/v0.8.0/tests/sanity/slots/sanity_slots_mainnet.yaml.
want, err := hex.DecodeString("21a67313b0c6f988aac4fb6dd68686e1329243f7f6af21b722f6b83ca8fed9a8")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], want) {
t.Errorf("Mismatched roots, wanted %#x == %#x", root, want)
}
}
// Regression test for https://github.com/prysmaticlabs/go-ssz/issues/46.
func TestHashTreeRoot_EncodeSliceLengthCorrectly(t *testing.T) {
type accountBalances struct {
Balances []uint64 `ssz-max:"1099511627776"` // Large uint64 capacity.
}
acct := accountBalances{
Balances: make([]uint64, 512),
}
for i := 0; i < len(acct.Balances); i++ {
acct.Balances[i] = 32000000000
}
root, err := HashTreeRoot(acct)
if err != nil {
t.Fatal(err)
}
// Test case taken from validator balances of the state value in:
// https://github.com/ethereum/eth2.0-spec-tests/blob/v0.8.0/tests/sanity/slots/sanity_slots_mainnet.yaml.
want, err := hex.DecodeString("21a67313b0c6f988aac4fb6dd68686e1329243f7f6af21b722f6b83ca8fed9a8")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], want) {
t.Errorf("Mismatched roots, wanted %#x == %#x", root, want)
}
}
func TestHashTreeRoot_ConcurrentAccess(t *testing.T) {
item := &truncateSignatureCase{
Slot: 10,
PreviousBlockRoot: []byte{'a', 'b'},
Signature: []byte("TESTING23"),
}
var wg sync.WaitGroup
// We ensure the hash tree root function can be computed in a thread-safe manner.
// No panic from this test is a successful run.
wg.Add(100)
for i := 0; i < 100; i++ {
go func(tt *testing.T, w *sync.WaitGroup) {
if _, err := HashTreeRoot(item); err != nil {
tt.Fatal(err)
}
w.Done()
}(t, &wg)
}
wg.Wait()
}
func TestSigningRoot(t *testing.T) {
type signingRootTest struct {
Val1 interface{}
Val2 interface{}
Err error
}
type truncateLastCase struct {
Slot uint64
StateRoot []byte
TruncatedField []byte
}
var signingRootTests = []signingRootTest{
{
Val1: &truncateSignatureCase{Slot: 20, Signature: []byte{'A', 'B'}},
Val2: &truncateSignatureCase{Slot: 20, Signature: []byte("TESTING")},
},
{
Val1: &truncateSignatureCase{
Slot: 10,
PreviousBlockRoot: []byte{'a', 'b'},
Signature: []byte("TESTINGDIFF")},
Val2: &truncateSignatureCase{
Slot: 10,
PreviousBlockRoot: []byte{'a', 'b'},
Signature: []byte("TESTING23")},
},
{
Val1: truncateSignatureCase{Slot: 50, Signature: []byte("THIS")},
Val2: truncateSignatureCase{Slot: 50, Signature: []byte("DOESNT")},
},
{
Val1: truncateSignatureCase{Signature: []byte("MATTER")},
Val2: truncateSignatureCase{Signature: []byte("TESTING")},
},
{
Val1: truncateLastCase{
Slot: 5,
StateRoot: []byte("MATTERS"),
TruncatedField: []byte("DOESNT MATTER"),
},
Val2: truncateLastCase{
Slot: 5,
StateRoot: []byte("MATTERS"),
TruncatedField: []byte("SHOULDNT MATTER"),
},
},
{
Val1: truncateLastCase{
Slot: 550,
StateRoot: []byte("SHOULD"),
TruncatedField: []byte("DOESNT"),
},
Val2: truncateLastCase{
Slot: 550,
StateRoot: []byte("SHOULD"),
TruncatedField: []byte("SHOULDNT"),
},
},
{
Val1: nil,
Err: errors.New("value cannot be nil"),
Val2: nil,
},
}
for i, test := range signingRootTests {
output1, err := SigningRoot(test.Val1)
if test.Err != nil {
if err == nil {
t.Fatalf("missing expected error of test %d value 1", i)
}
if test.Err.Error() != err.Error() {
t.Fatalf("incorrect error at test %d value 1 %v", i, err)
}
} else {
if err != nil {
t.Fatalf("could not get the signing root of test %d, value 1 %v", i, err)
}
output2, err := SigningRoot(test.Val2)
if err != nil {
t.Errorf("could not get the signing root of test %d, value 2 %v", i, err)
}
// Check values have same result hash
if !bytes.Equal(output1[:], output2[:]) {
t.Errorf("test %d: hash mismatch: %X\n != %X", i, output1, output2)
}
}
}
}
func TestSigningRoot_ConcurrentAccess(t *testing.T) {
item := &truncateSignatureCase{
Slot: 10,
PreviousBlockRoot: []byte{'a', 'b'},
Signature: []byte("TESTING23"),
}
var wg sync.WaitGroup
// We ensure the signing root function can be computed in a thread-safe manner.
// No panic from this test is a successful run.
wg.Add(100)
for i := 0; i < 100; i++ {
go func(tt *testing.T, w *sync.WaitGroup) {
if _, err := SigningRoot(item); err != nil {
tt.Fatal(err)
}
w.Done()
}(t, &wg)
}
wg.Wait()
}
func BenchmarkSSZ_NoCache(b *testing.B) {
b.StopTimer()
bs := &beaconState{
BlockRoots: make([][]byte, 65536),
}
for i := 0; i < len(bs.BlockRoots); i++ {
newItem := [32]byte{1, 2, 3}
bs.BlockRoots[i] = newItem[:]
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := HashTreeRoot(bs); err != nil {
b.Fatal(err)
}
}
types.ToggleCache(true)
}
func BenchmarkSSZ_WithCache(b *testing.B) {
b.StopTimer()
types.ToggleCache(true)
bs := &beaconState{
BlockRoots: make([][]byte, 65536),
}
for i := 0; i < len(bs.BlockRoots); i++ {
newItem := [32]byte{1, 2, 3}
bs.BlockRoots[i] = newItem[:]
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := HashTreeRoot(bs); err != nil {
b.Fatal(err)
}
}
types.ToggleCache(false)
}
func BenchmarkSSZ_SingleElementChanged(b *testing.B) {
b.StopTimer()
types.ToggleCache(true)
bs := &beaconState{
BlockRoots: make([][]byte, 65536),
}
for i := 0; i < len(bs.BlockRoots); i++ {
newItem := [32]byte{1, 2, 3}
bs.BlockRoots[i] = newItem[:]
}
if _, err := HashTreeRoot(bs); err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
newItem := []byte(strconv.Itoa(i))
newRoot := toBytes32(newItem)
bs.BlockRoots[i%len(bs.BlockRoots)] = newRoot[:]
if _, err := HashTreeRoot(bs); err != nil {
b.Fatal(err)
}
}
types.ToggleCache(false)
}
func toBytes32(x []byte) [32]byte {
var y [32]byte
copy(y[:], x)
return y
}
func TestBoolArray_MissingByte(t *testing.T) {
objBytes := hexDecodeOrDie(t, "010101010101010101010101010101")
var result [16]bool
if err := Unmarshal(objBytes, &result); err == nil {
t.Error("Expected message with missing byte to fail marshalling")
}
}
func TestBoolArray_ExtraByte(t *testing.T) {
objBytes := hexDecodeOrDie(t, "01010101010101010101010101010101ff")
var result [16]bool
if err := Unmarshal(objBytes, &result); err == nil {
t.Error("Expected message with extra byte to fail marshalling")
}
}
func TestBoolArray_Correct(t *testing.T) {
objBytes := hexDecodeOrDie(t, "01010101010101010101010101010101")
var result [16]bool
if err := Unmarshal(objBytes, &result); err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
func hexDecodeOrDie(t *testing.T, s string) []byte {
res, err := hex.DecodeString(s)
if err != nil {
t.Fatal(err)
}
return res
}