Skip to content
Merged
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
4 changes: 2 additions & 2 deletions crates/verify/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 13 additions & 18 deletions crates/verify/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ciborium::Value, _>(
&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::<ciborium::Value, _>(metadata).is_ok() {
rest
} else {
bytecode
}
Expand Down