Skip to content
Closed
Changes from 1 commit
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
24 changes: 22 additions & 2 deletions crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
pub mod eof;
pub mod legacy;

pub use eof::Eof;
pub use eof::{Eof, EofDecodeError};
pub use legacy::{JumpTable, LegacyAnalyzedBytecode};

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

/// EOF prefix as per EIP-3540.
const EOF_PREFIX: u8 = 0xEF;

/// Version byte that immediately follows the EOF prefix in EIP-3540.
// See https://github.com/bluealloy/revm/issues/1464
const EOF_VERSION: u8 = 0x01;

/// State of the [`Bytecode`] analysis.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -66,10 +73,23 @@ impl Bytecode {
matches!(self, Self::Eof(_))
}

/// Checks if the bytecode starts with the EOF prefix followed by the correct version.
#[inline]
pub fn is_eof_format(bytes: &Bytes) -> bool {
bytes.len() >= 2 && bytes[0] == EOF_PREFIX && bytes[1] == EOF_VERSION
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct. EOF_MAGIC is 0xEF00 while EOF_VERSION (At least version one) is 0x01. Would be good to move those in revm-primitives crate

prefix is 0xEF0001

}

/// Creates a new raw [`Bytecode`].
#[inline]
pub fn new_raw(bytecode: Bytes) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would return Error here as Legacy bytecode with 0xEF is considered invalid. And only valid EOF can have 0xEF prefix.

Would additionally add new_legacy function for legacy codes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would return error here

Self::LegacyRaw(bytecode)
if Self::is_eof_format(&bytecode) {
Eof::decode(bytecode.clone())
.map(Self::Eof)
.unwrap_or_else(|_| Self::LegacyRaw(bytecode))
} else {
// If not EOF format, use as legacy raw bytecode
Self::LegacyRaw(bytecode)
}
}

/// Create new checked bytecode.
Expand Down