Skip to content

Commit

Permalink
fix(backend): Make more operations deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-tomic committed Jun 14, 2023
1 parent 2cf0f98 commit c4f7745
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
20 changes: 10 additions & 10 deletions rs/decentralization/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod nakamoto;
pub mod network;
use colored::Colorize;
use itertools::{EitherOrBoth::*, Itertools};
use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};

use ic_base_types::PrincipalId;
Expand All @@ -20,11 +20,11 @@ pub struct SubnetChangeResponse {
pub motivation: Option<String>,
pub comment: Option<String>,
pub run_log: Option<Vec<String>>,
pub feature_diff: HashMap<NodeFeature, FeatureDiff>,
pub feature_diff: BTreeMap<NodeFeature, FeatureDiff>,
pub proposal_id: Option<u64>,
}

pub type FeatureDiff = HashMap<String, (usize, usize)>;
pub type FeatureDiff = BTreeMap<String, (usize, usize)>;

impl SubnetChangeResponse {
pub fn with_motivation(self, motivation: String) -> Self {
Expand Down Expand Up @@ -55,7 +55,7 @@ impl From<&network::SubnetChange> for SubnetChangeResponse {
NodeFeature::variants()
.into_iter()
.map(|f| (f, FeatureDiff::new()))
.collect::<HashMap<NodeFeature, FeatureDiff>>(),
.collect::<BTreeMap<NodeFeature, FeatureDiff>>(),
|mut acc, n| {
for f in NodeFeature::variants() {
acc.get_mut(&f).unwrap().entry(n.get_feature(&f)).or_insert((0, 0)).0 += 1;
Expand Down Expand Up @@ -126,25 +126,25 @@ impl Display for SubnetChangeResponse {
}
)?;

let feature_diff = BTreeMap::from_iter(self.feature_diff.iter());
let rows = feature_diff.values().map(|diff| diff.len()).max().unwrap_or(0);
let rows = self.feature_diff.values().map(|diff| diff.len()).max().unwrap_or(0);
let mut table = tabular::Table::new(
&feature_diff
&self
.feature_diff
.keys()
.map(|_| " {:<} {:>}")
.collect::<Vec<_>>()
.join(""),
);
table.add_row(
feature_diff
self.feature_diff
.keys()
.fold(tabular::Row::new(), |acc, k| acc.with_cell(k.to_string()).with_cell("")),
);
table.add_row(feature_diff.keys().fold(tabular::Row::new(), |acc, k| {
table.add_row(self.feature_diff.keys().fold(tabular::Row::new(), |acc, k| {
acc.with_cell("-".repeat(k.to_string().len())).with_cell("")
}));
for i in 0..rows {
table.add_row(feature_diff.values().fold(tabular::Row::new(), |acc, v| {
table.add_row(self.feature_diff.values().fold(tabular::Row::new(), |acc, v| {
let (value, change) = v
.iter()
.sorted()
Expand Down
12 changes: 6 additions & 6 deletions rs/decentralization/src/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use counter::Counter;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::fmt::{Debug, Display, Formatter};
use std::iter::{FromIterator, IntoIterator};

use ic_management_types::NodeFeature;

#[derive(Eq, PartialEq, Clone, Serialize, Deserialize, Debug)]
pub struct NodeFeatures {
pub feature_map: HashMap<NodeFeature, String>,
pub feature_map: BTreeMap<NodeFeature, String>,
}

impl NodeFeatures {
Expand All @@ -21,7 +21,7 @@ impl NodeFeatures {

#[cfg(test)]
fn new_test_feature_set(value: &str) -> Self {
let mut result = HashMap::new();
let mut result = BTreeMap::new();
for feature in NodeFeature::variants() {
result.insert(feature, value.to_string());
}
Expand All @@ -39,15 +39,15 @@ impl NodeFeatures {
impl FromIterator<(NodeFeature, &'static str)> for NodeFeatures {
fn from_iter<I: IntoIterator<Item = (NodeFeature, &'static str)>>(iter: I) -> Self {
Self {
feature_map: HashMap::from_iter(iter.into_iter().map(|x| (x.0, String::from(x.1)))),
feature_map: BTreeMap::from_iter(iter.into_iter().map(|x| (x.0, String::from(x.1)))),
}
}
}

impl FromIterator<(NodeFeature, std::string::String)> for NodeFeatures {
fn from_iter<I: IntoIterator<Item = (NodeFeature, std::string::String)>>(iter: I) -> Self {
Self {
feature_map: HashMap::from_iter(iter),
feature_map: BTreeMap::from_iter(iter),
}
}
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl NakamotoScore {
features_to_nodes_map.insert(feature, Vec::new());
}

// Convert a Vec<HashMap<NodeFeature, Value>> into a Vec<HashMap<NodeFeature,
// Convert a Vec<BTreeMap<NodeFeature, Value>> into a Vec<BTreeMap<NodeFeature,
// Vec<Values>>
for node_features in slice_node_features.iter() {
for feature in NodeFeature::variants() {
Expand Down
6 changes: 3 additions & 3 deletions rs/ic-management-backend/src/health.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, convert::TryInto, str::FromStr};
use std::{collections::BTreeMap, convert::TryInto, str::FromStr};

use ic_base_types::PrincipalId;
use ic_management_types::{Network, Status};
Expand All @@ -19,7 +19,7 @@ impl HealthClient {
}
}

pub async fn subnet(&self, subnet: PrincipalId) -> anyhow::Result<HashMap<PrincipalId, Status>> {
pub async fn subnet(&self, subnet: PrincipalId) -> anyhow::Result<BTreeMap<PrincipalId, Status>> {
let query: InstantVector = Selector::new()
.metric("up")
.with("ic", &self.network.legacy_name())
Expand All @@ -45,7 +45,7 @@ impl HealthClient {
.collect())
}

pub async fn nodes(&self) -> anyhow::Result<HashMap<PrincipalId, Status>> {
pub async fn nodes(&self) -> anyhow::Result<BTreeMap<PrincipalId, Status>> {
let query: InstantVector = InstantVector(format!(
r#"
label_replace(
Expand Down
28 changes: 14 additions & 14 deletions rs/ic-management-backend/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use itertools::Itertools;
use std::convert::TryFrom;
use std::sync::Arc;
use std::{
collections::{BTreeMap, HashMap, HashSet},
collections::{BTreeMap, BTreeSet},
net::Ipv6Addr,
};

Expand Down Expand Up @@ -80,12 +80,12 @@ impl RegistryEntry for SubnetRecord {
}

trait RegistryFamilyEntries {
fn get_family_entries<T: RegistryEntry + Default>(&self) -> Result<HashMap<String, T>>;
fn get_family_entries_versioned<T: RegistryEntry + Default>(&self) -> Result<HashMap<String, (u64, T)>>;
fn get_family_entries<T: RegistryEntry + Default>(&self) -> Result<BTreeMap<String, T>>;
fn get_family_entries_versioned<T: RegistryEntry + Default>(&self) -> Result<BTreeMap<String, (u64, T)>>;
}

impl RegistryFamilyEntries for LocalRegistry {
fn get_family_entries<T: RegistryEntry + Default>(&self) -> Result<HashMap<String, T>> {
fn get_family_entries<T: RegistryEntry + Default>(&self) -> Result<BTreeMap<String, T>> {
let prefix_length = T::KEY_PREFIX.len();
Ok(self
.get_key_family(T::KEY_PREFIX, self.get_latest_version())?
Expand All @@ -100,10 +100,10 @@ impl RegistryFamilyEntries for LocalRegistry {
)
})
})
.collect::<HashMap<_, _>>())
.collect::<BTreeMap<_, _>>())
}

fn get_family_entries_versioned<T: RegistryEntry + Default>(&self) -> Result<HashMap<String, (u64, T)>> {
fn get_family_entries_versioned<T: RegistryEntry + Default>(&self) -> Result<BTreeMap<String, (u64, T)>> {
let prefix_length = T::KEY_PREFIX.len();
Ok(self
.get_key_family(T::KEY_PREFIX, self.get_latest_version())?
Expand All @@ -123,7 +123,7 @@ impl RegistryFamilyEntries for LocalRegistry {
})
.unwrap_or_else(|_| panic!("failed to get entry {} for type {}", key, std::any::type_name::<T>()))
})
.collect::<HashMap<_, _>>())
.collect::<BTreeMap<_, _>>())
}
}

Expand Down Expand Up @@ -301,9 +301,9 @@ impl RegistryState {
let providers = providers
.into_iter()
.map(|p| (p.principal_id, p))
.collect::<HashMap<_, _>>();
let data_center_records: HashMap<String, DataCenterRecord> = self.local_registry.get_family_entries()?;
let operator_records: HashMap<String, NodeOperatorRecord> = self.local_registry.get_family_entries()?;
.collect::<BTreeMap<_, _>>();
let data_center_records: BTreeMap<String, DataCenterRecord> = self.local_registry.get_family_entries()?;
let operator_records: BTreeMap<String, NodeOperatorRecord> = self.local_registry.get_family_entries()?;

self.operators = operator_records
.iter()
Expand Down Expand Up @@ -503,7 +503,7 @@ impl RegistryState {
self.nodes.clone()
}

pub async fn nodes_with_proposals(&self) -> Result<HashMap<PrincipalId, Node>> {
pub async fn nodes_with_proposals(&self) -> Result<BTreeMap<PrincipalId, Node>> {
let nodes = self.nodes.clone();
let proposal_agent = proposal::ProposalAgent::new(self.nns_url.clone());

Expand All @@ -522,7 +522,7 @@ impl RegistryState {
.collect())
}

pub async fn subnets_with_proposals(&self) -> Result<HashMap<PrincipalId, Subnet>> {
pub async fn subnets_with_proposals(&self) -> Result<BTreeMap<PrincipalId, Subnet>> {
let subnets = self.subnets.clone();
let proposal_agent = proposal::ProposalAgent::new(self.nns_url.clone());

Expand Down Expand Up @@ -555,7 +555,7 @@ impl RegistryState {
.unique()
.take(NUM_RELEASE_BRANCHES_TO_KEEP)
.collect::<Vec<_>>();
let subnet_versions: HashSet<String> = self.subnets.values().map(|s| s.replica_version.clone()).collect();
let subnet_versions: BTreeSet<String> = self.subnets.values().map(|s| s.replica_version.clone()).collect();
let version_on_unassigned_nodes = self.get_unassigned_nodes_version().await?;
Ok(self
.replica_releases
Expand Down Expand Up @@ -645,7 +645,7 @@ impl SubnetQuerier for RegistryState {
.to_vec()
.iter()
.map(|n| self.nodes.get(n).and_then(|n| n.subnet))
.collect::<HashSet<_>>();
.collect::<BTreeSet<_>>();
if subnets.len() > 1 {
return Err(NetworkError::IllegalRequest(
"nodes don't belong to the same subnet".to_string(),
Expand Down

0 comments on commit c4f7745

Please sign in to comment.