Skip to content

Commit

Permalink
Merge pull request #36 from 0xOsiris/osi/script
Browse files Browse the repository at this point in the history
wip: forge scripts & silence anvil logs
  • Loading branch information
0xKitsune authored Jul 25, 2024
2 parents 16f78a5 + 80846c2 commit 6ba4301
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ alloy = { version = "0.1.4", features = [
"rpc-types-trace",
"providers"
] }
foundry-common = { git = "https://github.com/foundry-rs/foundry", default-features = true, rev = "7c4482fc9541f11b57575e2d8bf7bd190b61bda6" }
anvil = { git = "https://github.com/foundry-rs/foundry", default-features = true, rev = "7c4482fc9541f11b57575e2d8bf7bd190b61bda6" }
anvil-core = { git = "https://github.com/foundry-rs/foundry", default-features = true, rev = "7c4482fc9541f11b57575e2d8bf7bd190b61bda6" }
cast = { git = "https://github.com/foundry-rs/foundry", rev = "7c4482fc9541f11b57575e2d8bf7bd190b61bda6" }
Expand Down
1 change: 1 addition & 0 deletions crates/opt8n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ shellwords.workspace = true
tokio.workspace = true
futures.workspace = true
color-eyre.workspace = true
foundry-common.workspace = true
anvil.workspace = true
anvil-core.workspace = true
cast.workspace = true
Expand Down
20 changes: 8 additions & 12 deletions crates/opt8n/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,20 @@ async fn main() -> eyre::Result<()> {
});

let node_config = args.node_args.into_node_config();
let mut opt8n = Opt8n::new(Some(node_config), forking, args.output, args.genesis).await;
let mut opt8n = Opt8n::new(Some(node_config), forking, args.output, args.genesis).await?;

match args.command {
Commands::Repl {} => {
opt8n.repl().await?;
}
Commands::Script {
script_args: _script_args,
} => {
// TODO: Run foundry script, pass the opt8n anvil instance endpoint to the script
Commands::Script { mut script_args } => {
foundry_common::shell::set_shell(foundry_common::shell::Shell::from_args(
script_args.opts.silent,
script_args.json,
))?;

// foundry_common::shell::set_shell(foundry_common::shell::Shell::from_args(
// cmd.opts.silent,
// cmd.json,
// ))?;
// utils::block_on(cmd.run_script())

opt8n.mine_block().await;
script_args.evm_opts.fork_url = Some(opt8n.node_handle.http_endpoint());
opt8n.run_script(script_args).await?;
}
}

Expand Down
40 changes: 32 additions & 8 deletions crates/opt8n/src/opt8n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ use anvil::{cmd::NodeArgs, eth::EthApi, NodeConfig, NodeHandle};
use anvil_core::eth::block::Block;
use anvil_core::eth::transaction::PendingTransaction;
use cast::traces::{GethTraceBuilder, TracingInspectorConfig};
use forge_script::ScriptArgs;
use std::{
fs::{self, File},
future::IntoFuture,
path::PathBuf,
};

use clap::{CommandFactory, FromArgMatches, Parser};
use color_eyre::eyre::Result;
use futures::StreamExt;
use color_eyre::eyre::{Error, Result};
use futures::{join, StreamExt, TryFutureExt};
use op_test_vectors::execution::{ExecutionFixture, ExecutionReceipt, ExecutionResult};
use revm::{
db::{AlloyDB, CacheDB},
Expand All @@ -43,7 +45,7 @@ impl Opt8n {
fork: Option<Forking>,
output_file: PathBuf,
genesis: Option<PathBuf>,
) -> Self {
) -> Result<Self> {
let genesis = genesis.as_ref().map(|path| {
serde_json::from_reader(File::open(path).expect("TODO: handle error Invalid path"))
.expect("TODO: handle error Invalid genesis")
Expand All @@ -56,15 +58,15 @@ impl Opt8n {
.with_genesis(genesis);

let (eth_api, node_handle) = anvil::spawn(node_config.clone()).await;

Self {
eth_api.anvil_set_logging(false).await?;
Ok(Self {
eth_api,
node_handle,
execution_fixture: ExecutionFixture::default(),
fork,
node_config,
output_file,
}
})
}

/// Listens for commands, and new blocks from the block stream.
Expand Down Expand Up @@ -97,6 +99,30 @@ impl Opt8n {
Ok(())
}

/// Run a Forge script with the given arguments, and generate an execution fixture
/// from the broadcasted transactions.
pub async fn run_script(&mut self, script_args: Box<ScriptArgs>) -> Result<()> {
let mut new_block = self.eth_api.backend.new_block_notifications();
self.eth_api.anvil_set_interval_mining(12)?;

let f = async {
let new_block = new_block.next();
join!(
new_block.into_future(),
script_args.run_script().map_err(Error::from)
)
};

if let (Some(new_block), _) = f.await {
tracing::info!("New block: {:?}", new_block);
if let Some(block) = self.eth_api.backend.get_block_by_hash(new_block.hash) {
self.generate_execution_fixture(block).await?;
}
}

Ok(())
}

async fn receive_command(&self) -> Result<ReplCommand> {
let line = BufReader::new(tokio::io::stdin())
.lines()
Expand Down Expand Up @@ -169,8 +195,6 @@ impl Opt8n {
)?;
db.commit(result.state);

println!("Pre state: {:?}", pre_state_frame);

if let PreStateFrame::Diff(diff) = pre_state_frame {
diff.pre.into_iter().for_each(|(account, state)| {
self.execution_fixture.alloc.entry(account).or_insert(state);
Expand Down

0 comments on commit 6ba4301

Please sign in to comment.