Skip to content

fix(primitives) : Check first byte for EOF #1479

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

Closed
Closed
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
31 changes: 28 additions & 3 deletions crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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};
use crate::{keccak256, Bytes, B256, EOF_PREFIX, EOF_VERSION, KECCAK_EMPTY};

/// State of the [`Bytecode`] analysis.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -66,10 +66,35 @@ impl Bytecode {
matches!(self, Self::Eof(_))
}

/// Create a new legacy raw [`Bytecode`].
pub fn new_legacy(bytecode: Bytes) -> Self {
Self::LegacyRaw(bytecode)
}

/// Checks if the bytecode starts with the EOF prefix followed by the correct version.
#[inline]
pub fn is_eof_format(bytes: &Bytes) -> bool {
if bytes.len() >= 3 {
let prefix = u16::from_be_bytes([bytes[0], bytes[1]]);
let version = bytes[2];

prefix == EOF_PREFIX && version == EOF_VERSION
} else {
Comment on lines +77 to +82
Copy link
Member

@rakita rakita Jun 22, 2024

Choose a reason for hiding this comment

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

This can be missleading in tx create case. For EOF only first two bytes should be checked.

You can do this as:
inputs.init_code.get(..2) == Some(&EOF_MAGIC_BYTES)

false
}
}

/// 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
7 changes: 7 additions & 0 deletions crates/primitives/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ pub const BLOB_GASPRICE_UPDATE_FRACTION: u64 = 3338477;

/// First version of the blob.
pub const VERSIONED_HASH_VERSION_KZG: u8 = 0x01;

/// EOF prefix as per EIP-3540.
pub const EOF_PREFIX: u16 = 0xEF00;

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

Choose a reason for hiding this comment

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

EOF_MAGIC_BYTES const were added to src/bytecode/eof.rs

Loading