kona_interop/traits.rs
1//! Traits for the `kona-interop` crate.
2
3use crate::InteropValidationError;
4use alloc::{boxed::Box, vec::Vec};
5use alloy_consensus::Header;
6use alloy_primitives::{B256, ChainId};
7use async_trait::async_trait;
8use core::error::Error;
9use kona_protocol::BlockInfo;
10use op_alloy_consensus::OpReceiptEnvelope;
11
12/// Describes the interface of the interop data provider. This provider is multiplexed over several
13/// chains, with each method consuming a chain ID to determine the target chain.
14#[async_trait]
15pub trait InteropProvider {
16 /// The error type for the provider.
17 type Error: Error;
18
19 /// Fetch a [Header] by its number.
20 async fn header_by_number(&self, chain_id: u64, number: u64) -> Result<Header, Self::Error>;
21
22 /// Fetch all receipts for a given block by number.
23 async fn receipts_by_number(
24 &self,
25 chain_id: u64,
26 number: u64,
27 ) -> Result<Vec<OpReceiptEnvelope>, Self::Error>;
28
29 /// Fetch all receipts for a given block by hash.
30 async fn receipts_by_hash(
31 &self,
32 chain_id: u64,
33 block_hash: B256,
34 ) -> Result<Vec<OpReceiptEnvelope>, Self::Error>;
35}
36
37/// Trait for validating interop-related timestamps and blocks.
38pub trait InteropValidator: Send + Sync {
39 /// Validates that the provided timestamps and chain IDs are eligible for interop execution.
40 ///
41 /// # Arguments
42 /// * `initiating_chain_id` - The chain ID where the message was initiated
43 /// * `initiating_timestamp` - The timestamp when the message was initiated
44 /// * `executing_chain_id` - The chain ID where the message is being executed
45 /// * `executing_timestamp` - The timestamp when the message is being executed
46 /// * `timeout` - Optional timeout value to add to the execution deadline
47 ///
48 /// # Returns
49 /// * `Ok(())` if the timestamps are valid for interop execution
50 /// * `Err(InteropValidationError)` if validation fails
51 fn validate_interop_timestamps(
52 &self,
53 initiating_chain_id: ChainId,
54 initiating_timestamp: u64,
55 executing_chain_id: ChainId,
56 executing_timestamp: u64,
57 timeout: Option<u64>,
58 ) -> Result<(), InteropValidationError>;
59
60 /// Returns `true` if the timestamp is strictly after the interop activation block.
61 ///
62 /// This function checks whether the provided timestamp is *after* that activation,
63 /// skipping the activation block itself.
64 ///
65 /// Returns `false` if `interop_time` is not configured.
66 fn is_post_interop(&self, chain_id: ChainId, timestamp: u64) -> bool;
67
68 /// Returns `true` if the block is the interop activation block for the specified chain.
69 ///
70 /// An interop activation block is defined as the block that is right after the
71 /// interop activation time.
72 ///
73 /// Returns `false` if `interop_time` is not configured.
74 fn is_interop_activation_block(&self, chain_id: ChainId, block: BlockInfo) -> bool;
75}