@@ -5,6 +5,9 @@ use ssz::{Decode, DecodeError, Encode};
5
5
use ssz_types:: { typenum:: U128 , VariableList } ;
6
6
7
7
use crate :: {
8
+ consensus:: historical_summaries:: {
9
+ HistoricalSummariesWithProof , HistoricalSummariesWithProofElectra ,
10
+ } ,
8
11
light_client:: {
9
12
bootstrap:: { LightClientBootstrapDeneb , LightClientBootstrapElectra } ,
10
13
finality_update:: { LightClientFinalityUpdateDeneb , LightClientFinalityUpdateElectra } ,
@@ -14,7 +17,7 @@ use crate::{
14
17
types:: {
15
18
consensus:: {
16
19
fork:: { ForkDigest , ForkName } ,
17
- historical_summaries:: HistoricalSummariesWithProof ,
20
+ historical_summaries:: HistoricalSummariesWithProofDeneb ,
18
21
light_client:: {
19
22
bootstrap:: {
20
23
LightClientBootstrap , LightClientBootstrapBellatrix ,
@@ -482,52 +485,92 @@ pub struct ForkVersionedHistoricalSummariesWithProof {
482
485
pub historical_summaries_with_proof : HistoricalSummariesWithProof ,
483
486
}
484
487
485
- impl ForkVersionedHistoricalSummariesWithProof {
486
- pub fn encode ( & self ) -> Vec < u8 > {
487
- let fork_digest = self . fork_name . as_fork_digest ( ) ;
488
-
489
- let mut data = fork_digest. to_vec ( ) ;
490
- data. extend ( self . historical_summaries_with_proof . as_ssz_bytes ( ) ) ;
491
- data
488
+ impl From < HistoricalSummariesWithProof > for ForkVersionedHistoricalSummariesWithProof {
489
+ fn from ( historical_summaries_with_proof : HistoricalSummariesWithProof ) -> Self {
490
+ let fork_name = match & historical_summaries_with_proof {
491
+ HistoricalSummariesWithProof :: Deneb ( _) => ForkName :: Deneb ,
492
+ HistoricalSummariesWithProof :: Electra ( _) => ForkName :: Electra ,
493
+ } ;
494
+ Self {
495
+ fork_name,
496
+ historical_summaries_with_proof,
497
+ }
492
498
}
499
+ }
493
500
494
- pub fn decode ( buf : & [ u8 ] ) -> Result < Self , DecodeError > {
495
- let fork_digest = ForkDigest :: try_from ( & buf[ 0 ..4 ] ) . map_err ( |err| {
496
- DecodeError :: BytesInvalid ( format ! ( "Unable to decode fork digest: {err:?}" ) )
497
- } ) ?;
498
- let fork_name = ForkName :: try_from ( fork_digest) . map_err ( |_| {
499
- DecodeError :: BytesInvalid ( format ! ( "Unable to decode fork name: {fork_digest:?}" ) )
500
- } ) ?;
501
- let summaries_with_proof = HistoricalSummariesWithProof :: from_ssz_bytes ( & buf[ 4 ..] ) ?;
502
-
503
- Ok ( Self {
504
- fork_name,
505
- historical_summaries_with_proof : summaries_with_proof,
506
- } )
501
+ impl ForkVersionedHistoricalSummariesWithProof {
502
+ pub fn epoch ( & self ) -> u64 {
503
+ * self . historical_summaries_with_proof . epoch ( )
507
504
}
508
505
}
509
506
510
- impl Decode for ForkVersionedHistoricalSummariesWithProof {
507
+ impl Encode for ForkVersionedHistoricalSummariesWithProof {
511
508
fn is_ssz_fixed_len ( ) -> bool {
512
509
false
513
510
}
514
511
515
- fn from_ssz_bytes ( bytes : & [ u8 ] ) -> Result < Self , DecodeError > {
516
- Self :: decode ( bytes)
512
+ fn ssz_append ( & self , buf : & mut Vec < u8 > ) {
513
+ self . fork_name . as_fork_digest ( ) . ssz_append ( buf) ;
514
+ self . historical_summaries_with_proof . ssz_append ( buf) ;
515
+ }
516
+
517
+ fn ssz_fixed_len ( ) -> usize {
518
+ ssz:: BYTES_PER_LENGTH_OFFSET
519
+ }
520
+
521
+ fn ssz_bytes_len ( & self ) -> usize {
522
+ self . fork_name . as_fork_digest ( ) . ssz_bytes_len ( )
523
+ + self . historical_summaries_with_proof . ssz_bytes_len ( )
517
524
}
518
525
}
519
526
520
- impl Encode for ForkVersionedHistoricalSummariesWithProof {
527
+ impl Decode for ForkVersionedHistoricalSummariesWithProof {
521
528
fn is_ssz_fixed_len ( ) -> bool {
522
529
false
523
530
}
524
531
525
- fn ssz_append ( & self , buf : & mut Vec < u8 > ) {
526
- buf . extend_from_slice ( & self . encode ( ) ) ;
532
+ fn ssz_fixed_len ( ) -> usize {
533
+ ssz :: BYTES_PER_LENGTH_OFFSET
527
534
}
528
535
529
- fn ssz_bytes_len ( & self ) -> usize {
530
- self . encode ( ) . len ( )
536
+ fn from_ssz_bytes ( bytes : & [ u8 ] ) -> Result < Self , DecodeError > {
537
+ let fork_digest_len = <ForkDigest as Decode >:: ssz_fixed_len ( ) ;
538
+ let Some ( ( fork_digest_bytes, historical_summaries_with_proof_bytes) ) =
539
+ bytes. split_at_checked ( fork_digest_len)
540
+ else {
541
+ return Err ( DecodeError :: InvalidByteLength {
542
+ len : bytes. len ( ) ,
543
+ expected : fork_digest_len,
544
+ } ) ;
545
+ } ;
546
+
547
+ let fork_digest = ForkDigest :: from_ssz_bytes ( fork_digest_bytes) ?;
548
+ let fork_name = match ForkName :: try_from ( fork_digest) {
549
+ Ok ( fork_name) => fork_name,
550
+ Err ( err) => return Err ( DecodeError :: BytesInvalid ( err. to_string ( ) ) ) ,
551
+ } ;
552
+
553
+ let historical_summaries_with_proof = match fork_name {
554
+ ForkName :: Bellatrix | ForkName :: Capella => {
555
+ return Err ( DecodeError :: BytesInvalid ( format ! (
556
+ "HistoricalSummariesWithProof not supported for fork digest: {fork_digest:?}"
557
+ ) ) )
558
+ }
559
+ ForkName :: Deneb => HistoricalSummariesWithProof :: Deneb (
560
+ HistoricalSummariesWithProofDeneb :: from_ssz_bytes (
561
+ historical_summaries_with_proof_bytes,
562
+ ) ?,
563
+ ) ,
564
+ ForkName :: Electra => HistoricalSummariesWithProof :: Electra (
565
+ HistoricalSummariesWithProofElectra :: from_ssz_bytes (
566
+ historical_summaries_with_proof_bytes,
567
+ ) ?,
568
+ ) ,
569
+ } ;
570
+ Ok ( Self {
571
+ fork_name,
572
+ historical_summaries_with_proof,
573
+ } )
531
574
}
532
575
}
533
576
@@ -710,11 +753,14 @@ mod test {
710
753
711
754
match & beacon_content {
712
755
BeaconContentValue :: HistoricalSummariesWithProof ( content) => {
713
- assert_eq ! (
714
- expected_epoch,
715
- content. historical_summaries_with_proof. epoch
716
- ) ;
717
756
assert_eq ! ( ForkName :: Deneb , content. fork_name) ;
757
+
758
+ let Ok ( historical_summaries_with_proof) =
759
+ content. historical_summaries_with_proof . as_deneb ( )
760
+ else {
761
+ panic ! ( "Expected Deneb historical summaries, actual: {content:?}" ) ;
762
+ } ;
763
+ assert_eq ! ( expected_epoch, historical_summaries_with_proof. epoch) ;
718
764
}
719
765
_ => panic ! ( "Invalid beacon content type!" ) ,
720
766
}
0 commit comments