diff --git a/crates/verify/src/bytecode.rs b/crates/verify/src/bytecode.rs index c3ddd3c4df867..66e95c7e790b3 100644 --- a/crates/verify/src/bytecode.rs +++ b/crates/verify/src/bytecode.rs @@ -291,7 +291,7 @@ impl VerifyBytecodeArgs { .await?; let match_type = crate::utils::match_bytecodes( - &deployed_bytecode.original_bytes(), + deployed_bytecode.original_byte_slice(), &onchain_runtime_code, &constructor_args, true, @@ -501,7 +501,7 @@ impl VerifyBytecodeArgs { // Compare the onchain runtime bytecode with the runtime code from the fork. let match_type = crate::utils::match_bytecodes( - &fork_runtime_code.original_bytes(), + fork_runtime_code.original_byte_slice(), &onchain_runtime_code, &constructor_args, true, diff --git a/crates/verify/src/utils.rs b/crates/verify/src/utils.rs index f79f2dfce52f8..714fe4eee0e36 100644 --- a/crates/verify/src/utils.rs +++ b/crates/verify/src/utils.rs @@ -197,29 +197,24 @@ fn is_partial_match( } fn try_extract_and_compare_bytecode(mut local_bytecode: &[u8], mut bytecode: &[u8]) -> bool { - local_bytecode = extract_metadata_hash(local_bytecode); - bytecode = extract_metadata_hash(bytecode); + local_bytecode = ignore_metadata_hash(local_bytecode); + bytecode = ignore_metadata_hash(bytecode); // Now compare the local code and bytecode local_bytecode == bytecode } -/// @dev This assumes that the metadata is at the end of the bytecode -fn extract_metadata_hash(bytecode: &[u8]) -> &[u8] { - // Get the last two bytes of the bytecode to find the length of CBOR metadata - let metadata_len = &bytecode[bytecode.len() - 2..]; - let metadata_len = u16::from_be_bytes([metadata_len[0], metadata_len[1]]); - - if metadata_len as usize <= bytecode.len() { - if ciborium::from_reader::( - &bytecode[bytecode.len() - 2 - metadata_len as usize..bytecode.len() - 2], - ) - .is_ok() - { - &bytecode[..bytecode.len() - 2 - metadata_len as usize] - } else { - bytecode - } +/// This assumes that the metadata is at the end of the bytecode. +fn ignore_metadata_hash(bytecode: &[u8]) -> &[u8] { + // Get the last two bytes of the bytecode to find the length of CBOR metadata. + let Some((rest, metadata_len_bytes)) = bytecode.split_last_chunk() else { return bytecode }; + let metadata_len = u16::from_be_bytes(*metadata_len_bytes) as usize; + if metadata_len > rest.len() { + return bytecode; + } + let (rest, metadata) = rest.split_at(rest.len() - metadata_len); + if ciborium::from_reader::(metadata).is_ok() { + rest } else { bytecode }