Skip to content

Commit

Permalink
feat(backend): Improve the Nakamoto comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-tomic committed Jul 18, 2023
1 parent 1af2cf4 commit 9d7149a
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions rs/decentralization/src/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,21 @@ impl NakamotoScore {
/// in each of these features.
/// - Top Node Providers control 5 nodes
/// - Top Countries control 7 nodes
/// In that case we would return 5 + 7 = 12
pub fn critical_features_num_nodes(&self) -> usize {
/// In that case we would return (5, 7)
pub fn critical_features_num_nodes(&self) -> Vec<usize> {
[NodeFeature::NodeProvider, NodeFeature::Country]
.iter()
.map(|feat| self.controlled_nodes.get(feat).cloned().unwrap_or_default())
.sum()
.collect()
}

/// Number of unique actors for the critical features.
/// E.g. if there are 5 unique (different) NPs in a subnet ==> return 5
pub fn critical_features_unique_actors(&self) -> Vec<usize> {
[NodeFeature::NodeProvider, NodeFeature::Country]
.iter()
.map(|feat| self.feature_value_counts(feat).len())
.collect()
}

/// Return the number of nodes that the top actors control
Expand Down Expand Up @@ -302,6 +311,16 @@ impl PartialOrd for NakamotoScore {
return cmp;
}

// Compare the number of unique actors for the critical features
// E.g. self has 5 NPs and other has 4 NPs ==> prefer self
cmp = self
.critical_features_unique_actors()
.partial_cmp(&other.critical_features_unique_actors());

if cmp != Some(Ordering::Equal) {
return cmp;
}

// Compare the count of below-average coefficients
// and prefer candidates that decrease the number of low-value coefficients
let c1 = self.coefficients.values().filter(|c| **c < 3.0).count();
Expand Down Expand Up @@ -349,10 +368,11 @@ impl Display for NakamotoScore {
};
write!(
f,
"NakamotoScore: min {:0.2} avg log2 {} #crit nodes {} #crit coeff {} avg linear {:0.2}",
"NakamotoScore: min {:0.2} avg log2 {} #crit nodes {:?} # crit uniq {:?} #crit coeff {} avg linear {:0.2}",
self.min,
avg_log2_str,
self.critical_features_num_nodes(),
self.critical_features_unique_actors(),
self.coefficients.values().filter(|c| **c < 3.0).count(),
self.avg_linear,
)
Expand Down Expand Up @@ -767,7 +787,7 @@ mod tests {

// Check against the close-to-optimal values obtained by data analysis
assert!(nakamoto_score_after.score_min() >= 1.0);
assert!(nakamoto_score_after.critical_features_num_nodes() <= 25);
assert!(nakamoto_score_after.critical_features_num_nodes()[0] <= 25);
assert!(nakamoto_score_after.score_avg_linear() >= 3.0);
assert!(nakamoto_score_after.score_avg_log2() >= Some(1.32));
}
Expand Down

0 comments on commit 9d7149a

Please sign in to comment.