Skip to content

Commit

Permalink
Fix: Check empty input bytecode in find_by_deployed_code_exact (#8257)
Browse files Browse the repository at this point in the history
* fix: find by deployed code extract

* chore: add unit test

* chore: minor refactor
  • Loading branch information
huyhuynh3103 committed Jun 25, 2024
1 parent 374a645 commit 7bef9ca
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/common/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ impl ContractsByArtifact {
/// Finds a contract which deployed bytecode exactly matches the given code. Accounts for link
/// references and immutables.
pub fn find_by_deployed_code_exact(&self, code: &[u8]) -> Option<ArtifactWithContractRef<'_>> {
// Immediately return None if the code is empty.
if code.is_empty() {
return None;
}

self.iter().find(|(_, contract)| {
let Some(deployed_bytecode) = &contract.deployed_bytecode else {
return false;
Expand Down Expand Up @@ -403,4 +408,11 @@ mod tests {
let a_99 = &b"a".repeat(99)[..];
assert!(bytecode_diff_score(a_100, a_99) <= 0.01);
}

#[test]
fn find_by_deployed_code_exact_with_empty_deployed() {
let contracts = ContractsByArtifact::new(vec![]);

assert!(contracts.find_by_deployed_code_exact(&[]).is_none());
}
}

0 comments on commit 7bef9ca

Please sign in to comment.