Skip to content

Commit ffe96f1

Browse files
committed
feat: answer comments
1 parent 088a8d4 commit ffe96f1

File tree

3 files changed

+33
-60
lines changed

3 files changed

+33
-60
lines changed

crates/chain-orchestrator/src/lib.rs

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,7 @@ impl<
295295
{
296296
let block_info: L2BlockInfoWithL1Messages = (&block).into();
297297
self.database
298-
.tx_mut(move |tx| {
299-
let block_info = block_info.clone();
300-
async move {
301-
tx.update_l1_messages_from_l2_blocks(vec![block_info.clone()]).await
302-
}
303-
})
298+
.update_l1_messages_from_l2_blocks(vec![block_info.clone()])
304299
.await?;
305300
self.signer
306301
.as_mut()
@@ -334,7 +329,7 @@ impl<
334329
ChainOrchestratorCommand::Status(tx) => {
335330
let (l1_latest, l1_finalized, l1_processed) = self
336331
.database
337-
.tx_mut(|tx| async move {
332+
.tx(|tx| async move {
338333
let l1_latest = tx.get_latest_l1_block_number().await?;
339334
let l1_finalized = tx.get_finalized_l1_block_number().await?;
340335
let l1_processed = tx.get_processed_l1_block_number().await?;
@@ -491,12 +486,7 @@ impl<
491486

492487
// Insert the batch consolidation outcome into the database.
493488
let consolidation_outcome = batch_consolidation_outcome.clone();
494-
self.database
495-
.tx_mut(move |tx| {
496-
let consolidation_outcome = consolidation_outcome.clone();
497-
async move { tx.insert_batch_consolidation_outcome(consolidation_outcome).await }
498-
})
499-
.await?;
489+
self.database.insert_batch_consolidation_outcome(consolidation_outcome).await?;
500490

501491
Ok(Some(ChainOrchestratorEvent::BatchConsolidated(batch_consolidation_outcome)))
502492
}
@@ -509,11 +499,7 @@ impl<
509499
match &*notification {
510500
L1Notification::Processed(block_number) => {
511501
let block_number = *block_number;
512-
self.database
513-
.tx_mut(move |tx| async move {
514-
tx.set_processed_l1_block_number(block_number).await
515-
})
516-
.await?;
502+
self.database.set_processed_l1_block_number(block_number).await?;
517503
Ok(None)
518504
}
519505
L1Notification::Reorg(block_number) => self.handle_l1_reorg(*block_number).await,
@@ -548,10 +534,7 @@ impl<
548534
&self,
549535
block_number: u64,
550536
) -> Result<Option<ChainOrchestratorEvent>, ChainOrchestratorError> {
551-
self.database
552-
.tx_mut(move |tx| async move { tx.set_latest_l1_block_number(block_number).await })
553-
.await?;
554-
537+
self.database.set_latest_l1_block_number(block_number).await?;
555538
Ok(Some(ChainOrchestratorEvent::NewL1Block(block_number)))
556539
}
557540

@@ -565,9 +548,7 @@ impl<
565548
let now = Instant::now();
566549
let genesis_hash = self.config.chain_spec().genesis_hash();
567550
let UnwindResult { l1_block_number, queue_index, l2_head_block_number, l2_safe_block_info } =
568-
self.database
569-
.tx_mut(move |tx| async move { tx.unwind(genesis_hash, block_number).await })
570-
.await?;
551+
self.database.unwind(genesis_hash, block_number).await?;
571552

572553
let l2_head_block_info = if let Some(block_number) = l2_head_block_number {
573554
// Fetch the block hash of the new L2 head block.
@@ -840,11 +821,7 @@ impl<
840821

841822
// We optimistically persist the signature upon passing consensus checks.
842823
let block_hash = block_with_peer.block.header.hash_slow();
843-
self.database
844-
.tx_mut(move |tx| async move {
845-
tx.insert_signature(block_hash, block_with_peer.signature).await
846-
})
847-
.await?;
824+
self.database.insert_signature(block_hash, block_with_peer.signature).await?;
848825

849826
let received_block_number = block_with_peer.block.number;
850827
let received_block_hash = block_with_peer.block.header.hash_slow();
@@ -867,9 +844,7 @@ impl<
867844

868845
// Purge all L1 message to L2 block mappings as they may be invalid after an
869846
// optimistic sync.
870-
self.database
871-
.tx_mut(|tx| async move { tx.purge_l1_message_to_l2_block_mappings(None).await })
872-
.await?;
847+
self.database.purge_l1_message_to_l2_block_mappings(None).await?;
873848

874849
return Ok(Some(ChainOrchestratorEvent::OptimisticSync(block_info)));
875850
}
@@ -1151,15 +1126,9 @@ impl<
11511126
self.validate_l1_messages(&blocks_to_validate).await?;
11521127

11531128
self.database
1154-
.tx_mut(move |tx| {
1155-
let blocks_to_validate = blocks_to_validate.clone();
1156-
async move {
1157-
tx.update_l1_messages_from_l2_blocks(
1158-
blocks_to_validate.into_iter().map(|b| (&b).into()).collect(),
1159-
)
1160-
.await
1161-
}
1162-
})
1129+
.update_l1_messages_from_l2_blocks(
1130+
blocks_to_validate.into_iter().map(|b| (&b).into()).collect(),
1131+
)
11631132
.await?;
11641133

11651134
self.notify(ChainOrchestratorEvent::ChainConsolidated {
@@ -1200,10 +1169,7 @@ impl<
12001169
let count = l1_message_hashes.len();
12011170
let mut database_messages = self
12021171
.database
1203-
.tx(move |tx| async move {
1204-
tx.get_n_l1_messages(Some(L1MessageKey::block_number(first_block_number)), count)
1205-
.await
1206-
})
1172+
.get_n_l1_messages(Some(L1MessageKey::block_number(first_block_number)), count)
12071173
.await?
12081174
.into_iter();
12091175

@@ -1257,9 +1223,7 @@ async fn compute_l1_message_queue_hash(
12571223
} else if l1_message.queue_index > l1_v2_message_queue_start_index {
12581224
let index = l1_message.queue_index - 1;
12591225
let mut input = database
1260-
.tx(move |tx| async move {
1261-
tx.get_n_l1_messages(Some(L1MessageKey::from_queue_index(index)), 1).await
1262-
})
1226+
.get_n_l1_messages(Some(L1MessageKey::from_queue_index(index)), 1)
12631227
.await?
12641228
.first()
12651229
.map(|m| m.queue_hash)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ impl DatabaseService for Arc<DatabaseInner> {
4646
let tx = Arc::try_unwrap(tx);
4747

4848
if res.is_ok() {
49-
tx.map_err(|_| DatabaseError::CommitFailed)?.commit().await?;
49+
tx.map(|tx| tx.commit()).map_err(|_| DatabaseError::CommitFailed)?.await?;
5050
} else {
51-
tx.map_err(|_| DatabaseError::RollbackFailed)?.rollback().await?;
51+
tx.map(|tx| tx.rollback()).map_err(|_| DatabaseError::RollbackFailed)?.await?;
5252
}
5353
res
5454
}

crates/node/src/args.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ use scroll_alloy_hardforks::ScrollHardforks;
4343
use scroll_alloy_network::Scroll;
4444
use scroll_alloy_provider::{ScrollAuthApiEngineClient, ScrollEngineApi};
4545
use scroll_db::{
46-
Database, DatabaseConnectionProvider, DatabaseReadOperations, DatabaseWriteOperations,
46+
Database, DatabaseConnectionProvider, DatabaseError, DatabaseReadOperations,
47+
DatabaseWriteOperations,
4748
};
4849
use scroll_derivation_pipeline::DerivationPipeline;
4950
use scroll_engine::{Engine, ForkchoiceState};
@@ -257,16 +258,24 @@ impl ScrollRollupNodeConfig {
257258
let mut fcs =
258259
ForkchoiceState::from_provider(&l2_provider).await.unwrap_or_else(chain_spec_fcs);
259260

260-
// On startup we replay the latest batch of blocks from the database as such we set the safe
261-
// block hash to the latest block hash associated with the previous consolidated
262-
// batch in the database.
263-
let (_startup_safe_block, l1_start_block_number) =
264-
db.prepare_on_startup(chain_spec.genesis_hash()).await?;
265-
let l2_head_block_number = db.get_l2_head_block_number().await?;
266-
db.purge_l1_message_to_l2_block_mappings(Some(l2_head_block_number + 1)).await?;
261+
let genesis_hash = chain_spec.genesis_hash();
262+
let (l1_start_block_number, l2_head_block_number) = db
263+
.tx_mut(move |tx| async move {
264+
// On startup we replay the latest batch of blocks from the database as such we set
265+
// the safe block hash to the latest block hash associated with the
266+
// previous consolidated batch in the database.
267+
let (_startup_safe_block, l1_start_block_number) =
268+
tx.prepare_on_startup(genesis_hash).await?;
269+
270+
let l2_head_block_number = tx.get_l2_head_block_number().await?;
271+
tx.purge_l1_message_to_l2_block_mappings(Some(l2_head_block_number + 1)).await?;
272+
273+
let l2_head_block_number = tx.get_l2_head_block_number().await?;
274+
Ok::<_, DatabaseError>((l1_start_block_number, l2_head_block_number))
275+
})
276+
.await?;
267277

268278
// Update the head block info if available and ahead of finalized.
269-
let l2_head_block_number = db.get_l2_head_block_number().await?;
270279
if l2_head_block_number > fcs.finalized_block_info().number {
271280
let block = l2_provider
272281
.get_block(l2_head_block_number.into())

0 commit comments

Comments
 (0)