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] ArbSys precompile support #7754

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl Cheatcode for rollCall {
impl Cheatcode for getBlockNumberCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self {} = self;
Ok(ccx.ecx.env.block.number.abi_encode())
Ok(ccx.db.get_block_number(&ccx.env)?.abi_encode())
}
}

Expand Down
15 changes: 13 additions & 2 deletions crates/cheatcodes/src/evm/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ impl Cheatcode for selectForkCall {
// fork.
ccx.state.corrected_nonce = true;

ccx.ecx.db.select_fork(*forkId, &mut ccx.ecx.env, &mut ccx.ecx.journaled_state)?;
ccx.ecx.db.select_fork(
*forkId,
&mut ccx.ecx.env,
ccx.precompiles,
&mut ccx.ecx.journaled_state,
)?;
Ok(Default::default())
}
}
Expand Down Expand Up @@ -306,7 +311,12 @@ fn create_select_fork<DB: DatabaseExt>(
ccx.state.corrected_nonce = true;

let fork = create_fork_request(ccx, url_or_alias, block)?;
let id = ccx.ecx.db.create_select_fork(fork, &mut ccx.ecx.env, &mut ccx.ecx.journaled_state)?;
let id = ccx.ecx.db.create_select_fork(
fork,
&mut ccx.ecx.env,
ccx.precompiles,
&mut ccx.ecx.journaled_state,
)?;
Ok(id.abi_encode())
}

Expand Down Expand Up @@ -337,6 +347,7 @@ fn create_select_fork_at_transaction<DB: DatabaseExt>(
fork,
&mut ccx.ecx.env,
&mut ccx.ecx.journaled_state,
ccx.precompiles,
*transaction,
)?;
Ok(id.abi_encode())
Expand Down
1 change: 1 addition & 0 deletions crates/evm/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ alloy-provider.workspace = true
alloy-transport.workspace = true
alloy-rpc-types.workspace = true
alloy-sol-types.workspace = true
alloy-chains.workspace = true

revm = { workspace = true, default-features = false, features = [
"std",
Expand Down
73 changes: 73 additions & 0 deletions crates/evm/core/src/arbitrum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use self::ArbSys::ArbSysCalls;
use crate::backend::{DatabaseError, ForkDB};
use alloy_chains::{Chain, NamedChain};
use alloy_primitives::{address, Address, Bytes, U256};
use alloy_sol_types::{sol, SolInterface, SolValue};
use revm::{
primitives::{Env, PrecompileResult},
ContextStatefulPrecompile,
};

pub const ARB_SYS_ADDRESS: Address = address!("0000000000000000000000000000000000000064");

#[derive(Debug, thiserror::Error)]
pub enum ArbitrumError {
#[error("missing L1 block field in L2 block data")]
MissingL1BlockField,

#[error(transparent)]
Database(#[from] DatabaseError),

#[error(transparent)]
Serde(#[from] serde_json::Error),
}

sol! {
interface ArbSys {
function arbBlockNumber() external view returns (uint);
}
}

#[derive(Clone, Debug)]
pub struct ArbSysPrecompile;

impl<DB: revm::Database> ContextStatefulPrecompile<DB> for ArbSysPrecompile {
fn call(
&self,
bytes: &Bytes,
_: u64,
evmctx: &mut revm::InnerEvmContext<DB>,
) -> PrecompileResult {
match ArbSysCalls::abi_decode(bytes.as_ref(), false).map_err(|_| {
revm::precompile::Error::other("failed to decode ArbSys precompile call")
})? {
ArbSysCalls::arbBlockNumber(_) => {
Ok((0, U256::from(evmctx.env.block.number).abi_encode().into()))
}
_ => unimplemented!(),
}
}
}

pub fn get_block_number(active_fork_db: &ForkDB, env: &Env) -> Result<U256, ArbitrumError> {
let block = active_fork_db.db.get_full_block(env.block.number.to::<u64>())?;
let l1_block_number = block
.other
.get_deserialized::<U256>("l1BlockNumber")
.ok_or(ArbitrumError::MissingL1BlockField)??;

Ok(l1_block_number)
}

pub fn is_arbitrum(chain_id: u64) -> bool {
let chain = Chain::from(chain_id);
matches!(
chain.named(),
Some(
NamedChain::Arbitrum |
NamedChain::ArbitrumGoerli |
NamedChain::ArbitrumNova |
NamedChain::ArbitrumTestnet
)
)
}
13 changes: 9 additions & 4 deletions crates/evm/core/src/backend/cow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use revm::{
Account, AccountInfo, Bytecode, Env, EnvWithHandlerCfg, HashMap as Map, ResultAndState,
SpecId,
},
Database, DatabaseCommit, JournaledState,
ContextPrecompiles, Database, DatabaseCommit, JournaledState,
};
use std::{borrow::Cow, collections::BTreeMap};

Expand Down Expand Up @@ -68,7 +68,7 @@ impl<'a> CowBackend<'a> {
// already, we reset the initialized state
self.is_initialized = false;
self.spec_id = env.handler_cfg.spec_id;
let mut evm = crate::utils::new_evm_with_inspector(self, env.clone(), inspector);
let mut evm = crate::utils::new_extended_evm_with_inspector(self, env.clone(), inspector);

let res = evm.transact().wrap_err("backend: failed while inspecting")?;

Expand Down Expand Up @@ -148,13 +148,14 @@ impl<'a> DatabaseExt for CowBackend<'a> {
self.backend.to_mut().create_fork_at_transaction(fork, transaction)
}

fn select_fork(
fn select_fork<DB: DatabaseExt>(
&mut self,
id: LocalForkId,
env: &mut Env,
precompiles: &mut ContextPrecompiles<DB>,
journaled_state: &mut JournaledState,
) -> eyre::Result<()> {
self.backend_mut(env).select_fork(id, env, journaled_state)
self.backend_mut(env).select_fork(id, env, precompiles, journaled_state)
}

fn roll_fork(
Expand Down Expand Up @@ -243,6 +244,10 @@ impl<'a> DatabaseExt for CowBackend<'a> {
fn has_cheatcode_access(&self, account: &Address) -> bool {
self.backend.has_cheatcode_access(account)
}

fn get_block_number(&self, env: &Env) -> Result<U256, DatabaseError> {
self.backend.get_block_number(env)
}
}

impl<'a> DatabaseRef for CowBackend<'a> {
Expand Down
Loading
Loading