-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate builder transaction against sequencer
- Loading branch information
Showing
8 changed files
with
385 additions
and
66 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use alloy_primitives::Log; | ||
use alloy_sol_types::SolEvent; | ||
use async_trait::async_trait; | ||
use core::time::Duration; | ||
use kona_interop::{ | ||
ExecutingMessage, SafetyLevel, Supervisor, SupervisorClient, SupervisorError, | ||
CROSS_L2_INBOX_ADDRESS, | ||
}; | ||
use tokio::time::error::Elapsed; | ||
|
||
pub struct SupervisorValidator; | ||
|
||
impl ExecutingMessageValidator for SupervisorValidator { | ||
const DEFAULT_TIMEOUT: Duration = Duration::from_millis(100); | ||
} | ||
|
||
/// Failures occurring during validation of [`ExecutingMessage`]s. | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum ExecutingMessageValidatorError { | ||
/// Failure from the [`SupervisorApiClient`] when validating messages. | ||
#[error("Supervisor determined messages are invalid: {0}")] | ||
SupervisorRpcError(#[from] SupervisorError), | ||
|
||
/// Message validation against the Supervisor took longer than allowed. | ||
#[error("Message validation timed out: {0}")] | ||
ValidationTimeout(#[from] Elapsed), | ||
} | ||
|
||
/// Interacts with a Supervisor to validate [`ExecutingMessage`]s. | ||
#[async_trait] | ||
pub trait ExecutingMessageValidator { | ||
/// Default duration that message validation is not allowed to exceed. | ||
const DEFAULT_TIMEOUT: Duration; | ||
|
||
/// Extracts [`ExecutingMessage`]s from the [`Log`] if there are any. | ||
fn parse_messages(logs: &[Log]) -> impl Iterator<Item = Option<ExecutingMessage>> { | ||
logs.iter().map(|log| { | ||
(log.address == CROSS_L2_INBOX_ADDRESS && log.topics().len() == 2) | ||
.then(|| ExecutingMessage::decode_log_data(&log.data, true).ok()) | ||
.flatten() | ||
}) | ||
} | ||
|
||
/// Validates a list of [`ExecutingMessage`]s against a Supervisor. | ||
async fn validate_messages( | ||
supervisor: &SupervisorClient, | ||
messages: &[ExecutingMessage], | ||
safety: SafetyLevel, | ||
timeout: Option<Duration>, | ||
) -> Result<(), ExecutingMessageValidatorError> { | ||
// Set timeout duration based on input if provided. | ||
let timeout = timeout.map_or(Self::DEFAULT_TIMEOUT, |t| t); | ||
|
||
// Construct the future to validate all messages using supervisor. | ||
let fut = async { | ||
supervisor | ||
.check_messages(messages, safety) | ||
.await | ||
.map_err(ExecutingMessageValidatorError::SupervisorRpcError) | ||
}; | ||
|
||
// Await the validation future with timeout. | ||
tokio::time::timeout(timeout, fut) | ||
.await | ||
.map_err(ExecutingMessageValidatorError::ValidationTimeout)? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters