Skip to content

Commit 6ff7d55

Browse files
committed
address feedback
1 parent 44424d6 commit 6ff7d55

File tree

9 files changed

+48
-79
lines changed

9 files changed

+48
-79
lines changed

crates/chain-orchestrator/src/consolidation.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ pub(crate) async fn reconcile_batch<L2P: Provider<Scroll>>(
3030
};
3131

3232
// Check if the block matches the derived attributes.
33-
if block_matches_attributes(
34-
&attributes.attributes,
35-
&current_block,
36-
current_block.parent_hash,
37-
) {
33+
if block_matches_attributes(&attributes.attributes, &current_block) {
3834
// Extract the block info with L1 messages.
3935
let block_info: L2BlockInfoWithL1Messages = (&current_block).into();
4036

crates/chain-orchestrator/src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ impl<
255255
match event {
256256
SignerEvent::SignedBlock { block, signature } => {
257257
let tx = self.database.tx_mut().await?;
258+
tx.set_l2_head_block_number(block.header.number).await?;
258259
tx.insert_signature(block.hash_slow(), signature).await?;
259260
tx.commit().await?;
260261
self.network.handle().announce_block(block.clone(), signature);
@@ -295,7 +296,6 @@ impl<
295296
let tx = self.database.tx_mut().await?;
296297
let block_info: L2BlockInfoWithL1Messages = (&block).into();
297298
tx.update_l1_messages_from_l2_blocks(vec![block_info.clone()]).await?;
298-
tx.set_l2_head_block_number(block_info.block_info.number).await?;
299299
tx.commit().await?;
300300
self.signer
301301
.as_mut()
@@ -1066,18 +1066,14 @@ impl<
10661066
}
10671067
}
10681068

1069-
// Persist the L1 message to L2 block mappings for reorg awareness, the block signature and
1070-
// handle the valid block import if we are in a synced state and the result is valid.
1069+
// Persist the L1 message to L2 block mappings for reorg awareness, update the l2 head block
1070+
// number and handle the valid block import if we are in a synced state and the
1071+
// result is valid.
10711072
if self.sync_state.is_synced() && result.is_valid() {
10721073
let blocks = chain.iter().map(|block| block.into()).collect::<Vec<_>>();
10731074
let tx = self.database.tx_mut().await?;
10741075
tx.update_l1_messages_from_l2_blocks(blocks).await?;
1075-
tx.commit().await?;
1076-
1077-
// Persist the signature for the block and notify the network manager of a successful
1078-
// import.
1079-
let tx = self.database.tx_mut().await?;
1080-
tx.insert_signature(chain_head_hash, block_with_peer.signature).await?;
1076+
tx.set_l2_head_block_number(block_with_peer.block.header.number).await?;
10811077
tx.commit().await?;
10821078

10831079
self.network.handle().block_import_outcome(BlockImportOutcome::valid_block(

crates/database/db/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ mod test {
940940

941941
// Retrieve and verify the head block info.
942942
let tx = db.tx().await.unwrap();
943-
let head_block_info = tx.get_l2_head_block_number().await.unwrap().unwrap();
943+
let head_block_info = tx.get_l2_head_block_number().await.unwrap();
944944

945945
assert_eq!(head_block_info, block_info.number);
946946
}

crates/database/db/src/operations.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,15 +544,17 @@ pub trait DatabaseReadOperations: ReadConnectionProvider + Sync {
544544
}
545545

546546
/// Get the latest L2 head block info.
547-
async fn get_l2_head_block_number(&self) -> Result<Option<u64>, DatabaseError> {
547+
async fn get_l2_head_block_number(&self) -> Result<u64, DatabaseError> {
548548
Ok(models::metadata::Entity::find()
549549
.filter(models::metadata::Column::Key.eq("l2_head_block"))
550550
.select_only()
551551
.column(models::metadata::Column::Value)
552552
.into_tuple::<String>()
553553
.one(self.get_connection())
554-
.await
555-
.map(|x| x.and_then(|x| serde_json::from_str(&x).ok()))?)
554+
.await?
555+
.expect("l2_head_block should always be set")
556+
.parse::<u64>()
557+
.expect("l2_head_block should always be a valid u64"))
556558
}
557559

558560
/// Get an iterator over all [`BatchCommitData`]s in the database.

crates/database/migration/src/m20251005_160938_add_initial_l1_block_numbers.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ impl MigrationTrait for Migration {
1414
INSERT INTO metadata (key, value)
1515
VALUES
1616
('l1_finalized_block', '0'),
17-
('l1_latest_block', '0')
17+
('l1_latest_block', '0'),
18+
('l2_head_block', '0')
1819
ON CONFLICT(key) DO NOTHING;
1920
"#,
2021
)
@@ -29,7 +30,7 @@ impl MigrationTrait for Migration {
2930
db.execute_unprepared(
3031
r#"
3132
DELETE FROM metadata
32-
WHERE key IN ('l1_finalized_block', 'l1_latest_block');
33+
WHERE key IN ('l1_finalized_block', 'l1_latest_block', 'l2_head_block');
3334
"#,
3435
)
3536
.await?;

crates/engine/src/payload.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
use alloy_primitives::{B256, B64};
1+
use alloy_primitives::B64;
22
use reth_primitives_traits::{AlloyBlockHeader, Block, BlockBody};
33
use scroll_alloy_rpc_types_engine::ScrollPayloadAttributes;
44

55
use tracing::debug;
66

77
/// Returns true if the [`Block`] matches the [`ScrollPayloadAttributes`]:
8-
/// - parent hash matches the parent hash of the [`Block`].
98
/// - all transactions match.
109
/// - timestamps are equal.
1110
/// - `prev_randaos` are equal.
1211
/// - `block_data_hint` matches the block data if present.
13-
pub fn block_matches_attributes<B: Block>(
14-
attributes: &ScrollPayloadAttributes,
15-
block: &B,
16-
parent_hash: B256,
17-
) -> bool {
12+
pub fn block_matches_attributes<B: Block>(attributes: &ScrollPayloadAttributes, block: &B) -> bool {
1813
let header = block.header();
19-
if header.parent_hash() != parent_hash {
20-
debug!(
21-
target: "scroll::engine::driver",
22-
expected = ?parent_hash,
23-
got = ?header.parent_hash(),
24-
"reorg: mismatch in parent hash"
25-
);
26-
return false;
27-
}
2814

2915
let payload_transactions = &block.body().encoded_2718_transactions();
3016
let matching_transactions =
@@ -109,7 +95,7 @@ mod tests {
10995

11096
use alloy_consensus::Header;
11197
use alloy_eips::Encodable2718;
112-
use alloy_primitives::{Bytes, U256};
98+
use alloy_primitives::{Bytes, B256, U256};
11399
use arbitrary::{Arbitrary, Unstructured};
114100
use reth_scroll_primitives::ScrollBlock;
115101
use reth_testing_utils::{generators, generators::Rng};
@@ -154,7 +140,7 @@ mod tests {
154140
body: alloy_consensus::BlockBody { transactions, ..Default::default() },
155141
};
156142

157-
assert!(block_matches_attributes(&attributes, &block, parent_hash));
143+
assert!(block_matches_attributes(&attributes, &block));
158144

159145
Ok(())
160146
}
@@ -185,7 +171,7 @@ mod tests {
185171
body: alloy_consensus::BlockBody { transactions, ..Default::default() },
186172
};
187173

188-
assert!(!block_matches_attributes(&attributes, &block, parent_hash));
174+
assert!(!block_matches_attributes(&attributes, &block));
189175

190176
Ok(())
191177
}

crates/node/src/args.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -266,34 +266,23 @@ impl ScrollRollupNodeConfig {
266266
let tx = db.tx_mut().await?;
267267
let (_startup_safe_block, l1_start_block_number) =
268268
tx.prepare_on_startup(chain_spec.genesis_hash()).await?;
269+
let l2_head_block_number = tx.get_l2_head_block_number().await?;
270+
tx.purge_l1_message_to_l2_block_mappings(Some(l2_head_block_number + 1)).await?;
269271
tx.commit().await?;
270-
// if let Some(block_info) = startup_safe_block {
271-
// fcs.update(None, Some(block_info), Some(block_info))?;
272-
// } else {
273-
// fcs.update(
274-
// None,
275-
// Some(BlockInfo {
276-
// hash: genesis_hash_from_chain_spec(chain_spec.clone()).unwrap(),
277-
// number: 0,
278-
// }),
279-
// None,
280-
// )?;
281-
// }
282272

283273
// Update the head block info if available and ahead of finalized.
284-
if let Some(latest_block_number) = db.tx().await?.get_l2_head_block_number().await? {
285-
if latest_block_number > fcs.finalized_block_info().number {
286-
let block = l2_provider
287-
.get_block(latest_block_number.into())
288-
.full()
289-
.await?
290-
.expect("latest block from db should exist")
291-
.into_consensus()
292-
.map_transactions(|tx| tx.inner.into_inner());
293-
let block_info: BlockInfo = (&block).into();
294-
295-
fcs.update(Some(block_info), None, None)?;
296-
}
274+
let l2_head_block_number = db.tx().await?.get_l2_head_block_number().await?;
275+
if l2_head_block_number > fcs.finalized_block_info().number {
276+
let block = l2_provider
277+
.get_block(l2_head_block_number.into())
278+
.full()
279+
.await?
280+
.expect("latest block from db should exist")
281+
.into_consensus()
282+
.map_transactions(|tx| tx.inner.into_inner());
283+
let block_info: BlockInfo = (&block).into();
284+
285+
fcs.update(Some(block_info), None, None)?;
297286
}
298287

299288
let chain_spec = Arc::new(chain_spec.clone());

crates/node/tests/e2e.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,10 +1080,9 @@ async fn shutdown_consolidates_most_recent_batch_on_startup() -> eyre::Result<()
10801080
}
10811081

10821082
/// Test that when the rollup node manager is shutdown, it restarts with the head set to the latest
1083-
/// sequenced block stored in database.
1083+
/// signed block stored in database.
10841084
#[tokio::test]
1085-
async fn graceful_shutdown_sets_fcs_to_latest_sequenced_block_in_db_on_start_up() -> eyre::Result<()>
1086-
{
1085+
async fn graceful_shutdown_sets_fcs_to_latest_signed_block_in_db_on_start_up() -> eyre::Result<()> {
10871086
reth_tracing::init_test_tracing();
10881087
let chain_spec = (*SCROLL_DEV).clone();
10891088

@@ -1152,7 +1151,7 @@ async fn graceful_shutdown_sets_fcs_to_latest_sequenced_block_in_db_on_start_up(
11521151
handle.build_block();
11531152
let block_number = loop {
11541153
let _ = rnm.poll_unpin(&mut Context::from_waker(noop_waker_ref()));
1155-
if let Poll::Ready(Some(ChainOrchestratorEvent::BlockSequenced(block))) =
1154+
if let Poll::Ready(Some(ChainOrchestratorEvent::SignedBlock { block, signature: _ })) =
11561155
rnm_events.poll_next_unpin(&mut Context::from_waker(noop_waker_ref()))
11571156
{
11581157
break block.header.number

crates/sequencer/src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ impl<SMP, CS> Stream for Sequencer<SMP, CS> {
236236
let this = self.get_mut();
237237
this.waker.register(cx.waker());
238238

239+
// If there is an inflight payload building job, poll it.
240+
if let Some(payload_building_job) = this.payload_building_job.as_mut() {
241+
match payload_building_job.future.as_mut().poll(cx) {
242+
Poll::Ready(payload_id) => {
243+
this.payload_building_job = None;
244+
return Poll::Ready(Some(SequencerEvent::PayloadReady(payload_id)));
245+
}
246+
Poll::Pending => {}
247+
}
248+
}
249+
239250
// Poll the trigger to see if it's time to build a new block.
240251
if let Some(trigger) = this.trigger.as_mut() {
241252
match trigger.poll_tick(cx) {
@@ -250,17 +261,6 @@ impl<SMP, CS> Stream for Sequencer<SMP, CS> {
250261
}
251262
}
252263

253-
// If there is an inflight payload building job, poll it.
254-
if let Some(payload_building_job) = this.payload_building_job.as_mut() {
255-
match payload_building_job.future.as_mut().poll(cx) {
256-
Poll::Ready(payload_id) => {
257-
this.payload_building_job = None;
258-
return Poll::Ready(Some(SequencerEvent::PayloadReady(payload_id)));
259-
}
260-
Poll::Pending => {}
261-
}
262-
}
263-
264264
Poll::Pending
265265
}
266266
}

0 commit comments

Comments
 (0)