Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Sep 25, 2024
1 parent 55409a0 commit a4bbcfe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
17 changes: 12 additions & 5 deletions crates/exex/exex/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,16 +610,21 @@ impl ExExManager {

/// Canonicalizes the WAL by inserting inverts of notifications that have non-canonical blocks.
///
/// 1. Iterates over all notifications in the WAL in reverse order.
/// 2. Takes notifications until we find a notification with only canonical blocks. This
/// 1. Iterate over all notifications in the WAL in reverse order.
/// 2. Remove dangling inverted notifications from the previous run of this function.
/// 3. Take notifications until we find a notification with only canonical blocks. This
/// notification is not included in the result.
/// 3. Inverts the notifications using [`ExExNotification::into_inverted`].
/// 4. Commits the inverted notifications using [`Wal::commit`].
/// 4. Invert the notifications using [`ExExNotification::into_inverted`].
/// 5. Commit the inverted notifications using [`Wal::commit`].
///
/// Effectivel, it reverts the WAL to the state of the passed provider.
/// Effectivel, it reverts the WAL to the state of the passed provider, ensuring that the
/// WAL height is <= provider height.
pub fn canonicalize_wal<P: HeaderProvider>(&mut self, provider: P) -> eyre::Result<()> {
let mut reverse_entries = self.wal.entries()?.rev().peekable();

// In case the canonicalization was interrupted before, we need to remove all the dangling
// inverted notifications. For that, we collect all file IDs that have entries with a target
// of `NotificationCommitTarget::Canonicalize`.
let mut file_ids_to_remove = vec![];
while let Some(entry) = reverse_entries.next_if(|entry| {
entry.as_ref().map_or(true, |(_, entry)| entry.target.is_canonicalize())
Expand All @@ -628,6 +633,8 @@ impl ExExManager {
file_ids_to_remove.push(file_id);
}

// After that, we can collect all the notifications that have non-canonical blocks, and
// invert them.
let inverted_notifications = reverse_entries
.map(|entry| entry.map(|(_, entry)| entry.notification))
// Collect notifications until we find a notification with only canonical blocks
Expand Down
2 changes: 1 addition & 1 deletion crates/exex/exex/src/wal/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use reth_primitives::BlockNumHash;
/// This cache is needed to avoid walking the WAL directory every time we want to find a
/// notification corresponding to a block.
#[derive(Debug)]
pub struct BlockCache(BTreeMap<u64, VecDeque<CachedBlock>>);
pub(super) struct BlockCache(BTreeMap<u64, VecDeque<CachedBlock>>);

impl BlockCache {
/// Creates a new instance of [`BlockCache`].
Expand Down
20 changes: 12 additions & 8 deletions crates/exex/exex/src/wal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#![allow(dead_code)]

mod cache;
pub(crate) use cache::BlockCache;
use cache::BlockCache;

mod entry;
pub(crate) use entry::NotificationCommitTarget;
mod storage;
use entry::WalEntry;
pub(crate) use storage::Storage;

mod storage;
use storage::Storage;

use std::path::Path;

use reth_exex_types::ExExNotification;
use reth_primitives::BlockNumHash;
use reth_tracing::tracing::{debug, instrument};

/// A double-ended iterator over file IDs and entries in the WAL.
type EntriesIterator<'a> = Box<dyn DoubleEndedIterator<Item = eyre::Result<(u64, WalEntry)>> + 'a>;

/// WAL is a write-ahead log (WAL) that stores the notifications sent to ExExes.
///
/// WAL is backed by a directory of binary files represented by [`Storage`] and a block cache
Expand Down Expand Up @@ -69,6 +74,7 @@ impl Wal {
Ok(())
}

/// Removes the notification with the given file ID from the WAL.
#[instrument(target = "exex::wal", skip(self))]
pub fn remove(&mut self, file_id: u64) -> eyre::Result<()> {
self.storage.remove_entry(file_id)?;
Expand Down Expand Up @@ -171,9 +177,7 @@ impl Wal {
}

/// Returns an iterator over all file IDs and entries in the WAL.
pub(crate) fn entries(
&self,
) -> eyre::Result<Box<dyn DoubleEndedIterator<Item = eyre::Result<(u64, WalEntry)>> + '_>> {
pub(crate) fn entries(&self) -> eyre::Result<EntriesIterator<'_>> {
let Some(range) = self.storage.files_range()? else {
return Ok(Box::new(std::iter::empty()))
};
Expand Down Expand Up @@ -393,8 +397,8 @@ mod tests {
assert_eq!(
read_notifications(&wal)?,
vec![
committed_notification_1.clone(),
reverted_notification.clone(),
committed_notification_1,
reverted_notification,
committed_notification_2.clone(),
reorged_notification.clone()
]
Expand Down
2 changes: 1 addition & 1 deletion crates/exex/exex/src/wal/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::entry::WalEntry;
/// Each notification is represented by a single file that contains a MessagePack-encoded
/// notification.
#[derive(Debug)]
pub struct Storage {
pub(super) struct Storage {
/// The path to the WAL file.
path: PathBuf,
}
Expand Down

0 comments on commit a4bbcfe

Please sign in to comment.