From 7957c020a1171e083cbdfc860c3eff988d076f18 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 13 Apr 2024 14:11:45 +0200 Subject: [PATCH] feat: add `Bytecode::original_bytecode_slice` to match `BytecodeLocked` (#1286) --- crates/primitives/src/bytecode.rs | 12 +++++++++++- crates/primitives/src/bytecode/legacy.rs | 7 ++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/primitives/src/bytecode.rs b/crates/primitives/src/bytecode.rs index 538a57af44..e36afa48ba 100644 --- a/crates/primitives/src/bytecode.rs +++ b/crates/primitives/src/bytecode.rs @@ -47,7 +47,7 @@ impl Bytecode { if self.is_empty() { KECCAK_EMPTY } else { - keccak256(&self.original_bytes()) + keccak256(self.original_byte_slice()) } } @@ -118,6 +118,16 @@ impl Bytecode { } } + /// Returns the original bytecode as a byte slice. + #[inline] + pub fn original_byte_slice(&self) -> &[u8] { + match self { + Self::LegacyRaw(bytes) => bytes, + Self::LegacyAnalyzed(analyzed) => analyzed.original_byte_slice(), + Self::Eof(eof) => eof.raw(), + } + } + /// Returns the length of the raw bytes. #[inline] pub fn len(&self) -> usize { diff --git a/crates/primitives/src/bytecode/legacy.rs b/crates/primitives/src/bytecode/legacy.rs index 18949f0aa6..3e6f2d2b90 100644 --- a/crates/primitives/src/bytecode/legacy.rs +++ b/crates/primitives/src/bytecode/legacy.rs @@ -53,7 +53,12 @@ impl LegacyAnalyzedBytecode { /// Original bytes without padding. pub fn original_bytes(&self) -> Bytes { - self.bytecode.slice(0..self.original_len) + self.bytecode.slice(..self.original_len) + } + + /// Original bytes without padding. + pub fn original_byte_slice(&self) -> &[u8] { + &self.bytecode[..self.original_len] } /// Jumptable of analyzed bytes.