Skip to content

Commit 485f66c

Browse files
committed
fix: answer comments
1 parent 4bba46d commit 485f66c

File tree

15 files changed

+85
-58
lines changed

15 files changed

+85
-58
lines changed

crates/codec/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct BlockContext {
4040
impl BlockContext {
4141
pub const BYTES_LENGTH: usize = 60;
4242

43-
/// Pushes all fields of the [`BlockContext`] into the provided buf.
43+
/// Returns an owned array which contains all fields of the [`BlockContext`].
4444
pub fn to_be_bytes(&self) -> [u8; Self::BYTES_LENGTH] {
4545
let mut buf = [0u8; Self::BYTES_LENGTH];
4646

crates/codec/src/decoding/batch.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::{decoding::payload::PayloadData, BlockContext, L2Block};
1+
use crate::{
2+
decoding::{constants::KECCAK_256_DIGEST_BYTES_SIZE, payload::PayloadData},
3+
BlockContext, L2Block,
4+
};
25

36
use alloy_primitives::{bytes::BufMut, keccak256, B256};
47
use scroll_alloy_consensus::TxL1Message;
@@ -37,7 +40,8 @@ impl Batch {
3740
let blocks_buf = &mut (&**self.data.l2_blocks());
3841
let l1_messages_buf = &mut (&*l1_messages);
3942

40-
let mut chunk_hashes = Vec::with_capacity(chunks_count.len() * 32);
43+
let mut chunk_hashes =
44+
Vec::with_capacity(chunks_count.len() * KECCAK_256_DIGEST_BYTES_SIZE);
4145

4246
for chunk_count in chunks_count {
4347
// slice the blocks at chunk_count.
@@ -75,7 +79,8 @@ fn hash_chunk(
7579
// reserve the correct capacity.
7680
let l1_messages_count: usize =
7781
l1_messages_per_block.iter().map(|messages| messages.len()).sum();
78-
let mut capacity = l2_blocks.len() * (BlockContext::BYTES_LENGTH - 2) + l1_messages_count * 32;
82+
let mut capacity = l2_blocks.len() * (BlockContext::BYTES_LENGTH - 2) +
83+
l1_messages_count * KECCAK_256_DIGEST_BYTES_SIZE;
7984
if version == 0 {
8085
capacity += l2_blocks.iter().map(|b| b.transactions.len()).sum::<usize>();
8186
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// The length in bytes of the Keccak 256 hash digest.
2+
pub const KECCAK_256_DIGEST_BYTES_SIZE: usize = 32;
3+
4+
/// The length in bytes of each item in the skipped L1 messages bitmap.
5+
pub const SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE: usize = 32;

crates/codec/src/decoding/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ pub mod batch;
66
/// Blob related helpers.
77
pub mod blob;
88

9+
/// Constants involved in the decoding process.
10+
pub mod constants;
11+
912
mod macros;
1013

1114
/// V0 implementation of the decoding.

crates/codec/src/decoding/payload.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub struct PayloadData {
1616
/// Information about the state of the L1 message queue.
1717
#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
1818
pub enum L1MessageQueueInfo {
19-
/// The queue index of the l1 message.
20-
Indexed(u64),
19+
/// The initial index of the l1 message queue, before commitment of the payload.
20+
InitialQueueIndex(u64),
2121
/// The hashed state of the l1 message queue.
2222
Hashed {
2323
/// The previous l1 message queue hash.
@@ -41,15 +41,15 @@ impl PayloadData {
4141
/// Returns the l1 message queue index of the first message in the batch.
4242
pub fn queue_index_start(&self) -> Option<u64> {
4343
match self.l1_message_queue_info {
44-
L1MessageQueueInfo::Indexed(index) => Some(index),
44+
L1MessageQueueInfo::InitialQueueIndex(index) => Some(index),
4545
L1MessageQueueInfo::Hashed { .. } => None,
4646
}
4747
}
4848

4949
/// Returns the l1 message queue hash before the commitment of the batch.
5050
pub fn prev_l1_message_queue_hash(&self) -> Option<&B256> {
5151
match self.l1_message_queue_info {
52-
L1MessageQueueInfo::Indexed(_) => None,
52+
L1MessageQueueInfo::InitialQueueIndex(_) => None,
5353
L1MessageQueueInfo::Hashed { ref prev_l1_message_queue_hash, .. } => {
5454
Some(prev_l1_message_queue_hash)
5555
}
@@ -59,7 +59,7 @@ impl PayloadData {
5959
/// Returns the l1 message queue hash after the commitment of the batch.
6060
pub fn post_l1_message_queue_hash(&self) -> Option<&B256> {
6161
match self.l1_message_queue_info {
62-
L1MessageQueueInfo::Indexed(_) => None,
62+
L1MessageQueueInfo::InitialQueueIndex(_) => None,
6363
L1MessageQueueInfo::Hashed { ref post_l1_message_queue_hash, .. } => {
6464
Some(post_l1_message_queue_hash)
6565
}

crates/codec/src/decoding/v0/batch_header.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
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

36
use alloy_primitives::{
47
bytes::{Buf, BufMut},
@@ -49,10 +52,10 @@ impl BatchHeaderV0 {
4952
}
5053

5154
/// Tries to read from the input buffer into the [`BatchHeaderV0`].
52-
/// Returns [`None`] if the buffer.len() < [`BatchHeaderV0::BYTES_LENGTH`].
53-
pub fn try_from_buf(buf: &mut &[u8]) -> Option<Self> {
55+
/// Returns [`DecodingError::Eof`] if the buffer.len() < [`BatchHeaderV0::BYTES_LENGTH`].
56+
pub fn try_from_buf(buf: &mut &[u8]) -> Result<Self, DecodingError> {
5457
if buf.len() < Self::BYTES_LENGTH {
55-
return None
58+
return Err(DecodingError::Eof)
5659
}
5760

5861
let version = from_be_bytes_slice_and_advance_buf!(u8, buf);
@@ -64,16 +67,20 @@ impl BatchHeaderV0 {
6467
let data_hash = from_slice_and_advance_buf!(B256, buf);
6568
let parent_batch_hash = from_slice_and_advance_buf!(B256, buf);
6669

67-
let skipped_l1_message_bitmap: Vec<_> =
68-
buf.chunks(32).map(|chunk| U256::from_be_slice(chunk)).collect();
70+
let skipped_l1_message_bitmap: Vec<_> = buf
71+
.chunks(SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE)
72+
.map(|chunk| U256::from_be_slice(chunk))
73+
.collect();
6974

7075
// check leftover bytes are correct.
71-
if buf.len() as u64 != l1_message_popped.div_ceil(256) * 32 {
72-
return None
76+
if buf.len() as u64 !=
77+
l1_message_popped.div_ceil(256) * SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE as u64
78+
{
79+
return Err(DecodingError::Eof)
7380
}
74-
buf.advance(skipped_l1_message_bitmap.len() * 32);
81+
buf.advance(skipped_l1_message_bitmap.len() * SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE);
7582

76-
Some(Self {
83+
Ok(Self {
7784
version,
7885
batch_index,
7986
l1_message_popped,
@@ -87,7 +94,8 @@ impl BatchHeaderV0 {
8794
/// Computes the hash for the header.
8895
pub fn hash_slow(&self) -> B256 {
8996
let mut bytes = Vec::<u8>::with_capacity(
90-
Self::BYTES_LENGTH + self.skipped_l1_message_bitmap.len() * 32,
97+
Self::BYTES_LENGTH +
98+
self.skipped_l1_message_bitmap.len() * SKIPPED_L1_MESSAGE_BITMAP_ITEM_BYTES_SIZE,
9199
);
92100
bytes.put_slice(&self.version.to_be_bytes());
93101
bytes.put_slice(&self.batch_index.to_be_bytes());

crates/codec/src/decoding/v0/block_context.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{from_be_bytes_slice_and_advance_buf, BlockContext};
1+
use crate::{error::DecodingError, from_be_bytes_slice_and_advance_buf, BlockContext};
22

33
use alloy_primitives::{bytes::Buf, U256};
44

@@ -16,22 +16,22 @@ impl BlockContextV0 {
1616
pub(crate) const BYTES_LENGTH: usize = 60;
1717

1818
/// Tries to read from the input buffer into the [`BlockContextV0`].
19-
/// Returns [`None`] if the buffer.len() < [`BlockContextV0::BYTES_LENGTH`].
20-
pub(crate) fn try_from_buf(buf: &mut &[u8]) -> Option<Self> {
19+
/// Returns [`DecodingError::Eof`] if the buffer.len() < [`BlockContextV0::BYTES_LENGTH`].
20+
pub(crate) fn try_from_buf(buf: &mut &[u8]) -> Result<Self, DecodingError> {
2121
if buf.len() < Self::BYTES_LENGTH {
22-
return None
22+
return Err(DecodingError::Eof)
2323
}
2424
let number = from_be_bytes_slice_and_advance_buf!(u64, buf);
2525
let timestamp = from_be_bytes_slice_and_advance_buf!(u64, buf);
2626

27-
let base_fee = U256::from_be_slice(&buf[0..32]);
27+
let base_fee = U256::from_be_slice(&buf[..32]);
2828
buf.advance(32);
2929

3030
let gas_limit = from_be_bytes_slice_and_advance_buf!(u64, buf);
3131
let num_transactions = from_be_bytes_slice_and_advance_buf!(u16, buf);
3232
let num_l1_messages = from_be_bytes_slice_and_advance_buf!(u16, buf);
3333

34-
Some(Self { number, timestamp, base_fee, gas_limit, num_transactions, num_l1_messages })
34+
Ok(Self { number, timestamp, base_fee, gas_limit, num_transactions, num_l1_messages })
3535
}
3636

3737
/// Returns the L2 transaction count for the block, excluding L1 messages.

crates/codec/src/decoding/v0/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn decode_v0(calldata: &[u8]) -> Result<Batch, DecodingError> {
3838

3939
// for each block, decode into a block context
4040
for _ in 0..blocks_count {
41-
let context = BlockContextV0::try_from_buf(buf).ok_or(DecodingError::Eof)?;
41+
let context = BlockContextV0::try_from_buf(buf)?;
4242
block_contexts.push(context);
4343
}
4444

@@ -58,8 +58,7 @@ pub fn decode_v0(calldata: &[u8]) -> Result<Batch, DecodingError> {
5858

5959
// decode the parent batch header.
6060
let raw_parent_header = call.parent_batch_header().ok_or(DecodingError::MissingParentHeader)?;
61-
let parent_header = BatchHeaderV0::try_from_buf(&mut (&*raw_parent_header))
62-
.ok_or(DecodingError::InvalidParentHeaderFormat)?;
61+
let parent_header = BatchHeaderV0::try_from_buf(&mut (&*raw_parent_header))?;
6362
let l1_message_start_index = parent_header.total_l1_message_popped;
6463

6564
let payload =

crates/codec/src/decoding/v1/batch_header.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
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

36
use 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());

crates/codec/src/decoding/v1/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ pub fn decode_v1(calldata: &[u8], blob: &[u8]) -> Result<Batch, DecodingError> {
3333

3434
// decode the parent batch header.
3535
let raw_parent_header = call.parent_batch_header().ok_or(DecodingError::MissingParentHeader)?;
36-
let parent_header = BatchHeaderV1::try_from_buf(&mut (&*raw_parent_header))
37-
.ok_or(DecodingError::InvalidParentHeaderFormat)?;
36+
let parent_header = BatchHeaderV1::try_from_buf(&mut (&*raw_parent_header))?;
3837
let l1_message_start_index = parent_header.total_l1_message_popped;
3938

4039
let chunks = call.chunks().ok_or(DecodingError::MissingChunkData)?;
@@ -81,7 +80,7 @@ pub(crate) fn decode_v1_chunk(
8180

8281
// for each block, decode into a block context
8382
for _ in 0..blocks_count {
84-
let context = BlockContextV1::try_from_buf(buf).ok_or(DecodingError::Eof)?;
83+
let context = BlockContextV1::try_from_buf(buf)?;
8584
block_contexts.push(context);
8685
}
8786

0 commit comments

Comments
 (0)