Skip to content

Commit eeb7a63

Browse files
committed
fix: answer comments
1 parent 3a6da41 commit eeb7a63

File tree

10 files changed

+68
-57
lines changed

10 files changed

+68
-57
lines changed

Diff for: crates/codec/src/decoding/batch.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl Batch {
5454
let num_l1_messages = b.context.num_l1_messages as usize;
5555
let block_messages = l1_messages_buf.get(..num_l1_messages).unwrap_or(&[]);
5656
*l1_messages_buf = l1_messages_buf.get(num_l1_messages..).unwrap_or(&[]);
57+
5758
block_messages
5859
})
5960
.collect::<Vec<_>>();

Diff for: crates/codec/src/lib.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::decoding::{
1313
};
1414

1515
use alloy_eips::eip4844::Blob;
16-
use alloy_primitives::{Bytes, U256};
16+
use alloy_primitives::{ruint::UintTryTo, Bytes, U256};
1717

1818
/// The Codec.
1919
#[derive(Debug)]
@@ -43,7 +43,7 @@ impl Codec {
4343
/// Decodes the input data and returns the decoded [`Batch`].
4444
pub fn decode<T: CommitDataSource>(input: &T) -> Result<Batch, CodecError> {
4545
let calldata = input.calldata();
46-
let version = get_codec_version(calldata).ok_or(DecodingError::MissingCodecVersion)?;
46+
let version = get_codec_version(calldata)?;
4747

4848
let payload = match version {
4949
0 => decode_v0(calldata)?,
@@ -79,19 +79,22 @@ pub trait CommitDataSource {
7979
}
8080

8181
/// Returns the codec version from the calldata.
82-
fn get_codec_version(calldata: &[u8]) -> Option<u8> {
82+
fn get_codec_version(calldata: &[u8]) -> Result<u8, DecodingError> {
8383
const CODEC_VERSION_OFFSET_START: usize = 4;
84-
const CODEC_VERSION_OFFSET_END: usize = 4 + 32;
84+
const CODEC_VERSION_LEN: usize = 32;
85+
const CODEC_VERSION_OFFSET_END: usize = CODEC_VERSION_OFFSET_START + CODEC_VERSION_LEN;
8586
const HIGH_BYTES_FLAG: U256 =
8687
U256::from_limbs([u64::MAX, u64::MAX, u64::MAX, 0xffffffffffffff00]);
8788
const LOW_BYTES_FLAG: U256 = U256::from_limbs([0, 0, 0, 0xff]);
8889

89-
let version = calldata.get(CODEC_VERSION_OFFSET_START..CODEC_VERSION_OFFSET_END)?;
90+
let version = calldata
91+
.get(CODEC_VERSION_OFFSET_START..CODEC_VERSION_OFFSET_END)
92+
.ok_or(DecodingError::Eof)?;
9093
let version = U256::from_be_slice(version);
9194

9295
if (version & HIGH_BYTES_FLAG) != U256::ZERO {
93-
return None
96+
return Err(DecodingError::MissingCodecVersion)
9497
}
9598

96-
Some((version & LOW_BYTES_FLAG).saturating_to())
99+
Ok((version & LOW_BYTES_FLAG).uint_try_to().expect("masked to a single byte"))
97100
}

Diff for: crates/database/db/src/db.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mod test {
5353
use rollup_node_primitives::{BatchCommitData, L1MessageWithBlockNumber};
5454

5555
#[tokio::test]
56-
async fn test_database_round_trip_batch_input() {
56+
async fn test_database_round_trip_batch_commit() {
5757
// Set up the test database.
5858
let db = setup_test_db().await;
5959

@@ -63,12 +63,13 @@ mod test {
6363
let mut u = Unstructured::new(&bytes);
6464

6565
// Generate a random BatchInputV1.
66-
let batch_input = BatchCommitData::arbitrary(&mut u).unwrap();
66+
let batch_commit = BatchCommitData::arbitrary(&mut u).unwrap();
6767

6868
// Round trip the BatchInput through the database.
69-
db.insert_batch(batch_input.clone()).await.unwrap();
70-
let batch_input_from_db = db.get_batch_by_index(batch_input.index).await.unwrap().unwrap();
71-
assert_eq!(batch_input, batch_input_from_db);
69+
db.insert_batch(batch_commit.clone()).await.unwrap();
70+
let batch_commit_from_db =
71+
db.get_batch_by_index(batch_commit.index).await.unwrap().unwrap();
72+
assert_eq!(batch_commit, batch_commit_from_db);
7273
}
7374

7475
#[tokio::test]

Diff for: crates/database/db/src/models/batch_input.rs renamed to crates/database/db/src/models/batch_commit.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use sea_orm::{entity::prelude::*, ActiveValue};
55

66
/// A database model that represents a batch input.
77
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
8-
#[sea_orm(table_name = "batch_input")]
8+
#[sea_orm(table_name = "batch_commit")]
99
pub struct Model {
1010
#[sea_orm(primary_key)]
1111
index: i64,
@@ -24,15 +24,17 @@ pub enum Relation {}
2424
impl ActiveModelBehavior for ActiveModel {}
2525

2626
impl From<BatchCommitData> for ActiveModel {
27-
fn from(batch_input: BatchCommitData) -> Self {
27+
fn from(batch_commit: BatchCommitData) -> Self {
2828
Self {
29-
index: ActiveValue::Set(batch_input.index.try_into().expect("index should fit in i64")),
30-
hash: ActiveValue::Set(batch_input.hash.to_vec()),
29+
index: ActiveValue::Set(
30+
batch_commit.index.try_into().expect("index should fit in i64"),
31+
),
32+
hash: ActiveValue::Set(batch_commit.hash.to_vec()),
3133
block_number: ActiveValue::Set(
32-
batch_input.block_number.try_into().expect("block number should fit in i64"),
34+
batch_commit.block_number.try_into().expect("block number should fit in i64"),
3335
),
34-
calldata: ActiveValue::Set(batch_input.calldata.0.to_vec()),
35-
blob_hash: ActiveValue::Set(batch_input.blob_versioned_hash.map(|b| b.to_vec())),
36+
calldata: ActiveValue::Set(batch_commit.calldata.0.to_vec()),
37+
blob_hash: ActiveValue::Set(batch_commit.blob_versioned_hash.map(|b| b.to_vec())),
3638
finalized_block_number: ActiveValue::Unchanged(None),
3739
}
3840
}

Diff for: crates/database/db/src/models/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/// This module contains the batch input database model.
2-
pub mod batch_input;
1+
/// This module contains the batch commit database model.
2+
pub mod batch_commit;
33

44
/// This module contains the L1 message database model.
55
pub mod l1_message;

Diff for: crates/database/db/src/operations.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use sea_orm::{ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter, Se
1010
#[async_trait::async_trait]
1111
pub trait DatabaseOperations: DatabaseConnectionProvider {
1212
/// Insert a [`BatchCommitData`] into the database.
13-
async fn insert_batch(&self, batch_input: BatchCommitData) -> Result<(), DatabaseError> {
14-
tracing::trace!(target: "scroll::db", batch_hash = ?batch_input.hash, batch_index = batch_input.index, "Inserting batch input into database.");
15-
let batch_input: models::batch_input::ActiveModel = batch_input.into();
16-
batch_input.insert(self.get_connection()).await?;
13+
async fn insert_batch(&self, batch_commit: BatchCommitData) -> Result<(), DatabaseError> {
14+
tracing::trace!(target: "scroll::db", batch_hash = ?batch_commit.hash, batch_index = batch_commit.index, "Inserting batch input into database.");
15+
let batch_commit: models::batch_commit::ActiveModel = batch_commit.into();
16+
batch_commit.insert(self.get_connection()).await?;
1717
Ok(())
1818
}
1919

@@ -27,13 +27,13 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
2727
batch_hash: B256,
2828
block_number: u64,
2929
) -> Result<(), DatabaseError> {
30-
if let Some(batch) = models::batch_input::Entity::find()
31-
.filter(models::batch_input::Column::Hash.eq(batch_hash.to_vec()))
30+
if let Some(batch) = models::batch_commit::Entity::find()
31+
.filter(models::batch_commit::Column::Hash.eq(batch_hash.to_vec()))
3232
.one(self.get_connection())
3333
.await?
3434
{
3535
tracing::trace!(target: "scroll::db", batch_hash = ?batch_hash, block_number, "Finalizing batch input in database.");
36-
let mut batch: models::batch_input::ActiveModel = batch.into();
36+
let mut batch: models::batch_commit::ActiveModel = batch.into();
3737
batch.finalized_block_number = Set(Some(block_number as i64));
3838
batch.update(self.get_connection()).await?;
3939
} else {
@@ -54,7 +54,7 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
5454
&self,
5555
batch_index: u64,
5656
) -> Result<Option<BatchCommitData>, DatabaseError> {
57-
Ok(models::batch_input::Entity::find_by_id(
57+
Ok(models::batch_commit::Entity::find_by_id(
5858
TryInto::<i64>::try_into(batch_index).expect("index should fit in i64"),
5959
)
6060
.one(self.get_connection())
@@ -65,8 +65,8 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
6565
/// Delete all [`BatchCommitData`]s with a block number greater than the provided block number.
6666
async fn delete_batches_gt(&self, block_number: u64) -> Result<(), DatabaseError> {
6767
tracing::trace!(target: "scroll::db", block_number, "Deleting batch inputs greater than block number.");
68-
Ok(models::batch_input::Entity::delete_many()
69-
.filter(models::batch_input::Column::BlockNumber.gt(block_number as i64))
68+
Ok(models::batch_commit::Entity::delete_many()
69+
.filter(models::batch_commit::Column::BlockNumber.gt(block_number as i64))
7070
.exec(self.get_connection())
7171
.await
7272
.map(|_| ())?)
@@ -76,7 +76,7 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
7676
async fn get_batches<'a>(
7777
&'a self,
7878
) -> Result<impl Stream<Item = Result<BatchCommitData, DbErr>> + 'a, DbErr> {
79-
Ok(models::batch_input::Entity::find()
79+
Ok(models::batch_commit::Entity::find()
8080
.stream(self.get_connection())
8181
.await?
8282
.map(|res| res.map(Into::into)))

Diff for: crates/database/migration/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub use sea_orm_migration::prelude::*;
22

3-
mod m20220101_000001_create_batch_input_table;
3+
mod m20220101_000001_create_batch_commit_table;
44
mod m20250304_125946_add_l1_msg_table;
55

66
pub struct Migrator;
@@ -9,7 +9,7 @@ pub struct Migrator;
99
impl MigratorTrait for Migrator {
1010
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
1111
vec![
12-
Box::new(m20220101_000001_create_batch_input_table::Migration),
12+
Box::new(m20220101_000001_create_batch_commit_table::Migration),
1313
Box::new(m20250304_125946_add_l1_msg_table::Migration),
1414
]
1515
}

Diff for: crates/derivation-pipeline/src/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ pub trait L1MessageProvider {
3535
fn set_hash_cursor(&mut self, hash: B256);
3636
}
3737

38-
/// An instance of the trait can be used to provide L1 data.
39-
pub trait L1Provider: L1MessageProvider {
38+
/// An instance of the trait can be used to fetch L1 blob data.
39+
pub trait L1BlobProvider {
4040
/// Returns corresponding blob data for the provided hash.
4141
fn blob(&self, hash: B256) -> Option<Blob>;
4242
}
4343

44+
/// An instance of the trait can be used to provide L1 data.
45+
pub trait L1Provider: L1BlobProvider + L1MessageProvider {}
46+
impl<T> L1Provider for T where T: L1BlobProvider + L1MessageProvider {}
47+
4448
/// Returns an iterator over [`ScrollPayloadAttributes`] from the [`BatchCommitData`] and a
4549
/// [`L1Provider`].
4650
pub fn derive<P: L1Provider>(
@@ -110,7 +114,7 @@ mod tests {
110114
messages: RefCell<Vec<TxL1Message>>,
111115
}
112116

113-
impl L1Provider for TestL1MessageProvider {
117+
impl L1BlobProvider for TestL1MessageProvider {
114118
fn blob(&self, _hash: B256) -> Option<Blob> {
115119
None
116120
}

Diff for: crates/indexer/src/lib.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ mod test {
154154
rand::rng().fill(bytes.as_mut_slice());
155155
let mut u = Unstructured::new(&bytes);
156156

157-
let batch_input = BatchCommitData::arbitrary(&mut u).unwrap();
158-
indexer.handle_l1_notification(L1Notification::BatchCommit(batch_input.clone()));
157+
let batch_commit = BatchCommitData::arbitrary(&mut u).unwrap();
158+
indexer.handle_l1_notification(L1Notification::BatchCommit(batch_commit.clone()));
159159

160160
let _ = indexer.next().await;
161161

162-
let batch_input_result = db.get_batch_by_index(batch_input.index).await.unwrap().unwrap();
162+
let batch_commit_result = db.get_batch_by_index(batch_commit.index).await.unwrap().unwrap();
163163

164-
assert_eq!(batch_input, batch_input_result);
164+
assert_eq!(batch_commit, batch_commit_result);
165165
}
166166

167167
#[tokio::test]
@@ -196,22 +196,22 @@ mod test {
196196
let mut u = Unstructured::new(&bytes);
197197

198198
// Generate a 3 random batch inputs and set their block numbers
199-
let mut batch_input_block_1 = BatchCommitData::arbitrary(&mut u).unwrap();
200-
batch_input_block_1.block_number = 1;
201-
let batch_input_block_1 = batch_input_block_1;
199+
let mut batch_commit_block_1 = BatchCommitData::arbitrary(&mut u).unwrap();
200+
batch_commit_block_1.block_number = 1;
201+
let batch_commit_block_1 = batch_commit_block_1;
202202

203-
let mut batch_input_block_20 = BatchCommitData::arbitrary(&mut u).unwrap();
204-
batch_input_block_20.block_number = 20;
205-
let batch_input_block_20 = batch_input_block_20;
203+
let mut batch_commit_block_20 = BatchCommitData::arbitrary(&mut u).unwrap();
204+
batch_commit_block_20.block_number = 20;
205+
let batch_commit_block_20 = batch_commit_block_20;
206206

207-
let mut batch_input_block_30 = BatchCommitData::arbitrary(&mut u).unwrap();
208-
batch_input_block_30.block_number = 30;
209-
let batch_input_block_30 = batch_input_block_30;
207+
let mut batch_commit_block_30 = BatchCommitData::arbitrary(&mut u).unwrap();
208+
batch_commit_block_30.block_number = 30;
209+
let batch_commit_block_30 = batch_commit_block_30;
210210

211211
// Index batch inputs
212-
indexer.handle_l1_notification(L1Notification::BatchCommit(batch_input_block_1.clone()));
213-
indexer.handle_l1_notification(L1Notification::BatchCommit(batch_input_block_20.clone()));
214-
indexer.handle_l1_notification(L1Notification::BatchCommit(batch_input_block_30.clone()));
212+
indexer.handle_l1_notification(L1Notification::BatchCommit(batch_commit_block_1.clone()));
213+
indexer.handle_l1_notification(L1Notification::BatchCommit(batch_commit_block_20.clone()));
214+
indexer.handle_l1_notification(L1Notification::BatchCommit(batch_commit_block_30.clone()));
215215

216216
// Generate 3 random L1 messages and set their block numbers
217217
let mut l1_message_block_1 = L1MessageWithBlockNumber::arbitrary(&mut u).unwrap();
@@ -239,12 +239,12 @@ mod test {
239239
}
240240

241241
// Check that the batch input at block 30 is deleted
242-
let batch_inputs =
242+
let batch_commits =
243243
db.get_batches().await.unwrap().map(|res| res.unwrap()).collect::<Vec<_>>().await;
244244

245-
assert_eq!(2, batch_inputs.len());
246-
assert!(batch_inputs.contains(&batch_input_block_1));
247-
assert!(batch_inputs.contains(&batch_input_block_20));
245+
assert_eq!(2, batch_commits.len());
246+
assert!(batch_commits.contains(&batch_commit_block_1));
247+
assert!(batch_commits.contains(&batch_commit_block_20));
248248

249249
// check that the L1 message at block 30 is deleted
250250
let l1_messages =

0 commit comments

Comments
 (0)