Skip to content

Commit

Permalink
fix(forge): more precise diff_score (#7057)
Browse files Browse the repository at this point in the history
* more precise diff_score

* avoid 0 division
  • Loading branch information
klkvr authored Feb 9, 2024
1 parent b174c3a commit 2ca9350
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions crates/common/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,23 @@ pub type ContractsByAddress = BTreeMap<Address, (String, JsonAbi)>;
///
/// Will fail for small contracts that are essentially all immutable variables.
pub fn diff_score(a: &[u8], b: &[u8]) -> f64 {
let cutoff_len = usize::min(a.len(), b.len());
if cutoff_len == 0 {
return 1.0
let max_len = usize::max(a.len(), b.len());
let min_len = usize::min(a.len(), b.len());

if max_len == 0 {
return 1.0;
}

let a = &a[..cutoff_len];
let b = &b[..cutoff_len];
let a = &a[..min_len];
let b = &b[..min_len];
let mut diff_chars = 0;
for i in 0..cutoff_len {
for i in 0..min_len {
if a[i] != b[i] {
diff_chars += 1;
}
}
diff_chars as f64 / cutoff_len as f64
diff_chars += max_len - min_len;
diff_chars as f64 / max_len as f64
}

/// Flattens the contracts into (`id` -> (`JsonAbi`, `Vec<u8>`)) pairs
Expand Down

0 comments on commit 2ca9350

Please sign in to comment.