Skip to content

Commit

Permalink
refactor: move Ext to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Aug 1, 2024
1 parent 2840b30 commit f2d340e
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trevm"
version = "0.2.0"
version = "0.3.0"
rust-version = "1.79.0"
edition = "2021"
authors = ["init4"]
Expand Down
7 changes: 3 additions & 4 deletions src/driver/alloy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ impl<Db: Database> From<EVMError<Db::Error>> for AlloyBlockError<Db> {
}
}

impl<'b> BlockDriver<'b, Shanghai<'b>> for alloy_rpc_types_eth::Block<TxEnvelope> {
impl<'b, Ext> BlockDriver<'b, Ext, Shanghai<'b>> for alloy_rpc_types_eth::Block<TxEnvelope> {
type Block = alloy_rpc_types_eth::Header;

// TODO: Implement this
type Error<Db: Database> = AlloyBlockError<Db>;

fn block(&self) -> &Self::Block {
Expand All @@ -77,7 +76,7 @@ impl<'b> BlockDriver<'b, Shanghai<'b>> for alloy_rpc_types_eth::Block<TxEnvelope
self.withdrawals.as_ref().map(|w| Shanghai::new(w.as_slice())).unwrap_or_default()
}

fn run_txns<'a, Ext, Db: Database + DatabaseCommit>(
fn run_txns<'a, Db: Database + DatabaseCommit>(
&self,
mut trevm: EvmNeedsTx<'a, Ext, Db, Shanghai<'b>>,
) -> RunTxResult<'a, 'b, Ext, Db, Shanghai<'b>, Self> {
Expand All @@ -91,7 +90,7 @@ impl<'b> BlockDriver<'b, Shanghai<'b>> for alloy_rpc_types_eth::Block<TxEnvelope
Ok(trevm)
}

fn post_block_checks<Ext, Db: Database + DatabaseCommit>(
fn post_block_checks<Db: Database + DatabaseCommit>(
&self,
_trevm: &crate::EvmBlockComplete<'_, Ext, Db, Shanghai<'b>>,
) -> Result<(), Self::Error<Db>> {
Expand Down
8 changes: 4 additions & 4 deletions src/driver/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub type DriveBlockResult<'a, 'b, Ext, Db, C, T> =
/// Driver for a single trevm block. This trait allows a type to specify the
/// entire lifecycle of a trevm block, from opening the block to driving the
/// trevm to completion.
pub trait BlockDriver<'b, C: BlockContext>
pub trait BlockDriver<'b, Ext, C: BlockContext<Ext>>
where
Self: 'b,
{
Expand All @@ -22,7 +22,7 @@ where
/// An error type for this driver.
type Error<Db: Database>: std::error::Error
+ From<EVMError<Db::Error>>
+ From<<C as BlockContext>::Error<Db>>;
+ From<<C as BlockContext<Ext>>::Error<Db>>;

/// Get a reference to the block filler for this driver.
fn block(&self) -> &Self::Block;
Expand All @@ -31,13 +31,13 @@ where
fn context(&'b self) -> C;

/// Run the transactions for the block.
fn run_txns<'a, Ext, Db: Database + DatabaseCommit>(
fn run_txns<'a, Db: Database + DatabaseCommit>(
&self,
trevm: EvmNeedsTx<'a, Ext, Db, C>,
) -> RunTxResult<'a, 'b, Ext, Db, C, Self>;

/// Run post
fn post_block_checks<Ext, Db: Database + DatabaseCommit>(
fn post_block_checks<Db: Database + DatabaseCommit>(
&self,
trevm: &EvmBlockComplete<'_, Ext, Db, C>,
) -> Result<(), Self::Error<Db>>;
Expand Down
12 changes: 6 additions & 6 deletions src/driver/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ pub type DriveChainResult<'a, 'b, Ext, Db, C, D> =
Result<(Vec<C>, EvmNeedsBlock<'a, Ext, Db>), EvmChainDriverErrored<'a, 'b, Ext, Db, C, D>>;

/// Driver for a chain of blocks.
pub trait ChainDriver<'b, C: BlockContext> {
pub trait ChainDriver<'b, Ext, C: BlockContext<Ext>> {
/// The block driver for this chain.
type BlockDriver: BlockDriver<'b, C>;
type BlockDriver: BlockDriver<'b, Ext, C>;

/// An error type for this driver.
type Error<Db: Database>: std::error::Error
+ From<EVMError<Db::Error>>
+ From<<C as BlockContext>::Error<Db>>
+ From<<Self::BlockDriver as BlockDriver<'b, C>>::Error<Db>>;
+ From<<C as BlockContext<Ext>>::Error<Db>>
+ From<<Self::BlockDriver as BlockDriver<'b, Ext, C>>::Error<Db>>;

/// Get the spec id for a block.
fn spec_id_for(&self, block: &<Self::BlockDriver as BlockDriver<'b, C>>::Block) -> SpecId;
fn spec_id_for(&self, block: &<Self::BlockDriver as BlockDriver<'b, Ext, C>>::Block) -> SpecId;

/// Get the blocks in this chain. The blocks should be in order, and this
/// function MUST NOT return an empty slice.
Expand All @@ -30,7 +30,7 @@ pub trait ChainDriver<'b, C: BlockContext> {
/// or parent-child relationships.
///
/// The `idx` parameter is the index of the block in the chain.
fn check_interblock<Ext, Db: Database + DatabaseCommit>(
fn check_interblock<Db: Database + DatabaseCommit>(
&self,
trevm: &EvmBlockComplete<'_, Ext, Db, C>,
idx: usize,
Expand Down
34 changes: 18 additions & 16 deletions src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit> EvmNeedsBlock<'a, Ext, Db> {
) -> Result<EvmNeedsTx<'a, Ext, Db, C>, EvmErrored<'a, Ext, Db, C>>
where
B: Block,
C: BlockContext,
C: BlockContext<Ext>,
Db: Database + DatabaseCommit,
{
let res = context.open_block(self.inner_mut_unchecked(), filler);
Expand All @@ -774,12 +774,12 @@ impl<'a, Ext, Db: Database + DatabaseCommit> EvmNeedsBlock<'a, Ext, Db> {
/// block.
pub fn drive_block<'b, B, C>(self, driver: &'b B) -> DriveBlockResult<'a, 'b, Ext, Db, C, B>
where
C: BlockContext,
B: BlockDriver<'b, C>,
C: BlockContext<Ext>,
B: BlockDriver<'b, Ext, C>,
{
let trevm = self
.open_block(driver.block(), driver.context())
.map_err(EvmErrored::err_into::<<B as BlockDriver<'b, C>>::Error<Db>>)?;
.map_err(EvmErrored::err_into::<<B as BlockDriver<'b, Ext, C>>::Error<Db>>)?;
let trevm = driver.run_txns(trevm)?;

let trevm = trevm.close_block().map_err(EvmErrored::err_into)?;
Expand All @@ -798,15 +798,15 @@ impl<'a, Ext, Db: Database + DatabaseCommit> EvmNeedsBlock<'a, Ext, Db> {
/// If the driver contains no blocks.
pub fn drive_chain<'b, D, C>(self, driver: &'b mut D) -> DriveChainResult<'a, 'b, Ext, Db, C, D>
where
D: ChainDriver<'b, C>,
C: BlockContext,
D: ChainDriver<'b, Ext, C>,
C: BlockContext<Ext>,
{
let block_count = driver.blocks().len();
let mut contexts = Vec::with_capacity(block_count);

let trevm = self
.drive_block(&driver.blocks()[0])
.map_err(EvmErrored::err_into::<<D as ChainDriver<'b, C>>::Error<Db>>)?;
.map_err(EvmErrored::err_into::<<D as ChainDriver<'b, Ext, C>>::Error<Db>>)?;

if let Err(e) = driver.check_interblock(&trevm, 0) {
return Err(trevm.errored(e));
Expand All @@ -820,7 +820,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit> EvmNeedsBlock<'a, Ext, Db> {
(context, trevm) = {
let trevm = trevm
.drive_block(&driver.blocks()[i])
.map_err(EvmErrored::err_into::<<D as ChainDriver<'b, C>>::Error<Db>>)?;
.map_err(EvmErrored::err_into::<<D as ChainDriver<'b, Ext, C>>::Error<Db>>)?;
if let Err(e) = driver.check_interblock(&trevm, i) {
return Err(trevm.errored(e));
}
Expand Down Expand Up @@ -936,7 +936,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasContext>
error: E,
) -> EvmErrored<'a, Ext, Db, <TrevmState as HasContext>::Context, E>
where
<TrevmState as HasContext>::Context: BlockContext,
<TrevmState as HasContext>::Context: BlockContext<Ext>,
{
EvmErrored {
inner: self.inner,
Expand All @@ -947,7 +947,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasContext>

// --- NEEDS TX

impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext> EvmNeedsTx<'a, Ext, Db, C> {
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext>> EvmNeedsTx<'a, Ext, Db, C> {
/// Close the current block, applying some logic, and returning the EVM
/// ready for the next block.
///
Expand Down Expand Up @@ -1015,7 +1015,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasTx> Trevm<'a, Ext, D

// --- READY

impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext> EvmReady<'a, Ext, Db, C> {
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext>> EvmReady<'a, Ext, Db, C> {
/// Clear the current transaction environment.
pub fn clear_tx(self) -> EvmNeedsTx<'a, Ext, Db, C> {
// NB: we do not clear the tx env here, as we may read it during `BlockContext::post_tx`
Expand Down Expand Up @@ -1047,7 +1047,9 @@ impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext> EvmReady<'a, Ext,

// --- ERRORED

impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext, E> EvmErrored<'a, Ext, Db, C, E> {
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext>, E>
EvmErrored<'a, Ext, Db, C, E>
{
/// Get a reference to the error.
pub const fn error(&self) -> &E {
&self.state.error
Expand Down Expand Up @@ -1095,7 +1097,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext, E> EvmErrored<'a,
}
}

impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext>
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext>>
EvmErrored<'a, Ext, Db, C, EVMError<Db::Error>>
{
/// Check if the error is a transaction error. This is provided as a
Expand Down Expand Up @@ -1126,23 +1128,23 @@ impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext>

// --- TRANSACTED

impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext> AsRef<ResultAndState>
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext>> AsRef<ResultAndState>
for EvmTransacted<'a, Ext, Db, C>
{
fn as_ref(&self) -> &ResultAndState {
&self.state.result
}
}

impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext> AsRef<ExecutionResult>
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext>> AsRef<ExecutionResult>
for EvmTransacted<'a, Ext, Db, C>
{
fn as_ref(&self) -> &ExecutionResult {
&self.state.result.result
}
}

impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext> EvmTransacted<'a, Ext, Db, C> {
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext>> EvmTransacted<'a, Ext, Db, C> {
/// Get a reference to the result.
pub fn result(&self) -> &ExecutionResult {
self.as_ref()
Expand Down
Loading

0 comments on commit f2d340e

Please sign in to comment.