From 8a35dcb2bd308ee872d6aa186c30b8e08e519ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cperekopskiy=E2=80=9D?= Date: Fri, 3 Nov 2023 13:34:10 +0200 Subject: [PATCH] Cache blocks in `fetch_l2_block` --- core/lib/zksync_core/src/sync_layer/client.rs | 17 +++++++++-------- .../zksync_core/src/sync_layer/external_io.rs | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/core/lib/zksync_core/src/sync_layer/client.rs b/core/lib/zksync_core/src/sync_layer/client.rs index 148953572477..5d4f61a4f2a3 100644 --- a/core/lib/zksync_core/src/sync_layer/client.rs +++ b/core/lib/zksync_core/src/sync_layer/client.rs @@ -182,18 +182,19 @@ impl CachingMainNodeClient { /// Cached version of [`HttpClient::sync_l2_block`]. pub async fn fetch_l2_block( - &self, + &mut self, miniblock: MiniblockNumber, ) -> anyhow::Result> { - let block = self.blocks.get(&miniblock).cloned(); FETCHER_METRICS.cache_total[&CachedMethod::SyncL2Block].inc(); - match block { - Some(block) => { - FETCHER_METRICS.cache_hit[&CachedMethod::SyncL2Block].inc(); - Ok(Some(block)) - } - None => self.client.fetch_l2_block(miniblock, true).await, + if let Some(block) = self.blocks.get(&miniblock).cloned() { + FETCHER_METRICS.cache_hit[&CachedMethod::SyncL2Block].inc(); + return Ok(Some(block)); + } + let block = self.client.fetch_l2_block(miniblock, true).await?; + if let Some(block) = block.clone() { + self.blocks.insert(miniblock, block); } + Ok(block) } /// Re-export of [`MainNodeClient::fetch_l2_block_number()`]. Added to not expose the internal client. diff --git a/core/lib/zksync_core/src/sync_layer/external_io.rs b/core/lib/zksync_core/src/sync_layer/external_io.rs index c451587ff025..e7b49fedddcb 100644 --- a/core/lib/zksync_core/src/sync_layer/external_io.rs +++ b/core/lib/zksync_core/src/sync_layer/external_io.rs @@ -543,8 +543,8 @@ impl StateKeeperIO for ExternalIO { self.sync_state .set_local_block(self.current_miniblock_number); - self.current_miniblock_number += 1; tracing::info!("Miniblock {} is sealed", self.current_miniblock_number); + self.current_miniblock_number += 1; } async fn seal_l1_batch(