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

refactor(l1, l2, levm): replace levm feature flag with vm CLI flag #1706

Closed
wants to merge 14 commits into from
3 changes: 3 additions & 0 deletions Cargo.lock

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

13 changes: 3 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,14 @@ install-cli: ## 🛠️ Installs the ethrex-l2 cli
cargo install --path cmd/ethrex_l2/ --force

start-node-with-flamegraph: rm-test-db ## 🚀🔥 Starts an ethrex client used for testing
@if [ -z "$$L" ]; then \
LEVM=""; \
echo "Running the test-node without the LEVM feature"; \
echo "If you want to use levm, run the target with an L at the end: make <target> L=1"; \
else \
LEVM=",levm"; \
echo "Running the test-node with the LEVM feature"; \
fi; \
sudo CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph \
--bin ethrex \
--features "dev$$LEVM" \
--features "dev" \
-- \
--network test_data/genesis-l2.json \
--http.port 1729 \
--datadir test_ethrex
--datadir test_ethrex \
--vm $(EVM) \

load-node: install-cli ## 🚧 Runs a load-test. Run make start-node-with-flamegraph and in a new terminal make load-node
@if [ -z "$$C" ]; then \
Expand Down
1 change: 1 addition & 0 deletions cmd/ef_tests/ethrex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ethrex-blockchain.workspace = true
ethrex-core.workspace = true
ethrex-storage.workspace = true
ethrex-rlp.workspace = true
ethrex-vm.workspace = true
serde.workspace = true
serde_json.workspace = true
bytes.workspace = true
Expand Down
8 changes: 5 additions & 3 deletions cmd/ef_tests/ethrex/test_runner.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{collections::HashMap, path::Path};

use crate::types::{BlockWithRLP, TestUnit};
use ethrex_blockchain::{add_block, fork_choice::apply_fork_choice};
use ethrex_blockchain::BlockChain;
use ethrex_core::types::{
Account as CoreAccount, Block as CoreBlock, BlockHeader as CoreBlockHeader,
};
use ethrex_rlp::decode::RLPDecode;
use ethrex_storage::{EngineType, Store};
use ethrex_vm::EVM;

pub fn run_ef_test(test_key: &str, test: &TestUnit) {
// check that the decoded genesis block header matches the deserialized one
Expand All @@ -16,6 +17,7 @@ pub fn run_ef_test(test_key: &str, test: &TestUnit) {
assert_eq!(decoded_block.header, genesis_block_header);

let store = build_store_for_test(test);
let chain = BlockChain::new(store.clone(), EVM::REVM);

// Check world_state
check_prestate_against_db(test_key, test, &store);
Expand All @@ -33,7 +35,7 @@ pub fn run_ef_test(test_key: &str, test: &TestUnit) {
let hash = block.hash();

// Attempt to add the block as the head of the chain
let chain_result = add_block(block, &store);
let chain_result = chain.add_block(block);
match chain_result {
Err(error) => {
assert!(
Expand All @@ -50,7 +52,7 @@ pub fn run_ef_test(test_key: &str, test: &TestUnit) {
test_key,
block_fixture.expect_exception.clone().unwrap()
);
apply_fork_choice(&store, hash, hash, hash).unwrap();
chain.apply_fork_choice(hash, hash, hash).unwrap();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ef_tests/levm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ version.workspace = true
edition.workspace = true

[dependencies]
ethrex-blockchain = { workspace = true, features = ["levm"] }
ethrex-blockchain.workspace = true
ethrex-core.workspace = true
ethrex-storage.workspace = true
ethrex-rlp.workspace = true
ethrex-vm = { workspace = true, features = ["levm"] }
ethrex-vm.workspace = true
ethrex-levm = { path = "../../../crates/vm/levm" }
serde.workspace = true
serde_json.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion cmd/ef_tests/levm/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ethrex_levm::{
Account, StorageSlot,
};
use ethrex_storage::{error::StoreError, AccountUpdate};
use ethrex_vm::SpecId;
use ethrex_vm::revm::SpecId;
use itertools::Itertools;
use revm::primitives::{EVMError, ExecutionResult as RevmExecutionResult};
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion cmd/ef_tests/levm/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use clap::Parser;
use colored::Colorize;
use ethrex_levm::errors::{TransactionReport, VMError};
use ethrex_vm::SpecId;
use ethrex_vm::revm::SpecId;
use serde::{Deserialize, Serialize};
use spinoff::{spinners::Dots, Color, Spinner};

Expand Down
10 changes: 7 additions & 3 deletions cmd/ef_tests/levm/runner/revm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ use ethrex_levm::{
Account, StorageSlot,
};
use ethrex_storage::{error::StoreError, AccountUpdate};
use ethrex_vm::{db::StoreWrapper, EvmState, RevmAddress, RevmU256};
use ethrex_vm::{
db::StoreWrapper,
revm::{RevmAddress, RevmU256},
EvmState,
};
use revm::{
db::State,
inspectors::TracerEip3155 as RevmTracerEip3155,
Expand Down Expand Up @@ -281,7 +285,7 @@ pub fn ensure_post_state(
block_hash,
levm_execution_report,
);
let revm_account_updates = ethrex_vm::get_state_transitions(revm_state);
let revm_account_updates = ethrex_vm::revm::get_state_transitions(revm_state);
let account_updates_report = compare_levm_revm_account_updates(
test,
&levm_account_updates,
Expand Down Expand Up @@ -443,7 +447,7 @@ pub fn _ensure_post_state_revm(
}
// Execution result was successful and no exception was expected.
None => {
let revm_account_updates = ethrex_vm::get_state_transitions(revm_state);
let revm_account_updates = ethrex_vm::revm::get_state_transitions(revm_state);
let pos_state_root = post_state_root(&revm_account_updates, test);
let expected_post_state_root_hash = test.post.vector_post_value(vector).hash;
if expected_post_state_root_hash != pos_state_root {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ef_tests/levm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ethrex_core::{
types::{Genesis, GenesisAccount, TxKind},
Address, H256, U256,
};
use ethrex_vm::SpecId;
use ethrex_vm::revm::SpecId;
use serde::Deserialize;
use std::collections::HashMap;

Expand Down
4 changes: 2 additions & 2 deletions cmd/ef_tests/levm/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use ethrex_core::{types::Genesis, H256, U256};
use ethrex_storage::{EngineType, Store};
use ethrex_vm::{evm_state, EvmState};
use ethrex_vm::{revm, EvmState};
use spinoff::Spinner;

pub fn load_initial_state(test: &EFTest) -> (EvmState, H256) {
Expand All @@ -14,7 +14,7 @@ pub fn load_initial_state(test: &EFTest) -> (EvmState, H256) {
storage.add_initial_state(genesis.clone()).unwrap();

(
evm_state(
revm::evm_state(
storage.clone(),
genesis.get_block().header.compute_block_hash(),
),
Expand Down
1 change: 0 additions & 1 deletion cmd/ethrex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ metrics = ["ethrex-blockchain/metrics", "ethrex-l2/metrics"]
libmdbx = ["dep:libmdbx", "ethrex-storage/libmdbx"]
redb = ["dep:redb", "ethrex-storage/redb"]
l2 = ["dep:ethrex-l2", "ethrex-vm/l2"]
levm = ["default", "ethrex-vm/levm", "ethrex-blockchain/levm"]
9 changes: 9 additions & 0 deletions cmd/ethrex/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::{Arg, ArgAction, Command};
use ethrex_net::bootnode::BootNode;
use ethrex_vm::EVM;
use tracing::Level;

pub fn cli() -> Command {
Expand Down Expand Up @@ -122,6 +123,14 @@ pub fn cli() -> Command {
.required(false)
.value_name("PROMETHEUS_METRICS_PORT"),
)
.arg(
Arg::new("vm")
.long("vm")
.required(false)
.value_name("VM_BACKEND")
.value_parser(clap::value_parser!(EVM))
.default_value("revm"),
)
.subcommand(
Command::new("removedb").about("Remove the database").arg(
Arg::new("datadir")
Expand Down
40 changes: 24 additions & 16 deletions cmd/ethrex/ethrex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bytes::Bytes;
use directories::ProjectDirs;
use ethrex_blockchain::{add_block, fork_choice::apply_fork_choice};
use ethrex_blockchain::BlockChain;
use ethrex_core::{
types::{Block, Genesis},
H256,
Expand All @@ -13,6 +13,7 @@ use ethrex_net::{
};
use ethrex_rlp::decode::RLPDecode;
use ethrex_storage::{EngineType, Store};
use ethrex_vm::EVM;
use k256::ecdsa::SigningKey;
use local_ip_address::local_ip;
use std::{
Expand Down Expand Up @@ -134,6 +135,8 @@ async fn main() {
.get_one::<String>("datadir")
.map_or(set_datadir(DEFAULT_DATADIR), |datadir| set_datadir(datadir));

let evm = matches.get_one::<EVM>("evm").unwrap_or(&EVM::REVM).clone();

let sync_mode = sync_mode(&matches);

cfg_if::cfg_if! {
Expand All @@ -146,15 +149,18 @@ async fn main() {
}
}

let chain = BlockChain::new(store.clone(), evm);

let genesis = read_genesis_file(&network);
store
chain
.store()
.add_initial_state(genesis.clone())
.expect("Failed to create genesis block");

if let Some(chain_rlp_path) = matches.get_one::<String>("import") {
info!("Importing blocks from chain file: {}", chain_rlp_path);
let blocks = read_chain_file(chain_rlp_path);
import_blocks(&store, &blocks);
import_blocks(&blocks, &chain);
}

if let Some(blocks_path) = matches.get_one::<String>("import_dir") {
Expand All @@ -173,7 +179,7 @@ async fn main() {
blocks.push(read_block_file(s));
}

import_blocks(&store, &blocks);
import_blocks(&blocks, &chain);
}

let jwt_secret = read_jwtsecret_file(authrpc_jwtsecret);
Expand Down Expand Up @@ -210,10 +216,10 @@ async fn main() {
let rpc_api = ethrex_rpc::start_api(
http_socket_addr,
authrpc_socket_addr,
store.clone(),
jwt_secret,
local_p2p_node,
syncer,
chain.clone(),
)
.into_future();

Expand Down Expand Up @@ -259,7 +265,7 @@ async fn main() {
bootnodes,
signer,
peer_table,
store,
chain.store().clone(),
)
.into_future();
tracker.spawn(networking);
Expand Down Expand Up @@ -348,29 +354,31 @@ fn set_datadir(datadir: &str) -> String {
.to_owned()
}

fn import_blocks(store: &Store, blocks: &Vec<Block>) {
fn import_blocks(blocks: &Vec<Block>, chain: &BlockChain) {
let size = blocks.len();
for block in blocks {
let hash = block.hash();
info!(
"Adding block {} with hash {:#x}.",
block.header.number, hash
);
let result = add_block(block, store);
let result = chain.add_block(block);
if let Some(error) = result.err() {
warn!(
"Failed to add block {} with hash {:#x}: {}.",
block.header.number, hash, error
);
}
if store
if chain
.store()
.update_latest_block_number(block.header.number)
.is_err()
{
error!("Fatal: added block {} but could not update the block number -- aborting block import", block.header.number);
break;
};
if store
if chain
.store()
.set_canonical_block(block.header.number, hash)
.is_err()
{
Expand All @@ -383,15 +391,15 @@ fn import_blocks(store: &Store, blocks: &Vec<Block>) {
}
if let Some(last_block) = blocks.last() {
let hash = last_block.hash();
cfg_if::cfg_if! {
if #[cfg(feature = "levm")] {
match chain.evm() {
EVM::LEVM => {
// We are allowing this not to unwrap so that tests can run even if block execution results in the wrong root hash with LEVM.
let _ = apply_fork_choice(store, hash, hash, hash);
let _ = chain.apply_fork_choice(hash, hash, hash);
}
else {
apply_fork_choice(store, hash, hash, hash).unwrap();
EVM::REVM => {
chain.apply_fork_choice(hash, hash, hash).unwrap();
}
}
};
}
info!("Added {} blocks to blockchain", size);
}
2 changes: 2 additions & 0 deletions cmd/ethrex_l2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ ethrex-blockchain.workspace = true
ethrex-prover.workspace = true
ethrex-rlp.workspace = true
ethrex-rpc.workspace = true
ethrex-storage.workspace = true
ethrex-vm.workspace = true

[[bin]]
name = "ethrex_l2"
Expand Down
9 changes: 7 additions & 2 deletions cmd/ethrex_l2/src/commands/prove.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use clap::Args;
use ethrex_blockchain::BlockChain;
use ethrex_l2::utils::{
prover::proving_systems::ProverType,
test_data_io::{generate_program_input, read_chain_file, read_genesis_file},
};
use ethrex_prover_lib::prover::create_prover;
use ethrex_storage::{EngineType, Store};
use ethrex_vm::EVM;

#[derive(Args)]
pub(crate) struct Command {
Expand All @@ -30,8 +33,10 @@ pub(crate) struct Command {
impl Command {
pub fn run(self) -> eyre::Result<()> {
let genesis = read_genesis_file(&self.genesis);
let chain = read_chain_file(&self.chain);
let program_input = generate_program_input(genesis, chain, self.block_number)?;
let blocks = read_chain_file(&self.chain);
let chain = BlockChain::new(Store::new("memory", EngineType::InMemory)?, EVM::REVM);
chain.store().add_initial_state(genesis)?;
let program_input = generate_program_input(chain, blocks, self.block_number)?;

let mut prover = create_prover(ProverType::RISC0);
prover.prove(program_input).expect("proving failed");
Expand Down
5 changes: 3 additions & 2 deletions crates/blockchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ hex = "0.4.3"
path = "./blockchain.rs"

[features]
default = ["c-kzg"]
levm = ["default", "ethrex-vm/levm"]
default = ["c-kzg", "levm"]
libmdbx = ["ethrex-core/libmdbx", "ethrex-storage/default", "ethrex-vm/libmdbx"]
c-kzg = ["ethrex-core/c-kzg"]
# This feature exists only to allow l2/prover compilation.
levm = ["ethrex-vm/levm"]
metrics = ["ethrex-metrics/transactions"]
Loading
Loading