Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Simulation Engine #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "trevm-it"
name = "pawn"
version = "0.1.0"
edition = "2021"

Expand All @@ -18,4 +18,6 @@ alloy-contract = { version = "=0.5.4", features = ["pubsub"] }
tokio = { version = "1.41.0", features = ["full", "sync"] }
eyre = "0.6.12"
alloy-rlp = "0.3.9"
rayon = "1.10.0"
dashmap = "6.1.0"

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
experimental repo for sample code. Expect no order, rhyme, or reason here
154 changes: 151 additions & 3 deletions src/db_connect.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,157 @@
use trevm::revm::{Database, DatabaseCommit};
use std::marker::PhantomData;
use trevm::{
revm::{
primitives::{EVMError, ResultAndState},
Database, DatabaseCommit, EvmBuilder,
},
EvmErrored, EvmNeedsBlock, EvmNeedsCfg, EvmNeedsTx, EvmReady, EvmTransacted, TrevmBuilder,
};

pub struct EvmParts<Ext, Db> {
db: Db,
ext: PhantomData<fn() -> Ext>,
}

impl<Ext, Db> EvmParts<Ext, Db>
where
Ext: Default + Send + Sync + 'static,
Db: DbConnect,
{
pub fn new(db: Db) -> Self {
Self {
db,
ext: PhantomData,
}
}
}

impl<Ext, Db> DbConnect for EvmParts<Ext, Db>
where
Ext: Default + Send + Sync + 'static,
Db: DbConnect,
{
type Database = Db::Database;
type Error = Db::Error;

fn connect(&self) -> Result<Self::Database, Self::Error> {
self.db.connect()
}
}

impl<Ext, Db> EvmFactory for EvmParts<Ext, Db>
where
Ext: Default + Send + Sync + 'static,
Db: DbConnect,
{
type Ext = Ext;

fn create<'a>(&self) -> Result<EvmNeedsCfg<'a, Self::Ext, Self::Database>, Self::Error> {
let db = self.db.connect()?;
let evm = EvmBuilder::default()
.with_external_context(Ext::default())
.with_db(db)
.build_trevm();
Ok(evm)
}
}

/// Trait for types that can be used to connect to a database.
pub trait DbConnect: Send + Sync + 'static {
pub trait DbConnect: Sync + 'static {
/// The database type returned when connecting.
type Database: Database + DatabaseCommit;

/// The error type returned when connecting to the database.
type Error: core::error::Error;

fn connect(&self) -> impl std::future::Future<Output = Result<Self::Database, Self::Error>>;
/// Connect to the database.
fn connect(&self) -> Result<Self::Database, Self::Error>;
}

/// Trait for types that can create EVM instances.
pub trait EvmFactory: DbConnect {
type Ext: Sync + 'static;

/// Create a new EVM instance with the given database connection and extension
fn create<'a>(&self) -> Result<EvmNeedsCfg<'a, Self::Ext, Self::Database>, Self::Error>;

fn create_with_cfg<'a, Cfg>(
&'a self,
cfg: &Cfg,
) -> Result<EvmNeedsBlock<'a, Self::Ext, Self::Database>, Self::Error>
where
Cfg: trevm::Cfg,
{
self.create().map(|evm| evm.fill_cfg(cfg))
}

fn create_with_block<'a, Cfg, Blk>(
&'a self,
cfg: &Cfg,
block: &Blk,
) -> Result<EvmNeedsTx<'a, Self::Ext, Self::Database>, Self::Error>
where
Cfg: trevm::Cfg,
Blk: trevm::Block,
{
self.create_with_cfg(cfg).map(|evm| evm.fill_block(block))
}

fn create_with_tx<'a, Cfg, Blk, Tx>(
&'a self,
cfg: &Cfg,
block: &Blk,
tx: &Tx,
) -> Result<EvmReady<'a, Self::Ext, Self::Database>, Self::Error>
where
Cfg: trevm::Cfg,
Blk: trevm::Block,
Tx: trevm::Tx,
{
self.create_with_block(cfg, block)
.map(|evm| evm.fill_tx(tx))
}

fn transact<'a, Cfg, Blk, Tx>(
&'a self,
cfg: &Cfg,
block: &Blk,
tx: &Tx,
) -> Result<
Result<
EvmTransacted<'a, Self::Ext, Self::Database>,
EvmErrored<'a, Self::Ext, Self::Database>,
>,
Self::Error,
>
where
Cfg: trevm::Cfg,
Blk: trevm::Block,
Tx: trevm::Tx,
{
let evm = self.create_with_tx(cfg, block, tx)?;
Ok(evm.run())
}

/// High level function to run the EVM with the given configuration, block,
/// and transaction.
fn run<Cfg, Blk, Tx>(
&self,
cfg: &Cfg,
block: &Blk,
tx: &Tx,
) -> Result<ResultAndState, EVMError<<Self::Database as Database>::Error>>
where
Cfg: trevm::Cfg,
Blk: trevm::Block,
Tx: trevm::Tx,
{
let trevm = self
.transact(cfg, block, tx)
.map_err(|e| EVMError::Custom(format!("{e}")))?;

match trevm {
Ok(t) => Ok(t.into_result_and_state()),
Err(t) => Err(t.into_error()),
}
}
}
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
mod db_connect;
pub use db_connect::DbConnect;
#![allow(async_fn_in_trait)]

mod pawn;
pub use pawn::{Pawn, PawnHandle};
mod db_connect;
pub use db_connect::{DbConnect, EvmFactory, EvmParts};

mod extractor;
pub use extractor::BlockExtractor;

mod example;

mod pool;
pub use pool::{Best, EvmPool};

mod shared;
pub use shared::{Child, ConcurrentCache, Root};
82 changes: 0 additions & 82 deletions src/pawn.rs

This file was deleted.

Loading