Skip to content

Commit 4a3a5ae

Browse files
authored
refactor(l2): apply fcu only on the last block of the batch for the block fetcher (#3782)
**Motivation** With the actual implementation of the block fetcher, we apply a fork choice update for every block. This is not the optimal way since we can apply only on the last block. **Description** - Move the `apply_fork_choice` call after the loop and only call it with the last block - Add new type of error `EmptyBatchError`
1 parent c36b343 commit 4a3a5ae

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

crates/l2/based/block_fetcher.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub enum BlockFetcherError {
6464
// See https://github.com/lambdaclass/ethrex/issues/3376
6565
#[error("Spawned GenServer Error")]
6666
GenServerError(GenServerError),
67+
#[error("Tried to store an empty batch")]
68+
EmptyBatchError,
6769
}
6870

6971
#[derive(Clone)]
@@ -409,13 +411,22 @@ async fn store_batch(
409411

410412
let block_hash = block.hash();
411413

412-
apply_fork_choice(&state.store, block_hash, block_hash, block_hash).await?;
413-
414414
info!(
415415
"Added fetched block {} with hash {block_hash:#x}",
416416
block.header.number,
417417
);
418418
}
419+
let latest_hash_on_batch = batch
420+
.last()
421+
.ok_or(BlockFetcherError::EmptyBatchError)?
422+
.hash();
423+
apply_fork_choice(
424+
&state.store,
425+
latest_hash_on_batch,
426+
latest_hash_on_batch,
427+
latest_hash_on_batch,
428+
)
429+
.await?;
419430

420431
Ok(())
421432
}

0 commit comments

Comments
 (0)