Skip to content

Commit

Permalink
minimize diff to upstream (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsing authored Aug 8, 2024
1 parent 9e445b2 commit a404ed0
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 289 deletions.
15 changes: 2 additions & 13 deletions bins/revm-test/src/bin/burntpix/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,11 @@ 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-poseidon-codehash")]
let poseidon_code_hash = hex::encode(revm::primitives::poseidon(&code));
#[cfg(not(feature = "scroll-poseidon-codehash"))]
let code_hash = hex::encode(keccak256(&code));
let account_info = AccountInfo::new(
U256::from(0),
0,
B256::from_str(&keccak256_code_hash).unwrap(),
Bytecode::new_raw(code),
);
#[cfg(feature = "scroll-poseidon-codehash")]
let account_info = AccountInfo::new(
U256::from(0),
0,
B256::from_str(&poseidon_code_hash).unwrap(),
B256::from_str(&keccak256_code_hash).unwrap(),
B256::from_str(&code_hash).unwrap(),
Bytecode::new_raw(code),
);
cache_db.insert_account_info(addr, account_info);
Expand Down
5 changes: 1 addition & 4 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,9 @@ pub fn execute_test_suite(
balance: info.balance,
#[cfg(feature = "scroll")]
code_size,
#[cfg(not(feature = "scroll-poseidon-codehash"))]
code_hash: keccak_code_hash,
#[cfg(feature = "scroll-poseidon-codehash")]
code_hash: poseidon_code_hash,
#[cfg(feature = "scroll-poseidon-codehash")]
keccak_code_hash,
poseidon_code_hash,
code: Some(bytecode),
nonce: info.nonce,
};
Expand Down
4 changes: 0 additions & 4 deletions crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ 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-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)>;

/// Get storage value of `address` at `index` and if the account is cold.
fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)>;

Expand Down
15 changes: 0 additions & 15 deletions crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,10 @@ impl Host for DummyHost {
}

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

#[inline]
#[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-poseidon-codehash")]
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) {
Expand Down
8 changes: 1 addition & 7 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ pub fn extcodesize<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,
pub fn extcodehash<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check!(interpreter, CONSTANTINOPLE);
pop_address!(interpreter, address);

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

let Some((code_hash, is_cold)) = result else {
let Some((code_hash, is_cold)) = host.code_hash(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
Expand Down
30 changes: 10 additions & 20 deletions crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,22 @@ impl Bytecode {

/// Calculate hash of the bytecode.
pub fn hash_slow(&self) -> B256 {
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};

if self.is_empty() {
POSEIDON_EMPTY
} else {
poseidon(self.original_byte_slice())
}
}
if self.is_empty() {
KECCAK_EMPTY
} else {
keccak256(self.original_byte_slice())
}
}

/// Calculate keccak hash of the bytecode.
/// Calculate poseidon hash of the bytecode.
#[cfg(feature = "scroll-poseidon-codehash")]
pub fn keccak_hash_slow(&self) -> B256 {
pub fn poseidon_hash_slow(&self) -> B256 {
use crate::{poseidon, POSEIDON_EMPTY};

if self.is_empty() {
KECCAK_EMPTY
POSEIDON_EMPTY
} else {
keccak256(self.original_byte_slice())
poseidon(self.original_byte_slice())
}
}

Expand Down
20 changes: 4 additions & 16 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub use handler_cfg::{CfgEnvWithHandlerCfg, EnvWithHandlerCfg, HandlerCfg};

use crate::{
calc_blob_gasprice, AccessListItem, Account, Address, Bytes, InvalidHeader, InvalidTransaction,
Spec, SpecId, B256, GAS_PER_BLOB, MAX_BLOB_NUMBER_PER_BLOCK, MAX_CODE_SIZE, MAX_INITCODE_SIZE,
U256, VERSIONED_HASH_VERSION_KZG,
Spec, SpecId, B256, GAS_PER_BLOB, KECCAK_EMPTY, MAX_BLOB_NUMBER_PER_BLOCK, MAX_CODE_SIZE,
MAX_INITCODE_SIZE, U256, VERSIONED_HASH_VERSION_KZG,
};
use alloy_primitives::TxKind;
use core::cmp::{min, Ordering};
Expand Down Expand Up @@ -227,20 +227,8 @@ 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
cfg_if::cfg_if! {
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);
}
}
if !self.cfg.is_eip3607_disabled() && account.info.code_hash != KECCAK_EMPTY {
return Err(InvalidTransaction::RejectCallerWithCode);
}

// Check that the transaction's nonce is correct
Expand Down
128 changes: 18 additions & 110 deletions crates/primitives/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ use crate::{Address, Bytecode, HashMap, B256, KECCAK_EMPTY, U256};
use bitflags::bitflags;
use core::hash::{Hash, Hasher};

#[cfg(feature = "scroll-poseidon-codehash")]
use crate::POSEIDON_EMPTY;

/// EVM State is a mapping from addresses to accounts.
pub type EvmState = HashMap<Address, Account>;

Expand Down Expand Up @@ -225,8 +222,8 @@ pub struct AccountInfo {
/// code hash,
pub code_hash: B256,
#[cfg(feature = "scroll-poseidon-codehash")]
/// keccak code hash,
pub keccak_code_hash: B256,
/// poseidon code hash,
pub poseidon_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<Bytecode>,
Expand All @@ -238,12 +235,9 @@ impl Default for AccountInfo {
balance: U256::ZERO,
#[cfg(feature = "scroll")]
code_size: 0,
#[cfg(not(feature = "scroll-poseidon-codehash"))]
code_hash: KECCAK_EMPTY,
#[cfg(feature = "scroll-poseidon-codehash")]
code_hash: POSEIDON_EMPTY,
#[cfg(feature = "scroll-poseidon-codehash")]
keccak_code_hash: KECCAK_EMPTY,
poseidon_code_hash: crate::POSEIDON_EMPTY,
code: Some(Bytecode::default()),
nonce: 0,
}
Expand All @@ -261,7 +255,7 @@ impl PartialEq for AccountInfo {
if eq {
assert_eq!(self.code_size, other.code_size);
#[cfg(feature = "scroll-poseidon-codehash")]
assert_eq!(self.keccak_code_hash, other.keccak_code_hash);
assert_eq!(self.poseidon_code_hash, other.poseidon_code_hash);
}
eq
}
Expand All @@ -276,13 +270,9 @@ impl Hash for AccountInfo {
}

impl AccountInfo {
pub fn new(
balance: U256,
nonce: u64,
code_hash: B256,
#[cfg(feature = "scroll-poseidon-codehash")] keccak_code_hash: B256,
code: Bytecode,
) -> Self {
pub fn new(balance: U256, nonce: u64, code_hash: B256, code: Bytecode) -> Self {
#[cfg(feature = "scroll-poseidon-codehash")]
let poseidon_code_hash = code.poseidon_hash_slow();
Self {
balance,
nonce,
Expand All @@ -291,7 +281,7 @@ impl AccountInfo {
code: Some(code),
code_hash,
#[cfg(feature = "scroll-poseidon-codehash")]
keccak_code_hash,
poseidon_code_hash,
}
}

Expand Down Expand Up @@ -332,104 +322,28 @@ impl AccountInfo {
}

/// Return bytecode hash associated with this account.
/// If account does not have code,
#[cfg_attr(
not(feature = "scroll-poseidon-codehash"),
doc = "it return's `KECCAK_EMPTY` hash."
)]
#[cfg_attr(
feature = "scroll-poseidon-codehash",
doc = "it return's `POSEIDON_EMPTY` hash."
)]
/// If account does not have code, it returns `KECCAK_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-poseidon-codehash")]
pub fn keccak_code_hash(&self) -> B256 {
self.keccak_code_hash
}

/// Returns true if the code hash is the Keccak256 hash of the empty string `""`.
#[inline]
pub fn is_empty_code_hash(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "scroll-poseidon-codehash")] {
#[cfg(debug_assertions)]
if self.code_hash == POSEIDON_EMPTY {
assert_eq!(self.code_size, 0);
assert_eq!(self.keccak_code_hash, KECCAK_EMPTY);
}

self.code_hash == POSEIDON_EMPTY
} else {
self.code_hash == KECCAK_EMPTY
}
#[cfg(all(debug_assertions, feature = "scroll-poseidon-codehash"))]
if self.code_hash == KECCAK_EMPTY {
assert_eq!(self.code_size, 0);
assert_eq!(self.poseidon_code_hash, crate::POSEIDON_EMPTY);
}

self.code_hash == KECCAK_EMPTY
}

/// Take bytecode from account. Code will be set to None.
pub fn take_bytecode(&mut self) -> Option<Bytecode> {
self.code.take()
}

/// Set code and its hash to the account.
pub fn set_code_with_hash(
&mut self,
code: Bytecode,
hash: B256,
#[cfg(feature = "scroll-poseidon-codehash")] keccak_code_hash: B256,
) {
#[cfg(feature = "scroll")]
{
self.code_size = code.len();
#[cfg(feature = "scroll-poseidon-codehash")]
{
self.keccak_code_hash = keccak_code_hash;
}
}

self.code = Some(code);
self.code_hash = hash;
}

/// Re-hash the code, set to empty if code is None,
/// otherwise update the code hash.
pub fn set_code_rehash_slow(&mut self, code: Option<Bytecode>) {
match code {
Some(code) => {
self.code_hash = code.hash_slow();
#[cfg(feature = "scroll")]
{
self.code_size = code.len();
#[cfg(feature = "scroll-poseidon-codehash")]
{
self.keccak_code_hash = code.keccak_hash_slow();
}
}

self.code = Some(code);
}
None => {
self.code_hash = KECCAK_EMPTY;

#[cfg(feature = "scroll")]
{
self.code_size = 0;
#[cfg(feature = "scroll-poseidon-codehash")]
{
self.code_hash = POSEIDON_EMPTY;
self.keccak_code_hash = KECCAK_EMPTY;
}
}

self.code = None;
}
}
}

pub fn from_balance(balance: U256) -> Self {
AccountInfo {
balance,
Expand All @@ -450,7 +364,7 @@ impl AccountInfo {
} else {
let code_size = bytecode.len();
#[cfg(feature = "scroll-poseidon-codehash")]
let keccak_code_hash = bytecode.keccak_hash_slow();
let poseidon_code_hash = bytecode.poseidon_hash_slow();

AccountInfo {
balance: U256::ZERO,
Expand All @@ -459,7 +373,7 @@ impl AccountInfo {
code: Some(bytecode),
code_hash,
#[cfg(feature = "scroll-poseidon-codehash")]
keccak_code_hash,
poseidon_code_hash,
}
}
}
Expand Down Expand Up @@ -505,13 +419,7 @@ mod tests {
account.info.code_hash = [0; 32].into();
assert!(account.is_empty());

cfg_if::cfg_if! {
if #[cfg(feature = "scroll-poseidon-codehash")] {
account.info.code_hash = crate::POSEIDON_EMPTY;
} else {
account.info.code_hash = crate::KECCAK_EMPTY;
}
}
account.info.code_hash = crate::KECCAK_EMPTY;
assert!(account.is_empty());
}

Expand Down
Loading

0 comments on commit a404ed0

Please sign in to comment.