Skip to content

Commit

Permalink
feat: flush oracle cache on reorg #724 (#756)
Browse files Browse the repository at this point in the history
* feat: flush oracle cache on reorg #724

* fix pr review
  • Loading branch information
piotr-roslaniec authored Oct 30, 2024
1 parent 9621797 commit 712fe20
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
17 changes: 17 additions & 0 deletions bin/client/src/caching_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ where
}
}

/// A trait that provides a method to flush a cache.
pub trait FlushableCache {
/// Flushes the cache, removing all entries.
fn flush(&self);
}

impl<OR, HW> FlushableCache for CachingOracle<OR, HW>
where
OR: PreimageOracleClient,
HW: HintWriterClient,
{
/// Flushes the cache, removing all entries.
fn flush(&self) {
self.cache.lock().clear();
}
}

#[async_trait]
impl<OR, HW> PreimageOracleClient for CachingOracle<OR, HW>
where
Expand Down
11 changes: 3 additions & 8 deletions bin/client/src/kona.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,9 @@ fn main() -> Result<()> {
////////////////////////////////////////////////////////////////

// Create a new derivation driver with the given boot information and oracle.
let mut driver = DerivationDriver::new(
boot.as_ref(),
oracle.as_ref(),
beacon,
l1_provider,
l2_provider.clone(),
)
.await?;
let mut driver =
DerivationDriver::new(boot.as_ref(), &oracle, beacon, l1_provider, l2_provider.clone())
.await?;

// Run the derivation pipeline until we are able to produce the output root of the claimed
// L2 block.
Expand Down
18 changes: 14 additions & 4 deletions bin/client/src/l1/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! [OpPayloadAttributes]: op_alloy_rpc_types_engine::OpPayloadAttributes
use super::OracleL1ChainProvider;
use crate::{l2::OracleL2ChainProvider, BootInfo, HintType};
use crate::{l2::OracleL2ChainProvider, BootInfo, FlushableCache, HintType};
use alloc::{sync::Arc, vec::Vec};
use alloy_consensus::{Header, Sealed};
use alloy_primitives::B256;
Expand Down Expand Up @@ -82,11 +82,13 @@ where
l2_safe_head_header: Sealed<Header>,
/// The inner pipeline.
pipeline: OraclePipeline<O, B>,
/// The caching oracle.
caching_oracle: Arc<O>,
}

impl<O, B> DerivationDriver<O, B>
where
O: CommsClient + Send + Sync + Debug,
O: CommsClient + FlushableCache + Send + Sync + Debug,
B: BlobProvider + Send + Sync + Debug + Clone,
{
/// Returns the current L2 safe head [L2BlockInfo].
Expand Down Expand Up @@ -117,7 +119,7 @@ where
/// - A new [DerivationDriver] instance.
pub async fn new(
boot_info: &BootInfo,
caching_oracle: &O,
caching_oracle: &Arc<O>,
blob_provider: B,
mut chain_provider: OracleL1ChainProvider<O>,
mut l2_chain_provider: OracleL2ChainProvider<O>,
Expand Down Expand Up @@ -160,7 +162,12 @@ where
.origin(l1_origin)
.build();

Ok(Self { l2_safe_head, l2_safe_head_header, pipeline })
Ok(Self {
l2_safe_head,
l2_safe_head_header,
pipeline,
caching_oracle: caching_oracle.clone(),
})
}

/// Produces the output root of the next L2 block.
Expand Down Expand Up @@ -281,6 +288,9 @@ where
)
.await?;
} else {
if matches!(e, ResetError::ReorgDetected(_, _)) {
self.caching_oracle.as_ref().flush();
}
// Reset the pipeline to the initial L2 safe head and L1 origin,
// and try again.
self.pipeline
Expand Down
2 changes: 1 addition & 1 deletion bin/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pub mod boot;
pub use boot::BootInfo;

mod caching_oracle;
pub use caching_oracle::CachingOracle;
pub use caching_oracle::{CachingOracle, FlushableCache};

0 comments on commit 712fe20

Please sign in to comment.