Skip to content

Commit

Permalink
feat(en): Cache blocks in fetch_l2_block (#403)
Browse files Browse the repository at this point in the history
# What ❔

- `fetch_l2_block` saves blocks in cache
- fixed log in `ExternalIO:: seal_miniblock`

## Why ❔

Making cache client to behave traditional way, i.e. cache results got
from server. This will also fix invariant that previous block is always
(except for first iteration after start) kept in cache and fix sporadic
panics caused by inconsistent API responses
 
## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
perekopskiy authored Nov 3, 2023
1 parent 3890542 commit b94c845
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions core/lib/zksync_core/src/sync_layer/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<SyncBlock>> {
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.
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/sync_layer/external_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b94c845

Please sign in to comment.