From 2ca93507e760f528cb24d33ddb036eb0d3f6ed1c Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Fri, 9 Feb 2024 14:59:43 +0300 Subject: [PATCH] fix(`forge`): more precise diff_score (#7057) * more precise diff_score * avoid 0 division --- crates/common/src/contracts.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/crates/common/src/contracts.rs b/crates/common/src/contracts.rs index cc3b63f52360..86e2abf5b851 100644 --- a/crates/common/src/contracts.rs +++ b/crates/common/src/contracts.rs @@ -97,20 +97,23 @@ pub type ContractsByAddress = BTreeMap; /// /// 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`)) pairs