@@ -6,12 +6,14 @@ use crate::{
66 SignedBeaconBlockHeader , Slot ,
77} ;
88use bls:: Signature ;
9+ use context_deserialize:: ContextDeserialize ;
910use derivative:: Derivative ;
1011use kzg:: Error as KzgError ;
1112use kzg:: { KzgCommitment , KzgProof } ;
1213use merkle_proof:: verify_merkle_proof;
1314use safe_arith:: ArithError ;
14- use serde:: { Deserialize , Serialize } ;
15+ use serde:: de:: Error ;
16+ use serde:: { Deserialize , Deserializer , Serialize } ;
1517use ssz:: { DecodeError , Encode } ;
1618use ssz_derive:: { Decode , Encode } ;
1719use ssz_types:: Error as SszError ;
@@ -26,12 +28,49 @@ pub type Cell<E> = FixedVector<u8, <E as EthSpec>::BytesPerCell>;
2628pub type DataColumn < E > = VariableList < Cell < E > , <E as EthSpec >:: MaxBlobCommitmentsPerBlock > ;
2729
2830/// Identifies a set of data columns associated with a specific beacon block.
29- #[ derive( Encode , Clone , Debug , PartialEq ) ]
31+ #[ derive( Encode , Clone , Debug , PartialEq , TreeHash ) ]
3032pub struct DataColumnsByRootIdentifier {
3133 pub block_root : Hash256 ,
3234 pub columns : RuntimeVariableList < ColumnIndex > ,
3335}
3436
37+ impl < ' de > ContextDeserialize < ' de , ( ForkName , usize ) > for DataColumnsByRootIdentifier {
38+ fn context_deserialize < D > ( deserializer : D , context : ( ForkName , usize ) ) -> Result < Self , D :: Error >
39+ where
40+ D : Deserializer < ' de > ,
41+ {
42+ #[ derive( Deserialize ) ]
43+ struct Helper {
44+ block_root : Hash256 ,
45+ columns : serde_json:: Value ,
46+ }
47+
48+ let helper = Helper :: deserialize ( deserializer) ?;
49+ Ok ( Self {
50+ block_root : helper. block_root ,
51+ columns : RuntimeVariableList :: context_deserialize ( helper. columns , context)
52+ . map_err ( Error :: custom) ?,
53+ } )
54+ }
55+ }
56+
57+ impl DataColumnsByRootIdentifier {
58+ pub fn from_ssz_bytes ( bytes : & [ u8 ] , num_columns : usize ) -> Result < Self , DecodeError > {
59+ let mut builder = ssz:: SszDecoderBuilder :: new ( bytes) ;
60+ builder. register_type :: < Hash256 > ( ) ?;
61+ builder. register_anonymous_variable_length_item ( ) ?;
62+
63+ let mut decoder = builder. build ( ) ?;
64+ let block_root = decoder. decode_next ( ) ?;
65+ let columns = decoder
66+ . decode_next_with ( |bytes| RuntimeVariableList :: from_ssz_bytes ( bytes, num_columns) ) ?;
67+ Ok ( DataColumnsByRootIdentifier {
68+ block_root,
69+ columns,
70+ } )
71+ }
72+ }
73+
3574impl RuntimeVariableList < DataColumnsByRootIdentifier > {
3675 pub fn from_ssz_bytes_with_nested (
3776 bytes : & [ u8 ] ,
@@ -47,21 +86,7 @@ impl RuntimeVariableList<DataColumnsByRootIdentifier> {
4786 Some ( max_len) ,
4887 ) ?
4988 . into_iter ( )
50- . map ( |bytes| {
51- let mut builder = ssz:: SszDecoderBuilder :: new ( & bytes) ;
52- builder. register_type :: < Hash256 > ( ) ?;
53- builder. register_anonymous_variable_length_item ( ) ?;
54-
55- let mut decoder = builder. build ( ) ?;
56- let block_root = decoder. decode_next ( ) ?;
57- let columns = decoder. decode_next_with ( |bytes| {
58- RuntimeVariableList :: from_ssz_bytes ( bytes, num_columns)
59- } ) ?;
60- Ok ( DataColumnsByRootIdentifier {
61- block_root,
62- columns,
63- } )
64- } )
89+ . map ( |bytes| DataColumnsByRootIdentifier :: from_ssz_bytes ( & bytes, num_columns) )
6590 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
6691
6792 Ok ( RuntimeVariableList :: from_vec ( vec, max_len) )
0 commit comments