Skip to content

Commit

Permalink
chore(verify-bytecode: refactor code for preventing code duplications (
Browse files Browse the repository at this point in the history
…foundry-rs#8292)

* fix: find by deployed code extract

* chore: add unit test

* chore: minor refactor

* feat: minor refactor

* feat: remove unused imports

* feat: remove unused imports

* test: fix unit test

* fix: check whether tx is tx creation
  • Loading branch information
huyhuynh3103 authored Jul 1, 2024
1 parent 9058812 commit 11e4e4e
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions crates/verify/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl VerifyBytecodeArgs {
let receipt = provider
.get_transaction_receipt(creation_data.transaction_hash)
.await
.or_else(|e| eyre::bail!("Couldn't fetch transacrion receipt from RPC: {:?}", e))?;
.or_else(|e| eyre::bail!("Couldn't fetch transaction receipt from RPC: {:?}", e))?;

let receipt = if let Some(receipt) = receipt {
receipt
Expand All @@ -199,17 +199,19 @@ impl VerifyBytecodeArgs {
creation_data.transaction_hash
);
};

// Extract creation code
let maybe_creation_code = if receipt.contract_address == Some(self.address) {
&transaction.input
} else if transaction.to == Some(DEFAULT_CREATE2_DEPLOYER) {
&transaction.input[32..]
} else {
eyre::bail!(
"Could not extract the creation code for contract at address {}",
self.address
);
};
let maybe_creation_code =
if receipt.to.is_none() && receipt.contract_address == Some(self.address) {
&transaction.input
} else if receipt.to == Some(DEFAULT_CREATE2_DEPLOYER) {
&transaction.input[32..]
} else {
eyre::bail!(
"Could not extract the creation code for contract at address {}",
self.address
);
};

// If bytecode_hash is disabled then its always partial verification
let (verification_type, has_metadata) =
Expand Down Expand Up @@ -583,36 +585,29 @@ fn try_partial_match(
has_metadata: bool,
) -> Result<bool> {
// 1. Check length of constructor args
if constructor_args.is_empty() {
if constructor_args.is_empty() || is_runtime {
// Assume metadata is at the end of the bytecode
if has_metadata {
local_bytecode = extract_metadata_hash(local_bytecode)?;
bytecode = extract_metadata_hash(bytecode)?;
}

// Now compare the creation code and bytecode
return Ok(local_bytecode == bytecode);
}

if is_runtime {
if has_metadata {
local_bytecode = extract_metadata_hash(local_bytecode)?;
bytecode = extract_metadata_hash(bytecode)?;
}

// Now compare the local code and bytecode
return Ok(local_bytecode == bytecode);
return try_extract_and_compare_bytecode(local_bytecode, bytecode, has_metadata)
}

// If not runtime, extract constructor args from the end of the bytecode
bytecode = &bytecode[..bytecode.len() - constructor_args.len()];
local_bytecode = &local_bytecode[..local_bytecode.len() - constructor_args.len()];

try_extract_and_compare_bytecode(local_bytecode, bytecode, has_metadata)
}

fn try_extract_and_compare_bytecode(
mut local_bytecode: &[u8],
mut bytecode: &[u8],
has_metadata: bool,
) -> Result<bool> {
if has_metadata {
local_bytecode = extract_metadata_hash(local_bytecode)?;
bytecode = extract_metadata_hash(bytecode)?;
}

// Now compare the local code and bytecode
Ok(local_bytecode == bytecode)
}

Expand Down

0 comments on commit 11e4e4e

Please sign in to comment.