Skip to content

Commit c501e90

Browse files
authored
Merge branch 'main' into feat/docker-compose-tests-issue-transactions
2 parents fcd5e1c + d6f9818 commit c501e90

File tree

27 files changed

+358
-119
lines changed

27 files changed

+358
-119
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ parking_lot = "0.12"
220220
rand = { version = "0.9" }
221221
reqwest = "0.12"
222222
serde = { version = "1.0" }
223+
serde_json = { version = "1.0" }
223224
sea-orm = { version = "1.1.0" }
224225
thiserror = "2.0"
225226
tokio = { version = "1.39", default-features = false }

crates/chain-orchestrator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ futures.workspace = true
7171
parking_lot.workspace = true
7272
rand.workspace = true
7373
reqwest.workspace = true
74-
serde_json = { version = "1.0" }
74+
serde_json.workspace = true
7575
tokio.workspace = true

crates/chain-orchestrator/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloy_json_rpc::RpcError;
22
use alloy_primitives::B256;
33
use alloy_transport::TransportErrorKind;
4-
use scroll_db::{DatabaseError, L1MessageStart};
4+
use scroll_db::{DatabaseError, L1MessageKey};
55

66
/// A type that represents an error that occurred in the chain orchestrator.
77
#[derive(Debug, thiserror::Error)]
@@ -28,7 +28,7 @@ pub enum ChainOrchestratorError {
2828
},
2929
/// An L1 message was not found in the database.
3030
#[error("L1 message not found at {0}")]
31-
L1MessageNotFound(L1MessageStart),
31+
L1MessageNotFound(L1MessageKey),
3232
/// A gap was detected in the L1 message queue: the previous message before index {0} is
3333
/// missing.
3434
#[error("L1 message queue gap detected at index {0}, previous L1 message not found")]

crates/chain-orchestrator/src/lib.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use scroll_alloy_hardforks::ScrollHardforks;
1919
use scroll_alloy_network::Scroll;
2020
use scroll_db::{
2121
Database, DatabaseError, DatabaseReadOperations, DatabaseTransactionProvider,
22-
DatabaseWriteOperations, L1MessageStart, UnwindResult,
22+
DatabaseWriteOperations, L1MessageKey, UnwindResult,
2323
};
2424
use scroll_network::NewBlockWithPeer;
2525
use std::{
@@ -215,9 +215,10 @@ impl<
215215
let block_info: L2BlockInfoWithL1Messages = (&block_with_peer.block).into();
216216
Self::do_handle_block_from_peer(ctx, block_with_peer).await?;
217217
Retry::default()
218-
.retry("update_l1_messages_with_l2_block", || async {
218+
.retry("handle_sequenced_block", || async {
219219
let tx = database.tx_mut().await?;
220220
tx.update_l1_messages_with_l2_block(block_info.clone()).await?;
221+
tx.set_l2_head_block_info(block_info.block_info).await?;
221222
tx.commit().await?;
222223
Ok::<_, ChainOrchestratorError>(())
223224
})
@@ -483,9 +484,7 @@ impl<
483484
Retry::default()
484485
.retry("insert_block", || async {
485486
let tx = database.tx_mut().await?;
486-
for block in block_infos.clone() {
487-
tx.insert_block(block, batch_info).await?;
488-
}
487+
tx.insert_blocks(block_infos.clone(), batch_info).await?;
489488
tx.commit().await?;
490489
Ok::<_, ChainOrchestratorError>(())
491490
})
@@ -537,6 +536,7 @@ impl<
537536
.retry("update_l1_messages_from_l2_blocks", || async {
538537
let tx = database.tx_mut().await?;
539538
tx.update_l1_messages_from_l2_blocks(block_info.clone()).await?;
539+
tx.set_l2_head_block_info(head.block_info).await?;
540540
tx.commit().await?;
541541
Ok::<_, ChainOrchestratorError>(())
542542
})
@@ -838,7 +838,7 @@ async fn compute_l1_message_queue_hash(
838838
})
839839
.await?
840840
.map(|m| m.queue_hash)
841-
.ok_or(DatabaseError::L1MessageNotFound(L1MessageStart::Index(index)))?
841+
.ok_or(DatabaseError::L1MessageNotFound(L1MessageKey::QueueIndex(index)))?
842842
.unwrap_or_default()
843843
.to_vec();
844844
input.append(&mut l1_message.tx_hash().to_vec());
@@ -1078,7 +1078,9 @@ async fn validate_l1_messages(
10781078
let l1_message_stream = Retry::default()
10791079
.retry("get_l1_messages", || async {
10801080
let messages = tx
1081-
.get_l1_messages(l1_message_hashes.first().map(|tx| L1MessageStart::Hash(*tx)))
1081+
.get_l1_messages(
1082+
l1_message_hashes.first().map(|tx| L1MessageKey::TransactionHash(*tx)),
1083+
)
10821084
.await?;
10831085
Ok::<_, ChainOrchestratorError>(messages)
10841086
})
@@ -1093,7 +1095,9 @@ async fn validate_l1_messages(
10931095
.await
10941096
.map(|m| m.map(|msg| msg.transaction.tx_hash()))
10951097
.transpose()?
1096-
.ok_or(ChainOrchestratorError::L1MessageNotFound(L1MessageStart::Hash(message_hash)))?;
1098+
.ok_or(ChainOrchestratorError::L1MessageNotFound(L1MessageKey::TransactionHash(
1099+
message_hash,
1100+
)))?;
10971101

10981102
// If the received and expected L1 messages do not match return an error.
10991103
if message_hash != expected_hash {

crates/database/db/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ futures.workspace = true
2929
metrics.workspace = true
3030
metrics-derive.workspace = true
3131
sea-orm = { workspace = true, features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros"] }
32+
serde_json.workspace = true
3233
tempfile = { version = "3.20.0", optional = true }
3334
thiserror.workspace = true
3435
tokio = { workspace = true, features = ["macros", "sync"] }

crates/database/db/src/db.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ impl Database {
9898
db.tmp_dir = Some(dir);
9999
Ok(db)
100100
}
101+
102+
/// Returns a reference to the database tmp dir.
103+
#[cfg(feature = "test-utils")]
104+
pub const fn tmp_dir(&self) -> Option<&tempfile::TempDir> {
105+
self.tmp_dir.as_ref()
106+
}
101107
}
102108

103109
#[async_trait::async_trait]
@@ -961,4 +967,27 @@ mod test {
961967
assert!(retried_block_3.is_none());
962968
assert!(retried_block_4.is_none());
963969
}
970+
971+
#[tokio::test]
972+
async fn test_l2_block_head_roundtrip() {
973+
// Set up the test database.
974+
let db = setup_test_db().await;
975+
let tx = db.tx_mut().await.unwrap();
976+
977+
// Generate unstructured bytes.
978+
let mut bytes = [0u8; 40];
979+
rand::rng().fill(bytes.as_mut_slice());
980+
let mut u = Unstructured::new(&bytes);
981+
982+
// Generate and insert a block info as the head.
983+
let block_info = BlockInfo::arbitrary(&mut u).unwrap();
984+
tx.set_l2_head_block_info(block_info).await.unwrap();
985+
tx.commit().await.unwrap();
986+
987+
// Retrieve and verify the head block info.
988+
let tx = db.tx().await.unwrap();
989+
let head_block_info = tx.get_l2_head_block_info().await.unwrap().unwrap();
990+
991+
assert_eq!(head_block_info, block_info);
992+
}
964993
}

crates/database/db/src/error.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::L1MessageStart;
1+
use super::L1MessageKey;
22
use sea_orm::sqlx::Error as SqlxError;
33

44
/// The error type for database operations.
@@ -13,7 +13,10 @@ pub enum DatabaseError {
1313
/// A generic error occurred.
1414
#[error("parse signature error: {0}")]
1515
ParseSignatureError(String),
16+
/// Failed to serde the metadata value.
17+
#[error("failed to serde metadata value: {0}")]
18+
MetadataSerdeError(#[from] serde_json::Error),
1619
/// The L1 message was not found in database.
17-
#[error("L1 message at index [{0}] not found in database")]
18-
L1MessageNotFound(L1MessageStart),
20+
#[error("L1 message at key [{0}] not found in database")]
21+
L1MessageNotFound(L1MessageKey),
1922
}

crates/database/db/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ mod models;
1515
pub use models::*;
1616

1717
mod operations;
18-
pub use operations::{
19-
DatabaseReadOperations, DatabaseWriteOperations, L1MessageStart, UnwindResult,
20-
};
18+
pub use operations::{DatabaseReadOperations, DatabaseWriteOperations, L1MessageKey, UnwindResult};
2119

2220
mod transaction;
2321
pub use transaction::{DatabaseTransactionProvider, TXMut, TX};

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ impl ActiveModelBehavior for ActiveModel {}
2121

2222
impl From<Metadata> for ActiveModel {
2323
fn from(metadata: Metadata) -> Self {
24-
Self {
25-
key: ActiveValue::Set("l1_finalized_block".to_owned()),
26-
value: ActiveValue::Set(metadata.l1_finalized_block.to_string()),
27-
}
28-
}
29-
}
30-
31-
impl From<Model> for Metadata {
32-
fn from(value: Model) -> Self {
33-
debug_assert!(value.key == "l1_finalized_block");
34-
Self { l1_finalized_block: value.value.parse().expect("invalid value") }
24+
Self { key: ActiveValue::Set(metadata.key), value: ActiveValue::Set(metadata.value) }
3525
}
3626
}

0 commit comments

Comments
 (0)