Skip to content

Commit

Permalink
set hash for interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Dec 31, 2024
1 parent 5197a14 commit afc98d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
20 changes: 9 additions & 11 deletions crates/handler/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::{cell::RefCell, cmp::min};
use handler_interface::{Frame, FrameOrResultGen, PrecompileProvider};
use interpreter::{
gas,
interpreter::{EthInterpreter, InstructionProvider},
interpreter::{EthInterpreter, ExtBytecode, InstructionProvider},
interpreter_types::{LoopControl, ReturnData, RuntimeFlag},
return_ok, return_revert, CallInputs, CallOutcome, CallValue, CreateInputs, CreateOutcome,
CreateScheme, EOFCreateInputs, EOFCreateKind, FrameInput, Gas, Host, InputsImpl,
Expand Down Expand Up @@ -147,8 +147,7 @@ where
.journal()
.load_account_code(inputs.bytecode_address)?;

// TODO : Request from foundry to get bytecode hash.
let _code_hash = account.info.code_hash();
let mut code_hash = account.info.code_hash();
let mut bytecode = account.info.code.clone().unwrap_or_default();

// ExtDelegateCall is not allowed to call non-EOF contracts.
Expand All @@ -164,13 +163,12 @@ where
}

if let Bytecode::Eip7702(eip7702_bytecode) = bytecode {
bytecode = context
let account = &context
.journal()
.load_account_code(eip7702_bytecode.delegated_address)?
.info
.code
.clone()
.unwrap_or_default();
.info;
bytecode = account.code.clone().unwrap_or_default();
code_hash = account.code_hash();
}

// Create interpreter and executes call and push new CallStackFrame.
Expand All @@ -188,7 +186,7 @@ where
depth,
Interpreter::new(
memory.clone(),
bytecode,
ExtBytecode::new_with_hash(bytecode, code_hash),
interpreter_input,
inputs.is_static,
false,
Expand Down Expand Up @@ -281,7 +279,7 @@ where
Err(e) => return return_error(e.into()),
};

let bytecode = Bytecode::new_legacy(inputs.init_code.clone());
let bytecode = ExtBytecode::new(Bytecode::new_legacy(inputs.init_code.clone()));

let interpreter_input = InputsImpl {
target_address: created_address,
Expand Down Expand Up @@ -412,7 +410,7 @@ where
depth,
Interpreter::new(
memory.clone(),
Bytecode::Eof(Arc::new(initcode)),
ExtBytecode::new(Bytecode::Eof(Arc::new(initcode))),
interpreter_input,
false,
true,
Expand Down
8 changes: 3 additions & 5 deletions crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use crate::{
interpreter_types::*, table::CustomInstruction, Gas, Host, Instruction, InstructionResult,
InterpreterAction,
};
use bytecode::Bytecode;

use core::cell::RefCell;
pub use ext_bytecode::ExtBytecode;
pub use input::InputsImpl;
Expand Down Expand Up @@ -46,7 +44,7 @@ impl<EXT: Default, MG: MemoryGetter> Interpreter<EthInterpreter<EXT, MG>> {
/// Create new interpreter
pub fn new(
memory: Rc<RefCell<MG>>,
bytecode: Bytecode,
bytecode: ExtBytecode,
inputs: InputsImpl,
is_static: bool,
is_eof_init: bool,
Expand All @@ -61,7 +59,7 @@ impl<EXT: Default, MG: MemoryGetter> Interpreter<EthInterpreter<EXT, MG>> {
};

Self {
bytecode: ExtBytecode::new(bytecode),
bytecode,
stack: Stack::new(),
return_data: ReturnDataImpl::default(),
memory,
Expand Down Expand Up @@ -297,7 +295,7 @@ mod tests {
let bytecode = Bytecode::new_raw(Bytes::from(&[0x60, 0x00, 0x60, 0x00, 0x01][..]));
let interpreter = Interpreter::<EthInterpreter>::new(
Rc::new(RefCell::new(SharedMemory::new())),
bytecode,
ExtBytecode::new(bytecode),
InputsImpl {
target_address: Address::ZERO,
caller_address: Address::ZERO,
Expand Down
16 changes: 14 additions & 2 deletions crates/interpreter/src/interpreter/ext_bytecode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::ops::Deref;

use bytecode::{
eof::TypesSection,
utils::{read_i16, read_u16},
Expand All @@ -17,8 +19,10 @@ pub struct ExtBytecode {
instruction_pointer: *const u8,
}

impl AsRef<Bytecode> for ExtBytecode {
fn as_ref(&self) -> &Bytecode {
impl Deref for ExtBytecode {
type Target = Bytecode;

fn deref(&self) -> &Self::Target {
&self.base
}
}
Expand All @@ -44,6 +48,14 @@ impl ExtBytecode {
}
}

/// Regenerates the bytecode hash.
pub fn regenerate_hash(&mut self) -> B256 {
let hash = self.base.hash_slow();
self.bytecode_hash = Some(hash);
hash
}

/// Returns the bytecode hash.
pub fn hash(&mut self) -> Option<B256> {
self.bytecode_hash
}
Expand Down

0 comments on commit afc98d0

Please sign in to comment.