Skip to content

Commit

Permalink
add scroll-poseidon-codehash gate
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsing committed Aug 7, 2024
1 parent 9b5275e commit ea9a256
Show file tree
Hide file tree
Showing 26 changed files with 165 additions and 160 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ jobs:
fail-fast: false
matrix:
rust: [ "stable", "beta", "nightly" ]
flags: [ "--no-default-features", "", "--features=\"all, scroll\"", "--features=\"all, optimism\"" ]
flags: [
"--no-default-features",
"",
"--features=\"scroll\"",
"--features=\"scroll, scroll-poseidon-codehash\"",
"--features=\"all, scroll\"",
"--features=\"all, scroll, scroll-poseidon-codehash\"",
"--features=\"optimism\""
"--features=\"all, optimism\""
]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
Expand All @@ -38,7 +47,7 @@ jobs:
strategy:
fail-fast: false
matrix:
features: ["", "optimism"]
features: [ "", "optimism" ]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
Expand All @@ -53,7 +62,7 @@ jobs:
strategy:
fail-fast: false
matrix:
features: ["", "serde", "std"]
features: [ "", "serde", "std" ]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
Expand Down
5 changes: 3 additions & 2 deletions bins/revm-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
bytes = "1.6"
hex = "0.4"
revm = { path = "../../crates/revm", version = "12.1.0", default-features=false }
revm = { path = "../../crates/revm", version = "12.1.0", default-features = false }
microbench = "0.5"
alloy-sol-macro = "0.7.7"
alloy-sol-types = "0.7.7"
Expand All @@ -28,4 +28,5 @@ name = "transfer"
name = "burntpix"

[features]
scroll = ["revm/scroll"]
scroll = ["revm/scroll"]
scroll-poseidon-codehash = ["scroll", "revm/scroll-poseidon-codehash"]
6 changes: 3 additions & 3 deletions bins/revm-test/src/bin/burntpix/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ fn try_from_hex_to_u32(hex: &str) -> eyre::Result<u32> {

fn insert_account_info(cache_db: &mut CacheDB<EmptyDB>, addr: Address, code: Bytes) {
let keccak256_code_hash = hex::encode(keccak256(&code));
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
let poseidon_code_hash = hex::encode(revm::primitives::poseidon(&code));
#[cfg(not(feature = "scroll"))]
#[cfg(not(feature = "scroll-poseidon-codehash"))]
let account_info = AccountInfo::new(
U256::from(0),
0,
B256::from_str(&keccak256_code_hash).unwrap(),
Bytecode::new_raw(code),
);
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
let account_info = AccountInfo::new(
U256::from(0),
0,
Expand Down
3 changes: 2 additions & 1 deletion bins/revme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ walkdir = "2.5"
k256 = { version = "0.13.3", features = ["ecdsa"] }

[features]
scroll = ["revm/scroll"]
scroll = ["revm/scroll"]
scroll-poseidon-codehash = ["scroll", "revm/scroll-poseidon-codehash"]
8 changes: 4 additions & 4 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,18 @@ pub fn execute_test_suite(
#[cfg(feature = "scroll")]
let code_size = info.code.len();
let keccak_code_hash = keccak256(&info.code);
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
let poseidon_code_hash = revm::primitives::poseidon(&info.code);
let bytecode = Bytecode::new_raw(info.code);
let acc_info = revm::primitives::AccountInfo {
balance: info.balance,
#[cfg(feature = "scroll")]
code_size,
#[cfg(not(feature = "scroll"))]
#[cfg(not(feature = "scroll-poseidon-codehash"))]
code_hash: keccak_code_hash,
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
code_hash: poseidon_code_hash,
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
keccak_code_hash,
code: Some(bytecode),
nonce: info.nonce,
Expand Down
4 changes: 4 additions & 0 deletions crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ scroll-default-handler = [
negate-scroll-default-handler = [
"revm-primitives/negate-scroll-default-handler",
]
scroll-poseidon-codehash = [
"scroll",
"revm-primitives/scroll-poseidon-codehash",
]

dev = [
"memory_limit",
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait Host {
/// Get code size of `address` and if the account is cold.
fn code_size(&mut self, address: Address) -> Option<(usize, bool)>;

#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
/// Get keccak code hash of `address` and if the account is cold.
fn keccak_code_hash(&mut self, address: Address) -> Option<(B256, bool)>;

Expand Down
11 changes: 5 additions & 6 deletions crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use std::vec::Vec;

use super::LoadAccountResult;

#[cfg(feature = "scroll")]
use revm_primitives::POSEIDON_EMPTY;

/// A dummy [Host] implementation.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DummyHost {
Expand Down Expand Up @@ -74,19 +71,21 @@ impl Host for DummyHost {
}

#[inline]
#[cfg(not(feature = "scroll"))]
#[cfg(not(feature = "scroll-poseidon-codehash"))]
fn code_hash(&mut self, _address: Address) -> Option<(B256, bool)> {
Some((KECCAK_EMPTY, false))
}

#[inline]
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
fn code_hash(&mut self, _address: Address) -> Option<(B256, bool)> {
use revm_primitives::POSEIDON_EMPTY;

Some((POSEIDON_EMPTY, false))
}

#[inline]
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
fn keccak_code_hash(&mut self, _address: Address) -> Option<(B256, bool)> {
Some((KECCAK_EMPTY, false))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ pub fn extcodehash<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,
check!(interpreter, CONSTANTINOPLE);
pop_address!(interpreter, address);

#[cfg(not(feature = "scroll"))]
#[cfg(not(feature = "scroll-poseidon-codehash"))]
let result = host.code_hash(address);
#[cfg(feature = "scroll")]
#[cfg(feature = "scroll-poseidon-codehash")]
let result = host.keccak_code_hash(address);

let Some((code_hash, is_cold)) = result else {
Expand Down
3 changes: 2 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ optimism = []
optimism-default-handler = ["optimism"]
negate-optimism-default-handler = []

scroll = ["halo2curves", "poseidon-base"]
scroll = []
# Scroll default handler enabled Scroll handler register by default in EvmBuilder.
scroll-default-handler = ["scroll"]
negate-scroll-default-handler = []
scroll-poseidon-codehash = ["scroll", "halo2curves", "poseidon-base"]

dev = [
"memory_limit",
Expand Down
35 changes: 17 additions & 18 deletions crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ pub use legacy::{JumpTable, LegacyAnalyzedBytecode};
use std::sync::Arc;

use crate::{keccak256, Bytes, B256, KECCAK_EMPTY};
#[cfg(feature = "scroll")]
use crate::{poseidon, POSEIDON_EMPTY};

/// State of the [`Bytecode`] analysis.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -47,27 +45,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_byte_slice())
}
}
cfg_if::cfg_if! {
if #[cfg(not(feature = "scroll-poseidon-codehash"))] {
if self.is_empty() {
KECCAK_EMPTY
} else {
keccak256(self.original_byte_slice())
}
} else {
use crate::{poseidon, POSEIDON_EMPTY};

/// Calculate poseidon hash of the bytecode.
#[cfg(feature = "scroll")]
pub fn poseidon_hash_slow(&self) -> B256 {
if self.is_empty() {
POSEIDON_EMPTY
} else {
poseidon(self.original_byte_slice())
if self.is_empty() {
POSEIDON_EMPTY
} else {
poseidon(self.original_byte_slice())
}
}
}
}

/// Calculate hash of the bytecode.
#[cfg(feature = "scroll")]
/// Calculate keccak hash of the bytecode.
#[cfg(feature = "scroll-poseidon-codehash")]
pub fn keccak_hash_slow(&self) -> B256 {
if self.is_empty() {
KECCAK_EMPTY
Expand Down
11 changes: 5 additions & 6 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ use core::hash::Hash;
use std::boxed::Box;
use std::vec::Vec;

#[cfg(not(feature = "scroll"))]
use crate::KECCAK_EMPTY;
#[cfg(feature = "scroll")]
use crate::POSEIDON_EMPTY;

/// EVM environment configuration.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -233,11 +228,15 @@ impl Env {
// This EIP is introduced after london but there was no collision in past
// so we can leave it enabled always
cfg_if::cfg_if! {
if #[cfg(not(feature = "scroll"))] {
if #[cfg(not(feature = "scroll-poseidon-codehash"))] {
use crate::KECCAK_EMPTY;

if !self.cfg.is_eip3607_disabled() && account.info.code_hash != KECCAK_EMPTY {
return Err(InvalidTransaction::RejectCallerWithCode);
}
} else {
use crate::POSEIDON_EMPTY;

if !self.cfg.is_eip3607_disabled() && account.info.code_hash != POSEIDON_EMPTY {
return Err(InvalidTransaction::RejectCallerWithCode);
}
Expand Down
1 change: 1 addition & 0 deletions crates/primitives/src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub enum SpecId {
/// Curie update introduces:
/// - Support `EIP-1559` transactions.
/// - Support the `BASEFEE`, `MCOPY`, `TLOAD`, `TSTORE` opcodes.
///
/// Although the Curie update include new opcodes in Cancun, the most important change
/// `EIP-4844` is not included. So we sort it before Cancun.
CURIE = 19,
Expand Down
Loading

0 comments on commit ea9a256

Please sign in to comment.