Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forge): more precise diff_score #7057

Merged
merged 2 commits into from
Feb 9, 2024
Merged
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
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
Loading