diff --git a/Cargo.toml b/Cargo.toml index e7683e61..19456ba8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,6 +85,7 @@ jsonrpsee-core = "0.24" jsonrpsee-types = "0.24" # misc +async-trait = "0.1.83" cfg-if = "1" spin = { version = "0.9.8", features = ["mutex"] } derive_more = { version = "1.0", default-features = false } diff --git a/crates/protocol/Cargo.toml b/crates/protocol/Cargo.toml index 59d66138..1538e192 100644 --- a/crates/protocol/Cargo.toml +++ b/crates/protocol/Cargo.toml @@ -27,6 +27,7 @@ alloy-consensus.workspace = true # Misc derive_more.workspace = true +async-trait.workspace = true # `arbitrary` feature arbitrary = { workspace = true, features = ["derive"], optional = true } diff --git a/crates/protocol/src/batch/mod.rs b/crates/protocol/src/batch/mod.rs index d29ab918..3c35a48b 100644 --- a/crates/protocol/src/batch/mod.rs +++ b/crates/protocol/src/batch/mod.rs @@ -8,3 +8,6 @@ pub use validity::BatchValidity; mod single; pub use single::SingleBatch; + +mod traits; +pub use traits::BatchValidationProvider; diff --git a/crates/protocol/src/batch/traits.rs b/crates/protocol/src/batch/traits.rs new file mode 100644 index 00000000..6ff03db1 --- /dev/null +++ b/crates/protocol/src/batch/traits.rs @@ -0,0 +1,25 @@ +//! Traits for working with protocol types. + +use alloc::{boxed::Box, string::ToString}; +use async_trait::async_trait; +use core::fmt::Display; +use op_alloy_consensus::OpBlock; + +use crate::L2BlockInfo; + +/// Describes the functionality of a data source that fetches safe blocks. +#[async_trait] +pub trait BatchValidationProvider { + /// The error type for the [BatchValidationProvider]. + type Error: Display + ToString; + + /// Returns the [L2BlockInfo] given a block number. + /// + /// Errors if the block does not exist. + async fn l2_block_info_by_number(&mut self, number: u64) -> Result; + + /// Returns the [OpBlock] for a given number. + /// + /// Errors if no block is available for the given block number. + async fn block_by_number(&mut self, number: u64) -> Result; +} diff --git a/crates/protocol/src/lib.rs b/crates/protocol/src/lib.rs index 102af03d..d1d719e8 100644 --- a/crates/protocol/src/lib.rs +++ b/crates/protocol/src/lib.rs @@ -10,7 +10,10 @@ extern crate alloc; mod batch; -pub use batch::{BatchType, BatchValidity, SingleBatch, SINGLE_BATCH_TYPE, SPAN_BATCH_TYPE}; +pub use batch::{ + BatchType, BatchValidationProvider, BatchValidity, SingleBatch, SINGLE_BATCH_TYPE, + SPAN_BATCH_TYPE, +}; mod block; pub use block::{BlockInfo, FromBlockError, L2BlockInfo};