Skip to content

Commit

Permalink
feat(backend): Add special handling for the European subnet
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-tomic committed Jan 4, 2024
1 parent 84ffcd1 commit 036c0ba
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
71 changes: 70 additions & 1 deletion rs/decentralization/src/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ impl Display for NakamotoScore {

#[cfg(test)]
mod tests {
use std::str::FromStr;

use crate::network::{DecentralizedSubnet, SubnetChangeRequest};
use ic_base_types::PrincipalId;
use itertools::Itertools;
Expand Down Expand Up @@ -747,7 +749,7 @@ mod tests {
&["NP1", "NP2", "NP2", "NP3", "NP4", "NP4", "NP5"],
),
);
subnet_initial.check_business_rules().unwrap();
assert_eq!(subnet_initial.check_business_rules().unwrap(), (0, vec![]));

// There are 2 spare nodes, but both are DFINITY
let nodes_available =
Expand Down Expand Up @@ -874,4 +876,71 @@ mod tests {
let new_subnet = new_subnet_result.unwrap();
assert_eq!(new_subnet.nodes.len(), want_subnet_size)
}

#[test]
fn test_european_subnet_european_nodes_good() {
let subnet_initial = new_test_subnet_with_overrides(
0,
0,
7,
1,
(
&NodeFeature::Continent,
&["Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe"],
),
)
.with_subnet_id(
PrincipalId::from_str("bkfrj-6k62g-dycql-7h53p-atvkj-zg4to-gaogh-netha-ptybj-ntsgw-rqe").unwrap(),
);
assert_eq!(subnet_initial.check_business_rules().unwrap(), (0, vec![]));
}

#[test]
fn test_european_subnet_european_nodes_bad_1() {
let subnet_mix = new_test_subnet_with_overrides(
1,
0,
7,
1,
(
&NodeFeature::Continent,
&["Europe", "Asia", "Europe", "Europe", "Europe", "Europe", "Europe"],
),
)
.with_subnet_id(
PrincipalId::from_str("bkfrj-6k62g-dycql-7h53p-atvkj-zg4to-gaogh-netha-ptybj-ntsgw-rqe").unwrap(),
);
assert_eq!(
subnet_mix.check_business_rules().unwrap(),
(1000, vec!["European subnet has 1 non-European node(s)".to_string()])
);
}
#[test]
fn test_european_subnet_european_nodes_bad_2() {
let subnet_mix = new_test_subnet_with_overrides(
1,
0,
7,
1,
(
&NodeFeature::Continent,
&[
"Europe",
"Asia",
"America",
"Australia",
"Europe",
"Africa",
"South America",
],
),
)
.with_subnet_id(
PrincipalId::from_str("bkfrj-6k62g-dycql-7h53p-atvkj-zg4to-gaogh-netha-ptybj-ntsgw-rqe").unwrap(),
);
assert_eq!(
subnet_mix.check_business_rules().unwrap(),
(5000, vec!["European subnet has 5 non-European node(s)".to_string()])
);
}
}
32 changes: 31 additions & 1 deletion rs/decentralization/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ struct ReplacementCandidate {
}

impl DecentralizedSubnet {
pub fn with_subnet_id(self, subnet_id: PrincipalId) -> Self {
Self { id: subnet_id, ..self }
}

/// Return a new instance of a DecentralizedSubnet that does not contain the
/// provided nodes.
pub fn without_nodes(&self, nodes: Vec<Node>) -> Result<Self, NetworkError> {
Expand Down Expand Up @@ -251,6 +255,7 @@ impl DecentralizedSubnet {

let nakamoto_scores = Self::_calc_nakamoto_score(nodes);
let subnet_id_str = subnet_id.to_string();
let is_european_subnet = subnet_id_str == *"bkfrj-6k62g-dycql-7h53p-atvkj-zg4to-gaogh-netha-ptybj-ntsgw-rqe";

let dfinity_owned_nodes_count: usize = nodes.iter().map(|n| n.dfinity_owned as usize).sum();
let target_dfinity_owned_nodes_count =
Expand Down Expand Up @@ -305,6 +310,29 @@ impl DecentralizedSubnet {
}
}

if is_european_subnet {
// European subnet should only take European nodes.
let continent_counts = nakamoto_scores.feature_value_counts(&NodeFeature::Continent);
println!("Continent counts: {:?}", continent_counts);
let non_european_nodes_count = continent_counts
.iter()
.filter_map(|(continent, count)| {
if continent == &"Europe".to_string() {
None
} else {
Some(*count)
}
})
.sum::<usize>();
if non_european_nodes_count > 0 {
checks.push(format!(
"European subnet has {} non-European node(s)",
non_european_nodes_count
));
penalties += non_european_nodes_count * 1000;
}
}

match nakamoto_scores.score_feature(&NodeFeature::NodeProvider) {
Some(score) => {
if score <= 1.0 && nodes.len() > 3 {
Expand Down Expand Up @@ -347,7 +375,9 @@ impl DecentralizedSubnet {
nakamoto_scores.controlled_nodes(feature),
) {
(Some(score), Some(controlled_nodes)) => {
if score == 1.0 && controlled_nodes > nodes.len() * 2 / 3 {
let european_subnet_continent_penalty = is_european_subnet && feature == &NodeFeature::Continent;

if score == 1.0 && controlled_nodes > nodes.len() * 2 / 3 && !european_subnet_continent_penalty {
checks.push(format!(
"NodeFeature '{}' controls {} of nodes, which is > {} (2/3 of all) nodes",
feature,
Expand Down
4 changes: 4 additions & 0 deletions rs/ic-management-backend/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ impl RegistryState {
"tECDSA signing",
),
("x33ed-h457x-bsgyx-oqxqf-6pzwv-wkhzr-rm2j3-npodi-purzm-n66cg-gae", "SNS"),
(
"bkfrj-6k62g-dycql-7h53p-atvkj-zg4to-gaogh-netha-ptybj-ntsgw-rqe",
"European",
),
]
.iter()
.map(|(p, name)| {
Expand Down

0 comments on commit 036c0ba

Please sign in to comment.