Skip to content

Commit f1364ed

Browse files
authored
feat: block data (#54)
* feat: add extra data migration * feat: add extra data to db operations * feat: block data provider * fix: constants * feat: move fcs to engine driver * feat: revert fcs to node manager * feat: point to reth * feat: remove BlockDataProvider * feat: update error * feat: bump reth * fix: answer comments * fix: lints * fix: include block data in binary * empty
1 parent 746bd79 commit f1364ed

File tree

20 files changed

+1300
-34
lines changed

20 files changed

+1300
-34
lines changed

Cargo.lock

+28-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ parking_lot = "0.12"
179179
rand = { version = "0.9" }
180180
reqwest = "0.12"
181181
secp256k1 = { version = "0.29", default-features = false }
182+
serde = { version = "1.0" }
183+
sea-orm = { version = "1.1.0" }
182184
thiserror = "2.0"
183185
tokio = { version = "1.39", default-features = false }
184186
tokio-stream = { version = "0.1", default-features = false }

bin/rollup/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ scroll-network.workspace = true
4848
scroll-wire.workspace = true
4949

5050
# rollup-node
51-
rollup-node-indexer.workspace = true
5251
rollup-node-manager.workspace = true
5352
rollup-node-providers.workspace = true
5453
rollup-node-watcher.workspace = true

crates/database/db/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ workspace = true
1111

1212
[dependencies]
1313
# alloy
14+
alloy-eips.workspace = true
1415
alloy-primitives.workspace = true
1516

1617
# scroll-alloy
1718
scroll-alloy-consensus.workspace = true
19+
scroll-alloy-rpc-types-engine.workspace = true
1820

1921
# scroll
2022
scroll-migration = { workspace = true, optional = true }
@@ -24,7 +26,7 @@ rollup-node-primitives.workspace = true
2426
async-trait.workspace = true
2527
auto_impl.workspace = true
2628
futures.workspace = true
27-
sea-orm = { version = "1.1.0", features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros"] }
29+
sea-orm = { workspace = true, features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros"] }
2830
thiserror.workspace = true
2931
tokio = { workspace = true, features = ["macros", "sync"] }
3032
tracing.workspace = true

crates/database/db/src/db.rs

+10
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ mod test {
126126
assert_eq!(l1_message, l1_message_from_db);
127127
}
128128

129+
#[tokio::test]
130+
async fn test_database_block_data_seed() {
131+
// Setup the test database.
132+
let db = setup_test_db().await;
133+
134+
// db should contain the seeded data after migration.
135+
let data = db.get_block_data(0.into()).await.unwrap();
136+
assert!(data.is_some());
137+
}
138+
129139
#[tokio::test]
130140
async fn test_database_tx() {
131141
// Setup the test database.

crates/database/db/src/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloy_eips::BlockId;
12
use alloy_primitives::B256;
23

34
/// The error type for database operations.
@@ -9,4 +10,7 @@ pub enum DatabaseError {
910
/// A batch was not found in the database.
1011
#[error("batch with hash [{0}] not found in database")]
1112
BatchNotFound(B256),
13+
/// The block was not found in database.
14+
#[error("no block for id {0}")]
15+
BlockNotFound(BlockId),
1216
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use alloy_primitives::U256;
2+
use scroll_alloy_rpc_types_engine::BlockDataHint;
3+
use sea_orm::entity::prelude::*;
4+
5+
/// A database model that represents extra data for the block.
6+
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
7+
#[sea_orm(table_name = "block_data")]
8+
pub struct Model {
9+
#[sea_orm(primary_key)]
10+
number: i64,
11+
hash: Vec<u8>,
12+
extra_data: Vec<u8>,
13+
difficulty: Vec<u8>,
14+
}
15+
16+
/// The relation for the extra data model.
17+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
18+
pub enum Relation {}
19+
20+
/// The active model behavior for the extra data model.
21+
impl ActiveModelBehavior for ActiveModel {}
22+
23+
impl From<Model> for BlockDataHint {
24+
fn from(value: Model) -> Self {
25+
Self {
26+
extra_data: value.extra_data.into(),
27+
difficulty: U256::from_be_slice(value.difficulty.as_ref()),
28+
}
29+
}
30+
}

crates/database/db/src/models/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/// This module contains the batch commit database model.
22
pub mod batch_commit;
33

4+
/// This module contains the block data database model.
5+
pub mod block_data;
6+
47
/// This module contains the L1 message database model.
58
pub mod l1_message;

crates/database/db/src/operations.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use super::{models, DatabaseError};
22
use crate::DatabaseConnectionProvider;
33

4+
use alloy_eips::{BlockId, BlockNumberOrTag};
45
use alloy_primitives::B256;
56
use futures::{Stream, StreamExt};
67
use rollup_node_primitives::{BatchCommitData, L1MessageWithBlockNumber};
8+
use scroll_alloy_rpc_types_engine::BlockDataHint;
79
use sea_orm::{ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter, Set};
810

911
/// The [`DatabaseOperations`] trait provides methods for interacting with the database.
@@ -125,6 +127,25 @@ pub trait DatabaseOperations: DatabaseConnectionProvider {
125127
.await?
126128
.map(|res| res.map(Into::into)))
127129
}
130+
131+
/// Get the extra data for the provided [`BlockId`].
132+
async fn get_block_data(
133+
&self,
134+
block_id: BlockId,
135+
) -> Result<Option<BlockDataHint>, DatabaseError> {
136+
let filter = match block_id {
137+
BlockId::Hash(hash) => models::block_data::Column::Hash.eq(hash.block_hash.to_vec()),
138+
BlockId::Number(BlockNumberOrTag::Number(number)) => {
139+
models::block_data::Column::Number.eq(number as i64)
140+
}
141+
x => return Err(DatabaseError::BlockNotFound(x)),
142+
};
143+
Ok(models::block_data::Entity::find()
144+
.filter(filter)
145+
.one(self.get_connection())
146+
.await
147+
.map(|x| x.map(Into::into))?)
148+
}
128149
}
129150

130151
impl<T> DatabaseOperations for T where T: DatabaseConnectionProvider {}

crates/database/migration/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ name = "migration"
99
path = "src/lib.rs"
1010

1111
[dependencies]
12+
alloy-primitives = { workspace = true, features = ["serde"] }
1213
async-std = { version = "1", features = ["attributes", "tokio1"] }
14+
csv = "1.3.1"
15+
sea-orm = { workspace = true, features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros"] }
16+
serde = { workspace = true, features = ["derive"] }
1317
tracing.workspace = true
1418

1519
[dependencies.sea-orm-migration]

0 commit comments

Comments
 (0)