Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/bytecode/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl Bytecode {
}

/// Calculates hash of the bytecode.
#[inline]
pub fn hash_slow(&self) -> B256 {
if self.is_empty() {
KECCAK_EMPTY
Expand All @@ -54,6 +55,7 @@ impl Bytecode {
}

/// Returns `true` if bytecode is EIP-7702.
#[inline]
pub const fn is_eip7702(&self) -> bool {
matches!(self, Self::Eip7702(_))
}
Expand Down Expand Up @@ -100,6 +102,7 @@ impl Bytecode {
/// # Panics
///
/// For possible panics see [`LegacyAnalyzedBytecode::new`].
#[inline]
pub fn new_analyzed(bytecode: Bytes, original_len: usize, jump_table: JumpTable) -> Self {
Self::LegacyAnalyzed(LegacyAnalyzedBytecode::new(
bytecode,
Expand All @@ -118,6 +121,7 @@ impl Bytecode {
}

/// Pointer to the executable bytecode.
#[inline]
pub fn bytecode_ptr(&self) -> *const u8 {
self.bytecode().as_ptr()
}
Expand Down
8 changes: 4 additions & 4 deletions crates/handler/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use primitives::{
constants::CALL_STACK_LIMIT,
hardfork::SpecId::{self, HOMESTEAD, LONDON, SPURIOUS_DRAGON},
};
use primitives::{keccak256, Address, Bytes, B256, U256};
use primitives::{keccak256, Address, Bytes, U256};
use state::Bytecode;
use std::borrow::ToOwned;
use std::boxed::Box;
Expand Down Expand Up @@ -308,11 +308,11 @@ impl EthFrame<EthInterpreter> {
.nonce_bump_journal_entry(inputs.caller);

// Create address
let mut init_code_hash = B256::ZERO;
let mut init_code_hash = None;
let created_address = match inputs.scheme {
CreateScheme::Create => inputs.caller.create(old_nonce),
CreateScheme::Create2 { salt } => {
init_code_hash = keccak256(&inputs.init_code);
let init_code_hash = *init_code_hash.insert(keccak256(&inputs.init_code));
inputs.caller.create2(salt.to_be_bytes(), init_code_hash)
}
CreateScheme::Custom { address } => address,
Expand All @@ -332,7 +332,7 @@ impl EthFrame<EthInterpreter> {
Err(e) => return return_error(e.into()),
};

let bytecode = ExtBytecode::new_with_hash(
let bytecode = ExtBytecode::new_with_optional_hash(
Bytecode::new_legacy(inputs.init_code.clone()),
init_code_hash,
);
Expand Down
44 changes: 35 additions & 9 deletions crates/interpreter/src/interpreter/ext_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ mod serde;
/// Extended bytecode structure that wraps base bytecode with additional execution metadata.
#[derive(Debug)]
pub struct ExtBytecode {
/// Bytecode Keccak-256 hash.
/// This is `None` if it hasn't been calculated yet.
/// Since it's not necessary for execution, it's not calculated by default.
bytecode_hash: Option<B256>,
/// Actions that the EVM should do. It contains return value of the Interpreter or inputs for `CALL` or `CREATE` instructions.
/// For `RETURN` or `REVERT` instructions it contains the result of the instruction.
Expand Down Expand Up @@ -38,41 +41,64 @@ impl Default for ExtBytecode {

impl ExtBytecode {
/// Create new extended bytecode and set the instruction pointer to the start of the bytecode.
///
/// The bytecode hash will not be calculated.
#[inline]
pub fn new(base: Bytecode) -> Self {
let instruction_pointer = base.bytecode_ptr();
Self {
base,
instruction_pointer,
bytecode_hash: None,
action: None,
has_set_action: false,
}
Self::new_with_optional_hash(base, None)
}

/// Creates new `ExtBytecode` with the given hash.
#[inline]
pub fn new_with_hash(base: Bytecode, hash: B256) -> Self {
Self::new_with_optional_hash(base, Some(hash))
}

/// Creates new `ExtBytecode` with the given hash.
#[inline]
pub fn new_with_optional_hash(base: Bytecode, hash: Option<B256>) -> Self {
let instruction_pointer = base.bytecode_ptr();
Self {
base,
instruction_pointer,
bytecode_hash: Some(hash),
bytecode_hash: hash,
action: None,
has_set_action: false,
}
}

/// Regenerates the bytecode hash.
#[inline]
#[deprecated(note = "use `get_or_calculate_hash` or `calculate_hash` instead")]
#[doc(hidden)]
pub fn regenerate_hash(&mut self) -> B256 {
self.calculate_hash()
}

/// Re-calculates the bytecode hash.
///
/// Prefer [`get_or_calculate_hash`](Self::get_or_calculate_hash) if you just need to get the hash.
#[inline]
pub fn calculate_hash(&mut self) -> B256 {
let hash = self.base.hash_slow();
self.bytecode_hash = Some(hash);
hash
}

/// Returns the bytecode hash.
#[inline]
pub fn hash(&mut self) -> Option<B256> {
self.bytecode_hash
}

/// Returns the bytecode hash or calculates it if it is not set.
#[inline]
pub fn get_or_calculate_hash(&mut self) -> B256 {
*self.bytecode_hash.get_or_insert_with(
#[cold]
|| self.base.hash_slow(),
)
}
}

impl LoopControl for ExtBytecode {
Expand Down
23 changes: 11 additions & 12 deletions crates/interpreter/src/interpreter/ext_bytecode/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use super::ExtBytecode;
use crate::interpreter::Jumps;
use primitives::B256;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::borrow::Cow;
use std::format;

#[derive(Serialize, Deserialize)]
struct ExtBytecodeSerde {
base: bytecode::Bytecode,
struct ExtBytecodeSerde<'a> {
base: Cow<'a, bytecode::Bytecode>,
program_counter: usize,
bytecode_hash: Option<B256>,
}
Expand All @@ -16,7 +18,7 @@ impl Serialize for ExtBytecode {
S: Serializer,
{
ExtBytecodeSerde {
base: self.base.clone(),
base: Cow::Borrowed(&self.base),
program_counter: self.pc(),
bytecode_hash: self.bytecode_hash,
}
Expand All @@ -34,15 +36,12 @@ impl<'de> Deserialize<'de> for ExtBytecode {
program_counter,
bytecode_hash,
} = ExtBytecodeSerde::deserialize(deserializer)?;

let mut bytecode = if let Some(hash) = bytecode_hash {
Self::new_with_hash(base, hash)
} else {
Self::new(base)
};

if program_counter >= bytecode.base.bytecode().len() {
panic!("serde pc: {program_counter} is greater than or equal to bytecode len");
let mut bytecode = Self::new_with_optional_hash(base.into_owned(), bytecode_hash);
let len = bytecode.base.bytecode().len();
if program_counter >= len {
return Err(serde::de::Error::custom(format!(
"program counter ({program_counter}) exceeds bytecode length ({len})"
)));
}
bytecode.absolute_jump(program_counter);
Ok(bytecode)
Expand Down
Loading