From 5d159575ceb0b46ca0c4ff6dab79799d3a5a7469 Mon Sep 17 00:00:00 2001 From: Noah Citron Date: Fri, 2 Aug 2024 00:34:03 -0400 Subject: [PATCH] refactor: upgrade revm (#343) --- Cargo.lock | 129 ++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 5 +- execution/src/evm.rs | 72 ++++++++++-------------- 3 files changed, 146 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 224bb775..5fc98b63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,11 +65,26 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "alloy-eips" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32a3e14fa0d152d00bd8daf605eb74ad397efb0f54bd7155585823dddb4401e" +dependencies = [ + "alloy-primitives", + "alloy-rlp", + "alloy-serde", + "c-kzg", + "k256", + "once_cell", + "serde", +] + [[package]] name = "alloy-primitives" -version = "0.4.2" +version = "0.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0628ec0ba5b98b3370bb6be17b12f23bfce8ee4ad83823325a20546d9b03b78" +checksum = "ccb3ead547f4532bc8af961649942f0b9c16ee9226e26caa3f38420651cc0bf4" dependencies = [ "alloy-rlp", "bytes", @@ -78,6 +93,8 @@ dependencies = [ "derive_more", "hex-literal", "itoa", + "k256", + "keccak-asm", "proptest", "rand 0.8.5", "ruint", @@ -107,6 +124,17 @@ dependencies = [ "syn 2.0.72", ] +[[package]] +name = "alloy-serde" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15c5b9057acc02aee1b8aac2b5a0729cb0f73d080082c111313e5d1f92a96630" +dependencies = [ + "alloy-primitives", + "serde", + "serde_json", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -532,6 +560,19 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bls12_381" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7bc6d6292be3a19e6379786dac800f551e5865a5bb51ebbe3064ab80433f403" +dependencies = [ + "ff", + "group", + "pairing", + "rand_core 0.6.4", + "subtle", +] + [[package]] name = "blst" version = "0.3.12" @@ -626,9 +667,9 @@ dependencies = [ [[package]] name = "c-kzg" -version = "0.4.2" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94a4bc5367b6284358d2a6a6a1dc2d92ec4b86034561c3b9d3341909752fd848" +checksum = "cdf100c4cea8f207e883ff91ca886d621d8a166cb04971dfaa9bb8fd99ed95df" dependencies = [ "blst", "cc", @@ -1396,6 +1437,12 @@ dependencies = [ "wio", ] +[[package]] +name = "dyn-clone" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" + [[package]] name = "ecdsa" version = "0.16.9" @@ -1906,6 +1953,7 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" dependencies = [ + "bitvec 1.0.1", "rand_core 0.6.4", "subtle", ] @@ -3117,6 +3165,16 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "keccak-asm" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47a3633291834c4fbebf8673acbc1b04ec9d151418ff9b8e26dcd79129928758" +dependencies = [ + "digest 0.10.7", + "sha3-asm", +] + [[package]] name = "keccak-hasher" version = "0.1.1" @@ -3128,6 +3186,21 @@ dependencies = [ "tiny-keccak 1.5.0", ] +[[package]] +name = "kzg-rs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd9920cd4460ce3cbca19c62f3bb9a9611562478a4dc9d2c556f4a7d049c5b6b" +dependencies = [ + "bls12_381", + "glob", + "hex", + "once_cell", + "serde", + "serde_derive", + "serde_yaml", +] + [[package]] name = "lalrpop" version = "0.20.2" @@ -3548,6 +3621,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "pairing" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fec4625e73cf41ef4bb6846cafa6d44736525f442ba45e407c4a000a13996f" +dependencies = [ + "group", +] + [[package]] name = "parity-scale-codec" version = "1.3.7" @@ -4311,10 +4393,13 @@ dependencies = [ [[package]] name = "revm" -version = "3.5.0" -source = "git+https://github.com/bluealloy/revm?rev=50726f3e1b2f7b4d50f6755ed30a1ae3108583bf#50726f3e1b2f7b4d50f6755ed30a1ae3108583bf" +version = "12.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6cfb48bce8ca2113e157bdbddbd5eeb09daac1c903d79ec17085897c38c7c91" dependencies = [ "auto_impl", + "cfg-if", + "dyn-clone", "revm-interpreter", "revm-precompile", "serde", @@ -4323,8 +4408,9 @@ dependencies = [ [[package]] name = "revm-interpreter" -version = "1.3.0" -source = "git+https://github.com/bluealloy/revm?rev=50726f3e1b2f7b4d50f6755ed30a1ae3108583bf#50726f3e1b2f7b4d50f6755ed30a1ae3108583bf" +version = "8.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6b0daddea06fc6da5346acc39b32a357bbe3579e9e3d94117d9ae125cd596fc" dependencies = [ "revm-primitives", "serde", @@ -4332,11 +4418,13 @@ dependencies = [ [[package]] name = "revm-precompile" -version = "2.2.0" -source = "git+https://github.com/bluealloy/revm?rev=50726f3e1b2f7b4d50f6755ed30a1ae3108583bf#50726f3e1b2f7b4d50f6755ed30a1ae3108583bf" +version = "9.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef55228211251d7b6c7707c3ee13bb70dea4d2fd81ec4034521e4fe31010b2ea" dependencies = [ "aurora-engine-modexp", "c-kzg", + "cfg-if", "k256", "once_cell", "revm-primitives", @@ -4348,18 +4436,22 @@ dependencies = [ [[package]] name = "revm-primitives" -version = "1.3.0" -source = "git+https://github.com/bluealloy/revm?rev=50726f3e1b2f7b4d50f6755ed30a1ae3108583bf#50726f3e1b2f7b4d50f6755ed30a1ae3108583bf" +version = "7.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fc4311037ee093ec50ec734e1424fcb3e12d535c6cef683b75d1c064639630c" dependencies = [ + "alloy-eips", "alloy-primitives", - "alloy-rlp", "auto_impl", "bitflags 2.6.0", "bitvec 1.0.1", "c-kzg", + "cfg-if", + "dyn-clone", "enumn", "hashbrown", "hex", + "kzg-rs", "serde", ] @@ -4745,6 +4837,7 @@ version = "0.28.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d24b59d129cdadea20aea4fb2352fa053712e5d713eee47d700cd4b2bc002f10" dependencies = [ + "rand 0.8.5", "secp256k1-sys", ] @@ -4954,6 +5047,16 @@ dependencies = [ "keccak", ] +[[package]] +name = "sha3-asm" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9b57fd861253bff08bb1919e995f90ba8f4889de2726091c8876f3a4e823b40" +dependencies = [ + "cc", + "cfg-if", +] + [[package]] name = "sharded-slab" version = "0.1.7" diff --git a/Cargo.toml b/Cargo.toml index 1bdf413b..97e8f274 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,10 +31,7 @@ milagro_bls = { package = "snowbridge-milagro-bls", git = "https://github.com/Sn # execution ethers-core = { git = "https://github.com/gakonst/ethers-rs", rev = "3bf1a9e0d698e9fdfc91d0353878901af5a5c5ef" } ethers = { git = "https://github.com/gakonst/ethers-rs", rev = "3bf1a9e0d698e9fdfc91d0353878901af5a5c5ef" } -revm = { git = "https://github.com/bluealloy/revm", rev = "50726f3e1b2f7b4d50f6755ed30a1ae3108583bf", default-features = false, features = [ - "std", - "serde", -] } +revm = { version = "12.1.0", default-features = false, features = ["std", "serde"] } triehash-ethereum = { git = "https://github.com/openethereum/parity-ethereum", rev = "55c90d4016505317034e3e98f699af07f5404b63" } # async/futures diff --git a/execution/src/evm.rs b/execution/src/evm.rs index 4ba2b9da..8dc4d142 100644 --- a/execution/src/evm.rs +++ b/execution/src/evm.rs @@ -1,6 +1,5 @@ -use std::{collections::HashMap, str::FromStr, sync::Arc}; +use std::{borrow::BorrowMut, collections::HashMap, str::FromStr, sync::Arc}; -use common::types::BlockTag; use ethers::types::transaction::eip2930::AccessListItem; use eyre::{Report, Result}; use futures::future::join_all; @@ -9,29 +8,29 @@ use revm::{ AccountInfo, Address, Bytecode, Bytes, Env, ExecutionResult, ResultAndState, TransactTo, B256, U256, }, - Database, EVM, + Database, Evm as Revm, }; use tracing::trace; use crate::{ constants::PARALLEL_QUERY_BATCH_SIZE, errors::EvmError, rpc::ExecutionRpc, types::CallOpts, + ExecutionClient, }; - -use super::ExecutionClient; +use common::types::BlockTag; pub struct Evm { - evm: EVM>, + execution: Arc>, chain_id: u64, tag: BlockTag, } impl Evm { pub fn new(execution: Arc>, chain_id: u64, tag: BlockTag) -> Self { - let mut evm: EVM> = EVM::new(); - let db = ProofDB::new(tag, execution); - evm.database(db); - - Evm { evm, chain_id, tag } + Evm { + execution, + chain_id, + tag, + } } pub async fn call(&mut self, opts: &CallOpts) -> Result, EvmError> { @@ -57,26 +56,25 @@ impl Evm { } async fn call_inner(&mut self, opts: &CallOpts) -> Result { - let env = self.get_env(opts, self.tag).await; - _ = self - .evm - .db - .as_mut() - .unwrap() - .state - .prefetch_state(opts) - .await; + let mut db = ProofDB::new(self.tag, self.execution.clone()); + _ = db.state.prefetch_state(opts).await; - let tx_res = loop { - self.evm.env = env.clone(); - let res = self.evm.transact(); - let mut db = self.evm.db.take().unwrap(); + let env = Box::new(self.get_env(opts, self.tag).await); + let evm = Revm::builder().with_db(db).with_env(env).build(); + let mut ctx = evm.into_context_with_handler_cfg(); - if res.is_err() && db.state.needs_update() { + let tx_res = loop { + let db = ctx.context.evm.db.borrow_mut(); + if db.state.needs_update() { db.state.update_state().await.unwrap(); - self.evm = EVM::>::new(); - self.evm.database(db); - } else { + } + + let mut evm = Revm::builder().with_context_with_handler_cfg(ctx).build(); + let res = evm.transact(); + + ctx = evm.into_context_with_handler_cfg(); + + if res.is_ok() { break res; } }; @@ -103,15 +101,7 @@ impl Evm { .map(|gas_price| convert_u256(&gas_price)) .unwrap_or_default(); - let block = self - .evm - .db - .as_ref() - .unwrap() - .execution - .get_block(tag, false) - .await - .unwrap(); + let block = self.execution.get_block(tag, false).await.unwrap(); env.block.number = U256::from(block.number.as_u64()); env.block.coinbase = convert_address(&block.miner); @@ -124,14 +114,13 @@ impl Evm { } struct ProofDB { - execution: Arc>, state: EvmState, } impl ProofDB { pub fn new(tag: BlockTag, execution: Arc>) -> Self { let state = EvmState::new(execution.clone(), tag); - ProofDB { execution, state } + ProofDB { state } } } @@ -346,11 +335,8 @@ impl Database for ProofDB { Ok(Some(self.state.get_basic(address)?)) } - fn block_hash(&mut self, number: U256) -> Result { + fn block_hash(&mut self, number: u64) -> Result { trace!(target: "helios::evm", "fetch block hash for block={:?}", number); - let number = number - .try_into() - .map_err(|_| eyre::eyre!("invalid block number"))?; self.state.get_block_hash(number) }