Skip to content

Commit

Permalink
refactor: generalise optimism implementation using traits
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Jul 22, 2024
1 parent 4856bea commit 93089c3
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 112 deletions.
2 changes: 1 addition & 1 deletion crates/revm/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ mod test {
use super::*;

#[cfg(feature = "optimism")]
type TestChainSpec = crate::optimism::OptimismChainSpec;
type TestChainSpec = crate::optimism::ChainSpec;
#[cfg(not(feature = "optimism"))]
type TestChainSpec = primitives::EthChainSpec;

Expand Down
60 changes: 60 additions & 0 deletions crates/revm/src/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,70 @@ mod l1block;
mod result;
mod spec;

use crate::primitives::{Bytes, TransactionValidation, B256};
pub use handler_register::{
deduct_caller, end, last_frame_return, load_precompiles, optimism_handle_register, output,
reward_beneficiary, validate_env, validate_tx_against_state,
};
pub use l1block::{L1BlockInfo, BASE_FEE_RECIPIENT, L1_BLOCK_CONTRACT, L1_FEE_RECIPIENT};
pub use result::{OptimismHaltReason, OptimismInvalidTransaction};
pub use spec::*;

pub trait OptimismContext {
/// A reference to the cached L1 block info.
fn l1_block_info(&self) -> Option<&L1BlockInfo>;

/// A mutable reference to the cached L1 block info.
fn l1_block_info_mut(&mut self) -> &mut Option<L1BlockInfo>;
}

/// Trait for an Optimism transaction.
pub trait OptimismTransaction {
/// The source hash is used to make sure that deposit transactions do
/// not have identical hashes.
///
/// L1 originated deposit transaction source hashes are computed using
/// the hash of the l1 block hash and the l1 log index.
/// L1 attributes deposit source hashes are computed with the l1 block
/// hash and the sequence number = l2 block number - l2 epoch start
/// block number.
///
/// These two deposit transaction sources specify a domain in the outer
/// hash so there are no collisions.
fn source_hash(&self) -> Option<&B256>;
/// The amount to increase the balance of the `from` account as part of
/// a deposit transaction. This is unconditional and is applied to the
/// `from` account even if the deposit transaction fails since
/// the deposit is pre-paid on L1.
fn mint(&self) -> Option<&u128>;
/// Whether or not the transaction is a system transaction.
fn is_system_transaction(&self) -> Option<bool>;
/// An enveloped EIP-2718 typed transaction. This is used
/// to compute the L1 tx cost using the L1 block info, as
/// opposed to requiring downstream apps to compute the cost
/// externally.
fn enveloped_tx(&self) -> Option<Bytes>;
}

/// Trait for an Optimism chain spec.
pub trait OptimismChainSpec:
crate::ChainSpec<
Context: OptimismContext,
Hardfork = OptimismSpecId,
HaltReason = OptimismHaltReason,
Transaction: OptimismTransaction
+ TransactionValidation<ValidationError = OptimismInvalidTransaction>,
>
{
}

impl<ChainSpecT> OptimismChainSpec for ChainSpecT where
ChainSpecT: crate::ChainSpec<
Context: OptimismContext,
Hardfork = OptimismSpecId,
HaltReason = OptimismHaltReason,
Transaction: OptimismTransaction
+ TransactionValidation<ValidationError = OptimismInvalidTransaction>,
>
{
}
32 changes: 25 additions & 7 deletions crates/revm/src/optimism/env.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::primitives::{
AccessListItem, Address, AuthorizationList, Bytes, Transaction, TransactionValidation, TxEnv,
TxKind, B256, U256,
AccessListItem, Address, AuthorizationList, Bytes, Transaction, TransactionValidation, TxKind,
B256, U256,
};

use super::OptimismInvalidTransaction;
use super::{OptimismInvalidTransaction, OptimismTransaction};

/// The Optimism transaction environment.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OptimismTransaction {
pub struct TxEnv {
#[cfg_attr(feature = "serde", serde(flatten))]
pub base: TxEnv,
pub base: crate::primitives::TxEnv,

/// The source hash is used to make sure that deposit transactions do
/// not have identical hashes.
Expand Down Expand Up @@ -38,7 +38,7 @@ pub struct OptimismTransaction {
pub enveloped_tx: Option<Bytes>,
}

impl Transaction for OptimismTransaction {
impl Transaction for TxEnv {
fn caller(&self) -> &Address {
self.base.caller()
}
Expand Down Expand Up @@ -92,6 +92,24 @@ impl Transaction for OptimismTransaction {
}
}

impl TransactionValidation for OptimismTransaction {
impl OptimismTransaction for TxEnv {
fn source_hash(&self) -> Option<&B256> {
self.source_hash.as_ref()
}

fn mint(&self) -> Option<&u128> {
self.mint.as_ref()
}

fn is_system_transaction(&self) -> Option<bool> {
self.is_system_transaction.clone()
}

fn enveloped_tx(&self) -> Option<Bytes> {
self.enveloped_tx.clone()
}
}

impl TransactionValidation for TxEnv {
type ValidationError = OptimismInvalidTransaction;
}
Loading

0 comments on commit 93089c3

Please sign in to comment.