Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(primitives): Use Upstream Alloy Create Implementations #775

Merged
merged 1 commit into from Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 1 addition & 14 deletions crates/primitives/src/utilities.rs
@@ -1,25 +1,12 @@
use crate::{
b256, Address, B256, BLOB_GASPRICE_UPDATE_FRACTION, MIN_BLOB_GASPRICE,
TARGET_BLOB_GAS_PER_BLOCK, U256,
b256, B256, BLOB_GASPRICE_UPDATE_FRACTION, MIN_BLOB_GASPRICE, TARGET_BLOB_GAS_PER_BLOCK,
};
pub use alloy_primitives::keccak256;

/// The Keccak-256 hash of the empty string `""`.
pub const KECCAK_EMPTY: B256 =
b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");

/// Returns the address for the legacy `CREATE` scheme: [`CreateScheme::Create`]
#[inline]
pub fn create_address(caller: Address, nonce: u64) -> Address {
caller.create(nonce)
}

/// Returns the address for the `CREATE2` scheme: [`CreateScheme::Create2`]
#[inline]
pub fn create2_address(caller: Address, code_hash: B256, salt: U256) -> Address {
caller.create2(salt.to_be_bytes::<32>(), code_hash)
}

/// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used` and `excess_blob_gas`.
///
/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers
Expand Down
9 changes: 4 additions & 5 deletions crates/revm/src/evm_impl.rs
Expand Up @@ -6,9 +6,8 @@ use crate::interpreter::{
};
use crate::journaled_state::{is_precompile, JournalCheckpoint};
use crate::primitives::{
create2_address, create_address, keccak256, Address, AnalysisKind, Bytecode, Bytes, EVMError,
EVMResult, Env, ExecutionResult, InvalidTransaction, Log, Output, ResultAndState, Spec,
SpecId::*, TransactTo, B256, U256,
keccak256, Address, AnalysisKind, Bytecode, Bytes, EVMError, EVMResult, Env, ExecutionResult,
InvalidTransaction, Log, Output, ResultAndState, Spec, SpecId::*, TransactTo, B256, U256,
};
use crate::{db::Database, journaled_state::JournaledState, precompile, Inspector};
use alloc::boxed::Box;
Expand Down Expand Up @@ -471,8 +470,8 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
// Create address
let code_hash = keccak256(&inputs.init_code);
let created_address = match inputs.scheme {
CreateScheme::Create => create_address(inputs.caller, old_nonce),
CreateScheme::Create2 { salt } => create2_address(inputs.caller, code_hash, salt),
CreateScheme::Create => inputs.caller.create(old_nonce),
CreateScheme::Create2 { salt } => inputs.caller.create2(B256::from(salt), code_hash),
};

// Load account so it needs to be marked as warm for access list.
Expand Down
4 changes: 0 additions & 4 deletions documentation/src/crates/primitives/utils.md
Expand Up @@ -5,7 +5,3 @@ This Rust module provides utility functions and constants for handling Keccak ha
The `KECCAK_EMPTY` constant represents the Keccak-256 hash of an empty input.

The `keccak256` function takes a byte slice input and returns its Keccak-256 hash as a `B256` value.

`create_address` function implements the address calculation for the Ethereum `CREATE` operation. It takes as parameters the address of the caller (`caller`) and a nonce (`nonce`). The function serializes these inputs using Recursive Length Prefix (RLP) encoding, calculates the Keccak-256 hash of the result, and returns the last 20 bytes of this hash as the created address.

`create2_address` function implements the address calculation for the Ethereum `CREATE2` operation. It takes as parameters the address of the caller (`caller`), a hash of the initializing code (`code_hash`), and a "salt" value (`salt`). The function hashes these inputs together in a specific way, as per the Ethereum `CREATE2` rules, and returns the last 20 bytes of the result as the created address.