Skip to content

Commit 95237eb

Browse files
authored
feat: standard order for received block headers (#272)
1 parent aea3c93 commit 95237eb

File tree

1 file changed

+13
-15
lines changed
  • crates/chain-orchestrator/src

1 file changed

+13
-15
lines changed

crates/chain-orchestrator/src/lib.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<
278278

279279
// We fetch headers for the received chain until we can reconcile it with the chain we
280280
// currently have in-memory.
281-
let mut received_chain_headers = vec![received_block.header.clone()];
281+
let mut received_chain_headers = VecDeque::from(vec![received_block.header.clone()]);
282282

283283
// We should never have a re-org that is deeper than the current safe head.
284284
let (latest_safe_block, _) =
@@ -290,7 +290,7 @@ impl<
290290
// If we are in optimistic mode and the received chain can not be reconciled with the
291291
// in-memory chain we break. We will reconcile after optimistic sync has completed.
292292
if *optimistic_mode.lock().await &&
293-
received_chain_headers.last().expect("chain can not be empty").number <
293+
received_chain_headers.front().expect("chain can not be empty").number <
294294
current_chain_headers.front().expect("chain can not be empty").number
295295
{
296296
return Ok(ChainOrchestratorEvent::InsufficientDataForReceivedBlock(
@@ -300,7 +300,7 @@ impl<
300300

301301
// If the current header block number is less than the latest safe block number then
302302
// we should error.
303-
if received_chain_headers.last().expect("chain can not be empty").number <=
303+
if received_chain_headers.front().expect("chain can not be empty").number <=
304304
latest_safe_block.number
305305
{
306306
return Err(ChainOrchestratorError::L2SafeBlockReorgDetected);
@@ -309,7 +309,7 @@ impl<
309309
// If the received header tail has a block number that is less than the current header
310310
// tail then we should fetch more headers for the current chain to aid
311311
// reconciliation.
312-
if received_chain_headers.last().expect("chain can not be empty").number <
312+
if received_chain_headers.front().expect("chain can not be empty").number <
313313
current_chain_headers.front().expect("chain can not be empty").number
314314
{
315315
for _ in 0..BATCH_FETCH_SIZE {
@@ -349,53 +349,51 @@ impl<
349349
// We search the in-memory chain to see if we can reconcile the block import.
350350
if let Some(pos) = current_chain_headers.iter().rposition(|h| {
351351
h.hash_slow() ==
352-
received_chain_headers.last().expect("chain can not be empty").parent_hash
352+
received_chain_headers.front().expect("chain can not be empty").parent_hash
353353
}) {
354354
// If the received fork is older than the current chain, we return an event
355355
// indicating that we have received an old fork.
356356
if (pos < current_chain_headers.len() - 1) &&
357357
current_chain_headers.get(pos + 1).expect("chain can not be empty").timestamp >
358358
received_chain_headers
359-
.last()
359+
.front()
360360
.expect("chain can not be empty")
361361
.timestamp
362362
{
363363
return Ok(ChainOrchestratorEvent::OldForkReceived {
364-
headers: received_chain_headers,
364+
headers: received_chain_headers.into(),
365365
peer_id,
366366
signature,
367367
});
368368
}
369369
break pos;
370370
}
371371

372-
tracing::trace!(target: "scroll::chain_orchestrator", number = ?(received_chain_headers.last().expect("chain can not be empty").number - 1), "fetching block");
372+
tracing::trace!(target: "scroll::chain_orchestrator", number = ?(received_chain_headers.front().expect("chain can not be empty").number - 1), "fetching block");
373373
if let Some(header) = network_client
374374
.get_header(BlockHashOrNumber::Hash(
375-
received_chain_headers.last().expect("chain can not be empty").parent_hash,
375+
received_chain_headers.front().expect("chain can not be empty").parent_hash,
376376
))
377377
.await?
378378
.into_data()
379379
{
380-
received_chain_headers.push(header.clone());
380+
received_chain_headers.push_front(header.clone());
381381
} else {
382382
return Err(ChainOrchestratorError::MissingBlockHeader {
383383
hash: received_chain_headers
384-
.last()
384+
.front()
385385
.expect("chain can not be empty")
386386
.parent_hash,
387387
});
388388
}
389389
};
390390

391-
// Reverse the new chain headers to have them in the correct order.
392-
received_chain_headers.reverse();
393-
394391
// Fetch the blocks associated with the new chain headers.
395392
let new_blocks = if received_chain_headers.len() == 1 {
396393
vec![received_block]
397394
} else {
398-
fetch_blocks_from_network(received_chain_headers.clone(), network_client.clone()).await
395+
fetch_blocks_from_network(received_chain_headers.clone().into(), network_client.clone())
396+
.await
399397
};
400398

401399
// If we are not in optimistic mode, we validate the L1 messages in the new blocks.

0 commit comments

Comments
 (0)