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

feat(EOF): Put EOF bytecode behind an Arc #1517

Merged
merged 2 commits into from
Jun 15, 2024
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
2 changes: 1 addition & 1 deletion bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub fn execute_test_suite(
let code_hash = keccak256(&info.code);
let bytecode = match info.code.get(..2) {
Some(magic) if magic == &EOF_MAGIC_BYTES => {
Bytecode::Eof(Eof::decode(info.code.clone()).unwrap())
Bytecode::Eof(Eof::decode(info.code.clone()).unwrap().into())
}
_ => Bytecode::new_raw(info.code),
};
Expand Down
4 changes: 3 additions & 1 deletion crates/interpreter/src/instructions/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ pub fn unknown<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {

#[cfg(test)]
mod test {
use std::sync::Arc;

use revm_primitives::{bytes, eof::TypesSection, Bytecode, Eof, PragueSpec};

use super::*;
Expand Down Expand Up @@ -322,7 +324,7 @@ mod test {
eof.body.code_section.push(bytes2.clone());
eof.body.types_section.push(types);

let mut interp = Interpreter::new_bytecode(Bytecode::Eof(eof));
let mut interp = Interpreter::new_bytecode(Bytecode::Eof(Arc::new(eof)));
interp.gas = Gas::new(10000);
interp
}
Expand Down
5 changes: 3 additions & 2 deletions crates/interpreter/src/instructions/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn data_copy<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H)
gas_or_fail!(interpreter, cost_per_word(size as u64, VERYLOW));

let offset = as_usize_saturated!(offset);
let data = interpreter.contract.bytecode.eof().expect("EOF").data();
let data = interpreter.contract.bytecode.eof().expect("eof").data();

// set data from the eof to the shared memory. Padd it with zeros.
interpreter
Expand All @@ -84,6 +84,7 @@ pub fn data_copy<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H)
#[cfg(test)]
mod test {
use revm_primitives::{b256, bytes, Bytecode, Bytes, Eof, PragueSpec};
use std::sync::Arc;

use super::*;
use crate::{
Expand All @@ -101,7 +102,7 @@ mod test {

eof.header.code_sizes[0] = code_bytes.len() as u16;
eof.body.code_section[0] = code_bytes;
Bytecode::Eof(eof)
Bytecode::Eof(Arc::new(eof))
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
use core::cmp::min;
use revm_primitives::{Bytecode, Eof, U256};
use std::borrow::ToOwned;
use std::sync::Arc;

/// EVM bytecode interpreter.
#[derive(Debug)]
Expand Down Expand Up @@ -100,7 +101,7 @@ impl Interpreter {
}

#[inline]
pub fn eof(&self) -> Option<&Eof> {
pub fn eof(&self) -> Option<&Arc<Eof>> {
self.contract.bytecode.eof()
}

Expand Down
29 changes: 27 additions & 2 deletions crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod legacy;

pub use eof::{Eof, EOF_MAGIC, EOF_MAGIC_BYTES, EOF_MAGIC_HASH};
pub use legacy::{JumpTable, LegacyAnalyzedBytecode};
use std::sync::Arc;

use crate::{keccak256, Bytes, B256, KECCAK_EMPTY};

Expand All @@ -15,7 +16,7 @@ pub enum Bytecode {
/// The bytecode has been analyzed for valid jump destinations.
LegacyAnalyzed(LegacyAnalyzedBytecode),
/// Ethereum Object Format
Eof(Eof),
Eof(Arc<Eof>),
}

impl Default for Bytecode {
Expand Down Expand Up @@ -53,7 +54,7 @@ impl Bytecode {

/// Return reference to the EOF if bytecode is EOF.
#[inline]
pub const fn eof(&self) -> Option<&Eof> {
pub const fn eof(&self) -> Option<&Arc<Eof>> {
match self {
Self::Eof(eof) => Some(eof),
_ => None,
Expand Down Expand Up @@ -166,3 +167,27 @@ impl Bytecode {
self.len() == 0
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;

#[test]
fn eof_arc_clone() {
let eof = Arc::new(Eof::default());
let bytecode = Bytecode::Eof(Arc::clone(&eof));

// Cloning the Bytecode should not clone the underlying Eof
let cloned_bytecode = bytecode.clone();
if let Bytecode::Eof(original_arc) = bytecode {
if let Bytecode::Eof(cloned_arc) = cloned_bytecode {
assert!(Arc::ptr_eq(&original_arc, &cloned_arc));
} else {
panic!("Cloned bytecode is not Eof");
}
} else {
panic!("Original bytecode is not Eof");
}
}
}
6 changes: 3 additions & 3 deletions crates/revm/src/context/inner_evm_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
},
FrameOrResult, JournalCheckpoint, CALL_STACK_LIMIT,
};
use std::boxed::Box;
use std::{boxed::Box, sync::Arc};

/// EVM contexts contains data that EVM needs for execution.
#[derive(Debug)]
Expand Down Expand Up @@ -297,7 +297,7 @@ impl<DB: Database> InnerEvmContext<DB> {
let contract = Contract::new(
inputs.input.clone(),
// fine to clone as it is Bytes.
Bytecode::Eof(inputs.eof_init_code.clone()),
Bytecode::Eof(Arc::new(inputs.eof_init_code.clone())),
None,
inputs.created_address,
inputs.caller,
Expand Down Expand Up @@ -343,7 +343,7 @@ impl<DB: Database> InnerEvmContext<DB> {

// eof bytecode is going to be hashed.
self.journaled_state
.set_code(address, Bytecode::Eof(bytecode));
.set_code(address, Bytecode::Eof(Arc::new(bytecode)));
}

/// Make create frame.
Expand Down
Loading