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

feat(hardfork): Specify hardfork environment #51

Merged
merged 8 commits into from
Aug 2, 2024
Merged
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
4 changes: 2 additions & 2 deletions Cargo.lock

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

77 changes: 55 additions & 22 deletions bin/opt8n/src/opt8n.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! opt8n binary logic

use alloy_eips::eip2718::Encodable2718;
use alloy_eips::BlockId;
use alloy_rpc_types::{
anvil::Forking,
Expand All @@ -11,6 +12,7 @@ use anvil_core::eth::transaction::PendingTransaction;
use cast::traces::{GethTraceBuilder, TracingInspectorConfig};
use forge_script::ScriptArgs;
use std::{
error::Error,
fs::{self, File},
path::PathBuf,
};
Expand All @@ -21,8 +23,8 @@ use futures::StreamExt;
use op_test_vectors::execution::{ExecutionFixture, ExecutionReceipt, ExecutionResult};
use revm::{
db::{AlloyDB, CacheDB},
primitives::{BlobExcessGasAndPrice, BlockEnv, U256},
DatabaseCommit, EvmBuilder,
primitives::{BlobExcessGasAndPrice, BlockEnv, CfgEnv, Env, SpecId, U256},
Database, DatabaseCommit, DatabaseRef, Evm, EvmBuilder,
};
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncBufReadExt, BufReader};
Expand Down Expand Up @@ -213,30 +215,24 @@ impl Opt8n {
.ok_or_else(|| eyre!("Failed to create AlloyDB"))?,
);

let block_env = BlockEnv {
number: U256::from(block.header.number),
coinbase: block.header.beneficiary,
timestamp: U256::from(block.header.timestamp),
difficulty: block.header.difficulty,
gas_limit: U256::from(block.header.gas_limit),
prevrandao: Some(block.header.mix_hash),
basefee: U256::from(block.header.base_fee_per_gas.unwrap_or_default()),
blob_excess_gas_and_price: block
.header
.excess_blob_gas
.map(|excess_gas| BlobExcessGasAndPrice::new(excess_gas as u64)),
};

let mut evm = EvmBuilder::default()
.with_db(Box::new(revm_db))
.with_block_env(block_env)
.build();
let mut evm = evm(
block,
self.eth_api.chain_id(),
CacheDB::new(revm_db),
SpecId::from(self.node_config.hardfork.unwrap_or_default()),
);

evm.context.evm.env.cfg.chain_id = self.eth_api.chain_id();
for tx in block.transactions.iter() {
let pending = PendingTransaction::new(tx.clone().into())?;
evm.context.evm.env.tx = pending.to_revm_tx_env();
let mut buff = Vec::<u8>::with_capacity(pending.transaction.encode_2718_len());
pending.transaction.encode_2718(&mut buff);

let mut tx_env = pending.to_revm_tx_env();
tx_env.optimism.enveloped_tx = Some(buff.into());
evm.context.evm.env.tx = tx_env;

let result = evm.transact()?;

let db = &mut evm.context.evm.db;
let pre_state_frame = GethTraceBuilder::new(vec![], TracingInspectorConfig::default())
.geth_prestate_traces(
Expand Down Expand Up @@ -310,6 +306,43 @@ impl Opt8n {
}
}

/// Creates a new EVM instance from a given block, chain, database, and spec id.
pub fn evm<'a, DB>(block: &Block, chain_id: u64, db: DB, spec_id: SpecId) -> Evm<'a, (), Box<DB>>
where
DB: Database + DatabaseRef + 'a,
<DB as Database>::Error: Error,
{
let block_env = BlockEnv {
number: U256::from(block.header.number),
coinbase: block.header.beneficiary,
timestamp: U256::from(block.header.timestamp),
difficulty: block.header.difficulty,
gas_limit: U256::from(block.header.gas_limit),
prevrandao: Some(block.header.mix_hash),
basefee: U256::from(block.header.base_fee_per_gas.unwrap_or_default()),
blob_excess_gas_and_price: block
.header
.excess_blob_gas
.map(|excess_gas| BlobExcessGasAndPrice::new(excess_gas as u64)),
};

let mut cfg = CfgEnv::default();
cfg.chain_id = chain_id;
let env = Env {
block: block_env,
cfg,
..Default::default()
};

let mut evm = EvmBuilder::default()
.with_db(Box::new(db))
.with_env(Box::new(env))
.optimism()
.build();
evm.modify_spec_id(spec_id);
evm
}

#[derive(Parser, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[clap(rename_all = "snake_case", infer_subcommands = true, multicall = true)]
pub enum ReplCommand {
Expand Down