1
1
import { describe , it , expect } from "vitest" ;
2
- import { deneb , Epoch , phase0 , RootHex , Slot , ssz } from "@lodestar/types" ;
2
+ import { BitArray } from "@chainsafe/ssz" ;
3
+ import { allForks , deneb , Epoch , isElectraAttestation , phase0 , RootHex , Slot , ssz } from "@lodestar/types" ;
3
4
import { fromHex , toHex } from "@lodestar/utils" ;
5
+ import { ForkName , MAX_COMMITTEES_PER_SLOT } from "@lodestar/params" ;
4
6
import {
5
7
getAttDataBase64FromAttestationSerialized ,
6
8
getAttDataBase64FromSignedAggregateAndProofSerialized ,
@@ -12,29 +14,52 @@ import {
12
14
getSignatureFromAttestationSerialized ,
13
15
getSlotFromSignedBeaconBlockSerialized ,
14
16
getSlotFromBlobSidecarSerialized ,
17
+ getCommitteeBitsFromAttestationSerialized ,
15
18
} from "../../../src/util/sszBytes.js" ;
16
19
17
20
describe ( "attestation SSZ serialized picking" , ( ) => {
18
- const testCases : phase0 . Attestation [ ] = [
21
+ const testCases : allForks . Attestation [ ] = [
19
22
ssz . phase0 . Attestation . defaultValue ( ) ,
20
23
attestationFromValues (
21
24
4_000_000 ,
22
25
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ,
23
26
200_00 ,
24
27
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffffffff"
25
28
) ,
29
+ ssz . electra . Attestation . defaultValue ( ) ,
30
+ {
31
+ ...attestationFromValues (
32
+ 4_000_000 ,
33
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ,
34
+ 200_00 ,
35
+ "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffffffff"
36
+ ) ,
37
+ committeeBits : BitArray . fromSingleBit ( MAX_COMMITTEES_PER_SLOT , 3 ) ,
38
+ } ,
26
39
] ;
27
40
28
41
for ( const [ i , attestation ] of testCases . entries ( ) ) {
29
42
it ( `attestation ${ i } ` , ( ) => {
30
- const bytes = ssz . phase0 . Attestation . serialize ( attestation ) ;
43
+ const isElectra = isElectraAttestation ( attestation ) ;
44
+ const bytes = isElectra
45
+ ? ssz . electra . Attestation . serialize ( attestation )
46
+ : ssz . phase0 . Attestation . serialize ( attestation ) ;
31
47
32
48
expect ( getSlotFromAttestationSerialized ( bytes ) ) . toBe ( attestation . data . slot ) ;
33
49
expect ( getBlockRootFromAttestationSerialized ( bytes ) ) . toBe ( toHex ( attestation . data . beaconBlockRoot ) ) ;
34
- expect ( getAggregationBitsFromAttestationSerialized ( bytes ) ?. toBoolArray ( ) ) . toEqual (
35
- attestation . aggregationBits . toBoolArray ( )
36
- ) ;
37
- expect ( getSignatureFromAttestationSerialized ( bytes ) ) . toEqual ( attestation . signature ) ;
50
+
51
+ if ( isElectra ) {
52
+ expect ( getAggregationBitsFromAttestationSerialized ( ForkName . electra , bytes ) ?. toBoolArray ( ) ) . toEqual (
53
+ attestation . aggregationBits . toBoolArray ( )
54
+ ) ;
55
+ expect ( getCommitteeBitsFromAttestationSerialized ( bytes ) ) . toEqual ( attestation . committeeBits ) ;
56
+ expect ( getSignatureFromAttestationSerialized ( ForkName . electra , bytes ) ) . toEqual ( attestation . signature ) ;
57
+ } else {
58
+ expect ( getAggregationBitsFromAttestationSerialized ( ForkName . phase0 , bytes ) ?. toBoolArray ( ) ) . toEqual (
59
+ attestation . aggregationBits . toBoolArray ( )
60
+ ) ;
61
+ expect ( getSignatureFromAttestationSerialized ( ForkName . phase0 , bytes ) ) . toEqual ( attestation . signature ) ;
62
+ }
38
63
39
64
const attDataBase64 = ssz . phase0 . AttestationData . serialize ( attestation . data ) ;
40
65
expect ( getAttDataBase64FromAttestationSerialized ( bytes ) ) . toBe ( Buffer . from ( attDataBase64 ) . toString ( "base64" ) ) ;
@@ -65,14 +90,16 @@ describe("attestation SSZ serialized picking", () => {
65
90
it ( "getAggregateionBitsFromAttestationSerialized - invalid data" , ( ) => {
66
91
const invalidAggregationBitsDataSizes = [ 0 , 4 , 100 , 128 , 227 ] ;
67
92
for ( const size of invalidAggregationBitsDataSizes ) {
68
- expect ( getAggregationBitsFromAttestationSerialized ( Buffer . alloc ( size ) ) ) . toBeNull ( ) ;
93
+ expect ( getAggregationBitsFromAttestationSerialized ( ForkName . phase0 , Buffer . alloc ( size ) ) ) . toBeNull ( ) ;
94
+ expect ( getAggregationBitsFromAttestationSerialized ( ForkName . electra , Buffer . alloc ( size ) ) ) . toBeNull ( ) ;
69
95
}
70
96
} ) ;
71
97
72
98
it ( "getSignatureFromAttestationSerialized - invalid data" , ( ) => {
73
99
const invalidSignatureDataSizes = [ 0 , 4 , 100 , 128 , 227 ] ;
74
100
for ( const size of invalidSignatureDataSizes ) {
75
- expect ( getSignatureFromAttestationSerialized ( Buffer . alloc ( size ) ) ) . toBeNull ( ) ;
101
+ expect ( getSignatureFromAttestationSerialized ( ForkName . phase0 , Buffer . alloc ( size ) ) ) . toBeNull ( ) ;
102
+ expect ( getSignatureFromAttestationSerialized ( ForkName . electra , Buffer . alloc ( size ) ) ) . toBeNull ( ) ;
76
103
}
77
104
} ) ;
78
105
} ) ;
@@ -86,6 +113,7 @@ describe("aggregateAndProof SSZ serialized picking", () => {
86
113
200_00 ,
87
114
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffffffff"
88
115
) ,
116
+ ssz . electra . SignedAggregateAndProof . defaultValue ( ) ,
89
117
] ;
90
118
91
119
for ( const [ i , signedAggregateAndProof ] of testCases . entries ( ) ) {
0 commit comments