1- use crate :: { from_be_bytes_slice_and_advance_buf, from_slice_and_advance_buf} ;
1+ use crate :: {
2+ decoding:: constants:: SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE , error:: DecodingError ,
3+ from_be_bytes_slice_and_advance_buf, from_slice_and_advance_buf,
4+ } ;
25
36use alloy_primitives:: {
47 bytes:: { Buf , BufMut } ,
@@ -54,10 +57,10 @@ impl BatchHeaderV1 {
5457 }
5558
5659 /// Tries to read from the input buffer into the [`BatchHeaderV1`].
57- /// Returns [`None `] if the buffer.len() < [`BatchHeaderV1::BYTES_LENGTH`].
58- pub fn try_from_buf ( buf : & mut & [ u8 ] ) -> Option < Self > {
60+ /// Returns [`DecodingError::Eof `] if the buffer.len() < [`BatchHeaderV1::BYTES_LENGTH`].
61+ pub fn try_from_buf ( buf : & mut & [ u8 ] ) -> Result < Self , DecodingError > {
5962 if buf. len ( ) < Self :: BYTES_LENGTH {
60- return None
63+ return Err ( DecodingError :: Eof )
6164 }
6265
6366 let version = from_be_bytes_slice_and_advance_buf ! ( u8 , buf) ;
@@ -70,16 +73,20 @@ impl BatchHeaderV1 {
7073 let blob_versioned_hash = from_slice_and_advance_buf ! ( B256 , buf) ;
7174 let parent_batch_hash = from_slice_and_advance_buf ! ( B256 , buf) ;
7275
73- let skipped_l1_message_bitmap: Vec < _ > =
74- buf. chunks ( 32 ) . map ( |chunk| U256 :: from_be_slice ( chunk) ) . collect ( ) ;
76+ let skipped_l1_message_bitmap: Vec < _ > = buf
77+ . chunks ( SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE )
78+ . map ( |chunk| U256 :: from_be_slice ( chunk) )
79+ . collect ( ) ;
7580
7681 // check leftover bytes are correct.
77- if buf. len ( ) as u64 != l1_message_popped. div_ceil ( 256 ) * 32 {
78- return None
82+ if buf. len ( ) as u64 !=
83+ l1_message_popped. div_ceil ( 256 ) * SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE as u64
84+ {
85+ return Err ( DecodingError :: Eof )
7986 }
80- buf. advance ( skipped_l1_message_bitmap. len ( ) * 32 ) ;
87+ buf. advance ( skipped_l1_message_bitmap. len ( ) * SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE ) ;
8188
82- Some ( Self {
89+ Ok ( Self {
8390 version,
8491 batch_index,
8592 l1_message_popped,
@@ -94,7 +101,8 @@ impl BatchHeaderV1 {
94101 /// Computes the hash for the header.
95102 pub fn hash_slow ( & self ) -> B256 {
96103 let mut bytes = Vec :: < u8 > :: with_capacity (
97- Self :: BYTES_LENGTH + self . skipped_l1_message_bitmap . len ( ) * 32 ,
104+ Self :: BYTES_LENGTH +
105+ self . skipped_l1_message_bitmap . len ( ) * SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE ,
98106 ) ;
99107 bytes. put_slice ( & self . version . to_be_bytes ( ) ) ;
100108 bytes. put_slice ( & self . batch_index . to_be_bytes ( ) ) ;
0 commit comments