Skip to content

Commit 00d18f2

Browse files
committed
address comments
1 parent 000ade7 commit 00d18f2

File tree

5 files changed

+63
-44
lines changed

5 files changed

+63
-44
lines changed

crates/chain-orchestrator/src/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ pub enum ChainOrchestratorError {
1111
DatabaseError(#[from] DatabaseError),
1212
/// An error occurred while trying to fetch the L2 block from the database.
1313
#[error("L2 block not found - block number: {0}")]
14-
L2BlockNotFound(u64),
14+
L2BlockNotFoundInDatabase(u64),
15+
/// An error occurred while trying to fetch the L2 block from the L2 client.
16+
#[error("L2 block not found in L2 client - block number: {0}")]
17+
L2BlockNotFoundInL2Client(u64),
1518
/// A fork was received from the peer that is associated with a reorg of the safe chain.
1619
#[error("L2 safe block reorg detected")]
1720
L2SafeBlockReorgDetected,

crates/chain-orchestrator/src/lib.rs

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ impl<
9999
l2_client: P,
100100
optimistic_sync_threshold: u64,
101101
chain_buffer_size: usize,
102-
) -> Self {
103-
let chain = init_chain_from_db(&database, &l2_client, chain_buffer_size).await;
104-
Self {
102+
) -> Result<Self, ChainOrchestratorError> {
103+
let chain = init_chain_from_db(&database, &l2_client, chain_buffer_size).await?;
104+
Ok(Self {
105105
network_client: Arc::new(block_client),
106106
l2_client: Arc::new(l2_client),
107107
chain: Arc::new(Mutex::new(chain)),
@@ -121,7 +121,7 @@ impl<
121121
chain_buffer_size,
122122
l1_synced: false,
123123
waker: AtomicWaker::new(),
124-
}
124+
})
125125
}
126126

127127
/// Wraps a pending chain orchestrator future, metering the completion of it.
@@ -237,10 +237,11 @@ impl<
237237
let mut optimistic_headers = Chain::new(chain_buffer_size);
238238
optimistic_headers.push_front(received_block.header.clone());
239239
while optimistic_headers.len() < chain_buffer_size &&
240-
optimistic_headers.first().unwrap().number != 0
240+
optimistic_headers.first().expect("chain can not be empty").number != 0
241241
{
242-
tracing::trace!(target: "scroll::chain_orchestrator", number = ?(optimistic_headers.first().unwrap().number - 1), "fetching block");
243-
let parent_hash = optimistic_headers.first().unwrap().parent_hash;
242+
tracing::trace!(target: "scroll::chain_orchestrator", number = ?(optimistic_headers.first().expect("chain can not be empty").number - 1), "fetching block");
243+
let parent_hash =
244+
optimistic_headers.first().expect("chain can not be empty").parent_hash;
244245
let header = network_client
245246
.get_header(BlockHashOrNumber::Hash(parent_hash))
246247
.await?
@@ -289,8 +290,8 @@ impl<
289290
// If we are in optimistic mode and the received chain can not be reconciled with the
290291
// in-memory chain we break. We will reconcile after optimistic sync has completed.
291292
if *optimistic_mode.lock().await &&
292-
received_chain_headers.last().unwrap().number <
293-
current_chain_headers.front().unwrap().number
293+
received_chain_headers.last().expect("chain can not be empty").number <
294+
current_chain_headers.front().expect("chain can not be empty").number
294295
{
295296
return Ok(ChainOrchestratorEvent::InsufficientDataForReceivedBlock(
296297
received_block.hash_slow(),
@@ -300,41 +301,56 @@ impl<
300301
// If the received header tail has a block number that is less than the current header
301302
// tail then we should fetch more headers for the current chain to aid
302303
// reconciliation.
303-
if received_chain_headers.last().unwrap().number <
304+
if received_chain_headers.last().expect("chain can not be empty").number <
304305
current_chain_headers.front().expect("chain can not be empty").number
305306
{
306307
for _ in 0..BATCH_FETCH_SIZE {
307-
if current_chain_headers.front().unwrap().number.saturating_sub(1) <=
308+
if current_chain_headers
309+
.front()
310+
.expect("chain can not be empty")
311+
.number
312+
.saturating_sub(1) <=
308313
latest_safe_block.number
309314
{
310315
tracing::info!(target: "scroll::chain_orchestrator", hash = %latest_safe_block.hash, number = %latest_safe_block.number, "reached safe block number for current chain - terminating fetching.");
311316
break;
312317
}
313-
tracing::trace!(target: "scroll::chain_orchestrator", number = ?(current_chain_headers.front().unwrap().number - 1), "fetching block for current chain");
318+
tracing::trace!(target: "scroll::chain_orchestrator", number = ?(current_chain_headers.front().expect("chain can not be empty").number - 1), "fetching block for current chain");
314319
if let Some(block) = l2_client
315-
.get_block_by_hash(current_chain_headers.front().unwrap().parent_hash)
320+
.get_block_by_hash(
321+
current_chain_headers
322+
.front()
323+
.expect("chain can not be empty")
324+
.parent_hash,
325+
)
316326
.await?
317327
{
318328
let header = block.into_consensus_header();
319329
current_chain_headers.push_front(header.clone());
320330
} else {
321331
return Err(ChainOrchestratorError::MissingBlockHeader {
322-
hash: current_chain_headers.front().unwrap().parent_hash,
332+
hash: current_chain_headers
333+
.front()
334+
.expect("chain can not be empty")
335+
.parent_hash,
323336
});
324337
}
325338
}
326339
}
327340

328341
// We search the in-memory chain to see if we can reconcile the block import.
329-
if let Some(pos) = current_chain_headers
330-
.iter()
331-
.rposition(|h| h.hash_slow() == received_chain_headers.last().unwrap().parent_hash)
332-
{
342+
if let Some(pos) = current_chain_headers.iter().rposition(|h| {
343+
h.hash_slow() ==
344+
received_chain_headers.last().expect("chain can not be empty").parent_hash
345+
}) {
333346
// If the received fork is older than the current chain, we return an event
334347
// indicating that we have received an old fork.
335348
if (pos < current_chain_headers.len() - 1) &&
336-
current_chain_headers.get(pos + 1).unwrap().timestamp >
337-
received_chain_headers.last().unwrap().timestamp
349+
current_chain_headers.get(pos + 1).expect("chain can not be empty").timestamp >
350+
received_chain_headers
351+
.last()
352+
.expect("chain can not be empty")
353+
.timestamp
338354
{
339355
return Ok(ChainOrchestratorEvent::OldForkReceived {
340356
headers: received_chain_headers,
@@ -347,22 +363,27 @@ impl<
347363

348364
// If the current header block number is less than the latest safe block number then
349365
// we should error.
350-
if received_chain_headers.last().unwrap().number <= latest_safe_block.number {
366+
if received_chain_headers.last().expect("chain can not be empty").number <=
367+
latest_safe_block.number
368+
{
351369
return Err(ChainOrchestratorError::L2SafeBlockReorgDetected);
352370
}
353371

354-
tracing::trace!(target: "scroll::chain_orchestrator", number = ?(received_chain_headers.last().unwrap().number - 1), "fetching block");
372+
tracing::trace!(target: "scroll::chain_orchestrator", number = ?(received_chain_headers.last().expect("chain can not be empty").number - 1), "fetching block");
355373
if let Some(header) = network_client
356374
.get_header(BlockHashOrNumber::Hash(
357-
received_chain_headers.last().unwrap().parent_hash,
375+
received_chain_headers.last().expect("chain can not be empty").parent_hash,
358376
))
359377
.await?
360378
.into_data()
361379
{
362380
received_chain_headers.push(header.clone());
363381
} else {
364382
return Err(ChainOrchestratorError::MissingBlockHeader {
365-
hash: received_chain_headers.last().unwrap().parent_hash,
383+
hash: received_chain_headers
384+
.last()
385+
.expect("chain can not be empty")
386+
.parent_hash,
366387
});
367388
}
368389
};
@@ -727,16 +748,15 @@ async fn init_chain_from_db<P: Provider<Scroll> + 'static>(
727748
database: &Arc<Database>,
728749
l2_client: &P,
729750
chain_buffer_size: usize,
730-
) -> BoundedVec<Header> {
751+
) -> Result<BoundedVec<Header>, ChainOrchestratorError> {
731752
let blocks = {
732753
let mut blocks = Vec::with_capacity(chain_buffer_size);
733-
let mut blocks_stream = database.get_l2_blocks().await.unwrap().take(chain_buffer_size);
734-
while let Some(block_info) = blocks_stream.try_next().await.unwrap() {
754+
let mut blocks_stream = database.get_l2_blocks().await?.take(chain_buffer_size);
755+
while let Some(block_info) = blocks_stream.try_next().await? {
735756
let header = l2_client
736757
.get_block_by_hash(block_info.hash)
737-
.await
738-
.unwrap()
739-
.unwrap()
758+
.await?
759+
.ok_or(ChainOrchestratorError::L2BlockNotFoundInL2Client(block_info.number))?
740760
.header
741761
.into_consensus();
742762
blocks.push(header);
@@ -746,7 +766,7 @@ async fn init_chain_from_db<P: Provider<Scroll> + 'static>(
746766
};
747767
let mut chain: Chain = Chain::new(chain_buffer_size);
748768
chain.extend(blocks);
749-
chain
769+
Ok(chain)
750770
}
751771

752772
impl<
@@ -838,7 +858,7 @@ async fn consolidate_chain<P: Provider<Scroll> + 'static>(
838858
if in_mem_safe_hash != safe_head.hash {
839859
// If we did not consolidate back to the safe head, we return an error.
840860
*current_chain.lock().await =
841-
init_chain_from_db(&database, &l2_client, chain_buffer_size).await;
861+
init_chain_from_db(&database, &l2_client, chain_buffer_size).await?;
842862

843863
return Err(ChainOrchestratorError::ChainInconsistency);
844864
}
@@ -868,7 +888,7 @@ async fn consolidate_chain<P: Provider<Scroll> + 'static>(
868888
// should return an error.
869889
if blocks_to_consolidate.front().unwrap().header.hash_slow() != safe_head.hash {
870890
*current_chain.lock().await =
871-
init_chain_from_db(&database, &l2_client, chain_buffer_size).await;
891+
init_chain_from_db(&database, &l2_client, chain_buffer_size).await?;
872892
return Err(ChainOrchestratorError::ChainInconsistency);
873893
}
874894
}
@@ -1139,7 +1159,8 @@ mod test {
11391159
TEST_OPTIMISTIC_SYNC_THRESHOLD,
11401160
TEST_CHAIN_BUFFER_SIZE,
11411161
)
1142-
.await,
1162+
.await
1163+
.unwrap(),
11431164
db,
11441165
)
11451166
}

crates/node/src/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl ScrollRollupNodeConfig {
321321
self.chain_orchestrator_args.optimistic_sync_trigger,
322322
self.chain_orchestrator_args.chain_buffer_size,
323323
)
324-
.await;
324+
.await?;
325325

326326
// Spawn the rollup node manager
327327
let (rnm, handle) = RollupNodeManager::new(

crates/node/tests/sync.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ async fn test_should_consolidate_after_optimistic_sync() -> eyre::Result<()> {
208208
consensus_args: ConsensusArgs::noop(),
209209
};
210210

211-
// Create the chain spec for scroll dev with Euclid v2 activated and a test genesis.
211+
// Create the chain spec for scroll dev with Feynman activated and a test genesis.
212212
let chain_spec = (*SCROLL_DEV).clone();
213213

214214
// Create a sequencer node and an unsynced node.
@@ -457,7 +457,7 @@ async fn test_consolidation() -> eyre::Result<()> {
457457
consensus_args: ConsensusArgs::noop(),
458458
};
459459

460-
// Create the chain spec for scroll dev with Euclid v2 activated and a test genesis.
460+
// Create the chain spec for scroll dev with Feynman activated and a test genesis.
461461
let chain_spec = (*SCROLL_DEV).clone();
462462

463463
// Create a sequencer node and an unsynced node.
@@ -630,7 +630,7 @@ async fn test_chain_orchestrator_shallow_reorg_with_gap() -> eyre::Result<()> {
630630
consensus_args: ConsensusArgs::noop(),
631631
};
632632

633-
// Create the chain spec for scroll dev with Euclid v2 activated and a test genesis.
633+
// Create the chain spec for scroll dev with Feynman activated and a test genesis.
634634
let chain_spec = (*SCROLL_DEV).clone();
635635

636636
// Create a sequencer node and an unsynced node.

crates/sequencer/tests/e2e.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ async fn can_build_blocks() {
4141
reth_tracing::init_test_tracing();
4242

4343
const BLOCK_BUILDING_DURATION: Duration = Duration::from_millis(0);
44-
// const BLOCK_GAP_TRIGGER: u64 = 100;
4544

4645
// setup a test node
4746
let (mut nodes, _tasks, wallet) = setup(1, false).await.unwrap();
@@ -284,8 +283,6 @@ async fn can_build_blocks_with_finalized_l1_messages() {
284283

285284
let chain_spec = SCROLL_DEV.clone();
286285
const BLOCK_BUILDING_DURATION: Duration = tokio::time::Duration::from_millis(0);
287-
// const BLOCK_GAP_TRIGGER: u64 = 100;
288-
289286
// setup a test node
290287
let (mut nodes, _tasks, wallet) =
291288
setup_engine(default_test_scroll_rollup_node_config(), 1, chain_spec, false, false)
@@ -603,7 +600,6 @@ async fn can_build_blocks_and_exit_at_gas_limit() {
603600
let chain_spec = SCROLL_DEV.clone();
604601
const MIN_TRANSACTION_GAS_COST: u64 = 21_000;
605602
const BLOCK_BUILDING_DURATION: Duration = Duration::from_millis(250);
606-
// const BLOCK_GAP_TRIGGER: u64 = 100;
607603
const TRANSACTIONS_COUNT: usize = 2000;
608604

609605
// setup a test node. use a high value for the payload building duration to be sure we don't
@@ -689,7 +685,6 @@ async fn can_build_blocks_and_exit_at_time_limit() {
689685
let chain_spec = SCROLL_DEV.clone();
690686
const MIN_TRANSACTION_GAS_COST: u64 = 21_000;
691687
const BLOCK_BUILDING_DURATION: Duration = Duration::from_secs(1);
692-
// const BLOCK_GAP_TRIGGER: u64 = 100;
693688
const TRANSACTIONS_COUNT: usize = 2000;
694689

695690
// setup a test node. use a low payload building duration in order to exit before we reach the

0 commit comments

Comments
 (0)