From 2ef1c0c472162c61f449cb46c263538ebe52d36d Mon Sep 17 00:00:00 2001 From: Zhang Zhuo Date: Wed, 30 Aug 2023 17:17:49 +0800 Subject: [PATCH 01/24] fix modexp and pairing gas --- crates/precompile/src/bn128.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/precompile/src/bn128.rs b/crates/precompile/src/bn128.rs index 8d82a01db7..9c3640ae66 100644 --- a/crates/precompile/src/bn128.rs +++ b/crates/precompile/src/bn128.rs @@ -165,7 +165,8 @@ fn run_pair( pair_base_cost: u64, gas_limit: u64, ) -> PrecompileResult { - let gas_used = (input.len() / PAIR_ELEMENT_LEN) as u64 * pair_per_point_cost + pair_base_cost; + let gas_used = + (input.len() / PAIR_ELEMENT_LEN) as u64 * pair_per_point_cost + pair_base_cost; if gas_used > gas_limit { return Err(Error::OutOfGas); } From 098f35e8d0d34b3e91ef295f0806cafa10089be0 Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 20 Mar 2024 15:16:26 +0800 Subject: [PATCH 02/24] use poseidon --- Cargo.lock | 250 ++++++++++++++++-- Cargo.toml | 3 + .../interpreter/src/interpreter/analysis.rs | 15 +- crates/primitives/Cargo.toml | 4 + crates/primitives/src/bytecode.rs | 13 +- crates/primitives/src/env.rs | 4 +- crates/primitives/src/state.rs | 36 ++- crates/primitives/src/utilities.rs | 58 ++++ crates/revm/src/db/in_memory_db.rs | 27 +- crates/revm/src/db/states/bundle_state.rs | 29 +- crates/revm/src/db/states/cache_account.rs | 8 +- crates/revm/src/evm_impl.rs | 4 +- crates/revm/src/journaled_state.rs | 15 +- 13 files changed, 396 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3092a8e401..83cf1e94f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,7 +23,7 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "once_cell", "version_check", ] @@ -229,6 +229,12 @@ dependencies = [ "nodrop", ] +[[package]] +name = "arrayref" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" + [[package]] name = "arrayvec" version = "0.7.4" @@ -294,7 +300,7 @@ checksum = "4319208da049c43661739c5fade2ba182f09d1dc2299b32298d3a31692b17e12" dependencies = [ "addr2line", "cc", - "cfg-if", + "cfg-if 1.0.0", "libc", "miniz_oxide", "object", @@ -392,6 +398,27 @@ dependencies = [ "wyz", ] +[[package]] +name = "blake2b_simd" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23285ad32269793932e830392f2fe2f83e26488fd3ec778883a93c8323735780" +dependencies = [ + "arrayref", + "arrayvec", + "constant_time_eq", +] + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "block-padding", + "generic-array", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -401,6 +428,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-padding" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" + [[package]] name = "blst" version = "0.3.11" @@ -479,6 +512,12 @@ dependencies = [ "nom", ] +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -592,7 +631,7 @@ version = "1.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca268df6cd88e646b564e6aff1a016834e5f42077c736ef6b6789c31ef9ec5dc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "hex", "serde", @@ -604,6 +643,12 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28c122c3980598d243d63d9a704629a2d748d101f278052ff068be5a4423ab6f" +[[package]] +name = "constant_time_eq" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" + [[package]] name = "convert_case" version = "0.4.0" @@ -655,13 +700,27 @@ dependencies = [ "itertools", ] +[[package]] +name = "crossbeam" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2801af0d36612ae591caa9568261fddce32ce6e08a7275ea334a06a4ad021a2c" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + [[package]] name = "crossbeam-channel" version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", ] @@ -671,7 +730,7 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-epoch", "crossbeam-utils", ] @@ -683,19 +742,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", "memoffset", "scopeguard", ] +[[package]] +name = "crossbeam-queue" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -798,7 +867,7 @@ version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", + "block-buffer 0.10.4", "const-oid", "crypto-common", "subtle", @@ -855,7 +924,7 @@ version = "0.8.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071a31f4ee85403370b58aca746f01041ede6f0da2730960ad001edc2b71b394" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -873,7 +942,7 @@ dependencies = [ "rlp", "serde", "serde-hex", - "sha3", + "sha3 0.10.8", "zeroize", ] @@ -927,7 +996,7 @@ dependencies = [ "regex", "serde", "serde_json", - "sha3", + "sha3 0.10.8", "thiserror", "uint", ] @@ -1067,6 +1136,7 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" dependencies = [ + "bitvec", "rand_core", "subtle", ] @@ -1229,7 +1299,7 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi", ] @@ -1294,6 +1364,53 @@ version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" +[[package]] +name = "halo2_proofs" +version = "1.1.0" +source = "git+https://github.com/scroll-tech/halo2.git?branch=v1.1#7179a60e4b4b1dafff084deac7b4bea235eecf5f" +dependencies = [ + "ark-std 0.3.0", + "blake2b_simd", + "cfg-if 0.1.10", + "crossbeam", + "ff", + "group", + "halo2curves", + "log", + "maybe-rayon", + "num-bigint", + "num-integer", + "poseidon", + "rand_chacha", + "rand_core", + "rayon", + "sha3 0.9.1", + "subtle", + "tracing", +] + +[[package]] +name = "halo2curves" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6b1142bd1059aacde1b477e0c80c142910f1ceae67fc619311d6a17428007ab" +dependencies = [ + "blake2b_simd", + "ff", + "group", + "lazy_static", + "num-bigint", + "num-traits", + "pasta_curves", + "paste", + "rand", + "rand_core", + "serde", + "serde_arrays", + "static_assertions", + "subtle", +] + [[package]] name = "hash-db" version = "0.15.2" @@ -1539,7 +1656,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1603,7 +1720,7 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cadb76004ed8e97623117f3df85b17aaa6626ab0b0831e6573f104df16cd1bcc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "ecdsa", "elliptic-curve", "once_cell", @@ -1647,7 +1764,7 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "winapi", ] @@ -1669,6 +1786,16 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "maybe-rayon" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" +dependencies = [ + "cfg-if 1.0.0", + "rayon", +] + [[package]] name = "maybe-uninit" version = "2.0.0" @@ -1761,6 +1888,7 @@ dependencies = [ "autocfg", "num-integer", "num-traits", + "rand", ] [[package]] @@ -1873,6 +2001,12 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "open-fastrlp" version = "0.1.4" @@ -1924,6 +2058,21 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "pasta_curves" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e57598f73cc7e1b2ac63c79c517b31a0877cd7c402cdcaa311b5208de7a095" +dependencies = [ + "blake2b_simd", + "ff", + "group", + "lazy_static", + "rand", + "static_assertions", + "subtle", +] + [[package]] name = "paste" version = "1.0.14" @@ -2056,6 +2205,30 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f32154ba0af3a075eefa1eda8bb414ee928f62303a54ea85b8d6638ff1a6ee9e" +[[package]] +name = "poseidon" +version = "0.2.0" +source = "git+https://github.com/scroll-tech/poseidon.git?branch=main#5787dd3d2ce7a9e9601a035c396ac0c03449b54d" +dependencies = [ + "halo2curves", + "subtle", +] + +[[package]] +name = "poseidon-circuit" +version = "0.1.0" +source = "git+https://github.com/scroll-tech/poseidon-circuit.git?branch=scroll-dev-1201#c6f058bcf3bb0c7933d1979563c414f5cc480f25" +dependencies = [ + "bitvec", + "ff", + "halo2_proofs", + "lazy_static", + "log", + "rand", + "rand_xorshift", + "thiserror", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -2357,7 +2530,7 @@ dependencies = [ "proptest-derive", "revm-primitives", "serde", - "sha3", + "sha3 0.10.8", ] [[package]] @@ -2373,7 +2546,7 @@ dependencies = [ "ripemd", "secp256k1", "sha2", - "sha3", + "sha3 0.10.8", "substrate-bn", ] @@ -2390,17 +2563,19 @@ dependencies = [ "derive_more", "enumn", "fixed-hash", + "halo2_proofs", "hashbrown 0.14.0", "hex", "hex-literal", "once_cell", + "poseidon-circuit", "primitive-types", "proptest", "proptest-derive", "rlp", "ruint", "serde", - "sha3", + "sha3 0.10.8", ] [[package]] @@ -2420,7 +2595,7 @@ dependencies = [ "ruint", "serde", "serde_json", - "sha3", + "sha3 0.10.8", "structopt", "thiserror", "triehash", @@ -2642,7 +2817,7 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35c0a159d0c45c12b20c5a844feb1fe4bea86e28f17b92a5f0c42193634d3782" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "derive_more", "parity-scale-codec", "scale-info-derive", @@ -2764,6 +2939,15 @@ dependencies = [ "smallvec", ] +[[package]] +name = "serde_arrays" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38636132857f68ec3d5f3eb121166d2af33cb55174c4d5ff645db6165cbef0fd" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.188" @@ -2805,7 +2989,7 @@ version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.10.7", ] @@ -2816,11 +3000,23 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.10.7", ] +[[package]] +name = "sha3" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" +dependencies = [ + "block-buffer 0.9.0", + "digest 0.9.0", + "keccak", + "opaque-debug", +] + [[package]] name = "sha3" version = "0.10.8" @@ -3033,7 +3229,7 @@ version = "3.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "fastrand", "redox_syscall", "rustix", @@ -3236,7 +3432,7 @@ version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -3458,7 +3654,7 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] @@ -3483,7 +3679,7 @@ version = "0.4.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c02dbc21516f9f1f04f187958890d7e6026df8d16540b7ad9492bc34a67cea03" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -3732,7 +3928,7 @@ version = "0.50.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "windows-sys 0.48.0", ] diff --git a/Cargo.toml b/Cargo.toml index f76b7605fb..e6db91c895 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,6 @@ codegen-units = 1 [profile.ethtests] inherits = "test" opt-level = 3 + +[patch."https://github.com/privacy-scaling-explorations/halo2.git"] +halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.1" } \ No newline at end of file diff --git a/crates/interpreter/src/interpreter/analysis.rs b/crates/interpreter/src/interpreter/analysis.rs index d26fa85676..0ec60a802d 100644 --- a/crates/interpreter/src/interpreter/analysis.rs +++ b/crates/interpreter/src/interpreter/analysis.rs @@ -5,6 +5,7 @@ use crate::primitives::{ }; use alloc::sync::Arc; use core::fmt; +use revm_primitives::{hash_code_poseidon, POSEIDON_EMPTY}; /// Perform bytecode analysis. /// @@ -123,9 +124,19 @@ impl BytecodeLocked { self.len == 0 } - /// Calculate hash of the bytecode. + /// Calculate poseidon hash of the bytecode. #[inline] - pub fn hash_slow(&self) -> B256 { + pub fn poseidon_hash_slow(&self) -> B256 { + if self.is_empty() { + POSEIDON_EMPTY + } else { + hash_code_poseidon(self.original_bytecode_slice()) + } + } + + /// Calculate keccak hash of the bytecode. + #[inline] + pub fn keccak_hash_slow(&self) -> B256 { if self.is_empty() { KECCAK_EMPTY } else { diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 2d8c833889..0fc34e5895 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -46,6 +46,10 @@ arbitrary = { version = "1.3", features = ["derive"], optional = true } proptest = { version = "1.1", optional = true } proptest-derive = { version = "0.4", optional = true } +# scroll +halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.1" } +hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-1201" } + [dev-dependencies] arbitrary = { version = "1.3", features = ["derive"] } proptest = "1.1" diff --git a/crates/primitives/src/bytecode.rs b/crates/primitives/src/bytecode.rs index 5701654f58..895c6213b8 100644 --- a/crates/primitives/src/bytecode.rs +++ b/crates/primitives/src/bytecode.rs @@ -1,4 +1,4 @@ -use crate::{keccak256, B256, KECCAK_EMPTY}; +use crate::{hash_code_poseidon, keccak256, B256, KECCAK_EMPTY, POSEIDON_EMPTY}; use alloc::{sync::Arc, vec::Vec}; use bitvec::prelude::{bitvec, Lsb0}; use bitvec::vec::BitVec; @@ -87,8 +87,17 @@ impl Bytecode { } } + /// Calculate poseidon hash of the bytecode. + pub fn poseidon_hash_slow(&self) -> B256 { + if self.is_empty() { + POSEIDON_EMPTY + } else { + hash_code_poseidon(&self.original_bytes()) + } + } + /// Calculate hash of the bytecode. - pub fn hash_slow(&self) -> B256 { + pub fn keccak_hash_slow(&self) -> B256 { if self.is_empty() { KECCAK_EMPTY } else { diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index b876ef603c..c6848b6c8c 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -1,6 +1,6 @@ use crate::{ alloc::vec::Vec, calc_blob_gasprice, Account, InvalidHeader, InvalidTransaction, Spec, SpecId, - B160, B256, GAS_PER_BLOB, KECCAK_EMPTY, MAX_BLOB_NUMBER_PER_BLOCK, MAX_INITCODE_SIZE, U256, + B160, B256, GAS_PER_BLOB, MAX_BLOB_NUMBER_PER_BLOCK, MAX_INITCODE_SIZE, POSEIDON_EMPTY, U256, VERSIONED_HASH_VERSION_KZG, }; use bytes::Bytes; @@ -611,7 +611,7 @@ impl Env { // EIP-3607: Reject transactions from senders with deployed code // This EIP is introduced after london but there was no collision in past // so we can leave it enabled always - if !self.cfg.is_eip3607_disabled() && account.info.code_hash != KECCAK_EMPTY { + if !self.cfg.is_eip3607_disabled() && account.info.code_hash != POSEIDON_EMPTY { return Err(InvalidTransaction::RejectCallerWithCode); } diff --git a/crates/primitives/src/state.rs b/crates/primitives/src/state.rs index 3e9b0aa604..7c07ec7f62 100644 --- a/crates/primitives/src/state.rs +++ b/crates/primitives/src/state.rs @@ -1,4 +1,4 @@ -use crate::{Bytecode, B160, B256, KECCAK_EMPTY, U256}; +use crate::{Bytecode, B160, B256, KECCAK_EMPTY, POSEIDON_EMPTY, U256}; use bitflags::bitflags; use hashbrown::HashMap; @@ -172,6 +172,8 @@ pub struct AccountInfo { pub nonce: u64, /// code hash, pub code_hash: B256, + /// keccak code hash, + pub keccak_code_hash: B256, /// code: if None, `code_by_hash` will be used to fetch it if code needs to be loaded from /// inside of `revm`. pub code: Option, @@ -181,7 +183,8 @@ impl Default for AccountInfo { fn default() -> Self { Self { balance: U256::ZERO, - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: Some(Bytecode::new()), nonce: 0, } @@ -190,19 +193,32 @@ impl Default for AccountInfo { impl PartialEq for AccountInfo { fn eq(&self, other: &Self) -> bool { - self.balance == other.balance + let eq = self.balance == other.balance && self.nonce == other.nonce - && self.code_hash == other.code_hash + && self.code_hash == other.code_hash; + #[cfg(debug_assertions)] + if eq { + assert_eq!(self.keccak_code_hash, other.keccak_code_hash); + } + + eq } } impl AccountInfo { - pub fn new(balance: U256, nonce: u64, code_hash: B256, code: Bytecode) -> Self { + pub fn new( + balance: U256, + nonce: u64, + code_hash: B256, + keccak_code_hash: B256, + code: Bytecode, + ) -> Self { Self { balance, nonce, code: Some(code), code_hash, + keccak_code_hash, } } @@ -213,7 +229,7 @@ impl AccountInfo { } pub fn is_empty(&self) -> bool { - let code_empty = self.code_hash == KECCAK_EMPTY || self.code_hash == B256::zero(); + let code_empty = self.code_hash == POSEIDON_EMPTY || self.code_hash == B256::zero(); self.balance == U256::ZERO && self.nonce == 0 && code_empty } @@ -222,11 +238,17 @@ impl AccountInfo { } /// Return bytecode hash associated with this account. - /// If account does not have code, it return's `KECCAK_EMPTY` hash. + /// If account does not have code, it return's `POSEIDON_EMPTY` hash. pub fn code_hash(&self) -> B256 { self.code_hash } + /// Return keccak code hash associated with this account. + /// If account does not have code, it return's `KECCAK_EMPTY` hash. + pub fn keccak_code_hash(&self) -> B256 { + self.keccak_code_hash + } + /// Take bytecode from account. Code will be set to None. pub fn take_bytecode(&mut self) -> Option { self.code.take() diff --git a/crates/primitives/src/utilities.rs b/crates/primitives/src/utilities.rs index a1469fc8df..fe55cdde39 100644 --- a/crates/primitives/src/utilities.rs +++ b/crates/primitives/src/utilities.rs @@ -1,6 +1,8 @@ use crate::{ B160, B256, BLOB_GASPRICE_UPDATE_FRACTION, MIN_BLOB_GASPRICE, TARGET_BLOB_GAS_PER_BLOCK, U256, }; +use halo2_proofs::halo2curves::bn256::Fr; +use halo2_proofs::halo2curves::ff::PrimeField; use hex_literal::hex; use sha3::{Digest, Keccak256}; @@ -8,11 +10,67 @@ pub const KECCAK_EMPTY: B256 = B256(hex!( "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" )); +pub const POSEIDON_EMPTY: B256 = B256(hex!( + "2098f5fb9e239eab3ceac3f27b81e481dc3124d55ffed523a839ee8446b64864" +)); + #[inline(always)] pub fn keccak256(input: &[u8]) -> B256 { B256(Keccak256::digest(input)[..].try_into().unwrap()) } +/// Default number of bytes to pack into a field element. +pub const POSEIDON_HASH_BYTES_IN_FIELD: usize = 31; + +/// Poseidon code hash +pub fn hash_code_poseidon(code: &[u8]) -> B256 { + use hash_circuit::hash::{Hashable, MessageHashable, HASHABLE_DOMAIN_SPEC}; + + let bytes_in_field = POSEIDON_HASH_BYTES_IN_FIELD; + let fls = (0..(code.len() / bytes_in_field)) + .map(|i| i * bytes_in_field) + .map(|i| { + Fr::from_bytes( + &U256::try_from_be_slice(&code[i..i + bytes_in_field]) + .expect("infallible") + .to_le_bytes(), + ) + .unwrap() + }); + let msgs: Vec<_> = fls + .chain(if code.len() % bytes_in_field == 0 { + None + } else { + let last_code = &code[code.len() - code.len() % bytes_in_field..]; + // pad to bytes_in_field + let mut last_buf = [0u8; POSEIDON_HASH_BYTES_IN_FIELD]; + last_buf.as_mut_slice()[..last_code.len()].copy_from_slice(last_code); + Some( + Fr::from_bytes( + &U256::try_from_be_slice(&last_buf) + .expect("infallible") + .to_le_bytes(), + ) + .unwrap(), + ) + }) + .collect(); + + let h = if msgs.is_empty() { + // the empty code hash is overlapped with simple hash on [0, 0] + // an issue in poseidon primitive prevent us calculate it from hash_msg + Fr::hash_with_domain([Fr::zero(), Fr::zero()], Fr::zero()) + } else { + Fr::hash_msg(&msgs, Some(code.len() as u128 * HASHABLE_DOMAIN_SPEC)) + }; + + B256::from( + &U256::try_from_be_slice(h.to_repr().as_ref()) + .expect("infallible") + .to_be_bytes(), + ) +} + /// Returns the address for the legacy `CREATE` scheme: [`crate::env::CreateScheme::Create`] pub fn create_address(caller: B160, nonce: u64) -> B160 { let mut stream = rlp::RlpStream::new_list(2); diff --git a/crates/revm/src/db/in_memory_db.rs b/crates/revm/src/db/in_memory_db.rs index 4909b0bab2..9d12054469 100644 --- a/crates/revm/src/db/in_memory_db.rs +++ b/crates/revm/src/db/in_memory_db.rs @@ -1,6 +1,7 @@ use super::{DatabaseCommit, DatabaseRef, EmptyDB}; use crate::primitives::{ - hash_map::Entry, Account, AccountInfo, Bytecode, HashMap, Log, B160, B256, KECCAK_EMPTY, U256, + hash_map::Entry, Account, AccountInfo, Bytecode, HashMap, Log, B160, B256, KECCAK_EMPTY, + POSEIDON_EMPTY, U256, }; use crate::Database; use alloc::vec::Vec; @@ -42,7 +43,7 @@ impl Default for CacheDB { impl CacheDB { pub fn new(db: ExtDB) -> Self { let mut contracts = HashMap::new(); - contracts.insert(KECCAK_EMPTY, Bytecode::new()); + contracts.insert(POSEIDON_EMPTY, Bytecode::new()); contracts.insert(B256::zero(), Bytecode::new()); Self { accounts: HashMap::new(), @@ -61,8 +62,11 @@ impl CacheDB { pub fn insert_contract(&mut self, account: &mut AccountInfo) { if let Some(code) = &account.code { if !code.is_empty() { - if account.code_hash == KECCAK_EMPTY { - account.code_hash = code.hash_slow(); + if account.code_hash == POSEIDON_EMPTY { + account.code_hash = code.poseidon_hash_slow(); + } + if account.keccak_code_hash == KECCAK_EMPTY { + account.keccak_code_hash = code.keccak_hash_slow(); } self.contracts .entry(account.code_hash) @@ -70,7 +74,9 @@ impl CacheDB { } } if account.code_hash == B256::zero() { - account.code_hash = KECCAK_EMPTY; + debug_assert_eq!(account.keccak_code_hash, B256::zero()); + account.code_hash = POSEIDON_EMPTY; + account.keccak_code_hash = KECCAK_EMPTY; } } @@ -354,12 +360,13 @@ impl AccountState { /// /// Any other address will return an empty account. #[derive(Debug, Default, Clone)] -pub struct BenchmarkDB(pub Bytecode, B256); +pub struct BenchmarkDB(pub Bytecode, B256, B256); impl BenchmarkDB { pub fn new_bytecode(bytecode: Bytecode) -> Self { - let hash = bytecode.hash_slow(); - Self(bytecode, hash) + let poseidon_hash = bytecode.poseidon_hash_slow(); + let keccak_hash = bytecode.keccak_hash_slow(); + Self(bytecode, poseidon_hash, keccak_hash) } } @@ -373,6 +380,7 @@ impl Database for BenchmarkDB { balance: U256::from(10000000), code: Some(self.0.clone()), code_hash: self.1, + keccak_code_hash: self.2, })); } if address == B160::from(1) { @@ -380,7 +388,8 @@ impl Database for BenchmarkDB { nonce: 0, balance: U256::from(10000000), code: None, - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, })); } Ok(None) diff --git a/crates/revm/src/db/states/bundle_state.rs b/crates/revm/src/db/states/bundle_state.rs index baa5e570a0..049cdc0caf 100644 --- a/crates/revm/src/db/states/bundle_state.rs +++ b/crates/revm/src/db/states/bundle_state.rs @@ -659,7 +659,7 @@ impl BundleState { mod tests { use super::*; use crate::{db::StorageWithOriginalValues, TransitionAccount}; - use revm_interpreter::primitives::KECCAK_EMPTY; + use revm_interpreter::primitives::{KECCAK_EMPTY, POSEIDON_EMPTY}; #[test] fn transition_states() { @@ -668,7 +668,8 @@ mod tests { let acc1 = AccountInfo { balance: U256::from(10), nonce: 1, - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: None, }; @@ -719,7 +720,8 @@ mod tests { Some(AccountInfo { nonce: 1, balance: U256::from(10), - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: None, }), HashMap::from([ @@ -733,7 +735,8 @@ mod tests { Some(AccountInfo { nonce: 1, balance: U256::from(10), - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: None, }), HashMap::from([]), @@ -761,7 +764,8 @@ mod tests { Some(AccountInfo { nonce: 3, balance: U256::from(20), - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: None, }), HashMap::from([(slot1(), (U256::from(0), U256::from(15)))]), @@ -771,7 +775,8 @@ mod tests { Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: None, })), vec![(slot1(), U256::from(10))], @@ -788,7 +793,8 @@ mod tests { AccountInfo { nonce: 1, balance: U256::from(10), - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: None, }, ) @@ -802,7 +808,8 @@ mod tests { AccountInfo { nonce: 1, balance: U256::from(10), - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: None, }, ) @@ -821,7 +828,8 @@ mod tests { AccountInfo { nonce: 3, balance: U256::from(20), - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: None, }, ) @@ -836,7 +844,8 @@ mod tests { Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), - code_hash: KECCAK_EMPTY, + code_hash: POSEIDON_EMPTY, + keccak_code_hash: KECCAK_EMPTY, code: None, })), ) diff --git a/crates/revm/src/db/states/cache_account.rs b/crates/revm/src/db/states/cache_account.rs index f6b85436c6..7cb54b928a 100644 --- a/crates/revm/src/db/states/cache_account.rs +++ b/crates/revm/src/db/states/cache_account.rs @@ -2,7 +2,7 @@ use super::{ plain_account::PlainStorage, AccountStatus, BundleAccount, PlainAccount, StorageWithOriginalValues, TransitionAccount, }; -use revm_interpreter::primitives::{AccountInfo, KECCAK_EMPTY, U256}; +use revm_interpreter::primitives::{AccountInfo, POSEIDON_EMPTY, U256}; use revm_precompile::HashMap; /// Cache account contains plain state that gets updated @@ -315,7 +315,8 @@ impl CacheAccount { self.status = match self.status { AccountStatus::Loaded => { // Account that have nonce zero and empty code hash is considered to be fully in memory. - if previous_info.as_ref().map(|a| (a.code_hash, a.nonce)) == Some((KECCAK_EMPTY, 0)) + if previous_info.as_ref().map(|a| (a.code_hash, a.nonce)) + == Some((POSEIDON_EMPTY, 0)) { AccountStatus::InMemoryChange } else { @@ -376,7 +377,8 @@ impl CacheAccount { self.status = match self.status { AccountStatus::Loaded => { - if previous_info.as_ref().map(|a| (a.code_hash, a.nonce)) == Some((KECCAK_EMPTY, 0)) + if previous_info.as_ref().map(|a| (a.code_hash, a.nonce)) + == Some((POSEIDON_EMPTY, 0)) { // account is fully in memory AccountStatus::InMemoryChange diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 98997d5ca2..81f5d631d3 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -726,7 +726,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, }); } }; - let code_hash = account.info.code_hash(); + let code_hash = account.info.keccak_code_hash(); let bytecode = account.info.code.clone().unwrap_or_default(); // Check depth @@ -887,7 +887,7 @@ impl<'a, GSPEC: Spec, DB: Database + 'a, const INSPECT: bool> Host return Some((B256::zero(), is_cold)); } - Some((acc.info.code_hash, is_cold)) + Some((acc.info.keccak_code_hash, is_cold)) } fn sload(&mut self, address: B160, index: U256) -> Option<(U256, bool)> { diff --git a/crates/revm/src/journaled_state.rs b/crates/revm/src/journaled_state.rs index 8e8c9c1d77..26ec4f793f 100644 --- a/crates/revm/src/journaled_state.rs +++ b/crates/revm/src/journaled_state.rs @@ -1,7 +1,7 @@ use crate::interpreter::{inner_models::SelfDestructResult, InstructionResult}; use crate::primitives::{ db::Database, hash_map::Entry, Account, Bytecode, HashMap, Log, Spec, SpecId::*, State, - StorageSlot, TransientStorage, B160, KECCAK_EMPTY, PRECOMPILE3, U256, + StorageSlot, TransientStorage, B160, KECCAK_EMPTY, POSEIDON_EMPTY, PRECOMPILE3, U256, }; use alloc::vec::Vec; use core::mem; @@ -164,7 +164,8 @@ impl JournaledState { .unwrap() .push(JournalEntry::CodeChange { address }); - account.info.code_hash = code.hash_slow(); + account.info.code_hash = code.poseidon_hash_slow(); + account.info.keccak_code_hash = code.keccak_hash_slow(); account.info.code = Some(code); } @@ -315,7 +316,8 @@ impl JournaledState { num_of_precompiles: usize, ) -> bool { // Check collision. Bytecode needs to be empty. - if account.info.code_hash != KECCAK_EMPTY { + if account.info.code_hash != POSEIDON_EMPTY && account.info.keccak_code_hash != KECCAK_EMPTY + { return true; } // Check collision. Nonce is not zero @@ -415,7 +417,8 @@ impl JournaledState { } JournalEntry::CodeChange { address } => { let acc = state.get_mut(&address).unwrap(); - acc.info.code_hash = KECCAK_EMPTY; + acc.info.code_hash = POSEIDON_EMPTY; + acc.info.keccak_code_hash = KECCAK_EMPTY; acc.info.code = None; } } @@ -537,7 +540,7 @@ impl JournaledState { ) -> Result<&mut Account, DB::Error> { let account = self.initial_account_load(address, &[], db)?; if account.info.code.is_none() { - if account.info.code_hash == KECCAK_EMPTY { + if account.info.code_hash == POSEIDON_EMPTY { account.info.code = Some(Bytecode::new()); } else { // load code if requested @@ -629,7 +632,7 @@ impl JournaledState { ) -> Result<(&mut Account, bool), DB::Error> { let (acc, is_cold) = self.load_account(address, db)?; if acc.info.code.is_none() { - if acc.info.code_hash == KECCAK_EMPTY { + if acc.info.code_hash == POSEIDON_EMPTY { let empty = Bytecode::new(); acc.info.code = Some(empty); } else { From ae94addef55e6badbde9cbb84190cec7bc729dd7 Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 17 Apr 2024 12:22:36 +0800 Subject: [PATCH 03/24] port changes --- crates/primitives/src/env.rs | 3 +++ crates/revm/src/evm_impl.rs | 6 ++++-- crates/revm/src/handler/mainnet.rs | 15 +++++++++------ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index c6848b6c8c..d3ccbf8b0b 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -183,6 +183,8 @@ pub struct TxEnv { #[cfg_attr(feature = "serde", serde(flatten))] #[cfg(feature = "optimism")] pub optimism: OptimismFields, + + pub l1_fee: U256, } impl TxEnv { @@ -442,6 +444,7 @@ impl Default for TxEnv { max_fee_per_blob_gas: None, #[cfg(feature = "optimism")] optimism: OptimismFields::default(), + l1_fee: U256::ZERO, } } } diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 81f5d631d3..8972cf5f1a 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -193,6 +193,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact let tx_value = env.tx.value; let tx_data = env.tx.data.clone(); let tx_gas_limit = env.tx.gas_limit; + let l1_fee = env.tx.l1_fee; #[cfg(feature = "optimism")] let tx_l1_cost = { @@ -269,8 +270,9 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact // Subtract gas costs from the caller's account. // We need to saturate the gas cost to prevent underflow in case that `disable_balance_check` is enabled. - let mut gas_cost = - U256::from(tx_gas_limit).saturating_mul(self.data.env.effective_gas_price()); + let mut gas_cost = U256::from(tx_gas_limit) + .saturating_mul(self.data.env.effective_gas_price()) + .saturating_add(l1_fee); // EIP-4844 if GSPEC::enabled(CANCUN) { diff --git a/crates/revm/src/handler/mainnet.rs b/crates/revm/src/handler/mainnet.rs index 81f12fdb6c..0565756b59 100644 --- a/crates/revm/src/handler/mainnet.rs +++ b/crates/revm/src/handler/mainnet.rs @@ -64,14 +64,16 @@ pub fn reward_beneficiary( ) -> Result<(), EVMError> { let beneficiary = data.env.block.coinbase; let effective_gas_price = data.env.effective_gas_price(); + let l1_fee = data.env.tx.l1_fee; // transfer fee to coinbase/beneficiary. // EIP-1559 discard basefee for coinbase transfer. Basefee amount of gas is discarded. - let coinbase_gas_price = if SPEC::enabled(LONDON) { - effective_gas_price.saturating_sub(data.env.block.basefee) - } else { - effective_gas_price - }; + // let coinbase_gas_price = if SPEC::enabled(LONDON) { + // effective_gas_price.saturating_sub(data.env.block.basefee) + // } else { + // effective_gas_price + // }; + let coinbase_gas_price = effective_gas_price; let (coinbase_account, _) = data .journaled_state @@ -82,7 +84,8 @@ pub fn reward_beneficiary( coinbase_account.info.balance = coinbase_account .info .balance - .saturating_add(coinbase_gas_price * U256::from(gas.spend() - gas_refund)); + .saturating_add(coinbase_gas_price * U256::from(gas.spend() - gas_refund)) + .saturating_add(l1_fee); Ok(()) } From 301846bb985fb281ef281d8cb2ecaee0be640d72 Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 17 Apr 2024 13:37:11 +0800 Subject: [PATCH 04/24] add scroll feature --- crates/interpreter/Cargo.toml | 1 + .../interpreter/src/interpreter/analysis.rs | 19 +++++- crates/precompile/Cargo.toml | 3 +- crates/precompile/src/bn128.rs | 3 +- crates/primitives/Cargo.toml | 5 +- crates/primitives/src/bytecode.rs | 19 +++++- crates/primitives/src/env.rs | 18 +++++- crates/primitives/src/state.rs | 23 +++++-- crates/primitives/src/utilities.rs | 12 +++- crates/revm/Cargo.toml | 1 + crates/revm/src/db/in_memory_db.rs | 64 +++++++++++++++---- crates/revm/src/db/states/bundle_state.rs | 40 +++++++++++- crates/revm/src/db/states/cache_account.rs | 27 +++++++- crates/revm/src/evm_impl.rs | 8 ++- crates/revm/src/journaled_state.rs | 46 +++++++++++-- 15 files changed, 246 insertions(+), 43 deletions(-) diff --git a/crates/interpreter/Cargo.toml b/crates/interpreter/Cargo.toml index 5932ba414f..89b8b22441 100644 --- a/crates/interpreter/Cargo.toml +++ b/crates/interpreter/Cargo.toml @@ -43,6 +43,7 @@ arbitrary = [ ] optimism = ["revm-primitives/optimism"] +scroll = ["revm-primitives/scroll"] dev = [ "memory_limit", diff --git a/crates/interpreter/src/interpreter/analysis.rs b/crates/interpreter/src/interpreter/analysis.rs index 0ec60a802d..ab5c4825c9 100644 --- a/crates/interpreter/src/interpreter/analysis.rs +++ b/crates/interpreter/src/interpreter/analysis.rs @@ -5,7 +5,9 @@ use crate::primitives::{ }; use alloc::sync::Arc; use core::fmt; -use revm_primitives::{hash_code_poseidon, POSEIDON_EMPTY}; + +#[cfg(feature = "scroll")] +use crate::primitives::{poseidon, POSEIDON_EMPTY}; /// Perform bytecode analysis. /// @@ -126,16 +128,29 @@ impl BytecodeLocked { /// Calculate poseidon hash of the bytecode. #[inline] + #[cfg(not(feature = "scroll"))] + pub fn hash_slow(&self) -> B256 { + if self.is_empty() { + KECCAK_EMPTY + } else { + keccak256(self.original_bytecode_slice()) + } + } + + /// Calculate poseidon hash of the bytecode. + #[inline] + #[cfg(feature = "scroll")] pub fn poseidon_hash_slow(&self) -> B256 { if self.is_empty() { POSEIDON_EMPTY } else { - hash_code_poseidon(self.original_bytecode_slice()) + poseidon(self.original_bytecode_slice()) } } /// Calculate keccak hash of the bytecode. #[inline] + #[cfg(feature = "scroll")] pub fn keccak_hash_slow(&self) -> B256 { if self.is_empty() { KECCAK_EMPTY diff --git a/crates/precompile/Cargo.toml b/crates/precompile/Cargo.toml index b5887d4571..14f4909254 100644 --- a/crates/precompile/Cargo.toml +++ b/crates/precompile/Cargo.toml @@ -24,7 +24,7 @@ secp256k1 = { version = "0.27.0", default-features = false, features = [ ], optional = true } sha2 = { version = "0.10.8", default-features = false } sha3 = { version = "0.10.7", default-features = false } -c-kzg = { version="0.1.1", default-features = false, optional = true } +c-kzg = { version = "0.1.1", default-features = false, optional = true } [dev-dependencies] hex = "0.4" @@ -55,3 +55,4 @@ c-kzg = ["dep:c-kzg", "revm-primitives/c-kzg"] # In Linux it passes. If you don't require to build wasm on win/mac, it is safe to use it and it is enabled by default. secp256k1 = ["dep:secp256k1"] optimism = ["revm-primitives/optimism"] +scroll = ["revm-primitives/scroll"] diff --git a/crates/precompile/src/bn128.rs b/crates/precompile/src/bn128.rs index 9c3640ae66..8d82a01db7 100644 --- a/crates/precompile/src/bn128.rs +++ b/crates/precompile/src/bn128.rs @@ -165,8 +165,7 @@ fn run_pair( pair_base_cost: u64, gas_limit: u64, ) -> PrecompileResult { - let gas_used = - (input.len() / PAIR_ELEMENT_LEN) as u64 * pair_per_point_cost + pair_base_cost; + let gas_used = (input.len() / PAIR_ELEMENT_LEN) as u64 * pair_per_point_cost + pair_base_cost; if gas_used > gas_limit { return Err(Error::OutOfGas); } diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 0fc34e5895..31a0d6b4ca 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -47,8 +47,8 @@ proptest = { version = "1.1", optional = true } proptest-derive = { version = "0.4", optional = true } # scroll -halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.1" } -hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-1201" } +halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.1", optional = true } +hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-1201", optional = true } [dev-dependencies] arbitrary = { version = "1.3", features = ["derive"] } @@ -88,6 +88,7 @@ arbitrary = [ ] optimism = [] +scroll = ["halo2_proofs", "hash-circuit"] dev = [ "memory_limit", diff --git a/crates/primitives/src/bytecode.rs b/crates/primitives/src/bytecode.rs index 895c6213b8..41f9cd6770 100644 --- a/crates/primitives/src/bytecode.rs +++ b/crates/primitives/src/bytecode.rs @@ -1,10 +1,13 @@ -use crate::{hash_code_poseidon, keccak256, B256, KECCAK_EMPTY, POSEIDON_EMPTY}; +use crate::{keccak256, B256, KECCAK_EMPTY}; use alloc::{sync::Arc, vec::Vec}; use bitvec::prelude::{bitvec, Lsb0}; use bitvec::vec::BitVec; use bytes::Bytes; use core::fmt::Debug; +#[cfg(feature = "scroll")] +use crate::{poseidon, POSEIDON_EMPTY}; + /// A map of valid `jump` destinations. #[derive(Clone, Eq, PartialEq, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -87,16 +90,28 @@ impl Bytecode { } } + /// Calculate hash of the bytecode. + #[cfg(not(feature = "scroll"))] + pub fn hash_slow(&self) -> B256 { + if self.is_empty() { + KECCAK_EMPTY + } else { + keccak256(&self.original_bytes()) + } + } + /// Calculate poseidon hash of the bytecode. + #[cfg(feature = "scroll")] pub fn poseidon_hash_slow(&self) -> B256 { if self.is_empty() { POSEIDON_EMPTY } else { - hash_code_poseidon(&self.original_bytes()) + poseidon(&self.original_bytes()) } } /// Calculate hash of the bytecode. + #[cfg(feature = "scroll")] pub fn keccak_hash_slow(&self) -> B256 { if self.is_empty() { KECCAK_EMPTY diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index d3ccbf8b0b..9e08574729 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -1,11 +1,16 @@ use crate::{ alloc::vec::Vec, calc_blob_gasprice, Account, InvalidHeader, InvalidTransaction, Spec, SpecId, - B160, B256, GAS_PER_BLOB, MAX_BLOB_NUMBER_PER_BLOCK, MAX_INITCODE_SIZE, POSEIDON_EMPTY, U256, + B160, B256, GAS_PER_BLOB, MAX_BLOB_NUMBER_PER_BLOCK, MAX_INITCODE_SIZE, U256, VERSIONED_HASH_VERSION_KZG, }; use bytes::Bytes; use core::cmp::{min, Ordering}; +#[cfg(not(feature = "scroll"))] +use crate::KECCAK_EMPTY; +#[cfg(feature = "scroll")] +use crate::POSEIDON_EMPTY; + #[derive(Clone, Debug, Default, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Env { @@ -614,8 +619,15 @@ impl Env { // EIP-3607: Reject transactions from senders with deployed code // This EIP is introduced after london but there was no collision in past // so we can leave it enabled always - if !self.cfg.is_eip3607_disabled() && account.info.code_hash != POSEIDON_EMPTY { - return Err(InvalidTransaction::RejectCallerWithCode); + if !self.cfg.is_eip3607_disabled() { + #[cfg(not(feature = "scroll"))] + if account.info.code_hash != KECCAK_EMPTY { + return Err(InvalidTransaction::RejectCallerWithCode); + } + #[cfg(feature = "scroll")] + if account.info.code_hash != POSEIDON_EMPTY { + return Err(InvalidTransaction::RejectCallerWithCode); + } } // On Optimism, deposit transactions do not have verification on the nonce diff --git a/crates/primitives/src/state.rs b/crates/primitives/src/state.rs index 7c07ec7f62..7a551bc7f4 100644 --- a/crates/primitives/src/state.rs +++ b/crates/primitives/src/state.rs @@ -1,7 +1,10 @@ -use crate::{Bytecode, B160, B256, KECCAK_EMPTY, POSEIDON_EMPTY, U256}; +use crate::{Bytecode, B160, B256, KECCAK_EMPTY, U256}; use bitflags::bitflags; use hashbrown::HashMap; +#[cfg(feature = "scroll")] +use crate::POSEIDON_EMPTY; + #[derive(Debug, Clone, Eq, PartialEq, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Account { @@ -172,6 +175,7 @@ pub struct AccountInfo { pub nonce: u64, /// code hash, pub code_hash: B256, + #[cfg(feature = "scroll")] /// keccak code hash, pub keccak_code_hash: B256, /// code: if None, `code_by_hash` will be used to fetch it if code needs to be loaded from @@ -183,7 +187,11 @@ impl Default for AccountInfo { fn default() -> Self { Self { balance: U256::ZERO, + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: Some(Bytecode::new()), nonce: 0, @@ -196,7 +204,7 @@ impl PartialEq for AccountInfo { let eq = self.balance == other.balance && self.nonce == other.nonce && self.code_hash == other.code_hash; - #[cfg(debug_assertions)] + #[cfg(all(debug_assertions, feature = "scroll"))] if eq { assert_eq!(self.keccak_code_hash, other.keccak_code_hash); } @@ -210,7 +218,7 @@ impl AccountInfo { balance: U256, nonce: u64, code_hash: B256, - keccak_code_hash: B256, + #[cfg(feature = "scroll")] keccak_code_hash: B256, code: Bytecode, ) -> Self { Self { @@ -218,6 +226,7 @@ impl AccountInfo { nonce, code: Some(code), code_hash, + #[cfg(feature = "scroll")] keccak_code_hash, } } @@ -229,6 +238,9 @@ impl AccountInfo { } pub fn is_empty(&self) -> bool { + #[cfg(not(feature = "scroll"))] + let code_empty = self.code_hash == KECCAK_EMPTY || self.code_hash == B256::zero(); + #[cfg(feature = "scroll")] let code_empty = self.code_hash == POSEIDON_EMPTY || self.code_hash == B256::zero(); self.balance == U256::ZERO && self.nonce == 0 && code_empty } @@ -238,13 +250,16 @@ impl AccountInfo { } /// Return bytecode hash associated with this account. - /// If account does not have code, it return's `POSEIDON_EMPTY` hash. + /// If account does not have code, + #[cfg_attr(not(feature = "scroll"), doc = "it return's `KECCAK_EMPTY` hash.")] + #[cfg_attr(feature = "scroll", doc = "it return's `POSEIDON_EMPTY` hash.")] pub fn code_hash(&self) -> B256 { self.code_hash } /// Return keccak code hash associated with this account. /// If account does not have code, it return's `KECCAK_EMPTY` hash. + #[cfg(feature = "scroll")] pub fn keccak_code_hash(&self) -> B256 { self.keccak_code_hash } diff --git a/crates/primitives/src/utilities.rs b/crates/primitives/src/utilities.rs index fe55cdde39..42636f6411 100644 --- a/crates/primitives/src/utilities.rs +++ b/crates/primitives/src/utilities.rs @@ -1,15 +1,19 @@ use crate::{ B160, B256, BLOB_GASPRICE_UPDATE_FRACTION, MIN_BLOB_GASPRICE, TARGET_BLOB_GAS_PER_BLOCK, U256, }; -use halo2_proofs::halo2curves::bn256::Fr; -use halo2_proofs::halo2curves::ff::PrimeField; use hex_literal::hex; use sha3::{Digest, Keccak256}; +#[cfg(feature = "scroll")] +use halo2_proofs::halo2curves::bn256::Fr; +#[cfg(feature = "scroll")] +use halo2_proofs::halo2curves::ff::PrimeField; + pub const KECCAK_EMPTY: B256 = B256(hex!( "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" )); +#[cfg(feature = "scroll")] pub const POSEIDON_EMPTY: B256 = B256(hex!( "2098f5fb9e239eab3ceac3f27b81e481dc3124d55ffed523a839ee8446b64864" )); @@ -20,10 +24,12 @@ pub fn keccak256(input: &[u8]) -> B256 { } /// Default number of bytes to pack into a field element. +#[cfg(feature = "scroll")] pub const POSEIDON_HASH_BYTES_IN_FIELD: usize = 31; /// Poseidon code hash -pub fn hash_code_poseidon(code: &[u8]) -> B256 { +#[cfg(feature = "scroll")] +pub fn poseidon(code: &[u8]) -> B256 { use hash_circuit::hash::{Hashable, MessageHashable, HASHABLE_DOMAIN_SPEC}; let bytes_in_field = POSEIDON_HASH_BYTES_IN_FIELD; diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index ea0553fcb7..bd108c5832 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -69,6 +69,7 @@ c-kzg = ["revm-precompile/c-kzg"] web3db = [] with-serde = [] optimism = ["revm-interpreter/optimism", "revm-precompile/optimism"] +scroll = ["revm-interpreter/scroll", "revm-precompile/scroll"] [[example]] name = "fork_ref_transact" diff --git a/crates/revm/src/db/in_memory_db.rs b/crates/revm/src/db/in_memory_db.rs index 9d12054469..5711fe5074 100644 --- a/crates/revm/src/db/in_memory_db.rs +++ b/crates/revm/src/db/in_memory_db.rs @@ -1,12 +1,14 @@ use super::{DatabaseCommit, DatabaseRef, EmptyDB}; use crate::primitives::{ - hash_map::Entry, Account, AccountInfo, Bytecode, HashMap, Log, B160, B256, KECCAK_EMPTY, - POSEIDON_EMPTY, U256, + hash_map::Entry, Account, AccountInfo, Bytecode, HashMap, Log, B160, B256, KECCAK_EMPTY, U256, }; use crate::Database; use alloc::vec::Vec; use core::convert::Infallible; +#[cfg(feature = "scroll")] +use crate::primitives::POSEIDON_EMPTY; + /// A [Database] implementation that stores all state changes in memory. pub type InMemoryDB = CacheDB; @@ -43,6 +45,9 @@ impl Default for CacheDB { impl CacheDB { pub fn new(db: ExtDB) -> Self { let mut contracts = HashMap::new(); + #[cfg(not(feature = "scroll"))] + contracts.insert(KECCAK_EMPTY, Bytecode::new()); + #[cfg(feature = "scroll")] contracts.insert(POSEIDON_EMPTY, Bytecode::new()); contracts.insert(B256::zero(), Bytecode::new()); Self { @@ -62,11 +67,18 @@ impl CacheDB { pub fn insert_contract(&mut self, account: &mut AccountInfo) { if let Some(code) = &account.code { if !code.is_empty() { - if account.code_hash == POSEIDON_EMPTY { - account.code_hash = code.poseidon_hash_slow(); + #[cfg(not(feature = "scroll"))] + if account.code_hash == KECCAK_EMPTY { + account.code_hash = code.hash_slow(); } - if account.keccak_code_hash == KECCAK_EMPTY { - account.keccak_code_hash = code.keccak_hash_slow(); + #[cfg(feature = "scroll")] + { + if account.code_hash == POSEIDON_EMPTY { + account.code_hash = code.poseidon_hash_slow(); + } + if account.keccak_code_hash == KECCAK_EMPTY { + account.keccak_code_hash = code.keccak_hash_slow(); + } } self.contracts .entry(account.code_hash) @@ -74,9 +86,16 @@ impl CacheDB { } } if account.code_hash == B256::zero() { - debug_assert_eq!(account.keccak_code_hash, B256::zero()); - account.code_hash = POSEIDON_EMPTY; - account.keccak_code_hash = KECCAK_EMPTY; + #[cfg(not(feature = "scroll"))] + { + account.code_hash = KECCAK_EMPTY; + } + #[cfg(feature = "scroll")] + { + debug_assert_eq!(account.keccak_code_hash, B256::zero()); + account.code_hash = POSEIDON_EMPTY; + account.keccak_code_hash = KECCAK_EMPTY; + } } } @@ -359,14 +378,30 @@ impl AccountState { /// Custom benchmarking DB that only has account info for the zero address. /// /// Any other address will return an empty account. +#[cfg(not(feature = "scroll"))] +#[derive(Debug, Default, Clone)] +pub struct BenchmarkDB(pub Bytecode, B256); + +/// Custom benchmarking DB that only has account info for the zero address. +/// +/// Any other address will return an empty account. +#[cfg(feature = "scroll")] #[derive(Debug, Default, Clone)] pub struct BenchmarkDB(pub Bytecode, B256, B256); impl BenchmarkDB { pub fn new_bytecode(bytecode: Bytecode) -> Self { - let poseidon_hash = bytecode.poseidon_hash_slow(); - let keccak_hash = bytecode.keccak_hash_slow(); - Self(bytecode, poseidon_hash, keccak_hash) + #[cfg(not(feature = "scroll"))] + { + let hash = bytecode.hash_slow(); + Self(bytecode, hash) + } + #[cfg(feature = "scroll")] + { + let poseidon_hash = bytecode.poseidon_hash_slow(); + let keccak_hash = bytecode.keccak_hash_slow(); + Self(bytecode, poseidon_hash, keccak_hash) + } } } @@ -380,6 +415,7 @@ impl Database for BenchmarkDB { balance: U256::from(10000000), code: Some(self.0.clone()), code_hash: self.1, + #[cfg(feature = "scroll")] keccak_code_hash: self.2, })); } @@ -388,7 +424,11 @@ impl Database for BenchmarkDB { nonce: 0, balance: U256::from(10000000), code: None, + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, })); } diff --git a/crates/revm/src/db/states/bundle_state.rs b/crates/revm/src/db/states/bundle_state.rs index 049cdc0caf..2e5bad38b6 100644 --- a/crates/revm/src/db/states/bundle_state.rs +++ b/crates/revm/src/db/states/bundle_state.rs @@ -659,7 +659,9 @@ impl BundleState { mod tests { use super::*; use crate::{db::StorageWithOriginalValues, TransitionAccount}; - use revm_interpreter::primitives::{KECCAK_EMPTY, POSEIDON_EMPTY}; + use revm_interpreter::primitives::KECCAK_EMPTY; + #[cfg(feature = "scroll")] + use revm_interpreter::primitives::POSEIDON_EMPTY; #[test] fn transition_states() { @@ -668,7 +670,11 @@ mod tests { let acc1 = AccountInfo { balance: U256::from(10), nonce: 1, + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: None, }; @@ -720,7 +726,11 @@ mod tests { Some(AccountInfo { nonce: 1, balance: U256::from(10), + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: None, }), @@ -735,7 +745,11 @@ mod tests { Some(AccountInfo { nonce: 1, balance: U256::from(10), + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: None, }), @@ -764,7 +778,11 @@ mod tests { Some(AccountInfo { nonce: 3, balance: U256::from(20), + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: None, }), @@ -775,7 +793,11 @@ mod tests { Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: None, })), @@ -793,7 +815,11 @@ mod tests { AccountInfo { nonce: 1, balance: U256::from(10), + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: None, }, @@ -808,7 +834,11 @@ mod tests { AccountInfo { nonce: 1, balance: U256::from(10), + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: None, }, @@ -828,7 +858,11 @@ mod tests { AccountInfo { nonce: 3, balance: U256::from(20), + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: None, }, @@ -844,7 +878,11 @@ mod tests { Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), + #[cfg(not(feature = "scroll"))] + code_hash: KECCAK_EMPTY, + #[cfg(feature = "scroll")] code_hash: POSEIDON_EMPTY, + #[cfg(feature = "scroll")] keccak_code_hash: KECCAK_EMPTY, code: None, })), diff --git a/crates/revm/src/db/states/cache_account.rs b/crates/revm/src/db/states/cache_account.rs index 7cb54b928a..04d94596f3 100644 --- a/crates/revm/src/db/states/cache_account.rs +++ b/crates/revm/src/db/states/cache_account.rs @@ -2,9 +2,14 @@ use super::{ plain_account::PlainStorage, AccountStatus, BundleAccount, PlainAccount, StorageWithOriginalValues, TransitionAccount, }; -use revm_interpreter::primitives::{AccountInfo, POSEIDON_EMPTY, U256}; +use revm_interpreter::primitives::{AccountInfo, U256}; use revm_precompile::HashMap; +#[cfg(not(feature = "scroll"))] +use revm_interpreter::primitives::KECCAK_EMPTY; +#[cfg(feature = "scroll")] +use revm_interpreter::primitives::POSEIDON_EMPTY; + /// Cache account contains plain state that gets updated /// at every transaction when evm output is applied to CacheState. #[derive(Clone, Debug)] @@ -315,6 +320,15 @@ impl CacheAccount { self.status = match self.status { AccountStatus::Loaded => { // Account that have nonce zero and empty code hash is considered to be fully in memory. + #[cfg(not(feature = "scroll"))] + if previous_info.as_ref().map(|a| (a.code_hash, a.nonce)) == Some((KECCAK_EMPTY, 0)) + { + AccountStatus::InMemoryChange + } else { + AccountStatus::Changed + } + + #[cfg(feature = "scroll")] if previous_info.as_ref().map(|a| (a.code_hash, a.nonce)) == Some((POSEIDON_EMPTY, 0)) { @@ -377,13 +391,20 @@ impl CacheAccount { self.status = match self.status { AccountStatus::Loaded => { + #[cfg(not(feature = "scroll"))] + if previous_info.as_ref().map(|a| (a.code_hash, a.nonce)) == Some((KECCAK_EMPTY, 0)) + { + AccountStatus::InMemoryChange + } else { + AccountStatus::Changed + } + + #[cfg(feature = "scroll")] if previous_info.as_ref().map(|a| (a.code_hash, a.nonce)) == Some((POSEIDON_EMPTY, 0)) { - // account is fully in memory AccountStatus::InMemoryChange } else { - // can be contract and some of storage slots can be present inside db. AccountStatus::Changed } } diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 8972cf5f1a..1f009166e7 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -728,6 +728,9 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, }); } }; + #[cfg(not(feature = "scroll"))] + let code_hash = account.info.code_hash(); + #[cfg(feature = "scroll")] let code_hash = account.info.keccak_code_hash(); let bytecode = account.info.code.clone().unwrap_or_default(); @@ -889,7 +892,10 @@ impl<'a, GSPEC: Spec, DB: Database + 'a, const INSPECT: bool> Host return Some((B256::zero(), is_cold)); } - Some((acc.info.keccak_code_hash, is_cold)) + #[cfg(not(feature = "scroll"))] + return Some((acc.info.code_hash, is_cold)); + #[cfg(feature = "scroll")] + return Some((acc.info.keccak_code_hash(), is_cold)); } fn sload(&mut self, address: B160, index: U256) -> Option<(U256, bool)> { diff --git a/crates/revm/src/journaled_state.rs b/crates/revm/src/journaled_state.rs index 26ec4f793f..7316f12149 100644 --- a/crates/revm/src/journaled_state.rs +++ b/crates/revm/src/journaled_state.rs @@ -1,12 +1,15 @@ use crate::interpreter::{inner_models::SelfDestructResult, InstructionResult}; use crate::primitives::{ db::Database, hash_map::Entry, Account, Bytecode, HashMap, Log, Spec, SpecId::*, State, - StorageSlot, TransientStorage, B160, KECCAK_EMPTY, POSEIDON_EMPTY, PRECOMPILE3, U256, + StorageSlot, TransientStorage, B160, KECCAK_EMPTY, PRECOMPILE3, U256, }; use alloc::vec::Vec; use core::mem; use revm_interpreter::primitives::SpecId; +#[cfg(feature = "scroll")] +use crate::primitives::POSEIDON_EMPTY; + #[derive(Debug, Clone, Eq, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct JournaledState { @@ -164,8 +167,15 @@ impl JournaledState { .unwrap() .push(JournalEntry::CodeChange { address }); - account.info.code_hash = code.poseidon_hash_slow(); - account.info.keccak_code_hash = code.keccak_hash_slow(); + #[cfg(not(feature = "scroll"))] + { + account.info.code_hash = code.hash_slow(); + } + #[cfg(feature = "scroll")] + { + account.info.code_hash = code.poseidon_hash_slow(); + account.info.keccak_code_hash = code.keccak_hash_slow(); + } account.info.code = Some(code); } @@ -316,6 +326,11 @@ impl JournaledState { num_of_precompiles: usize, ) -> bool { // Check collision. Bytecode needs to be empty. + #[cfg(not(feature = "scroll"))] + if account.info.code_hash != KECCAK_EMPTY { + return true; + } + #[cfg(feature = "scroll")] if account.info.code_hash != POSEIDON_EMPTY && account.info.keccak_code_hash != KECCAK_EMPTY { return true; @@ -417,8 +432,15 @@ impl JournaledState { } JournalEntry::CodeChange { address } => { let acc = state.get_mut(&address).unwrap(); - acc.info.code_hash = POSEIDON_EMPTY; - acc.info.keccak_code_hash = KECCAK_EMPTY; + #[cfg(not(feature = "scroll"))] + { + acc.info.code_hash = KECCAK_EMPTY; + } + #[cfg(feature = "scroll")] + { + acc.info.code_hash = POSEIDON_EMPTY; + acc.info.keccak_code_hash = KECCAK_EMPTY; + } acc.info.code = None; } } @@ -540,7 +562,12 @@ impl JournaledState { ) -> Result<&mut Account, DB::Error> { let account = self.initial_account_load(address, &[], db)?; if account.info.code.is_none() { - if account.info.code_hash == POSEIDON_EMPTY { + #[cfg(not(feature = "scroll"))] + let is_empty = account.info.code_hash == KECCAK_EMPTY; + #[cfg(feature = "scroll")] + let is_empty = account.info.code_hash == POSEIDON_EMPTY; + + if is_empty { account.info.code = Some(Bytecode::new()); } else { // load code if requested @@ -632,7 +659,12 @@ impl JournaledState { ) -> Result<(&mut Account, bool), DB::Error> { let (acc, is_cold) = self.load_account(address, db)?; if acc.info.code.is_none() { - if acc.info.code_hash == POSEIDON_EMPTY { + #[cfg(not(feature = "scroll"))] + let is_empty = acc.info.code_hash == KECCAK_EMPTY; + #[cfg(feature = "scroll")] + let is_empty = acc.info.code_hash == POSEIDON_EMPTY; + + if is_empty { let empty = Bytecode::new(); acc.info.code = Some(empty); } else { From 391421f2763d358196144a4a5b8335934c2555ae Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 17 Apr 2024 14:06:27 +0800 Subject: [PATCH 05/24] add scroll handler --- crates/revm/src/handler.rs | 13 ++++++ crates/revm/src/handler/mainnet.rs | 11 +++-- crates/revm/src/handler/scroll.rs | 72 ++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 crates/revm/src/handler/scroll.rs diff --git a/crates/revm/src/handler.rs b/crates/revm/src/handler.rs index 9a5e2f5b2f..ae82942fd8 100644 --- a/crates/revm/src/handler.rs +++ b/crates/revm/src/handler.rs @@ -1,6 +1,8 @@ pub mod mainnet; #[cfg(feature = "optimism")] pub mod optimism; +#[cfg(feature = "scroll")] +pub mod scroll; use revm_interpreter::primitives::db::Database; use revm_interpreter::primitives::{EVMError, EVMResultGeneric}; @@ -45,6 +47,17 @@ impl Handler { } } + /// Handler for the scroll + #[cfg(feature = "scroll")] + pub fn scroll() -> Self { + Self { + call_return: scroll::handle_call_return::, + calculate_gas_refund: scroll::calculate_gas_refund::, + reimburse_caller: scroll::handle_reimburse_caller::, + reward_beneficiary: scroll::reward_beneficiary::, + } + } + /// Handler for the optimism #[cfg(feature = "optimism")] pub fn optimism() -> Self { diff --git a/crates/revm/src/handler/mainnet.rs b/crates/revm/src/handler/mainnet.rs index 0565756b59..24f1722720 100644 --- a/crates/revm/src/handler/mainnet.rs +++ b/crates/revm/src/handler/mainnet.rs @@ -68,12 +68,11 @@ pub fn reward_beneficiary( // transfer fee to coinbase/beneficiary. // EIP-1559 discard basefee for coinbase transfer. Basefee amount of gas is discarded. - // let coinbase_gas_price = if SPEC::enabled(LONDON) { - // effective_gas_price.saturating_sub(data.env.block.basefee) - // } else { - // effective_gas_price - // }; - let coinbase_gas_price = effective_gas_price; + let coinbase_gas_price = if SPEC::enabled(LONDON) { + effective_gas_price.saturating_sub(data.env.block.basefee) + } else { + effective_gas_price + }; let (coinbase_account, _) = data .journaled_state diff --git a/crates/revm/src/handler/scroll.rs b/crates/revm/src/handler/scroll.rs new file mode 100644 index 0000000000..16052e79b5 --- /dev/null +++ b/crates/revm/src/handler/scroll.rs @@ -0,0 +1,72 @@ +//! Scroll related handlers. +use revm_interpreter::primitives::EVMError; + +use crate::{ + interpreter::{return_ok, return_revert, Gas, InstructionResult}, + primitives::{db::Database, Env, Spec, SpecId::LONDON, U256}, + EVMData, +}; + +/// Handle output of the transaction +pub fn handle_call_return( + env: &Env, + call_result: InstructionResult, + returned_gas: Gas, +) -> Gas { + super::mainnet::handle_call_return(env, call_result, returned_gas) +} + +#[inline] +pub fn handle_reimburse_caller( + data: &mut EVMData<'_, DB>, + gas: &Gas, + gas_refund: u64, +) -> Result<(), EVMError> { + super::mainnet::handle_reimburse_caller(data, gas, gas_refund) +} + +/// Reward beneficiary with gas fee. +#[inline] +pub fn reward_beneficiary( + data: &mut EVMData<'_, DB>, + gas: &Gas, + gas_refund: u64, +) -> Result<(), EVMError> { + let beneficiary = data.env.block.coinbase; + let effective_gas_price = data.env.effective_gas_price(); + let l1_fee = data.env.tx.l1_fee; + + // transfer fee to coinbase/beneficiary. + // EIP-1559 discard basefee for coinbase transfer. Basefee amount of gas is discarded. + // let coinbase_gas_price = if SPEC::enabled(LONDON) { + // effective_gas_price.saturating_sub(data.env.block.basefee) + // } else { + // effective_gas_price + // }; + let coinbase_gas_price = effective_gas_price; + + let (coinbase_account, _) = data + .journaled_state + .load_account(beneficiary, data.db) + .map_err(EVMError::Database)?; + + coinbase_account.mark_touch(); + coinbase_account.info.balance = coinbase_account + .info + .balance + .saturating_add(coinbase_gas_price * U256::from(gas.spend() - gas_refund)) + .saturating_add(l1_fee); + + Ok(()) +} + +/// Calculate gas refund for transaction. +/// +/// If config is set to disable gas refund, it will return 0. +/// +/// If spec is set to london, it will decrease the maximum refund amount to 5th part of +/// gas spend. (Before london it was 2th part of gas spend) +#[inline] +pub fn calculate_gas_refund(env: &Env, gas: &Gas) -> u64 { + super::mainnet::calculate_gas_refund(env, gas) +} From c9858f7c4a6642fb2eaa1795e28351e68f9ab556 Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 17 Apr 2024 18:29:16 +0800 Subject: [PATCH 06/24] impl l1fee --- crates/primitives/src/env.rs | 3 - crates/revm/src/context/evm_context.rs | 4 +- crates/revm/src/context/inner_evm_context.rs | 11 ++- crates/revm/src/scroll.rs | 4 +- crates/revm/src/scroll/handler_register.rs | 78 ++++++++++++-------- crates/revm/src/scroll/l1block.rs | 64 ++++++++++++++++ 6 files changed, 122 insertions(+), 42 deletions(-) create mode 100644 crates/revm/src/scroll/l1block.rs diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index ab76f7cddd..cd34f6bf7c 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -552,8 +552,6 @@ pub struct TxEnv { #[cfg_attr(feature = "serde", serde(flatten))] #[cfg(feature = "optimism")] pub optimism: OptimismFields, - - pub l1_fee: U256, } impl TxEnv { @@ -589,7 +587,6 @@ impl Default for TxEnv { max_fee_per_blob_gas: None, #[cfg(feature = "optimism")] optimism: OptimismFields::default(), - l1_fee: U256::ZERO, } } } diff --git a/crates/revm/src/context/evm_context.rs b/crates/revm/src/context/evm_context.rs index 3574a1b0bc..40fc892e2f 100644 --- a/crates/revm/src/context/evm_context.rs +++ b/crates/revm/src/context/evm_context.rs @@ -288,7 +288,7 @@ pub(crate) mod test_utils { journaled_state: JournaledState::new(SpecId::CANCUN, HashSet::new()), db, error: Ok(()), - #[cfg(feature = "optimism")] + #[cfg(any(feature = "optimism", feature = "scroll"))] l1_block_info: None, }, precompiles: ContextPrecompiles::default(), @@ -303,7 +303,7 @@ pub(crate) mod test_utils { journaled_state: JournaledState::new(SpecId::CANCUN, HashSet::new()), db, error: Ok(()), - #[cfg(feature = "optimism")] + #[cfg(any(feature = "optimism", feature = "scroll"))] l1_block_info: None, }, precompiles: ContextPrecompiles::default(), diff --git a/crates/revm/src/context/inner_evm_context.rs b/crates/revm/src/context/inner_evm_context.rs index 98a4d5679b..ac16033c4b 100644 --- a/crates/revm/src/context/inner_evm_context.rs +++ b/crates/revm/src/context/inner_evm_context.rs @@ -31,6 +31,9 @@ pub struct InnerEvmContext { /// Used as temporary value holder to store L1 block info. #[cfg(feature = "optimism")] pub l1_block_info: Option, + /// Used as temporary value holder to store L1 block info. + #[cfg(feature = "scroll")] + pub l1_block_info: Option, } impl Clone for InnerEvmContext @@ -43,7 +46,7 @@ where journaled_state: self.journaled_state.clone(), db: self.db.clone(), error: self.error.clone(), - #[cfg(feature = "optimism")] + #[cfg(any(feature = "optimism", feature = "scroll"))] l1_block_info: self.l1_block_info.clone(), } } @@ -56,7 +59,7 @@ impl InnerEvmContext { journaled_state: JournaledState::new(SpecId::LATEST, HashSet::new()), db, error: Ok(()), - #[cfg(feature = "optimism")] + #[cfg(any(feature = "optimism", feature = "scroll"))] l1_block_info: None, } } @@ -69,7 +72,7 @@ impl InnerEvmContext { journaled_state: JournaledState::new(SpecId::LATEST, HashSet::new()), db, error: Ok(()), - #[cfg(feature = "optimism")] + #[cfg(any(feature = "optimism", feature = "scroll"))] l1_block_info: None, } } @@ -84,7 +87,7 @@ impl InnerEvmContext { journaled_state: self.journaled_state, db, error: Ok(()), - #[cfg(feature = "optimism")] + #[cfg(any(feature = "optimism", feature = "scroll"))] l1_block_info: self.l1_block_info, } } diff --git a/crates/revm/src/scroll.rs b/crates/revm/src/scroll.rs index 4b6343b647..d8b4959184 100644 --- a/crates/revm/src/scroll.rs +++ b/crates/revm/src/scroll.rs @@ -1,5 +1,7 @@ mod handler_register; +mod l1block; pub use crate::scroll::handler_register::{ - deduct_caller, reward_beneficiary, scroll_handle_register, + deduct_caller, load_accounts, reward_beneficiary, scroll_handle_register, }; +pub use crate::scroll::l1block::{L1BlockInfo, L1_GAS_PRICE_ORACLE_ADDRESS}; diff --git a/crates/revm/src/scroll/handler_register.rs b/crates/revm/src/scroll/handler_register.rs index 7f9cc76736..2db833b35f 100644 --- a/crates/revm/src/scroll/handler_register.rs +++ b/crates/revm/src/scroll/handler_register.rs @@ -1,15 +1,20 @@ //! Handler related to Scroll chain +use crate::handler::mainnet; +use crate::handler::mainnet::deduct_caller_inner; +use crate::primitives::InvalidTransaction; use crate::{ handler::register::EvmHandler, interpreter::Gas, - primitives::{db::Database, spec_to_generic, EVMError, Spec, SpecId, TransactTo, CANCUN, U256}, + primitives::{db::Database, spec_to_generic, EVMError, Spec, SpecId, U256}, Context, }; use std::sync::Arc; pub fn scroll_handle_register(handler: &mut EvmHandler<'_, EXT, DB>) { spec_to_generic!(handler.cfg.spec_id, { + // load l1 data + handler.pre_execution.load_accounts = Arc::new(load_accounts::); // l1_fee is added to the gas cost. handler.pre_execution.deduct_caller = Arc::new(deduct_caller::); // basefee is sent to coinbase @@ -17,12 +22,23 @@ pub fn scroll_handle_register(handler: &mut EvmHandler<'_, EX }); } +/// Load account (make them warm) and l1 data from database. +#[inline] +pub fn load_accounts( + context: &mut Context, +) -> Result<(), EVMError> { + let l1_block_info = crate::scroll::L1BlockInfo::try_fetch(&mut context.evm.inner.db) + .map_err(EVMError::Database)?; + context.evm.inner.l1_block_info = Some(l1_block_info); + + mainnet::load_accounts::(context) +} + /// Deducts the caller balance to the transaction limit. #[inline] pub fn deduct_caller( context: &mut Context, ) -> Result<(), EVMError> { - let l1_fee = context.evm.env.tx.l1_fee; // load caller's account. let (caller_account, _) = context .evm @@ -30,36 +46,27 @@ pub fn deduct_caller( .journaled_state .load_account(context.evm.inner.env.tx.caller, &mut context.evm.inner.db)?; - // deduct gas cost from caller's account. - // Subtract gas costs from the caller's account. - // We need to saturate the gas cost to prevent underflow in case that `disable_balance_check` is enabled. - let mut gas_cost = U256::from(context.evm.inner.env.tx.gas_limit) - .saturating_mul(context.evm.inner.env.effective_gas_price()); + // We deduct caller max balance after minting and before deducing the + // l1 cost, max values is already checked in pre_validate but l1 cost wasn't. + deduct_caller_inner::(caller_account, &context.evm.inner.env); - gas_cost = gas_cost.saturating_add(l1_fee); - - // EIP-4844 - if SPEC::enabled(CANCUN) { - let data_fee = context - .evm - .inner - .env - .calc_data_fee() - .expect("already checked"); - gas_cost = gas_cost.saturating_add(data_fee); - } - - // set new caller account balance. - caller_account.info.balance = caller_account.info.balance.saturating_sub(gas_cost); - - // bump the nonce for calls. Nonce for CREATE will be bumped in `handle_create`. - if matches!(context.evm.inner.env.tx.transact_to, TransactTo::Call(_)) { - // Nonce is already checked - caller_account.info.nonce = caller_account.info.nonce.saturating_add(1); + // Deduct l1 fee from caller. + let tx_l1_cost = context + .evm + .inner + .l1_block_info + .as_ref() + .expect("L1BlockInfo should be loaded") + .calculate_tx_l1_cost(&context.evm.inner.env.tx.data); + if tx_l1_cost.gt(&caller_account.info.balance) { + return Err(EVMError::Transaction( + InvalidTransaction::LackOfFundForMaxFee { + fee: tx_l1_cost.into(), + balance: caller_account.info.balance.into(), + }, + )); } - - // touch account so we know it is changed. - caller_account.mark_touch(); + caller_account.info.balance = caller_account.info.balance.saturating_sub(tx_l1_cost); Ok(()) } @@ -71,7 +78,6 @@ pub fn reward_beneficiary( ) -> Result<(), EVMError> { let beneficiary = context.evm.env.block.coinbase; let effective_gas_price = context.evm.env.effective_gas_price(); - let l1_fee = context.evm.env.tx.l1_fee; // transfer fee to coinbase/beneficiary. let coinbase_gas_price = effective_gas_price; @@ -82,12 +88,20 @@ pub fn reward_beneficiary( .journaled_state .load_account(beneficiary, &mut context.evm.inner.db)?; + let Some(l1_block_info) = &context.evm.inner.l1_block_info else { + return Err(EVMError::Custom( + "[SCROLL] Failed to load L1 block information.".to_string(), + )); + }; + + let l1_cost = l1_block_info.calculate_tx_l1_cost(&context.evm.inner.env.tx.data); + coinbase_account.mark_touch(); coinbase_account.info.balance = coinbase_account .info .balance .saturating_add(coinbase_gas_price * U256::from(gas.spent() - gas.refunded() as u64)) - .saturating_add(l1_fee); + .saturating_add(l1_cost); Ok(()) } diff --git a/crates/revm/src/scroll/l1block.rs b/crates/revm/src/scroll/l1block.rs new file mode 100644 index 0000000000..f9f5c49d28 --- /dev/null +++ b/crates/revm/src/scroll/l1block.rs @@ -0,0 +1,64 @@ +use crate::primitives::{address, Address, U256}; +use crate::Database; + +const ZERO_BYTE_COST: u64 = 4; +const NON_ZERO_BYTE_COST: u64 = 16; + +const TX_L1_COMMIT_EXTRA_COST: U256 = U256::from_limbs([64u64, 0, 0, 0]); +const TX_L1_FEE_PRECISION: U256 = U256::from_limbs([1_000_000_000u64, 0, 0, 0]); + +pub const L1_GAS_PRICE_ORACLE_ADDRESS: Address = + address!("5300000000000000000000000000000000000002"); + +const L1_BASE_FEE_SLOT: U256 = U256::from_limbs([1u64, 0, 0, 0]); +const L1_OVERHEAD_SLOT: U256 = U256::from_limbs([2u64, 0, 0, 0]); +const L1_SCALAR_SLOT: U256 = U256::from_limbs([3u64, 0, 0, 0]); + +/// L1 block info +#[derive(Clone, Debug, Default)] +pub struct L1BlockInfo { + /// The base fee of the L1 origin block. + pub l1_base_fee: U256, + /// The current L1 fee overhead. + pub l1_fee_overhead: U256, + /// The current L1 fee scalar. + pub l1_base_fee_scalar: U256, +} + +impl L1BlockInfo { + /// Try to fetch the L1 block info from the database. + pub fn try_fetch(db: &mut DB) -> Result { + let l1_base_fee = db.storage(L1_GAS_PRICE_ORACLE_ADDRESS, L1_BASE_FEE_SLOT)?; + let l1_fee_overhead = db.storage(L1_GAS_PRICE_ORACLE_ADDRESS, L1_OVERHEAD_SLOT)?; + let l1_base_fee_scalar = db.storage(L1_GAS_PRICE_ORACLE_ADDRESS, L1_SCALAR_SLOT)?; + + Ok(L1BlockInfo { + l1_base_fee, + l1_fee_overhead, + l1_base_fee_scalar, + }) + } + + /// Calculate the data gas for posting the transaction on L1. Calldata costs 16 gas per non-zero + /// byte and 4 gas per zero byte. + pub fn data_gas(&self, input: &[u8]) -> U256 { + U256::from(input.iter().fold(0, |acc, byte| { + acc + if *byte == 0x00 { + ZERO_BYTE_COST + } else { + NON_ZERO_BYTE_COST + } + })) + } + + /// Calculate the gas cost of a transaction based on L1 block data posted on L2, depending on the [SpecId] passed. + pub fn calculate_tx_l1_cost(&self, input: &[u8]) -> U256 { + let tx_l1_gas = self.data_gas(input); + tx_l1_gas + .saturating_add(self.l1_fee_overhead) + .saturating_add(TX_L1_COMMIT_EXTRA_COST) + .saturating_mul(self.l1_base_fee) + .saturating_mul(self.l1_base_fee_scalar) + .wrapping_div(TX_L1_FEE_PRECISION) + } +} From 337a2f8bf97c389163e4c8e704cf089b07237bbf Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 17 Apr 2024 21:48:46 +0800 Subject: [PATCH 07/24] disable some precompiles --- crates/precompile/src/disable.rs | 20 ++++++++++++++++ crates/precompile/src/lib.rs | 27 ++++++++++++++++++++++ crates/revm/src/scroll.rs | 2 +- crates/revm/src/scroll/handler_register.rs | 14 ++++++++++- 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 crates/precompile/src/disable.rs diff --git a/crates/precompile/src/disable.rs b/crates/precompile/src/disable.rs new file mode 100644 index 0000000000..3c353be846 --- /dev/null +++ b/crates/precompile/src/disable.rs @@ -0,0 +1,20 @@ +use crate::{Bytes, PrecompileWithAddress}; +use revm_primitives::{Precompile, PrecompileError, PrecompileResult}; + +pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress( + crate::u64_to_address(0x03), + Precompile::Standard(disable_run), +); +pub const BLAKE2F: PrecompileWithAddress = PrecompileWithAddress( + crate::u64_to_address(0x09), + Precompile::Standard(disable_run), +); +pub const POINT_EVALUATION: PrecompileWithAddress = PrecompileWithAddress( + crate::u64_to_address(0x0A), + Precompile::Standard(disable_run), +); + +/// Always Out of Gas +pub fn disable_run(_input: &Bytes, _gas_limit: u64) -> PrecompileResult { + Err(PrecompileError::OutOfGas) +} diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index fd34bd6358..54a53fbc5e 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -12,6 +12,7 @@ extern crate alloc as std; pub mod blake2; pub mod bn128; +pub mod disable; pub mod hash; pub mod identity; #[cfg(feature = "c-kzg")] @@ -156,6 +157,32 @@ impl Precompiles { }) } + /// Returns precompiles for Scroll + #[cfg(feature = "scroll")] + pub fn scroll(spec: PrecompileSpecId) -> &'static Self { + static INSTANCE: OnceBox = OnceBox::new(); + INSTANCE.get_or_init(|| { + let mut precompiles = Precompiles::default(); + precompiles.extend([ + secp256k1::ECRECOVER, // 0x01 + hash::SHA256, // 0x02 + disable::RIPEMD160, // 0x03 + identity::FUN, // 0x04 + modexp::BERLIN, // 0x05 + bn128::add::ISTANBUL, // 0x06 + bn128::mul::ISTANBUL, // 0x07 + bn128::pair::ISTANBUL, // 0x08 + disable::BLAKE2F, // 0x09 + ]); + if spec >= PrecompileSpecId::CANCUN { + precompiles.extend([ + disable::POINT_EVALUATION, // 0x0A + ]); + } + Box::new(precompiles) + }) + } + /// Returns the precompiles for the latest spec. pub fn latest() -> &'static Self { Self::cancun() diff --git a/crates/revm/src/scroll.rs b/crates/revm/src/scroll.rs index d8b4959184..294b9ea6a3 100644 --- a/crates/revm/src/scroll.rs +++ b/crates/revm/src/scroll.rs @@ -2,6 +2,6 @@ mod handler_register; mod l1block; pub use crate::scroll::handler_register::{ - deduct_caller, load_accounts, reward_beneficiary, scroll_handle_register, + deduct_caller, load_accounts, load_precompiles, reward_beneficiary, scroll_handle_register, }; pub use crate::scroll::l1block::{L1BlockInfo, L1_GAS_PRICE_ORACLE_ADDRESS}; diff --git a/crates/revm/src/scroll/handler_register.rs b/crates/revm/src/scroll/handler_register.rs index 2db833b35f..0510ffc106 100644 --- a/crates/revm/src/scroll/handler_register.rs +++ b/crates/revm/src/scroll/handler_register.rs @@ -6,13 +6,17 @@ use crate::primitives::InvalidTransaction; use crate::{ handler::register::EvmHandler, interpreter::Gas, + precompile::Precompiles, primitives::{db::Database, spec_to_generic, EVMError, Spec, SpecId, U256}, - Context, + Context, ContextPrecompiles, }; +use revm_precompile::PrecompileSpecId; use std::sync::Arc; pub fn scroll_handle_register(handler: &mut EvmHandler<'_, EXT, DB>) { spec_to_generic!(handler.cfg.spec_id, { + // load precompiles + handler.pre_execution.load_precompiles = Arc::new(load_precompiles::); // load l1 data handler.pre_execution.load_accounts = Arc::new(load_accounts::); // l1_fee is added to the gas cost. @@ -22,6 +26,14 @@ pub fn scroll_handle_register(handler: &mut EvmHandler<'_, EX }); } +/// Main precompile load +#[inline] +pub fn load_precompiles() -> ContextPrecompiles { + Precompiles::scroll(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID)) + .clone() + .into() +} + /// Load account (make them warm) and l1 data from database. #[inline] pub fn load_accounts( From 591ee51a7089cf0ae7de873701a4caa2252d64e1 Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 17 Apr 2024 22:14:25 +0800 Subject: [PATCH 08/24] disable SELFDESTRUCT --- crates/interpreter/src/instructions/host.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/interpreter/src/instructions/host.rs b/crates/interpreter/src/instructions/host.rs index 7357adf4f3..4fabd85b04 100644 --- a/crates/interpreter/src/instructions/host.rs +++ b/crates/interpreter/src/instructions/host.rs @@ -227,6 +227,7 @@ pub fn log(interpreter: &mut Interpreter, host: &mut H) host.log(log); } +#[cfg(not(feature = "scroll"))] pub fn selfdestruct(interpreter: &mut Interpreter, host: &mut H) { check_staticcall!(interpreter); pop_address!(interpreter, target); @@ -245,6 +246,11 @@ pub fn selfdestruct(interpreter: &mut Interpreter, host: &m interpreter.instruction_result = InstructionResult::SelfDestruct; } +#[cfg(feature = "scroll")] +pub fn selfdestruct(interpreter: &mut Interpreter, _host: &mut H) { + interpreter.instruction_result = InstructionResult::InvalidFEOpcode; +} + pub fn create( interpreter: &mut Interpreter, host: &mut H, From 476d7ed67f92a4129ec8026bb74a5836fcc22386 Mon Sep 17 00:00:00 2001 From: lightsing Date: Thu, 18 Apr 2024 11:36:19 +0800 Subject: [PATCH 09/24] BLOCKHASH --- crates/interpreter/src/instructions/host.rs | 21 +++++++++++++++++++++ crates/primitives/src/utilities.rs | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/interpreter/src/instructions/host.rs b/crates/interpreter/src/instructions/host.rs index 4fabd85b04..1d67d6a564 100644 --- a/crates/interpreter/src/instructions/host.rs +++ b/crates/interpreter/src/instructions/host.rs @@ -120,6 +120,7 @@ pub fn extcodecopy(interpreter: &mut Interpreter, host: &mu .set_data(memory_offset, code_offset, len, code.bytes()); } +#[cfg(not(feature = "scroll"))] pub fn blockhash(interpreter: &mut Interpreter, host: &mut H) { gas!(interpreter, gas::BLOCKHASH); pop_top!(interpreter, number); @@ -139,6 +140,26 @@ pub fn blockhash(interpreter: &mut Interpreter, host: &mut H) { *number = U256::ZERO; } +#[cfg(feature = "scroll")] +pub fn blockhash(interpreter: &mut Interpreter, host: &mut H) { + gas!(interpreter, gas::BLOCKHASH); + pop_top!(interpreter, number); + + let number64 = as_usize_or_fail!(interpreter, number); + if let Some(diff) = host.env().block.number.checked_sub(*number) { + let diff = as_usize_saturated!(diff); + // blockhash should push zero if number is same as current block number. + if diff <= BLOCK_HASH_HISTORY && diff != 0 { + let mut hasher = crate::primitives::Keccak256::new(); + hasher.update(host.env().cfg.chain_id.to_be_bytes()); + hasher.update(number64.to_be_bytes()); + *number = U256::from_be_bytes(*hasher.finalize()); + return; + } + } + *number = U256::ZERO; +} + pub fn sload(interpreter: &mut Interpreter, host: &mut H) { pop!(interpreter, index); diff --git a/crates/primitives/src/utilities.rs b/crates/primitives/src/utilities.rs index e4562b1b3c..dfb63c0c03 100644 --- a/crates/primitives/src/utilities.rs +++ b/crates/primitives/src/utilities.rs @@ -1,7 +1,7 @@ use crate::{ b256, B256, BLOB_GASPRICE_UPDATE_FRACTION, MIN_BLOB_GASPRICE, TARGET_BLOB_GAS_PER_BLOCK, }; -pub use alloy_primitives::keccak256; +pub use alloy_primitives::{keccak256, Keccak256}; #[cfg(feature = "scroll")] use crate::U256; From fddc9dd52679dad4de50916222dff2956b60c7a3 Mon Sep 17 00:00:00 2001 From: lightsing Date: Thu, 18 Apr 2024 12:22:46 +0800 Subject: [PATCH 10/24] fix modexp --- crates/precompile/src/lib.rs | 2 +- crates/precompile/src/modexp.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 54a53fbc5e..3a5e75b193 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -168,7 +168,7 @@ impl Precompiles { hash::SHA256, // 0x02 disable::RIPEMD160, // 0x03 identity::FUN, // 0x04 - modexp::BERLIN, // 0x05 + modexp::SCROLL, // 0x05 bn128::add::ISTANBUL, // 0x06 bn128::mul::ISTANBUL, // 0x07 bn128::pair::ISTANBUL, // 0x08 diff --git a/crates/precompile/src/modexp.rs b/crates/precompile/src/modexp.rs index a55b445912..969df36eed 100644 --- a/crates/precompile/src/modexp.rs +++ b/crates/precompile/src/modexp.rs @@ -15,6 +15,10 @@ pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( pub const BERLIN: PrecompileWithAddress = PrecompileWithAddress(crate::u64_to_address(5), Precompile::Standard(berlin_run)); +#[cfg(feature = "scroll")] +pub const SCROLL: PrecompileWithAddress = + PrecompileWithAddress(crate::u64_to_address(5), Precompile::Standard(scroll_run)); + /// See: /// See: pub fn byzantium_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { @@ -29,6 +33,17 @@ pub fn berlin_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { }) } +#[cfg(feature = "scroll")] +pub fn scroll_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { + // modexp temporarily only accepts inputs of 32 bytes (256 bits) or less + if input.len() > 32 { + return Err(Error::OutOfGas); + } + run_inner(input, gas_limit, 200, |a, b, c, d| { + byzantium_gas_calc(a, b, c, d) + }) +} + pub fn calculate_iteration_count(exp_length: u64, exp_highp: &U256) -> u64 { let mut iteration_count: u64 = 0; From 91433b30d06ff72926ede52190656c22a9b2c149 Mon Sep 17 00:00:00 2001 From: lightsing Date: Thu, 18 Apr 2024 12:37:33 +0800 Subject: [PATCH 11/24] fix modexp --- crates/precompile/src/lib.rs | 2 +- crates/precompile/src/modexp.rs | 31 ++++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 3a5e75b193..54a53fbc5e 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -168,7 +168,7 @@ impl Precompiles { hash::SHA256, // 0x02 disable::RIPEMD160, // 0x03 identity::FUN, // 0x04 - modexp::SCROLL, // 0x05 + modexp::BERLIN, // 0x05 bn128::add::ISTANBUL, // 0x06 bn128::mul::ISTANBUL, // 0x07 bn128::pair::ISTANBUL, // 0x08 diff --git a/crates/precompile/src/modexp.rs b/crates/precompile/src/modexp.rs index 969df36eed..2d97a14b0f 100644 --- a/crates/precompile/src/modexp.rs +++ b/crates/precompile/src/modexp.rs @@ -7,6 +7,9 @@ use aurora_engine_modexp::modexp; use core::cmp::{max, min}; use revm_primitives::Bytes; +#[cfg(feature = "scroll")] +const SCROLL_LEN_LIMIT: U256 = U256::from_limbs([32, 0, 0, 0]); + pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(5), Precompile::Standard(byzantium_run), @@ -15,10 +18,6 @@ pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( pub const BERLIN: PrecompileWithAddress = PrecompileWithAddress(crate::u64_to_address(5), Precompile::Standard(berlin_run)); -#[cfg(feature = "scroll")] -pub const SCROLL: PrecompileWithAddress = - PrecompileWithAddress(crate::u64_to_address(5), Precompile::Standard(scroll_run)); - /// See: /// See: pub fn byzantium_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { @@ -33,17 +32,6 @@ pub fn berlin_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { }) } -#[cfg(feature = "scroll")] -pub fn scroll_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { - // modexp temporarily only accepts inputs of 32 bytes (256 bits) or less - if input.len() > 32 { - return Err(Error::OutOfGas); - } - run_inner(input, gas_limit, 200, |a, b, c, d| { - byzantium_gas_calc(a, b, c, d) - }) -} - pub fn calculate_iteration_count(exp_length: u64, exp_highp: &U256) -> u64 { let mut iteration_count: u64 = 0; @@ -79,6 +67,19 @@ where let exp_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 32).into_owned()); let mod_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 64).into_owned()); + #[cfg(feature = "scroll")] + { + if base_len > SCROLL_LEN_LIMIT { + return Err(Error::ModexpBaseOverflow); + } + if exp_len > SCROLL_LEN_LIMIT { + return Err(Error::ModexpExpOverflow); + } + if mod_len > SCROLL_LEN_LIMIT { + return Err(Error::ModexpModOverflow); + } + } + // cast base and modulus to usize, it does not make sense to handle larger values let Ok(base_len) = usize::try_from(base_len) else { return Err(Error::ModexpBaseOverflow); From f6d4bd8bf1b8d52d45e3dec72ed24cf73a01c276 Mon Sep 17 00:00:00 2001 From: lightsing Date: Thu, 18 Apr 2024 13:48:03 +0800 Subject: [PATCH 12/24] fix poseidon hash --- crates/primitives/src/utilities.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/primitives/src/utilities.rs b/crates/primitives/src/utilities.rs index dfb63c0c03..cc657d8e02 100644 --- a/crates/primitives/src/utilities.rs +++ b/crates/primitives/src/utilities.rs @@ -66,7 +66,7 @@ pub fn poseidon(code: &[u8]) -> B256 { }; B256::from( - &U256::try_from_be_slice(h.to_repr().as_ref()) + &U256::try_from_le_slice(h.to_repr().as_ref()) .expect("infallible") .to_be_bytes(), ) From d06633a3ad4a4e25f5b21fd4f87f92061e49fa78 Mon Sep 17 00:00:00 2001 From: lightsing Date: Thu, 18 Apr 2024 14:35:03 +0800 Subject: [PATCH 13/24] cleanup --- crates/interpreter/src/instructions/host.rs | 44 +++++-------- crates/interpreter/src/instructions/opcode.rs | 7 ++- crates/precompile/src/blake2.rs | 8 ++- crates/precompile/src/hash.rs | 8 ++- crates/precompile/src/lib.rs | 31 +++++----- crates/precompile/src/modexp.rs | 41 ++++++++---- crates/primitives/src/specification.rs | 62 ++++++++++++++++++- crates/revm/src/builder.rs | 2 +- crates/revm/src/scroll.rs | 2 +- crates/revm/src/scroll/handler_register.rs | 14 +---- 10 files changed, 143 insertions(+), 76 deletions(-) diff --git a/crates/interpreter/src/instructions/host.rs b/crates/interpreter/src/instructions/host.rs index 1d67d6a564..d6c127f241 100644 --- a/crates/interpreter/src/instructions/host.rs +++ b/crates/interpreter/src/instructions/host.rs @@ -120,8 +120,7 @@ pub fn extcodecopy(interpreter: &mut Interpreter, host: &mu .set_data(memory_offset, code_offset, len, code.bytes()); } -#[cfg(not(feature = "scroll"))] -pub fn blockhash(interpreter: &mut Interpreter, host: &mut H) { +pub fn blockhash(interpreter: &mut Interpreter, host: &mut H) { gas!(interpreter, gas::BLOCKHASH); pop_top!(interpreter, number); @@ -129,6 +128,15 @@ pub fn blockhash(interpreter: &mut Interpreter, host: &mut H) { let diff = as_usize_saturated!(diff); // blockhash should push zero if number is same as current block number. if diff <= BLOCK_HASH_HISTORY && diff != 0 { + #[cfg(feature = "scroll")] + if SPEC::enabled(BERNOULLI) { + let number64 = as_usize_or_fail!(interpreter, number); + let mut hasher = crate::primitives::Keccak256::new(); + hasher.update(host.env().cfg.chain_id.to_be_bytes()); + hasher.update(number64.to_be_bytes()); + *number = U256::from_be_bytes(*hasher.finalize()); + return; + } let Some(hash) = host.block_hash(*number) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; @@ -140,26 +148,6 @@ pub fn blockhash(interpreter: &mut Interpreter, host: &mut H) { *number = U256::ZERO; } -#[cfg(feature = "scroll")] -pub fn blockhash(interpreter: &mut Interpreter, host: &mut H) { - gas!(interpreter, gas::BLOCKHASH); - pop_top!(interpreter, number); - - let number64 = as_usize_or_fail!(interpreter, number); - if let Some(diff) = host.env().block.number.checked_sub(*number) { - let diff = as_usize_saturated!(diff); - // blockhash should push zero if number is same as current block number. - if diff <= BLOCK_HASH_HISTORY && diff != 0 { - let mut hasher = crate::primitives::Keccak256::new(); - hasher.update(host.env().cfg.chain_id.to_be_bytes()); - hasher.update(number64.to_be_bytes()); - *number = U256::from_be_bytes(*hasher.finalize()); - return; - } - } - *number = U256::ZERO; -} - pub fn sload(interpreter: &mut Interpreter, host: &mut H) { pop!(interpreter, index); @@ -248,11 +236,16 @@ pub fn log(interpreter: &mut Interpreter, host: &mut H) host.log(log); } -#[cfg(not(feature = "scroll"))] pub fn selfdestruct(interpreter: &mut Interpreter, host: &mut H) { check_staticcall!(interpreter); pop_address!(interpreter, target); + #[cfg(feature = "scroll")] + if SPEC::enabled(BERNOULLI) { + interpreter.instruction_result = InstructionResult::InvalidFEOpcode; + return; + } + let Some(res) = host.selfdestruct(interpreter.contract.address, target) else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; @@ -267,11 +260,6 @@ pub fn selfdestruct(interpreter: &mut Interpreter, host: &m interpreter.instruction_result = InstructionResult::SelfDestruct; } -#[cfg(feature = "scroll")] -pub fn selfdestruct(interpreter: &mut Interpreter, _host: &mut H) { - interpreter.instruction_result = InstructionResult::InvalidFEOpcode; -} - pub fn create( interpreter: &mut Interpreter, host: &mut H, diff --git a/crates/interpreter/src/instructions/opcode.rs b/crates/interpreter/src/instructions/opcode.rs index c935775a4e..872a52aa7e 100644 --- a/crates/interpreter/src/instructions/opcode.rs +++ b/crates/interpreter/src/instructions/opcode.rs @@ -235,7 +235,7 @@ opcodes! { 0x3D => RETURNDATASIZE => system::returndatasize::, 0x3E => RETURNDATACOPY => system::returndatacopy::, 0x3F => EXTCODEHASH => host::extcodehash::, - 0x40 => BLOCKHASH => host::blockhash, + 0x40 => BLOCKHASH => host::blockhash::, 0x41 => COINBASE => host_env::coinbase, 0x42 => TIMESTAMP => host_env::timestamp, 0x43 => NUMBER => host_env::number, @@ -947,6 +947,11 @@ pub const fn spec_opcode_gas(spec_id: SpecId) -> &'static [OpInfo; 256] { const TABLE: &[OpInfo;256] = &make_gas_table(SpecId::ECOTONE); TABLE } + #[cfg(feature = "scroll")] + SpecId::BERNOULLI => { + const TABLE: &[OpInfo;256] = &make_gas_table(SpecId::BERNOULLI); + TABLE + } } }; } diff --git a/crates/precompile/src/blake2.rs b/crates/precompile/src/blake2.rs index 48b566bb92..5d2af7254c 100644 --- a/crates/precompile/src/blake2.rs +++ b/crates/precompile/src/blake2.rs @@ -1,5 +1,5 @@ use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress}; -use revm_primitives::Bytes; +use revm_primitives::{Bytes, PrecompileError}; const F_ROUND: u64 = 1; const INPUT_LENGTH: usize = 213; @@ -7,6 +7,12 @@ const INPUT_LENGTH: usize = 213; pub const FUN: PrecompileWithAddress = PrecompileWithAddress(crate::u64_to_address(9), Precompile::Standard(run)); +#[cfg(feature = "scroll")] +pub const BERNOULLI: PrecompileWithAddress = PrecompileWithAddress( + crate::u64_to_address(9), + Precompile::Standard(|_input: &Bytes, _gas_limit: u64| Err(PrecompileError::OutOfGas)), +); + /// reference: /// input format: /// [4 bytes for rounds][64 bytes for h][128 bytes for m][8 bytes for t_0][8 bytes for t_1][1 byte for f] diff --git a/crates/precompile/src/hash.rs b/crates/precompile/src/hash.rs index 9fc6a8ae49..17a79ab636 100644 --- a/crates/precompile/src/hash.rs +++ b/crates/precompile/src/hash.rs @@ -1,6 +1,6 @@ use super::calc_linear_cost_u32; use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress}; -use revm_primitives::Bytes; +use revm_primitives::{Bytes, PrecompileError}; use sha2::Digest; pub const SHA256: PrecompileWithAddress = @@ -11,6 +11,12 @@ pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress( Precompile::Standard(ripemd160_run), ); +#[cfg(feature = "scroll")] +pub const RIPEMD160_BERNOULLI: PrecompileWithAddress = PrecompileWithAddress( + crate::u64_to_address(3), + Precompile::Standard(|_input: &Bytes, _gas_limit: u64| Err(PrecompileError::OutOfGas)), +); + /// See: /// See: /// See: diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 54a53fbc5e..8def77d09a 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -12,7 +12,6 @@ extern crate alloc as std; pub mod blake2; pub mod bn128; -pub mod disable; pub mod hash; pub mod identity; #[cfg(feature = "c-kzg")] @@ -65,6 +64,7 @@ impl Precompiles { PrecompileSpecId::BYZANTIUM => Self::byzantium(), PrecompileSpecId::ISTANBUL => Self::istanbul(), PrecompileSpecId::BERLIN => Self::berlin(), + PrecompileSpecId::BERNOULLI => Self::bernoulli(), PrecompileSpecId::CANCUN => Self::cancun(), PrecompileSpecId::LATEST => Self::latest(), } @@ -159,26 +159,21 @@ impl Precompiles { /// Returns precompiles for Scroll #[cfg(feature = "scroll")] - pub fn scroll(spec: PrecompileSpecId) -> &'static Self { + pub fn bernoulli() -> &'static Self { static INSTANCE: OnceBox = OnceBox::new(); INSTANCE.get_or_init(|| { let mut precompiles = Precompiles::default(); precompiles.extend([ - secp256k1::ECRECOVER, // 0x01 - hash::SHA256, // 0x02 - disable::RIPEMD160, // 0x03 - identity::FUN, // 0x04 - modexp::BERLIN, // 0x05 - bn128::add::ISTANBUL, // 0x06 - bn128::mul::ISTANBUL, // 0x07 - bn128::pair::ISTANBUL, // 0x08 - disable::BLAKE2F, // 0x09 + secp256k1::ECRECOVER, // 0x01 + hash::SHA256, // 0x02 + hash::RIPEMD160_BERNOULLI, // 0x03 + identity::FUN, // 0x04 + modexp::BERNOULLI, // 0x05 + bn128::add::ISTANBUL, // 0x06 + bn128::mul::ISTANBUL, // 0x07 + bn128::pair::ISTANBUL, // 0x08 + blake2::BERNOULLI, // 0x09 ]); - if spec >= PrecompileSpecId::CANCUN { - precompiles.extend([ - disable::POINT_EVALUATION, // 0x0A - ]); - } Box::new(precompiles) }) } @@ -257,6 +252,8 @@ pub enum PrecompileSpecId { BYZANTIUM, ISTANBUL, BERLIN, + #[cfg(feature = "scroll")] + BERNOULLI, CANCUN, LATEST, } @@ -278,6 +275,8 @@ impl PrecompileSpecId { BEDROCK | REGOLITH | CANYON => Self::BERLIN, #[cfg(feature = "optimism")] ECOTONE => Self::CANCUN, + #[cfg(feature = "scroll")] + BERNOULLI => Self::BERNOULLI, } } } diff --git a/crates/precompile/src/modexp.rs b/crates/precompile/src/modexp.rs index 2d97a14b0f..087da8a83a 100644 --- a/crates/precompile/src/modexp.rs +++ b/crates/precompile/src/modexp.rs @@ -18,6 +18,12 @@ pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( pub const BERLIN: PrecompileWithAddress = PrecompileWithAddress(crate::u64_to_address(5), Precompile::Standard(berlin_run)); +#[cfg(feature = "scroll")] +pub const BERNOULLI: PrecompileWithAddress = PrecompileWithAddress( + crate::u64_to_address(5), + Precompile::Standard(bernoilli_run), +); + /// See: /// See: pub fn byzantium_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { @@ -32,6 +38,28 @@ pub fn berlin_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { }) } +#[cfg(feature = "scroll")] +pub fn bernoilli_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { + let base_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 0).into_owned()); + let exp_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 32).into_owned()); + let mod_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 64).into_owned()); + + // modexp temporarily only accepts inputs of 32 bytes (256 bits) or less + if base_len > SCROLL_LEN_LIMIT { + return Err(Error::ModexpBaseOverflow); + } + if exp_len > SCROLL_LEN_LIMIT { + return Err(Error::ModexpExpOverflow); + } + if mod_len > SCROLL_LEN_LIMIT { + return Err(Error::ModexpModOverflow); + } + + run_inner(input, gas_limit, 200, |a, b, c, d| { + berlin_gas_calc(a, b, c, d) + }) +} + pub fn calculate_iteration_count(exp_length: u64, exp_highp: &U256) -> u64 { let mut iteration_count: u64 = 0; @@ -67,19 +95,6 @@ where let exp_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 32).into_owned()); let mod_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 64).into_owned()); - #[cfg(feature = "scroll")] - { - if base_len > SCROLL_LEN_LIMIT { - return Err(Error::ModexpBaseOverflow); - } - if exp_len > SCROLL_LEN_LIMIT { - return Err(Error::ModexpExpOverflow); - } - if mod_len > SCROLL_LEN_LIMIT { - return Err(Error::ModexpModOverflow); - } - } - // cast base and modulus to usize, it does not make sense to handle larger values let Ok(base_len) = usize::try_from(base_len) else { return Err(Error::ModexpBaseOverflow); diff --git a/crates/primitives/src/specification.rs b/crates/primitives/src/specification.rs index 6d75e25c44..607d208112 100644 --- a/crates/primitives/src/specification.rs +++ b/crates/primitives/src/specification.rs @@ -5,7 +5,7 @@ pub use SpecId::*; /// Specification IDs and their activation block. /// /// Information was obtained from the [Ethereum Execution Specifications](https://github.com/ethereum/execution-specs) -#[cfg(not(feature = "optimism"))] +#[cfg(not(any(feature = "optimism", feature = "scroll")))] #[repr(u8)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, enumn::N)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -66,6 +66,37 @@ pub enum SpecId { LATEST = u8::MAX, } +/// Specification IDs and their activation block. +/// +/// Information was obtained from the [Ethereum Execution Specifications](https://github.com/ethereum/execution-specs) +#[cfg(feature = "scroll")] +#[repr(u8)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, enumn::N)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum SpecId { + FRONTIER = 0, + FRONTIER_THAWING = 1, + HOMESTEAD = 2, + DAO_FORK = 3, + TANGERINE = 4, + SPURIOUS_DRAGON = 5, + BYZANTIUM = 6, + CONSTANTINOPLE = 7, + PETERSBURG = 8, + ISTANBUL = 9, + MUIR_GLACIER = 10, + BERLIN = 11, + LONDON = 12, + ARROW_GLACIER = 13, + GRAY_GLACIER = 14, + MERGE = 15, + SHANGHAI = 16, + BERNOULLI = 17, + CANCUN = 18, + #[default] + LATEST = u8::MAX, +} + impl SpecId { #[inline] pub fn try_from_u8(spec_id: u8) -> Option { @@ -107,6 +138,8 @@ impl From<&str> for SpecId { "Canyon" => SpecId::CANYON, #[cfg(feature = "optimism")] "Ecotone" => SpecId::ECOTONE, + #[cfg(feature = "scroll")] + "Bernoulli" => SpecId::BERNOULLI, _ => Self::LATEST, } } @@ -141,6 +174,8 @@ impl From for &'static str { SpecId::CANYON => "Canyon", #[cfg(feature = "optimism")] SpecId::ECOTONE => "Ecotone", + #[cfg(feature = "scroll")] + SpecId::BERNOULLI => "Bernoulli", SpecId::LATEST => "Latest", } } @@ -199,6 +234,10 @@ spec!(CANYON, CanyonSpec); #[cfg(feature = "optimism")] spec!(ECOTONE, EcotoneSpec); +// Scroll Hardforks +#[cfg(feature = "scroll")] +spec!(BERNOULLI, BernoulliSpec); + #[macro_export] macro_rules! spec_to_generic { ($spec_id:expr, $e:expr) => {{ @@ -278,6 +317,11 @@ macro_rules! spec_to_generic { use $crate::EcotoneSpec as SPEC; $e } + #[cfg(feature = "scroll")] + $crate::SpecId::BERNOULLI => { + use $crate::BernoulliSpec as SPEC; + $e + } } }}; } @@ -313,6 +357,8 @@ mod tests { spec_to_generic!(SHANGHAI, assert_eq!(SPEC::SPEC_ID, SHANGHAI)); #[cfg(feature = "optimism")] spec_to_generic!(CANYON, assert_eq!(SPEC::SPEC_ID, CANYON)); + #[cfg(feature = "scroll")] + spec_to_generic!(BERNOULLI, assert_eq!(SPEC::SPEC_ID, BERNOULLI)); spec_to_generic!(CANCUN, assert_eq!(SPEC::SPEC_ID, CANCUN)); spec_to_generic!(LATEST, assert_eq!(SPEC::SPEC_ID, LATEST)); } @@ -409,3 +455,17 @@ mod optimism_tests { assert!(SpecId::enabled(SpecId::ECOTONE, SpecId::ECOTONE)); } } + +#[cfg(feature = "scroll")] +#[cfg(test)] +mod scroll_tests { + use super::*; + + #[test] + fn test_bernoulli_post_merge_hardforks() { + assert!(BernoulliSpec::enabled(SpecId::MERGE)); + assert!(BernoulliSpec::enabled(SpecId::SHANGHAI)); + assert!(!BernoulliSpec::enabled(SpecId::CANCUN)); + assert!(!BernoulliSpec::enabled(SpecId::LATEST)); + } +} diff --git a/crates/revm/src/builder.rs b/crates/revm/src/builder.rs index 0e0dc18df9..4551c20b52 100644 --- a/crates/revm/src/builder.rs +++ b/crates/revm/src/builder.rs @@ -39,7 +39,7 @@ impl<'a> Default for EvmBuilder<'a, SetGenericStage, (), EmptyDB> { } else if #[cfg(all(feature = "scroll-default-handler", not(feature = "negate-scroll-default-handler")))] { - let mut handler_cfg = HandlerCfg::new(SpecId::LATEST); + let mut handler_cfg = HandlerCfg::new(SpecId::BERNOULLI); // set is_scroll to true by default. handler_cfg.is_scroll = true; diff --git a/crates/revm/src/scroll.rs b/crates/revm/src/scroll.rs index 294b9ea6a3..d8b4959184 100644 --- a/crates/revm/src/scroll.rs +++ b/crates/revm/src/scroll.rs @@ -2,6 +2,6 @@ mod handler_register; mod l1block; pub use crate::scroll::handler_register::{ - deduct_caller, load_accounts, load_precompiles, reward_beneficiary, scroll_handle_register, + deduct_caller, load_accounts, reward_beneficiary, scroll_handle_register, }; pub use crate::scroll::l1block::{L1BlockInfo, L1_GAS_PRICE_ORACLE_ADDRESS}; diff --git a/crates/revm/src/scroll/handler_register.rs b/crates/revm/src/scroll/handler_register.rs index 0510ffc106..2db833b35f 100644 --- a/crates/revm/src/scroll/handler_register.rs +++ b/crates/revm/src/scroll/handler_register.rs @@ -6,17 +6,13 @@ use crate::primitives::InvalidTransaction; use crate::{ handler::register::EvmHandler, interpreter::Gas, - precompile::Precompiles, primitives::{db::Database, spec_to_generic, EVMError, Spec, SpecId, U256}, - Context, ContextPrecompiles, + Context, }; -use revm_precompile::PrecompileSpecId; use std::sync::Arc; pub fn scroll_handle_register(handler: &mut EvmHandler<'_, EXT, DB>) { spec_to_generic!(handler.cfg.spec_id, { - // load precompiles - handler.pre_execution.load_precompiles = Arc::new(load_precompiles::); // load l1 data handler.pre_execution.load_accounts = Arc::new(load_accounts::); // l1_fee is added to the gas cost. @@ -26,14 +22,6 @@ pub fn scroll_handle_register(handler: &mut EvmHandler<'_, EX }); } -/// Main precompile load -#[inline] -pub fn load_precompiles() -> ContextPrecompiles { - Precompiles::scroll(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID)) - .clone() - .into() -} - /// Load account (make them warm) and l1 data from database. #[inline] pub fn load_accounts( From c554acfc47937aafece10dfef72bd61ad345b932 Mon Sep 17 00:00:00 2001 From: lightsing Date: Mon, 22 Apr 2024 10:33:32 +0800 Subject: [PATCH 14/24] use keccak code hash for evm --- crates/interpreter/src/host.rs | 4 ++++ crates/interpreter/src/host/dummy.rs | 16 ++++++++++++++++ crates/interpreter/src/instructions/host.rs | 8 +++++++- crates/precompile/src/blake2.rs | 5 ++++- crates/precompile/src/hash.rs | 5 ++++- crates/precompile/src/lib.rs | 1 + crates/revm/src/context/inner_evm_context.rs | 14 ++++++++++++++ crates/revm/src/evm.rs | 9 +++++++++ 8 files changed, 59 insertions(+), 3 deletions(-) diff --git a/crates/interpreter/src/host.rs b/crates/interpreter/src/host.rs index fff18bb848..052b618210 100644 --- a/crates/interpreter/src/host.rs +++ b/crates/interpreter/src/host.rs @@ -31,6 +31,10 @@ pub trait Host { /// Get code hash of `address` and if the account is cold. fn code_hash(&mut self, address: Address) -> Option<(B256, bool)>; + #[cfg(feature = "scroll")] + /// Get keccak code hash of `address` and if the account is cold. + fn keccak_code_hash(&mut self, address: Address) -> Option<(B256, bool)>; + /// Get storage value of `address` at `index` and if the account is cold. fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)>; diff --git a/crates/interpreter/src/host/dummy.rs b/crates/interpreter/src/host/dummy.rs index 519d3e06ee..64d940f302 100644 --- a/crates/interpreter/src/host/dummy.rs +++ b/crates/interpreter/src/host/dummy.rs @@ -5,6 +5,9 @@ use crate::{ }; use std::vec::Vec; +#[cfg(feature = "scroll")] +use revm_primitives::POSEIDON_EMPTY; + /// A dummy [Host] implementation. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct DummyHost { @@ -64,10 +67,23 @@ impl Host for DummyHost { } #[inline] + #[cfg(not(feature = "scroll"))] fn code_hash(&mut self, __address: Address) -> Option<(B256, bool)> { Some((KECCAK_EMPTY, false)) } + #[inline] + #[cfg(feature = "scroll")] + fn code_hash(&mut self, __address: Address) -> Option<(B256, bool)> { + Some((POSEIDON_EMPTY, false)) + } + + #[inline] + #[cfg(feature = "scroll")] + fn keccak_code_hash(&mut self, __address: Address) -> Option<(B256, bool)> { + Some((KECCAK_EMPTY, false)) + } + #[inline] fn sload(&mut self, __address: Address, index: U256) -> Option<(U256, bool)> { match self.storage.entry(index) { diff --git a/crates/interpreter/src/instructions/host.rs b/crates/interpreter/src/instructions/host.rs index d6c127f241..a4f2b7652b 100644 --- a/crates/interpreter/src/instructions/host.rs +++ b/crates/interpreter/src/instructions/host.rs @@ -72,7 +72,13 @@ pub fn extcodesize(interpreter: &mut Interpreter, host: &mu pub fn extcodehash(interpreter: &mut Interpreter, host: &mut H) { check!(interpreter, CONSTANTINOPLE); pop_address!(interpreter, address); - let Some((code_hash, is_cold)) = host.code_hash(address) else { + + #[cfg(not(feature = "scroll"))] + let result = host.code_hash(address); + #[cfg(feature = "scroll")] + let result = host.keccak_code_hash(address); + + let Some((code_hash, is_cold)) = result else { interpreter.instruction_result = InstructionResult::FatalExternalError; return; }; diff --git a/crates/precompile/src/blake2.rs b/crates/precompile/src/blake2.rs index 5d2af7254c..cf6d1f1b9e 100644 --- a/crates/precompile/src/blake2.rs +++ b/crates/precompile/src/blake2.rs @@ -1,5 +1,8 @@ use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress}; -use revm_primitives::{Bytes, PrecompileError}; +use revm_primitives::Bytes; + +#[cfg(feature = "scroll")] +use revm_primitives::PrecompileError; const F_ROUND: u64 = 1; const INPUT_LENGTH: usize = 213; diff --git a/crates/precompile/src/hash.rs b/crates/precompile/src/hash.rs index 17a79ab636..72ec600dc2 100644 --- a/crates/precompile/src/hash.rs +++ b/crates/precompile/src/hash.rs @@ -1,8 +1,11 @@ use super::calc_linear_cost_u32; use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress}; -use revm_primitives::{Bytes, PrecompileError}; +use revm_primitives::Bytes; use sha2::Digest; +#[cfg(feature = "scroll")] +use revm_primitives::PrecompileError; + pub const SHA256: PrecompileWithAddress = PrecompileWithAddress(crate::u64_to_address(2), Precompile::Standard(sha256_run)); diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 8def77d09a..6cba486c5b 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -64,6 +64,7 @@ impl Precompiles { PrecompileSpecId::BYZANTIUM => Self::byzantium(), PrecompileSpecId::ISTANBUL => Self::istanbul(), PrecompileSpecId::BERLIN => Self::berlin(), + #[cfg(feature = "scroll")] PrecompileSpecId::BERNOULLI => Self::bernoulli(), PrecompileSpecId::CANCUN => Self::cancun(), PrecompileSpecId::LATEST => Self::latest(), diff --git a/crates/revm/src/context/inner_evm_context.rs b/crates/revm/src/context/inner_evm_context.rs index ac16033c4b..e341e7b5c5 100644 --- a/crates/revm/src/context/inner_evm_context.rs +++ b/crates/revm/src/context/inner_evm_context.rs @@ -175,6 +175,20 @@ impl InnerEvmContext { Ok((acc.info.code_hash, is_cold)) } + /// Get keccak code hash of address. + #[inline] + #[cfg(feature = "scroll")] + pub fn keccak_code_hash( + &mut self, + address: Address, + ) -> Result<(B256, bool), EVMError> { + let (acc, is_cold) = self.journaled_state.load_code(address, &mut self.db)?; + if acc.is_empty() { + return Ok((B256::ZERO, is_cold)); + } + Ok((acc.info.keccak_code_hash, is_cold)) + } + /// Load storage slot, if storage is not present inside the account then it will be loaded from database. #[inline] pub fn sload( diff --git a/crates/revm/src/evm.rs b/crates/revm/src/evm.rs index 76bf4b633c..b5efe057c9 100644 --- a/crates/revm/src/evm.rs +++ b/crates/revm/src/evm.rs @@ -429,6 +429,15 @@ impl Host for Evm<'_, EXT, DB> { .ok() } + #[cfg(feature = "scroll")] + fn keccak_code_hash(&mut self, address: Address) -> Option<(B256, bool)> { + self.context + .evm + .keccak_code_hash(address) + .map_err(|e| self.context.evm.error = Err(e)) + .ok() + } + fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)> { self.context .evm From 06857da78eeb76fc1d194c7b79f5c5f7e0879cbd Mon Sep 17 00:00:00 2001 From: lightsing Date: Mon, 22 Apr 2024 10:50:06 +0800 Subject: [PATCH 15/24] disable BASEFEE --- crates/interpreter/src/instructions/host.rs | 2 +- crates/interpreter/src/instructions/host_env.rs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/interpreter/src/instructions/host.rs b/crates/interpreter/src/instructions/host.rs index a4f2b7652b..b0ebef02bd 100644 --- a/crates/interpreter/src/instructions/host.rs +++ b/crates/interpreter/src/instructions/host.rs @@ -248,7 +248,7 @@ pub fn selfdestruct(interpreter: &mut Interpreter, host: &m #[cfg(feature = "scroll")] if SPEC::enabled(BERNOULLI) { - interpreter.instruction_result = InstructionResult::InvalidFEOpcode; + interpreter.instruction_result = InstructionResult::NotActivated; return; } diff --git a/crates/interpreter/src/instructions/host_env.rs b/crates/interpreter/src/instructions/host_env.rs index 230bb39b37..62838700d0 100644 --- a/crates/interpreter/src/instructions/host_env.rs +++ b/crates/interpreter/src/instructions/host_env.rs @@ -1,7 +1,7 @@ use crate::{ gas, primitives::{Spec, SpecId::*, U256}, - Host, Interpreter, + Host, InstructionResult, Interpreter, }; /// EIP-1344: ChainID opcode @@ -47,6 +47,11 @@ pub fn gasprice(interpreter: &mut Interpreter, host: &mut H) { /// EIP-3198: BASEFEE opcode pub fn basefee(interpreter: &mut Interpreter, host: &mut H) { + #[cfg(feature = "scroll")] + if SPEC::enabled(BERNOULLI) { + interpreter.instruction_result = InstructionResult::NotActivated; + return; + } check!(interpreter, LONDON); gas!(interpreter, gas::BASE); push!(interpreter, host.env().block.basefee); From 61d8ca76170be530c4c7cd5eeb414e9efe17c756 Mon Sep 17 00:00:00 2001 From: lightsing Date: Mon, 22 Apr 2024 15:53:39 +0800 Subject: [PATCH 16/24] handle l1 tx --- .../interpreter/src/instructions/host_env.rs | 4 +- crates/primitives/src/env.rs | 22 +++++ crates/revm/src/scroll/handler_register.rs | 91 ++++++++++++------- 3 files changed, 82 insertions(+), 35 deletions(-) diff --git a/crates/interpreter/src/instructions/host_env.rs b/crates/interpreter/src/instructions/host_env.rs index 62838700d0..3406f32f41 100644 --- a/crates/interpreter/src/instructions/host_env.rs +++ b/crates/interpreter/src/instructions/host_env.rs @@ -1,7 +1,7 @@ use crate::{ gas, primitives::{Spec, SpecId::*, U256}, - Host, InstructionResult, Interpreter, + Host, Interpreter, }; /// EIP-1344: ChainID opcode @@ -49,7 +49,7 @@ pub fn gasprice(interpreter: &mut Interpreter, host: &mut H) { pub fn basefee(interpreter: &mut Interpreter, host: &mut H) { #[cfg(feature = "scroll")] if SPEC::enabled(BERNOULLI) { - interpreter.instruction_result = InstructionResult::NotActivated; + interpreter.instruction_result = crate::InstructionResult::NotActivated; return; } check!(interpreter, LONDON); diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index cd34f6bf7c..31060d3839 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -133,9 +133,14 @@ impl Env { // Check if the transaction's chain id is correct if let Some(tx_chain_id) = self.tx.chain_id { + #[cfg(not(feature = "scroll"))] if tx_chain_id != self.cfg.chain_id { return Err(InvalidTransaction::InvalidChainId); } + #[cfg(feature = "scroll")] + if !self.tx.scroll.is_l1_msg && tx_chain_id != self.cfg.chain_id { + return Err(InvalidTransaction::InvalidChainId); + } } // Check that access list is empty for transactions before BERLIN @@ -552,6 +557,10 @@ pub struct TxEnv { #[cfg_attr(feature = "serde", serde(flatten))] #[cfg(feature = "optimism")] pub optimism: OptimismFields, + + #[cfg_attr(feature = "serde", serde(flatten))] + #[cfg(feature = "scroll")] + pub scroll: ScrollFields, } impl TxEnv { @@ -587,6 +596,8 @@ impl Default for TxEnv { max_fee_per_blob_gas: None, #[cfg(feature = "optimism")] optimism: OptimismFields::default(), + #[cfg(feature = "scroll")] + scroll: ScrollFields::default(), } } } @@ -650,6 +661,17 @@ pub struct OptimismFields { pub enveloped_tx: Option, } +/// Additional [TxEnv] fields for scroll. +#[cfg(feature = "scroll")] +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct ScrollFields { + pub is_l1_msg: bool, + /// The RLP-encoded bytes of the transaction. This is used + /// to compute the L1 tx cost using the L1 block info. + pub rlp_bytes: Option, +} + /// Transaction destination. #[derive(Clone, Debug, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] diff --git a/crates/revm/src/scroll/handler_register.rs b/crates/revm/src/scroll/handler_register.rs index 2db833b35f..eda07b6932 100644 --- a/crates/revm/src/scroll/handler_register.rs +++ b/crates/revm/src/scroll/handler_register.rs @@ -2,11 +2,12 @@ use crate::handler::mainnet; use crate::handler::mainnet::deduct_caller_inner; -use crate::primitives::InvalidTransaction; use crate::{ handler::register::EvmHandler, interpreter::Gas, - primitives::{db::Database, spec_to_generic, EVMError, Spec, SpecId, U256}, + primitives::{ + db::Database, spec_to_generic, EVMError, InvalidTransaction, Spec, SpecId, TransactTo, U256, + }, Context, }; use std::sync::Arc; @@ -46,27 +47,43 @@ pub fn deduct_caller( .journaled_state .load_account(context.evm.inner.env.tx.caller, &mut context.evm.inner.db)?; - // We deduct caller max balance after minting and before deducing the - // l1 cost, max values is already checked in pre_validate but l1 cost wasn't. - deduct_caller_inner::(caller_account, &context.evm.inner.env); + if !context.evm.inner.env.tx.scroll.is_l1_msg { + // We deduct caller max balance after minting and before deducing the + // l1 cost, max values is already checked in pre_validate but l1 cost wasn't. + deduct_caller_inner::(caller_account, &context.evm.inner.env); - // Deduct l1 fee from caller. - let tx_l1_cost = context - .evm - .inner - .l1_block_info - .as_ref() - .expect("L1BlockInfo should be loaded") - .calculate_tx_l1_cost(&context.evm.inner.env.tx.data); - if tx_l1_cost.gt(&caller_account.info.balance) { - return Err(EVMError::Transaction( - InvalidTransaction::LackOfFundForMaxFee { - fee: tx_l1_cost.into(), - balance: caller_account.info.balance.into(), - }, - )); + let Some(rlp_bytes) = &context.evm.inner.env.tx.scroll.rlp_bytes else { + return Err(EVMError::Custom( + "[SCROLL] Failed to load transaction rlp_bytes.".to_string(), + )); + }; + // Deduct l1 fee from caller. + let tx_l1_cost = context + .evm + .inner + .l1_block_info + .as_ref() + .expect("L1BlockInfo should be loaded") + .calculate_tx_l1_cost(rlp_bytes); + if tx_l1_cost.gt(&caller_account.info.balance) { + return Err(EVMError::Transaction( + InvalidTransaction::LackOfFundForMaxFee { + fee: tx_l1_cost.into(), + balance: caller_account.info.balance.into(), + }, + )); + } + caller_account.info.balance = caller_account.info.balance.saturating_sub(tx_l1_cost); + } else { + // bump the nonce for calls. Nonce for CREATE will be bumped in `handle_create`. + if matches!(context.evm.inner.env.tx.transact_to, TransactTo::Call(_)) { + // Nonce is already checked + caller_account.info.nonce = caller_account.info.nonce.saturating_add(1); + } + + // touch account so we know it is changed. + caller_account.mark_touch(); } - caller_account.info.balance = caller_account.info.balance.saturating_sub(tx_l1_cost); Ok(()) } @@ -88,20 +105,28 @@ pub fn reward_beneficiary( .journaled_state .load_account(beneficiary, &mut context.evm.inner.db)?; - let Some(l1_block_info) = &context.evm.inner.l1_block_info else { - return Err(EVMError::Custom( - "[SCROLL] Failed to load L1 block information.".to_string(), - )); - }; + if !context.evm.inner.env.tx.scroll.is_l1_msg { + let Some(l1_block_info) = &context.evm.inner.l1_block_info else { + return Err(EVMError::Custom( + "[SCROLL] Failed to load L1 block information.".to_string(), + )); + }; - let l1_cost = l1_block_info.calculate_tx_l1_cost(&context.evm.inner.env.tx.data); + let Some(rlp_bytes) = &context.evm.inner.env.tx.scroll.rlp_bytes else { + return Err(EVMError::Custom( + "[SCROLL] Failed to load transaction rlp_bytes.".to_string(), + )); + }; - coinbase_account.mark_touch(); - coinbase_account.info.balance = coinbase_account - .info - .balance - .saturating_add(coinbase_gas_price * U256::from(gas.spent() - gas.refunded() as u64)) - .saturating_add(l1_cost); + let l1_cost = l1_block_info.calculate_tx_l1_cost(rlp_bytes); + + coinbase_account.mark_touch(); + coinbase_account.info.balance = coinbase_account + .info + .balance + .saturating_add(coinbase_gas_price * U256::from(gas.spent() - gas.refunded() as u64)) + .saturating_add(l1_cost); + } Ok(()) } From b57e7d9fb0763db8708428a4ea9e4158ca35ec41 Mon Sep 17 00:00:00 2001 From: lightsing Date: Mon, 22 Apr 2024 20:33:01 +0800 Subject: [PATCH 17/24] add NotImplemented error --- crates/precompile/src/blake2.rs | 2 +- crates/precompile/src/disable.rs | 20 -------------------- crates/precompile/src/hash.rs | 2 +- crates/primitives/src/precompile.rs | 2 ++ 4 files changed, 4 insertions(+), 22 deletions(-) delete mode 100644 crates/precompile/src/disable.rs diff --git a/crates/precompile/src/blake2.rs b/crates/precompile/src/blake2.rs index cf6d1f1b9e..70241ef6aa 100644 --- a/crates/precompile/src/blake2.rs +++ b/crates/precompile/src/blake2.rs @@ -13,7 +13,7 @@ pub const FUN: PrecompileWithAddress = #[cfg(feature = "scroll")] pub const BERNOULLI: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(9), - Precompile::Standard(|_input: &Bytes, _gas_limit: u64| Err(PrecompileError::OutOfGas)), + Precompile::Standard(|_input: &Bytes, _gas_limit: u64| Err(PrecompileError::NotImplemented)), ); /// reference: diff --git a/crates/precompile/src/disable.rs b/crates/precompile/src/disable.rs deleted file mode 100644 index 3c353be846..0000000000 --- a/crates/precompile/src/disable.rs +++ /dev/null @@ -1,20 +0,0 @@ -use crate::{Bytes, PrecompileWithAddress}; -use revm_primitives::{Precompile, PrecompileError, PrecompileResult}; - -pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress( - crate::u64_to_address(0x03), - Precompile::Standard(disable_run), -); -pub const BLAKE2F: PrecompileWithAddress = PrecompileWithAddress( - crate::u64_to_address(0x09), - Precompile::Standard(disable_run), -); -pub const POINT_EVALUATION: PrecompileWithAddress = PrecompileWithAddress( - crate::u64_to_address(0x0A), - Precompile::Standard(disable_run), -); - -/// Always Out of Gas -pub fn disable_run(_input: &Bytes, _gas_limit: u64) -> PrecompileResult { - Err(PrecompileError::OutOfGas) -} diff --git a/crates/precompile/src/hash.rs b/crates/precompile/src/hash.rs index 72ec600dc2..1541e86a30 100644 --- a/crates/precompile/src/hash.rs +++ b/crates/precompile/src/hash.rs @@ -17,7 +17,7 @@ pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress( #[cfg(feature = "scroll")] pub const RIPEMD160_BERNOULLI: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(3), - Precompile::Standard(|_input: &Bytes, _gas_limit: u64| Err(PrecompileError::OutOfGas)), + Precompile::Standard(|_input: &Bytes, _gas_limit: u64| Err(PrecompileError::NotImplemented)), ); /// See: diff --git a/crates/primitives/src/precompile.rs b/crates/primitives/src/precompile.rs index e585377537..6caf5fc3e5 100644 --- a/crates/primitives/src/precompile.rs +++ b/crates/primitives/src/precompile.rs @@ -125,6 +125,7 @@ pub enum PrecompileError { BlobMismatchedVersion, /// The proof verification failed. BlobVerifyKzgProofFailed, + NotImplemented, /// Catch-all variant for other errors. Other(String), } @@ -153,6 +154,7 @@ impl fmt::Display for PrecompileError { Self::BlobInvalidInputLength => "invalid blob input length", Self::BlobMismatchedVersion => "mismatched blob version", Self::BlobVerifyKzgProofFailed => "verifying blob kzg proof failed", + Self::NotImplemented => "not implemented", Self::Other(s) => s, }; f.write_str(s) From 9ecb479179b90d008f205ff21cb6787a2eab0de6 Mon Sep 17 00:00:00 2001 From: lightsing Date: Fri, 26 Apr 2024 15:54:45 +0800 Subject: [PATCH 18/24] fix pairing --- crates/precompile/src/bn128.rs | 29 +++++++++++++++++++++++++++++ crates/precompile/src/lib.rs | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/precompile/src/bn128.rs b/crates/precompile/src/bn128.rs index ce08da7994..fd59365d8f 100644 --- a/crates/precompile/src/bn128.rs +++ b/crates/precompile/src/bn128.rs @@ -45,6 +45,35 @@ pub mod pair { const ADDRESS: Address = crate::u64_to_address(8); + #[cfg(feature = "scroll")] + mod scroll { + use crate::{Precompile, PrecompileWithAddress}; + + /// The number of pairing inputs per pairing operation. If the inputs provided to the precompile + /// call are < 4, we append (G1::infinity, G2::generator) until we have the required no. of inputs. + const N_PAIRING_PER_OP: usize = 4; + + /// The number of bytes taken to represent a pair (G1, G2). + const N_BYTES_PER_PAIR: usize = 192; + + pub const BERNOULLI: PrecompileWithAddress = PrecompileWithAddress( + super::ADDRESS, + Precompile::Standard(|input, gas_limit| { + if input.len() > N_PAIRING_PER_OP * N_BYTES_PER_PAIR { + return Err(crate::PrecompileError::NotImplemented); + } + super::run_pair( + input, + super::ISTANBUL_PAIR_PER_POINT, + super::ISTANBUL_PAIR_BASE, + gas_limit, + ) + }), + ); + } + #[cfg(feature = "scroll")] + pub use scroll::*; + pub const ISTANBUL_PAIR_PER_POINT: u64 = 34_000; pub const ISTANBUL_PAIR_BASE: u64 = 45_000; pub const ISTANBUL: PrecompileWithAddress = PrecompileWithAddress( diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 6cba486c5b..b508b41694 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -172,7 +172,7 @@ impl Precompiles { modexp::BERNOULLI, // 0x05 bn128::add::ISTANBUL, // 0x06 bn128::mul::ISTANBUL, // 0x07 - bn128::pair::ISTANBUL, // 0x08 + bn128::pair::BERNOULLI, // 0x08 blake2::BERNOULLI, // 0x09 ]); Box::new(precompiles) From 74c5a775c8d5bb3f18661c29b36bda2592f22b74 Mon Sep 17 00:00:00 2001 From: lightsing Date: Mon, 29 Apr 2024 11:09:44 +0800 Subject: [PATCH 19/24] fix bins --- bins/revm-test/Cargo.toml | 5 ++++- bins/revm-test/src/bin/burntpix/main.rs | 15 +++++++++++++-- bins/revme/Cargo.toml | 3 +++ bins/revme/src/cmd/statetest/runner.rs | 5 +++++ crates/primitives/src/utilities.rs | 2 ++ crates/revm/src/db/ethersdb.rs | 14 ++++++++++++-- crates/revm/src/scroll/handler_register.rs | 2 ++ 7 files changed, 41 insertions(+), 5 deletions(-) diff --git a/bins/revm-test/Cargo.toml b/bins/revm-test/Cargo.toml index 6034e26f1c..c91453c3f6 100644 --- a/bins/revm-test/Cargo.toml +++ b/bins/revm-test/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] bytes = "1.6" hex = "0.4" -revm = { path = "../../crates/revm", version = "8.0.0",default-features=false } +revm = { path = "../../crates/revm", version = "8.0.0", default-features = false } microbench = "0.5" alloy-sol-macro = "0.7.0" alloy-sol-types = "0.7.0" @@ -26,3 +26,6 @@ name = "transfer" [[bin]] name = "burntpix" + +[features] +scroll = ["revm/scroll"] \ No newline at end of file diff --git a/bins/revm-test/src/bin/burntpix/main.rs b/bins/revm-test/src/bin/burntpix/main.rs index 8cd9eea66c..a7a44f8207 100644 --- a/bins/revm-test/src/bin/burntpix/main.rs +++ b/bins/revm-test/src/bin/burntpix/main.rs @@ -100,11 +100,22 @@ fn try_from_hex_to_u32(hex: &str) -> eyre::Result { } fn insert_account_info(cache_db: &mut CacheDB, addr: Address, code: Bytes) { - let code_hash = hex::encode(keccak256(code.clone())); + let keccak256_code_hash = hex::encode(keccak256(code.clone())); + #[cfg(feature = "scroll")] + let poseidon_code_hash = hex::encode(revm::primitives::poseidon(&code)); + #[cfg(not(feature = "scroll"))] let account_info = AccountInfo::new( U256::from(0), 0, - B256::from_str(&code_hash).unwrap(), + B256::from_str(&keccak256_code_hash).unwrap(), + Bytecode::new_raw(code), + ); + #[cfg(feature = "scroll")] + let account_info = AccountInfo::new( + U256::from(0), + 0, + B256::from_str(&poseidon_code_hash).unwrap(), + B256::from_str(&keccak256_code_hash).unwrap(), Bytecode::new_raw(code), ); cache_db.insert_account_info(addr, account_info); diff --git a/bins/revme/Cargo.toml b/bins/revme/Cargo.toml index 95290714c3..84156aa9b5 100644 --- a/bins/revme/Cargo.toml +++ b/bins/revme/Cargo.toml @@ -32,3 +32,6 @@ thiserror = "1.0" triehash = "0.8" walkdir = "2.5" k256 = { version = "0.13.3", features = ["ecdsa"] } + +[features] +scroll = ["revm/scroll"] \ No newline at end of file diff --git a/bins/revme/src/cmd/statetest/runner.rs b/bins/revme/src/cmd/statetest/runner.rs index b62e20c306..9eea0ae07d 100644 --- a/bins/revme/src/cmd/statetest/runner.rs +++ b/bins/revme/src/cmd/statetest/runner.rs @@ -233,7 +233,12 @@ pub fn execute_test_suite( for (address, info) in unit.pre { let acc_info = revm::primitives::AccountInfo { balance: info.balance, + #[cfg(not(feature = "scroll"))] code_hash: keccak256(&info.code), + #[cfg(feature = "scroll")] + code_hash: revm::primitives::poseidon(&info.code), + #[cfg(feature = "scroll")] + keccak_code_hash: keccak256(&info.code), code: Some(Bytecode::new_raw(info.code)), nonce: info.nonce, }; diff --git a/crates/primitives/src/utilities.rs b/crates/primitives/src/utilities.rs index cc657d8e02..5064bf7197 100644 --- a/crates/primitives/src/utilities.rs +++ b/crates/primitives/src/utilities.rs @@ -26,6 +26,8 @@ pub const POSEIDON_HASH_BYTES_IN_FIELD: usize = 31; #[cfg(feature = "scroll")] pub fn poseidon(code: &[u8]) -> B256 { use hash_circuit::hash::{Hashable, MessageHashable, HASHABLE_DOMAIN_SPEC}; + #[cfg(not(feature = "std"))] + use std::vec::Vec; let bytes_in_field = POSEIDON_HASH_BYTES_IN_FIELD; let fls = (0..(code.len() / bytes_in_field)) diff --git a/crates/revm/src/db/ethersdb.rs b/crates/revm/src/db/ethersdb.rs index d7863f100f..01e9e8f159 100644 --- a/crates/revm/src/db/ethersdb.rs +++ b/crates/revm/src/db/ethersdb.rs @@ -86,8 +86,18 @@ impl DatabaseRef for EthersDB { let balance = U256::from_limbs(balance?.0); let nonce = nonce?.as_u64(); let bytecode = Bytecode::new_raw(code?.0.into()); - let code_hash = bytecode.hash_slow(); - Ok(Some(AccountInfo::new(balance, nonce, code_hash, bytecode))) + #[cfg(not(feature = "scroll"))] + let acc = { + let code_hash = bytecode.hash_slow(); + AccountInfo::new(balance, nonce, code_hash, bytecode) + }; + #[cfg(feature = "scroll")] + let acc = { + let code_hash = bytecode.poseidon_hash_slow(); + let keccak_code_hash = bytecode.keccak_hash_slow(); + AccountInfo::new(balance, nonce, code_hash, keccak_code_hash, bytecode) + }; + Ok(Some(acc)) } fn code_by_hash_ref(&self, _code_hash: B256) -> Result { diff --git a/crates/revm/src/scroll/handler_register.rs b/crates/revm/src/scroll/handler_register.rs index eda07b6932..59357143be 100644 --- a/crates/revm/src/scroll/handler_register.rs +++ b/crates/revm/src/scroll/handler_register.rs @@ -10,6 +10,8 @@ use crate::{ }, Context, }; +#[cfg(not(feature = "std"))] +use std::string::ToString; use std::sync::Arc; pub fn scroll_handle_register(handler: &mut EvmHandler<'_, EXT, DB>) { From dae1ae4e3fb20f8df1690d0617cb31d56e77aa8e Mon Sep 17 00:00:00 2001 From: lightsing Date: Mon, 29 Apr 2024 14:38:59 +0800 Subject: [PATCH 20/24] remove halo2proofs dep --- Cargo.lock | 177 +++++------------------------ Cargo.toml | 5 +- crates/primitives/Cargo.toml | 6 +- crates/primitives/src/utilities.rs | 7 +- 4 files changed, 34 insertions(+), 161 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 454c42ab97..293867d61a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,7 +23,7 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "once_cell", "version_check", "zerocopy", @@ -53,7 +53,7 @@ dependencies = [ "alloy-rlp", "arbitrary", "bytes", - "cfg-if 1.0.0", + "cfg-if", "const-hex", "derive_arbitrary", "derive_more", @@ -374,7 +374,7 @@ checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" dependencies = [ "addr2line", "cc", - "cfg-if 1.0.0", + "cfg-if", "libc", "miniz_oxide", "object", @@ -460,16 +460,6 @@ dependencies = [ "constant_time_eq", ] -[[package]] -name = "block-buffer" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" -dependencies = [ - "block-padding", - "generic-array", -] - [[package]] name = "block-buffer" version = "0.10.4" @@ -479,12 +469,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "block-padding" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" - [[package]] name = "blst" version = "0.3.11" @@ -550,12 +534,6 @@ version = "1.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - [[package]] name = "cfg-if" version = "1.0.0" @@ -657,7 +635,7 @@ version = "1.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ba00838774b4ab0233e355d26710fbfc8327a05c017f6dc4873f876d1f79f78" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "hex", "proptest", @@ -743,28 +721,6 @@ dependencies = [ "itertools", ] -[[package]] -name = "crossbeam" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8" -dependencies = [ - "crossbeam-channel", - "crossbeam-deque", - "crossbeam-epoch", - "crossbeam-queue", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-channel" -version = "0.5.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "crossbeam-deque" version = "0.8.5" @@ -784,15 +740,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "crossbeam-queue" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "crossbeam-utils" version = "0.8.19" @@ -902,7 +849,7 @@ version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer 0.10.4", + "block-buffer", "const-oid", "crypto-common", "subtle", @@ -971,7 +918,7 @@ version = "0.8.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -988,7 +935,7 @@ dependencies = [ "rand", "rlp", "serde", - "sha3 0.10.8", + "sha3", "zeroize", ] @@ -1031,7 +978,7 @@ dependencies = [ "regex", "serde", "serde_json", - "sha3 0.10.8", + "sha3", "thiserror", "uint", ] @@ -1356,7 +1303,7 @@ version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "wasi", ] @@ -1421,35 +1368,10 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5eceaaeec696539ddaf7b333340f1af35a5aa87ae3e4f3ead0532f72affab2e" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "crunchy", ] -[[package]] -name = "halo2_proofs" -version = "1.1.0" -source = "git+https://github.com/scroll-tech/halo2.git?branch=v1.1#7179a60e4b4b1dafff084deac7b4bea235eecf5f" -dependencies = [ - "ark-std 0.3.0", - "blake2b_simd", - "cfg-if 0.1.10", - "crossbeam", - "ff", - "group", - "halo2curves", - "log", - "maybe-rayon", - "num-bigint", - "num-integer", - "poseidon", - "rand_chacha", - "rand_core", - "rayon", - "sha3 0.9.1", - "subtle", - "tracing", -] - [[package]] name = "halo2curves" version = "0.1.0" @@ -1713,7 +1635,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -1777,7 +1699,7 @@ version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "956ff9b67e26e1a6a866cb758f12c6f8746208489e3e4a4b5580802f2f0a587b" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "ecdsa", "elliptic-curve", "once_cell", @@ -1837,16 +1759,6 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" -[[package]] -name = "maybe-rayon" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" -dependencies = [ - "cfg-if 1.0.0", - "rayon", -] - [[package]] name = "memchr" version = "2.7.1" @@ -1908,7 +1820,6 @@ dependencies = [ "autocfg", "num-integer", "num-traits", - "rand", ] [[package]] @@ -2026,12 +1937,6 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" -[[package]] -name = "opaque-debug" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" - [[package]] name = "open-fastrlp" version = "0.1.4" @@ -2226,27 +2131,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" [[package]] -name = "poseidon" -version = "0.2.0" -source = "git+https://github.com/scroll-tech/poseidon.git?branch=main#5787dd3d2ce7a9e9601a035c396ac0c03449b54d" -dependencies = [ - "halo2curves", - "subtle", -] - -[[package]] -name = "poseidon-circuit" +name = "poseidon-base" version = "0.1.0" -source = "git+https://github.com/scroll-tech/poseidon-circuit.git?branch=scroll-dev-1201#c6f058bcf3bb0c7933d1979563c414f5cc480f25" +source = "git+https://github.com/scroll-tech/poseidon-circuit.git?rev=025a761#025a7610f01ec7b66d0cfc4e18e5f3f7c6f890bd" dependencies = [ "bitvec", - "ff", - "halo2_proofs", + "halo2curves", "lazy_static", - "log", - "rand", - "rand_xorshift", - "thiserror", ] [[package]] @@ -2523,7 +2414,7 @@ version = "8.0.0" dependencies = [ "anyhow", "auto_impl", - "cfg-if 1.0.0", + "cfg-if", "criterion", "dyn-clone", "ethers-contract", @@ -2571,15 +2462,15 @@ dependencies = [ "bitflags 2.5.0", "bitvec", "c-kzg", - "cfg-if 1.0.0", + "cfg-if", "derive_more", "dyn-clone", "enumn", - "halo2_proofs", + "halo2curves", "hashbrown", "hex", "once_cell", - "poseidon-circuit", + "poseidon-base", "serde", ] @@ -2650,7 +2541,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" dependencies = [ "cc", - "cfg-if 1.0.0", + "cfg-if", "getrandom", "libc", "spin 0.9.8", @@ -2833,7 +2724,7 @@ version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "788745a868b0e751750388f4e6546eb921ef714a4317fa6954f7cde114eb2eb7" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "derive_more", "parity-scale-codec", "scale-info-derive", @@ -2989,7 +2880,7 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest 0.10.7", ] @@ -3000,23 +2891,11 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest 0.10.7", ] -[[package]] -name = "sha3" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" -dependencies = [ - "block-buffer 0.9.0", - "digest 0.9.0", - "keccak", - "opaque-debug", -] - [[package]] name = "sha3" version = "0.10.8" @@ -3034,7 +2913,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bac61da6b35ad76b195eb4771210f947734321a8d81d7738e1580d953bc7a15e" dependencies = [ "cc", - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -3256,7 +3135,7 @@ version = "3.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "fastrand", "rustix", "windows-sys 0.52.0", @@ -3711,7 +3590,7 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "wasm-bindgen-macro", ] @@ -3736,7 +3615,7 @@ version = "0.4.42" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "js-sys", "wasm-bindgen", "web-sys", @@ -3965,7 +3844,7 @@ version = "0.50.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "windows-sys 0.48.0", ] diff --git a/Cargo.toml b/Cargo.toml index 57671cf1a9..bcad47214b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,4 @@ debug = true [profile.ethtests] inherits = "test" -opt-level = 3 - -[patch."https://github.com/privacy-scaling-explorations/halo2.git"] -halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.1" } \ No newline at end of file +opt-level = 3 \ No newline at end of file diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 500725c10e..a73945916a 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -42,8 +42,8 @@ serde = { version = "1.0", default-features = false, features = [ ], optional = true } # scroll -halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.1", optional = true } -hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-1201", optional = true } +halo2curves = { version = "0.1.0", optional = true } +poseidon-base = { git = "https://github.com/scroll-tech/poseidon-circuit.git", rev = "025a761", optional = true } [build-dependencies] hex = { version = "0.4", default-features = false } @@ -75,7 +75,7 @@ optimism = [] optimism-default-handler = ["optimism"] negate-optimism-default-handler = [] -scroll = ["halo2_proofs", "hash-circuit"] +scroll = ["halo2curves", "poseidon-base"] # Scroll default handler enabled Scroll handler register by default in EvmBuilder. scroll-default-handler = ["scroll"] negate-scroll-default-handler = [] diff --git a/crates/primitives/src/utilities.rs b/crates/primitives/src/utilities.rs index 5064bf7197..4442108f79 100644 --- a/crates/primitives/src/utilities.rs +++ b/crates/primitives/src/utilities.rs @@ -5,10 +5,6 @@ pub use alloy_primitives::{keccak256, Keccak256}; #[cfg(feature = "scroll")] use crate::U256; -#[cfg(feature = "scroll")] -use halo2_proofs::halo2curves::bn256::Fr; -#[cfg(feature = "scroll")] -use halo2_proofs::halo2curves::ff::PrimeField; /// The Keccak-256 hash of the empty string `""`. pub const KECCAK_EMPTY: B256 = @@ -25,7 +21,8 @@ pub const POSEIDON_HASH_BYTES_IN_FIELD: usize = 31; /// Poseidon code hash #[cfg(feature = "scroll")] pub fn poseidon(code: &[u8]) -> B256 { - use hash_circuit::hash::{Hashable, MessageHashable, HASHABLE_DOMAIN_SPEC}; + use halo2curves::{bn256::Fr, ff::PrimeField}; + use poseidon_base::hash::{Hashable, MessageHashable, HASHABLE_DOMAIN_SPEC}; #[cfg(not(feature = "std"))] use std::vec::Vec; From e1e8f7a2eb7ab7f0626713b7e33984881f8f7534 Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 30 Apr 2024 15:27:19 +0800 Subject: [PATCH 21/24] update poseidon dep --- Cargo.lock | 293 ++++++++++++++++++----------------- crates/primitives/Cargo.toml | 2 +- 2 files changed, 151 insertions(+), 144 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 293867d61a..762750ce83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,15 +40,15 @@ dependencies = [ [[package]] name = "allocator-api2" -version = "0.2.16" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" +checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" [[package]] name = "alloy-primitives" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99bbad0a6b588ef4aec1b5ddbbfdacd9ef04e00b979617765b03174318ee1f3a" +checksum = "50c715249705afa1e32be79dabfd35e2ef0f1cc02ad2cf48c9d1e20026ee637b" dependencies = [ "alloy-rlp", "arbitrary", @@ -89,14 +89,14 @@ checksum = "1a047897373be4bbb0224c1afdabca92648dc57a9c9ef6e7b0be3aff7a859c83" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] name = "alloy-sol-macro" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "452d929748ac948a10481fff4123affead32c553cf362841c5103dd508bdfc16" +checksum = "bef9a94a27345fb31e3fcb5f5e9f592bb4847493b07fa1e47dd9fde2222f2e28" dependencies = [ "alloy-sol-macro-input", "const-hex", @@ -105,31 +105,31 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", "syn-solidity", "tiny-keccak", ] [[package]] name = "alloy-sol-macro-input" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df64e094f6d2099339f9e82b5b38440b159757b6920878f28316243f8166c8d1" +checksum = "c31fe73cd259527e24dc2dbfe64bc95e5ddfcd2b2731f670a11ff72b2be2c25b" dependencies = [ "const-hex", "dunce", "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", "syn-solidity", ] [[package]] name = "alloy-sol-types" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43bc2d6dfc2a19fd56644494479510f98b1ee929e04cf0d4aa45e98baa3e545b" +checksum = "afaffed78bfb17526375754931e045f96018aa810844b29c7aef823266dd4b4b" dependencies = [ "alloy-primitives", "alloy-sol-macro", @@ -160,9 +160,9 @@ checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" [[package]] name = "anyhow" -version = "1.0.81" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" +checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" [[package]] name = "arbitrary" @@ -308,13 +308,13 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" [[package]] name = "async-trait" -version = "0.1.79" +version = "0.1.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681" +checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -341,9 +341,9 @@ dependencies = [ [[package]] name = "aurora-engine-modexp" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfacad86e9e138fca0670949eb8ed4ffdf73a55bded8887efe0863cd1a3a6f70" +checksum = "0aef7712851e524f35fbbb74fa6599c5cd8692056a1c36f9ca0d2001b670e7e5" dependencies = [ "hex", "num", @@ -357,14 +357,14 @@ checksum = "3c87f3f15e7794432337fc718554eaa4dc8f04c9677a950ffe366f20a162ae42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] name = "autocfg" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" [[package]] name = "backtrace" @@ -483,9 +483,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.15.4" +version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "byte-slice-cast" @@ -530,9 +530,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.0.90" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" +checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" [[package]] name = "cfg-if" @@ -542,9 +542,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.35" +version = "0.4.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" dependencies = [ "num-traits", ] @@ -593,9 +593,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.3" +version = "4.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "949626d00e063efc93b6dca932419ceb5432f99769911c0b995f7e884c778813" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" dependencies = [ "clap_builder", ] @@ -694,7 +694,7 @@ dependencies = [ "anes", "cast", "ciborium", - "clap 4.5.3", + "clap 4.5.4", "criterion-plot", "is-terminal", "itertools", @@ -776,15 +776,15 @@ dependencies = [ [[package]] name = "data-encoding" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" [[package]] name = "der" -version = "0.7.8" +version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fffa369a668c8af7dbf8b5e56c9f744fbd399949ed171606040001947de40b1c" +checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" dependencies = [ "const-oid", "zeroize", @@ -818,7 +818,7 @@ checksum = "67e77553c4162a157adbf834ebae5b415acbecbeafc7a74b0e886657506a7611" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -883,9 +883,9 @@ dependencies = [ [[package]] name = "either" -version = "1.10.0" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" +checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" [[package]] name = "elliptic-curve" @@ -914,9 +914,9 @@ checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" [[package]] name = "encoding_rs" -version = "0.8.33" +version = "0.8.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" dependencies = [ "cfg-if", ] @@ -947,7 +947,7 @@ checksum = "6fd000fd6988e73bbe993ea3db9b1aa64906ab88766d654973924340c8cddb42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -1118,9 +1118,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.2" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" +checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" [[package]] name = "fastrlp" @@ -1234,7 +1234,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -1299,9 +1299,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.12" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" dependencies = [ "cfg-if", "libc", @@ -1345,9 +1345,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fbd2820c5e49886948654ab546d0688ff24530286bdcf8fca3cefb16d4618eb" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" dependencies = [ "bytes", "fnv", @@ -1364,9 +1364,9 @@ dependencies = [ [[package]] name = "half" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5eceaaeec696539ddaf7b333340f1af35a5aa87ae3e4f3ead0532f72affab2e" +checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" dependencies = [ "cfg-if", "crunchy", @@ -1402,9 +1402,9 @@ checksum = "d23bd4e7b5eda0d0f3a307e8b381fdc8ba9000f26fbe912250c0a4cc3956364a" [[package]] name = "hashbrown" -version = "0.14.3" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" dependencies = [ "ahash", "allocator-api2", @@ -1666,9 +1666,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "js-sys" @@ -1737,9 +1737,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.153" +version = "0.2.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" [[package]] name = "libm" @@ -1761,9 +1761,9 @@ checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "memchr" -version = "2.7.1" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "microbench" @@ -1799,9 +1799,9 @@ dependencies = [ [[package]] name = "num" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +checksum = "3135b08af27d103b0a51f2ae0f8632117b7b185ccf931445affa8df530576a41" dependencies = [ "num-bigint", "num-complex", @@ -1907,7 +1907,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -2026,9 +2026,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pest" -version = "2.7.8" +version = "2.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56f8023d0fb78c8e03784ea1c7f3fa36e68a723138990b8d5a47d916b651e7a8" +checksum = "311fb059dee1a7b802f036316d790138c613a4e8b180c822e3925a662e9f0c95" dependencies = [ "memchr", "thiserror", @@ -2062,14 +2062,14 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] name = "pin-project-lite" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "pin-utils" @@ -2133,7 +2133,7 @@ checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" [[package]] name = "poseidon-base" version = "0.1.0" -source = "git+https://github.com/scroll-tech/poseidon-circuit.git?rev=025a761#025a7610f01ec7b66d0cfc4e18e5f3f7c6f890bd" +source = "git+https://github.com/scroll-tech/poseidon-circuit.git?branch=main#1133859759f9a6abe0707a27f82ea79c17503f4a" dependencies = [ "bitvec", "halo2curves", @@ -2220,9 +2220,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.79" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba" dependencies = [ "unicode-ident", ] @@ -2266,9 +2266,9 @@ checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -2363,9 +2363,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" [[package]] name = "reqwest" @@ -2643,9 +2643,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.32" +version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ "bitflags 2.5.0", "errno", @@ -2656,9 +2656,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.21.10" +version = "0.21.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba" +checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e" dependencies = [ "log", "ring 0.17.8", @@ -2687,9 +2687,9 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" +checksum = "80af6f9131f277a45a3fba6ce8e2258037bb0477a67e610d3c1fe046ab31de47" [[package]] name = "rusty-fork" @@ -2720,9 +2720,9 @@ dependencies = [ [[package]] name = "scale-info" -version = "2.11.1" +version = "2.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "788745a868b0e751750388f4e6546eb921ef714a4317fa6954f7cde114eb2eb7" +checksum = "7c453e59a955f81fb62ee5d596b450383d699f152d350e9d23a0db2adb78e4c0" dependencies = [ "cfg-if", "derive_more", @@ -2732,9 +2732,9 @@ dependencies = [ [[package]] name = "scale-info-derive" -version = "2.11.1" +version = "2.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dc2f4e8bc344b9fc3d5f74f72c2e55bfc38d28dc2ebc69c194a3df424e4d9ac" +checksum = "18cf6c6447f813ef19eb450e985bcce6705f9ce7660db221b59093d15c79c4b7" dependencies = [ "proc-macro-crate 1.3.1", "proc-macro2", @@ -2823,9 +2823,9 @@ checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73" [[package]] name = "serde" -version = "1.0.197" +version = "1.0.199" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a" dependencies = [ "serde_derive", ] @@ -2841,20 +2841,20 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.199" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] name = "serde_json" -version = "1.0.115" +version = "1.0.116" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" +checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813" dependencies = [ "indexmap", "itoa", @@ -2955,9 +2955,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" dependencies = [ "libc", "windows-sys 0.52.0", @@ -3040,7 +3040,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -3075,9 +3075,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.55" +version = "2.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0" +checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3" dependencies = [ "proc-macro2", "quote", @@ -3086,14 +3086,14 @@ dependencies = [ [[package]] name = "syn-solidity" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4497156948bd342b52038035a6fa514a89626e37af9d2c52a5e8d8ebcc7ee479" +checksum = "70aba06097b6eda3c15f6eebab8a6339e121475bcf08bbe6758807e716c372a1" dependencies = [ "paste", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -3152,22 +3152,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.58" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297" +checksum = "f0126ad08bff79f29fc3ae6a55cc72352056dfff61e3ff8bb7129476d44b23aa" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.58" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" +checksum = "d1cd413b5d558b4c5bf3680e324a6fa5014e7b7c067a51e69dbdf47eb7148b66" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -3181,9 +3181,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.34" +version = "0.3.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", "itoa", @@ -3202,9 +3202,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" dependencies = [ "num-conv", "time-core", @@ -3269,7 +3269,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -3375,7 +3375,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -3493,9 +3493,9 @@ checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" [[package]] name = "unicode-width" -version = "0.1.11" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +checksum = "68f5e5f3158ecfd4b8ff6fe086db7c8467a2dfdac97fe420f2b7c4aa97af66d6" [[package]] name = "unicode-xid" @@ -3605,7 +3605,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", "wasm-bindgen-shared", ] @@ -3639,7 +3639,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3684,11 +3684,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.6" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ - "winapi", + "windows-sys 0.52.0", ] [[package]] @@ -3712,7 +3712,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -3732,17 +3732,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ - "windows_aarch64_gnullvm 0.52.4", - "windows_aarch64_msvc 0.52.4", - "windows_i686_gnu 0.52.4", - "windows_i686_msvc 0.52.4", - "windows_x86_64_gnu 0.52.4", - "windows_x86_64_gnullvm 0.52.4", - "windows_x86_64_msvc 0.52.4", + "windows_aarch64_gnullvm 0.52.5", + "windows_aarch64_msvc 0.52.5", + "windows_i686_gnu 0.52.5", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.5", + "windows_x86_64_gnu 0.52.5", + "windows_x86_64_gnullvm 0.52.5", + "windows_x86_64_msvc 0.52.5", ] [[package]] @@ -3753,9 +3754,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" [[package]] name = "windows_aarch64_msvc" @@ -3765,9 +3766,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" [[package]] name = "windows_i686_gnu" @@ -3777,9 +3778,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" [[package]] name = "windows_i686_msvc" @@ -3789,9 +3796,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" [[package]] name = "windows_x86_64_gnu" @@ -3801,9 +3808,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" [[package]] name = "windows_x86_64_gnullvm" @@ -3813,9 +3820,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" [[package]] name = "windows_x86_64_msvc" @@ -3825,9 +3832,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "winnow" @@ -3893,7 +3900,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] [[package]] @@ -3913,5 +3920,5 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.60", ] diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index a73945916a..86ae19fd9c 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -43,7 +43,7 @@ serde = { version = "1.0", default-features = false, features = [ # scroll halo2curves = { version = "0.1.0", optional = true } -poseidon-base = { git = "https://github.com/scroll-tech/poseidon-circuit.git", rev = "025a761", optional = true } +poseidon-base = { git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "main", optional = true } [build-dependencies] hex = { version = "0.4", default-features = false } From 1d44eb00f9d6c36fd2365cb271ba208ef2bc0a08 Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Fri, 10 May 2024 12:43:05 +0800 Subject: [PATCH 22/24] [fix] ci (#4) * fix ci * update branch * fix clippy,test,docs * fix clippy * fix clippy --- .github/workflows/ci.yml | 13 +++++++------ .github/workflows/ethereum-tests.yml | 8 ++++---- bins/revme/src/cmd/statetest/runner.rs | 2 +- crates/interpreter/Cargo.toml | 9 +++++++++ crates/interpreter/src/interpreter.rs | 4 +++- crates/precompile/Cargo.toml | 8 ++++++++ crates/primitives/Cargo.toml | 9 +++++++++ crates/primitives/src/state.rs | 13 ++++++++++--- crates/revm/Cargo.toml | 13 +++++++++++++ crates/revm/src/builder.rs | 6 ++++++ crates/revm/src/context/evm_context.rs | 2 +- crates/revm/src/db/in_memory_db.rs | 2 +- crates/revm/src/db/states/transition_account.rs | 2 +- crates/revm/src/inspector/eip3155.rs | 2 +- crates/revm/src/scroll/l1block.rs | 2 +- 15 files changed, 75 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cbea7ba5c4..3d85f50a41 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,9 +6,9 @@ concurrency: on: push: - branches: [main, "release/**"] + branches: [ main, "release/**", "scroll-evm-executor/**" ] pull_request: - branches: [main, "release/**"] + branches: [ main, "release/**", "scroll-evm-executor/**" ] env: CARGO_TERM_COLOR: always @@ -21,8 +21,8 @@ jobs: strategy: fail-fast: false matrix: - rust: ["stable", "beta", "nightly"] - flags: ["--no-default-features", "", "--all-features"] + rust: [ "stable", "beta", "nightly" ] + flags: [ "--no-default-features", "", "--features=\"all, scroll\"", "--features=\"all, optimism\"" ] steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master @@ -79,9 +79,10 @@ jobs: steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - - run: cargo clippy --workspace --all-targets --all-features + - run: cargo clippy --workspace --all-targets --features all,scroll env: RUSTFLAGS: -Dwarnings + - run: cargo clippy --workspace --all-targets --features all,optimism docs: name: docs @@ -92,7 +93,7 @@ jobs: - uses: dtolnay/rust-toolchain@stable with: components: rust-docs - - run: cargo doc --workspace --all-features --no-deps --document-private-items + - run: cargo doc --workspace --features all,scroll --no-deps --document-private-items env: RUSTDOCFLAGS: "--cfg docsrs -D warnings" diff --git a/.github/workflows/ethereum-tests.yml b/.github/workflows/ethereum-tests.yml index 3b972f7721..38947873a7 100644 --- a/.github/workflows/ethereum-tests.yml +++ b/.github/workflows/ethereum-tests.yml @@ -6,9 +6,9 @@ concurrency: on: push: - branches: [main, "release/**"] + branches: [ main, "release/**", "scroll-evm-executor/**" ] pull_request: - branches: [main, "release/**"] + branches: [ main, "release/**", "scroll-evm-executor/**" ] jobs: tests-stable: @@ -17,8 +17,8 @@ jobs: timeout-minutes: 30 strategy: matrix: - profile: [ethtests, release] - target: [i686-unknown-linux-gnu, x86_64-unknown-linux-gnu] + profile: [ ethtests, release ] + target: [ i686-unknown-linux-gnu, x86_64-unknown-linux-gnu ] steps: - name: Checkout sources uses: actions/checkout@v4 diff --git a/bins/revme/src/cmd/statetest/runner.rs b/bins/revme/src/cmd/statetest/runner.rs index 9eea0ae07d..5969add095 100644 --- a/bins/revme/src/cmd/statetest/runner.rs +++ b/bins/revme/src/cmd/statetest/runner.rs @@ -352,7 +352,7 @@ pub fn execute_test_suite( .build(); let mut evm = Evm::builder() .with_db(&mut state) - .modify_env(|e| *e = env.clone()) + .modify_env(|e| e.clone_from(&env)) .with_spec_id(spec_id) .build(); diff --git a/crates/interpreter/Cargo.toml b/crates/interpreter/Cargo.toml index 3faf81111e..293b0c48c1 100644 --- a/crates/interpreter/Cargo.toml +++ b/crates/interpreter/Cargo.toml @@ -66,3 +66,12 @@ optional_eip3607 = ["revm-primitives/optional_eip3607"] optional_gas_refund = ["revm-primitives/optional_gas_refund"] optional_no_base_fee = ["revm-primitives/optional_no_base_fee"] optional_beneficiary_reward = ["revm-primitives/optional_beneficiary_reward"] + +all = [ + "std", + "serde", + "arbitrary", + "asm-keccak", + "portable", + "dev", +] \ No newline at end of file diff --git a/crates/interpreter/src/interpreter.rs b/crates/interpreter/src/interpreter.rs index 2a1e99a377..ffb125c872 100644 --- a/crates/interpreter/src/interpreter.rs +++ b/crates/interpreter/src/interpreter.rs @@ -217,7 +217,9 @@ impl Interpreter { let out_offset = call_outcome.memory_start(); let out_len = call_outcome.memory_length(); - self.return_data_buffer = call_outcome.output().to_owned(); + call_outcome + .output() + .clone_into(&mut self.return_data_buffer); let target_len = min(out_len, self.return_data_buffer.len()); match call_outcome.instruction_result() { diff --git a/crates/precompile/Cargo.toml b/crates/precompile/Cargo.toml index 2389cb2905..f45036ff47 100644 --- a/crates/precompile/Cargo.toml +++ b/crates/precompile/Cargo.toml @@ -81,6 +81,14 @@ portable = ["revm-primitives/portable", "c-kzg?/portable"] # In Linux it passes. If you don't require to build wasm on win/mac, it is safe to use it and it is enabled by default. secp256k1 = ["dep:secp256k1"] +all = [ + "std", + "asm-keccak", + "c-kzg", + "portable", + "secp256k1", +] + [[bench]] name = "bench" path = "benches/bench.rs" diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 86ae19fd9c..69a2cc728a 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -99,3 +99,12 @@ optional_beneficiary_reward = [] # See comments in `revm-precompile` c-kzg = ["dep:c-kzg", "dep:once_cell", "dep:derive_more"] + +all = [ + "std", + "serde", + "arbitrary", + "asm-keccak", + "portable", + "dev", +] diff --git a/crates/primitives/src/state.rs b/crates/primitives/src/state.rs index 3b5debad76..e4f4beef6e 100644 --- a/crates/primitives/src/state.rs +++ b/crates/primitives/src/state.rs @@ -220,15 +220,16 @@ impl Default for AccountInfo { } impl PartialEq for AccountInfo { + #[allow(clippy::let_and_return)] fn eq(&self, other: &Self) -> bool { let eq = self.balance == other.balance && self.nonce == other.nonce && self.code_hash == other.code_hash; + #[cfg(all(debug_assertions, feature = "scroll"))] if eq { assert_eq!(self.keccak_code_hash, other.keccak_code_hash); } - eq } } @@ -329,7 +330,7 @@ impl AccountInfo { #[cfg(test)] mod tests { - use crate::{Account, KECCAK_EMPTY, U256}; + use crate::{Account, U256}; #[test] fn account_is_empty_balance() { @@ -366,7 +367,13 @@ mod tests { account.info.code_hash = [0; 32].into(); assert!(account.is_empty()); - account.info.code_hash = KECCAK_EMPTY; + cfg_if::cfg_if! { + if #[cfg(feature = "scroll")] { + account.info.code_hash = crate::POSEIDON_EMPTY; + } else { + account.info.code_hash = crate::KECCAK_EMPTY; + } + } assert!(account.is_empty()); } diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index 99b5afc0ee..472575e0f4 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -116,6 +116,19 @@ optional_beneficiary_reward = ["revm-interpreter/optional_beneficiary_reward"] secp256k1 = ["revm-precompile/secp256k1"] c-kzg = ["revm-precompile/c-kzg"] +all = [ + "std", + "serde", + "serde-json", + "arbitrary", + "asm-keccak", + "portable", + "ethersdb", + "dev", + "revm-interpreter/all", + "revm-precompile/all", +] + [[example]] name = "fork_ref_transact" path = "../../examples/fork_ref_transact.rs" diff --git a/crates/revm/src/builder.rs b/crates/revm/src/builder.rs index 4551c20b52..b7acb61c13 100644 --- a/crates/revm/src/builder.rs +++ b/crates/revm/src/builder.rs @@ -487,7 +487,10 @@ mod test { let code = Bytecode::new_raw([0xEF, 0x00].into()); #[cfg(feature = "scroll")] let poseidon_code_hash = code.poseidon_hash_slow(); + #[cfg(feature = "scroll")] let keccak_code_hash = code.keccak_hash_slow(); + #[cfg(not(feature = "scroll"))] + let keccak_code_hash = code.hash_slow(); let to_addr = address!("ffffffffffffffffffffffffffffffffffffffff"); // initialize the custom context and make sure it's zero @@ -551,7 +554,10 @@ mod test { let code = Bytecode::new_raw([0xEF, 0x00].into()); #[cfg(feature = "scroll")] let poseidon_code_hash = code.poseidon_hash_slow(); + #[cfg(feature = "scroll")] let keccak_code_hash = code.keccak_hash_slow(); + #[cfg(not(feature = "scroll"))] + let keccak_code_hash = code.hash_slow(); let to_addr = address!("ffffffffffffffffffffffffffffffffffffffff"); let mut evm = Evm::builder() diff --git a/crates/revm/src/context/evm_context.rs b/crates/revm/src/context/evm_context.rs index 40fc892e2f..ce3e4791f7 100644 --- a/crates/revm/src/context/evm_context.rs +++ b/crates/revm/src/context/evm_context.rs @@ -396,7 +396,7 @@ mod tests { nonce: 0, balance: bal, #[cfg(not(feature = "scroll"))] - code_hash: by.clone().keccak_hash_slow(), + code_hash: by.clone().hash_slow(), #[cfg(feature = "scroll")] code_hash: by.clone().poseidon_hash_slow(), #[cfg(feature = "scroll")] diff --git a/crates/revm/src/db/in_memory_db.rs b/crates/revm/src/db/in_memory_db.rs index 733dde435c..09b4e89a49 100644 --- a/crates/revm/src/db/in_memory_db.rs +++ b/crates/revm/src/db/in_memory_db.rs @@ -531,7 +531,7 @@ mod tests { let serialized = serde_json::to_string(&init_state).unwrap(); let deserialized: CacheDB = serde_json::from_str(&serialized).unwrap(); - assert!(deserialized.accounts.get(&account).is_some()); + assert!(deserialized.accounts.contains_key(&account)); assert_eq!( deserialized.accounts.get(&account).unwrap().info.nonce, nonce diff --git a/crates/revm/src/db/states/transition_account.rs b/crates/revm/src/db/states/transition_account.rs index 1dc6bc33dd..599bc290b0 100644 --- a/crates/revm/src/db/states/transition_account.rs +++ b/crates/revm/src/db/states/transition_account.rs @@ -85,7 +85,7 @@ impl TransitionAccount { /// Update new values of transition. Don't override old values. /// Both account info and old storages need to be left intact. pub fn update(&mut self, other: Self) { - self.info = other.info.clone(); + self.info.clone_from(&other.info); self.status = other.status; // if transition is from some to destroyed drop the storage. diff --git a/crates/revm/src/inspector/eip3155.rs b/crates/revm/src/inspector/eip3155.rs index ca1f6cc574..c996fec916 100644 --- a/crates/revm/src/inspector/eip3155.rs +++ b/crates/revm/src/inspector/eip3155.rs @@ -192,7 +192,7 @@ impl Inspector for TracerEip3155 { fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext) { self.gas_inspector.step(interp, context); - self.stack = interp.stack.data().clone(); + self.stack.clone_from(interp.stack.data()); self.memory = if self.include_memory { Some(hex::encode_prefixed(interp.shared_memory.context_memory())) } else { diff --git a/crates/revm/src/scroll/l1block.rs b/crates/revm/src/scroll/l1block.rs index f9f5c49d28..bd13e7cdac 100644 --- a/crates/revm/src/scroll/l1block.rs +++ b/crates/revm/src/scroll/l1block.rs @@ -51,7 +51,7 @@ impl L1BlockInfo { })) } - /// Calculate the gas cost of a transaction based on L1 block data posted on L2, depending on the [SpecId] passed. + /// Calculate the gas cost of a transaction based on L1 block data posted on L2. pub fn calculate_tx_l1_cost(&self, input: &[u8]) -> U256 { let tx_l1_gas = self.data_gas(input); tx_l1_gas From abb93af450d70e454ee79dda73cd83b2a3d77ec5 Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Fri, 10 May 2024 14:01:12 +0800 Subject: [PATCH 23/24] add faster-hash gate (#3) --- crates/interpreter/Cargo.toml | 1 + crates/precompile/Cargo.toml | 3 +++ crates/primitives/Cargo.toml | 1 + crates/primitives/src/lib.rs | 4 +++- crates/revm/Cargo.toml | 1 + 5 files changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/interpreter/Cargo.toml b/crates/interpreter/Cargo.toml index 293b0c48c1..607ad3e450 100644 --- a/crates/interpreter/Cargo.toml +++ b/crates/interpreter/Cargo.toml @@ -29,6 +29,7 @@ serde = ["dep:serde", "revm-primitives/serde"] arbitrary = ["std", "revm-primitives/arbitrary"] asm-keccak = ["revm-primitives/asm-keccak"] portable = ["revm-primitives/portable"] +faster-hash = ["revm-primitives/faster-hash"] optimism = ["revm-primitives/optimism"] # Optimism default handler enabled Optimism handler register by default in EvmBuilder. diff --git a/crates/precompile/Cargo.toml b/crates/precompile/Cargo.toml index f45036ff47..0ab88aebb1 100644 --- a/crates/precompile/Cargo.toml +++ b/crates/precompile/Cargo.toml @@ -81,12 +81,15 @@ portable = ["revm-primitives/portable", "c-kzg?/portable"] # In Linux it passes. If you don't require to build wasm on win/mac, it is safe to use it and it is enabled by default. secp256k1 = ["dep:secp256k1"] +faster-hash = ["revm-primitives/faster-hash"] + all = [ "std", "asm-keccak", "c-kzg", "portable", "secp256k1", + "faster-hash" ] [[bench]] diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 69a2cc728a..c3d0f3c3a9 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -69,6 +69,7 @@ serde = [ arbitrary = ["std", "alloy-primitives/arbitrary", "bitflags/arbitrary"] asm-keccak = ["alloy-primitives/asm-keccak"] portable = ["c-kzg?/portable"] +faster-hash = ["hashbrown/ahash"] optimism = [] # Optimism default handler enabled Optimism handler register by default in EvmBuilder. diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 50e6d35d3f..0b683e2527 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -30,7 +30,9 @@ pub use constants::*; pub use env::*; cfg_if::cfg_if! { - if #[cfg(feature = "std")] { + if #[cfg(feature = "faster-hash")] { + pub use hashbrown::{hash_map, hash_set, HashMap, HashSet}; + } else if #[cfg(feature = "std")] { pub use std::collections::{hash_map, hash_set, HashMap, HashSet}; use hashbrown as _; } else { diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index 472575e0f4..acc7db51b1 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -60,6 +60,7 @@ serde-json = ["serde", "dep:serde_json"] arbitrary = ["revm-interpreter/arbitrary"] asm-keccak = ["revm-interpreter/asm-keccak", "revm-precompile/asm-keccak"] portable = ["revm-precompile/portable", "revm-interpreter/portable"] +faster-hash = ["revm-interpreter/faster-hash", "revm-precompile/faster-hash"] test-utils = [] From 09daf95f5895ee43aaba5b3c0f9506c07c75dfab Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Sun, 26 May 2024 17:48:52 +0800 Subject: [PATCH 24/24] fix: extcodesize regression (#5) * fix extcodesize regression * fix test --- bins/revme/src/cmd/statetest/runner.rs | 2 + crates/interpreter/src/host.rs | 4 ++ crates/interpreter/src/host/dummy.rs | 6 ++ crates/interpreter/src/instructions/host.rs | 20 ++++++ crates/primitives/src/state.rs | 7 ++ crates/revm/src/context/evm_context.rs | 9 ++- crates/revm/src/context/inner_evm_context.rs | 8 +++ crates/revm/src/db/in_memory_db.rs | 10 +-- crates/revm/src/db/states/bundle_state.rs | 75 +++----------------- crates/revm/src/evm.rs | 9 +++ crates/revm/src/inspector/customprinter.rs | 2 + 11 files changed, 74 insertions(+), 78 deletions(-) diff --git a/bins/revme/src/cmd/statetest/runner.rs b/bins/revme/src/cmd/statetest/runner.rs index 5969add095..0932b349ac 100644 --- a/bins/revme/src/cmd/statetest/runner.rs +++ b/bins/revme/src/cmd/statetest/runner.rs @@ -233,6 +233,8 @@ pub fn execute_test_suite( for (address, info) in unit.pre { let acc_info = revm::primitives::AccountInfo { balance: info.balance, + #[cfg(feature = "scroll")] + code_size: info.code.len(), #[cfg(not(feature = "scroll"))] code_hash: keccak256(&info.code), #[cfg(feature = "scroll")] diff --git a/crates/interpreter/src/host.rs b/crates/interpreter/src/host.rs index 052b618210..4cb274d6ba 100644 --- a/crates/interpreter/src/host.rs +++ b/crates/interpreter/src/host.rs @@ -31,6 +31,10 @@ pub trait Host { /// Get code hash of `address` and if the account is cold. fn code_hash(&mut self, address: Address) -> Option<(B256, bool)>; + #[cfg(feature = "scroll")] + /// Get code size of `address` and if the account is cold. + fn code_size(&mut self, address: Address) -> Option<(usize, bool)>; + #[cfg(feature = "scroll")] /// Get keccak code hash of `address` and if the account is cold. fn keccak_code_hash(&mut self, address: Address) -> Option<(B256, bool)>; diff --git a/crates/interpreter/src/host/dummy.rs b/crates/interpreter/src/host/dummy.rs index 64d940f302..5918ae4a3a 100644 --- a/crates/interpreter/src/host/dummy.rs +++ b/crates/interpreter/src/host/dummy.rs @@ -66,6 +66,12 @@ impl Host for DummyHost { Some((Bytecode::default(), false)) } + #[inline] + #[cfg(feature = "scroll")] + fn code_size(&mut self, _address: Address) -> Option<(usize, bool)> { + Some((0, false)) + } + #[inline] #[cfg(not(feature = "scroll"))] fn code_hash(&mut self, __address: Address) -> Option<(B256, bool)> { diff --git a/crates/interpreter/src/instructions/host.rs b/crates/interpreter/src/instructions/host.rs index b0ebef02bd..76a4e7cf7d 100644 --- a/crates/interpreter/src/instructions/host.rs +++ b/crates/interpreter/src/instructions/host.rs @@ -44,6 +44,7 @@ pub fn selfbalance(interpreter: &mut Interpreter, host: &mu push!(interpreter, balance); } +#[cfg(not(feature = "scroll"))] pub fn extcodesize(interpreter: &mut Interpreter, host: &mut H) { pop_address!(interpreter, address); let Some((code, is_cold)) = host.code(address) else { @@ -68,6 +69,25 @@ pub fn extcodesize(interpreter: &mut Interpreter, host: &mu push!(interpreter, U256::from(code.len())); } +#[cfg(feature = "scroll")] +pub fn extcodesize(interpreter: &mut Interpreter, host: &mut H) { + pop_address!(interpreter, address); + let Some((code_size, is_cold)) = host.code_size(address) else { + interpreter.instruction_result = InstructionResult::FatalExternalError; + return; + }; + gas!( + interpreter, + if is_cold { + COLD_ACCOUNT_ACCESS_COST + } else { + WARM_STORAGE_READ_COST + } + ); + + push!(interpreter, U256::from(code_size)); +} + /// EIP-1052: EXTCODEHASH opcode pub fn extcodehash(interpreter: &mut Interpreter, host: &mut H) { check!(interpreter, CONSTANTINOPLE); diff --git a/crates/primitives/src/state.rs b/crates/primitives/src/state.rs index e4f4beef6e..be9f7cd02a 100644 --- a/crates/primitives/src/state.rs +++ b/crates/primitives/src/state.rs @@ -193,6 +193,9 @@ pub struct AccountInfo { pub balance: U256, /// Account nonce. pub nonce: u64, + #[cfg(feature = "scroll")] + /// code size, + pub code_size: usize, /// code hash, pub code_hash: B256, #[cfg(feature = "scroll")] @@ -207,6 +210,8 @@ impl Default for AccountInfo { fn default() -> Self { Self { balance: U256::ZERO, + #[cfg(feature = "scroll")] + code_size: 0, #[cfg(not(feature = "scroll"))] code_hash: KECCAK_EMPTY, #[cfg(feature = "scroll")] @@ -253,6 +258,8 @@ impl AccountInfo { Self { balance, nonce, + #[cfg(feature = "scroll")] + code_size: code.len(), code: Some(code), code_hash, #[cfg(feature = "scroll")] diff --git a/crates/revm/src/context/evm_context.rs b/crates/revm/src/context/evm_context.rs index ce3e4791f7..2a3a4a9dc6 100644 --- a/crates/revm/src/context/evm_context.rs +++ b/crates/revm/src/context/evm_context.rs @@ -224,7 +224,7 @@ pub(crate) mod test_utils { use crate::{ db::{CacheDB, EmptyDB}, journaled_state::JournaledState, - primitives::{address, Address, Bytes, Env, HashSet, SpecId, B256, U256}, + primitives::{address, Address, Bytes, Env, HashSet, SpecId, U256}, InnerEvmContext, }; use std::boxed::Box; @@ -268,10 +268,7 @@ pub(crate) mod test_utils { crate::primitives::AccountInfo { nonce: 0, balance, - code_hash: B256::default(), - #[cfg(feature = "scroll")] - keccak_code_hash: B256::default(), - code: None, + ..Default::default() }, ); create_cache_db_evm_context(env, db) @@ -395,6 +392,8 @@ mod tests { crate::primitives::AccountInfo { nonce: 0, balance: bal, + #[cfg(feature = "scroll")] + code_size: by.len(), #[cfg(not(feature = "scroll"))] code_hash: by.clone().hash_slow(), #[cfg(feature = "scroll")] diff --git a/crates/revm/src/context/inner_evm_context.rs b/crates/revm/src/context/inner_evm_context.rs index e341e7b5c5..f311c89274 100644 --- a/crates/revm/src/context/inner_evm_context.rs +++ b/crates/revm/src/context/inner_evm_context.rs @@ -165,6 +165,14 @@ impl InnerEvmContext { .map(|(a, is_cold)| (a.info.code.clone().unwrap(), is_cold)) } + #[inline] + #[cfg(feature = "scroll")] + pub fn code_size(&mut self, address: Address) -> Result<(usize, bool), EVMError> { + self.journaled_state + .load_account(address, &mut self.db) + .map(|(acc, is_cold)| (acc.info.code_size, is_cold)) + } + /// Get code hash of address. #[inline] pub fn code_hash(&mut self, address: Address) -> Result<(B256, bool), EVMError> { diff --git a/crates/revm/src/db/in_memory_db.rs b/crates/revm/src/db/in_memory_db.rs index 09b4e89a49..68a9e4d578 100644 --- a/crates/revm/src/db/in_memory_db.rs +++ b/crates/revm/src/db/in_memory_db.rs @@ -419,6 +419,8 @@ impl Database for BenchmarkDB { return Ok(Some(AccountInfo { nonce: 1, balance: U256::from(10000000), + #[cfg(feature = "scroll")] + code_size: self.0.len(), code: Some(self.0.clone()), code_hash: self.1, #[cfg(feature = "scroll")] @@ -429,13 +431,7 @@ impl Database for BenchmarkDB { return Ok(Some(AccountInfo { nonce: 0, balance: U256::from(10000000), - code: None, - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, + ..Default::default() })); } Ok(None) diff --git a/crates/revm/src/db/states/bundle_state.rs b/crates/revm/src/db/states/bundle_state.rs index 279866ac98..1664a1aa4b 100644 --- a/crates/revm/src/db/states/bundle_state.rs +++ b/crates/revm/src/db/states/bundle_state.rs @@ -697,9 +697,6 @@ impl BundleState { mod tests { use super::*; use crate::{db::StorageWithOriginalValues, TransitionAccount}; - use revm_interpreter::primitives::KECCAK_EMPTY; - #[cfg(feature = "scroll")] - use revm_interpreter::primitives::POSEIDON_EMPTY; #[test] fn transition_states() { @@ -708,13 +705,7 @@ mod tests { let acc1 = AccountInfo { balance: U256::from(10), nonce: 1, - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, - code: None, + ..Default::default() }; let mut bundle_state = BundleState::default(); @@ -764,13 +755,7 @@ mod tests { Some(AccountInfo { nonce: 1, balance: U256::from(10), - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, - code: None, + ..Default::default() }), HashMap::from([ (slot1(), (U256::from(0), U256::from(10))), @@ -783,13 +768,7 @@ mod tests { Some(AccountInfo { nonce: 1, balance: U256::from(10), - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, - code: None, + ..Default::default() }), HashMap::from([]), ), @@ -816,13 +795,7 @@ mod tests { Some(AccountInfo { nonce: 3, balance: U256::from(20), - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, - code: None, + ..Default::default() }), HashMap::from([(slot1(), (U256::from(0), U256::from(15)))]), )], @@ -831,13 +804,7 @@ mod tests { Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, - code: None, + ..Default::default() })), vec![(slot1(), U256::from(10))], )]], @@ -853,13 +820,7 @@ mod tests { AccountInfo { nonce: 1, balance: U256::from(10), - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, - code: None, + ..Default::default() }, ) .state_storage( @@ -872,13 +833,7 @@ mod tests { AccountInfo { nonce: 1, balance: U256::from(10), - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, - code: None, + ..Default::default() }, ) .revert_address(0, account1()) @@ -896,13 +851,7 @@ mod tests { AccountInfo { nonce: 3, balance: U256::from(20), - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, - code: None, + ..Default::default() }, ) .state_storage( @@ -916,13 +865,7 @@ mod tests { Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), - #[cfg(not(feature = "scroll"))] - code_hash: KECCAK_EMPTY, - #[cfg(feature = "scroll")] - code_hash: POSEIDON_EMPTY, - #[cfg(feature = "scroll")] - keccak_code_hash: KECCAK_EMPTY, - code: None, + ..Default::default() })), ) .revert_storage(0, account1(), vec![(slot1(), U256::from(10))]) diff --git a/crates/revm/src/evm.rs b/crates/revm/src/evm.rs index b5efe057c9..48848730b6 100644 --- a/crates/revm/src/evm.rs +++ b/crates/revm/src/evm.rs @@ -421,6 +421,15 @@ impl Host for Evm<'_, EXT, DB> { .ok() } + #[cfg(feature = "scroll")] + fn code_size(&mut self, address: Address) -> Option<(usize, bool)> { + self.context + .evm + .code_size(address) + .map_err(|e| self.context.evm.error = Err(e)) + .ok() + } + fn code_hash(&mut self, address: Address) -> Option<(B256, bool)> { self.context .evm diff --git a/crates/revm/src/inspector/customprinter.rs b/crates/revm/src/inspector/customprinter.rs index 3a4ba603c8..2f0b3c7481 100644 --- a/crates/revm/src/inspector/customprinter.rs +++ b/crates/revm/src/inspector/customprinter.rs @@ -130,6 +130,8 @@ mod test { let code = bytes!("5b597fb075978b6c412c64d169d56d839a8fe01b3f4607ed603b2c78917ce8be1430fe6101e8527ffe64706ecad72a2f5c97a95e006e279dc57081902029ce96af7edae5de116fec610208527f9fc1ef09d4dd80683858ae3ea18869fe789ddc365d8d9d800e26c9872bac5e5b6102285260276102485360d461024953601661024a53600e61024b53607d61024c53600961024d53600b61024e5360b761024f5360596102505360796102515360a061025253607261025353603a6102545360fb61025553601261025653602861025753600761025853606f61025953601761025a53606161025b53606061025c5360a661025d53602b61025e53608961025f53607a61026053606461026153608c6102625360806102635360d56102645360826102655360ae61026653607f6101e8610146610220677a814b184591c555735fdcca53617f4d2b9134b29090c87d01058e27e962047654f259595947443b1b816b65cdb6277f4b59c10a36f4e7b8658f5a5e6f5561"); let info = crate::primitives::AccountInfo { balance: "0x100c5d668240db8e00".parse().unwrap(), + #[cfg(feature = "scroll")] + code_size: code.len(), #[cfg(not(feature = "scroll"))] code_hash: crate::primitives::keccak256(&code), #[cfg(feature = "scroll")]