-
Notifications
You must be signed in to change notification settings - Fork 682
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)] | ||
|
@@ -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 { | ||
false | ||
} | ||
} | ||
|
||
/// Creates a new raw [`Bytecode`]. | ||
#[inline] | ||
pub fn new_raw(bytecode: Bytes) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Would additionally add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment.
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)