Skip to content

Commit

Permalink
feat: integrate custom-evm example
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez committed Mar 5, 2024
1 parent 0917d94 commit 5e07bff
Show file tree
Hide file tree
Showing 9 changed files with 8,064 additions and 56 deletions.
7,963 changes: 7,913 additions & 50 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["bin/alphanet/"]
members = ["bin/alphanet/", "crates/node"]
default-members = ["bin/alphanet/"]
resolver = "2"

Expand Down Expand Up @@ -47,10 +47,14 @@ tracing = "0.1.0"
# tokio
tokio = { version = "1.21", default-features = false }

# reth
reth = { git = "https://github.com/paradigmxyz/reth.git", rev = "96fcdfb", features = ["optimism"] }
reth-node-api = { git = "https://github.com/paradigmxyz/reth.git", rev = "96fcdfb" }

# revm
revm = { version = "6.1.0", features = ["std", "secp256k1"], default-features = false }
revm-primitives = { version = "2.1.0", features = ["std"], default-features = false }
revm-inspectors = { git = "https://github.com/paradigmxyz/evm-inspectors", rev = "75a187b" }
revm-inspectors = { git = "https://github.com/paradigmxyz/evm-inspectors", rev = "7068d39" }

# misc
clap = "4"
6 changes: 3 additions & 3 deletions bin/alphanet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ workspace = true
tracing.workspace = true

[target.'cfg(not(windows))'.dependencies]
tikv-jemallocator = { version = "0.5.0", optional = true }
jemallocator = { version = "0.5", optional = true }

[features]
default = ["jemalloc"]
asm-keccak = []

jemalloc = ["dep:tikv-jemallocator"]
jemalloc-prof = ["jemalloc", "tikv-jemallocator?/profiling"]
jemalloc = ["dep:jemallocator"]
jemalloc-prof = ["jemalloc", "jemallocator?/profiling"]

min-error-logs = ["tracing/release_max_level_error"]
min-warn-logs = ["tracing/release_max_level_warn"]
Expand Down
2 changes: 1 addition & 1 deletion bin/alphanet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// We use jemalloc for performance reasons.
#[cfg(all(feature = "jemalloc", unix))]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

fn main() {
tracing::info!("Hello, world!");
Expand Down
17 changes: 17 additions & 0 deletions crates/node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "node"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true

[dependencies]
reth.workspace = true
reth-node-api.workspace = true

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/node/src/engine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

112 changes: 112 additions & 0 deletions crates/node/src/evm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use reth::{
primitives::{
address,
revm::{config::revm_spec, env::fill_op_tx_env},
revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, Env, PrecompileResult, TxEnv},
Address, Bytes, ChainSpec, Head, Header, Transaction, U256,
},
revm::{
handler::register::EvmHandler,
precompile::{Precompile, PrecompileSpecId, Precompiles},
Database,
},
};
use reth_node_api::ConfigureEvmEnv;
use std::sync::Arc;

/// Custom EVM configuration
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct AlphaNetEvmConfig;

impl AlphaNetEvmConfig {
/// Sets the precompiles to the EVM handler
///
/// This will be invoked when the EVM is created via [ConfigureEvm::evm] or
/// [ConfigureEvm::evm_with_inspector]
///
/// This will use the default mainnet precompiles and add additional precompiles.
pub fn set_precompiles<EXT, DB>(handler: &mut EvmHandler<'_, EXT, DB>)
where
DB: Database,
{
// first we need the evm spec id, which determines the precompiles
let spec_id = handler.cfg.spec_id;

// install the precompiles
handler.pre_execution.load_precompiles = Arc::new(move || {
let mut precompiles = Precompiles::new(PrecompileSpecId::from_spec_id(spec_id)).clone();
precompiles.inner.insert(
address!("0000000000000000000000000000000000000999"),
Precompile::Env(Self::my_precompile),
);
precompiles
});
}

/// A custom precompile that does nothing
fn my_precompile(_data: &Bytes, _gas: u64, _env: &Env) -> PrecompileResult {
Ok((0, Bytes::new()))
}
}

impl ConfigureEvmEnv for AlphaNetEvmConfig {
type TxMeta = Bytes;

fn fill_tx_env<T>(tx_env: &mut TxEnv, transaction: T, sender: Address, meta: Self::TxMeta)
where
T: AsRef<Transaction>,
{
fill_op_tx_env(tx_env, transaction, sender, meta)
}

fn fill_cfg_env(
cfg_env: &mut CfgEnvWithHandlerCfg,
chain_spec: &ChainSpec,
header: &Header,
total_difficulty: U256,
) {
let spec_id = revm_spec(
chain_spec,
Head {
number: header.number,
timestamp: header.timestamp,
difficulty: header.difficulty,
total_difficulty,
hash: Default::default(),
},
);

cfg_env.chain_id = chain_spec.chain().id();
cfg_env.perf_analyse_created_bytecodes = AnalysisKind::Analyse;

cfg_env.handler_cfg.spec_id = spec_id;
cfg_env.handler_cfg.is_optimism = chain_spec.is_optimism();
}
}

#[cfg(test)]
mod tests {
use super::*;
use reth_primitives::revm_primitives::{BlockEnv, CfgEnv};

#[test]
#[ignore]
fn test_fill_cfg_and_block_env() {
let mut cfg_env = CfgEnvWithHandlerCfg::new_with_spec_id(CfgEnv::default(), SpecId::LATEST);
let mut block_env = BlockEnv::default();
let header = Header::default();
let chain_spec = ChainSpec::default();
let total_difficulty = U256::ZERO;

AlphaNetEvmConfig::fill_cfg_and_block_env(
&mut cfg_env,
&mut block_env,
&chain_spec,
&header,
total_difficulty,
);

assert_eq!(cfg_env.chain_id, chain_spec.chain().id());
}
}
10 changes: 10 additions & 0 deletions crates/node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Standalone crate for Reth configuration and builder types.

#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

/// Implementation of the EngineTypes trait.
pub mod engine;
/// Implementation of the ConfigureEvmEnv trait.
pub mod evm;
/// Node types config.
pub mod node;
1 change: 1 addition & 0 deletions crates/node/src/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 5e07bff

Please sign in to comment.