Skip to content

Commit 46ae511

Browse files
committed
Convert a few collections to sets
1 parent 26d21ef commit 46ae511

4 files changed

Lines changed: 19 additions & 26 deletions

File tree

rs/protobuf/generator/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ fn build_registry_proto(def: &Path, out: &Path) {
243243

244244
config.type_attribute(
245245
".registry.hostos_version",
246-
"#[derive(serde::Serialize, serde::Deserialize)]",
246+
"#[derive(serde::Serialize, serde::Deserialize, Ord, PartialOrd, Eq)]",
247247
);
248248
config.type_attribute(
249249
".registry.node_rewards.v2",

rs/protobuf/src/gen/registry/registry.hostos_version.v1.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
/// accepted by token holders. Nodes can then be upgraded to any of those
66
/// versions. hostos_version_id commonly matches release_package_sha256_hex,
77
/// and is used in the key for this record.
8-
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, ::prost::Message)]
8+
#[derive(
9+
serde::Serialize, serde::Deserialize, Ord, PartialOrd, Eq, Clone, PartialEq, ::prost::Message,
10+
)]
911
pub struct HostosVersionRecord {
1012
/// The URLs against which a HTTP GET request will return a release package
1113
/// that corresponds to this version.

rs/registry/canister/src/invariants/common.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
collections::BTreeMap,
2+
collections::{BTreeMap, BTreeSet},
33
convert::TryFrom,
44
error,
55
fmt::{Display, Formatter, Result as FmtResult},
@@ -62,15 +62,15 @@ pub(crate) fn get_all_node_records(snapshot: &RegistrySnapshot) -> BTreeMap<Node
6262
/// Returns all replica version records in the snapshot.
6363
pub(crate) fn get_all_replica_version_records(
6464
snapshot: &RegistrySnapshot,
65-
) -> Vec<(String, ReplicaVersionRecord)> {
66-
let mut replica_versions = Vec::new();
65+
) -> BTreeMap<String, ReplicaVersionRecord> {
66+
let mut replica_versions = BTreeMap::new();
6767
for (k, v) in snapshot {
6868
if let Some(key) = str::from_utf8(k)
6969
.unwrap()
7070
.strip_prefix(REPLICA_VERSION_KEY_PREFIX)
7171
{
7272
let record = ReplicaVersionRecord::decode(v.as_slice()).unwrap();
73-
replica_versions.push((key.to_owned(), record));
73+
replica_versions.insert(key.to_owned(), record);
7474
}
7575
}
7676

@@ -114,12 +114,12 @@ pub(crate) fn get_all_chain_key_signing_subnet_list_records(
114114
// Retrieve all HostOS version records
115115
pub(crate) fn get_all_hostos_version_records(
116116
snapshot: &RegistrySnapshot,
117-
) -> Vec<HostosVersionRecord> {
118-
let mut result = Vec::new();
117+
) -> BTreeSet<HostosVersionRecord> {
118+
let mut result = BTreeSet::new();
119119
for (k, v) in snapshot {
120120
if k.starts_with(HOSTOS_VERSION_KEY_PREFIX.as_bytes()) {
121121
let hostos_version_record = HostosVersionRecord::decode(v.as_slice()).unwrap();
122-
result.push(hostos_version_record);
122+
result.insert(hostos_version_record);
123123
}
124124
}
125125

@@ -147,8 +147,8 @@ pub(crate) fn get_api_boundary_node_records_from_snapshot(
147147
/// Returns an all api boundary node ids record from the registry snapshot.
148148
pub(crate) fn get_api_boundary_node_ids_from_snapshot(
149149
snapshot: &RegistrySnapshot,
150-
) -> Result<Vec<NodeId>, InvariantCheckError> {
151-
let api_bn_ids: Result<Vec<NodeId>, InvariantCheckError> = snapshot
150+
) -> Result<BTreeSet<NodeId>, InvariantCheckError> {
151+
snapshot
152152
.keys()
153153
.cloned()
154154
.map(|key| {
@@ -157,15 +157,13 @@ pub(crate) fn get_api_boundary_node_ids_from_snapshot(
157157
source: None,
158158
})
159159
})
160-
.collect::<Result<Vec<String>, InvariantCheckError>>()
160+
.collect::<Result<BTreeSet<String>, InvariantCheckError>>()
161161
.map(|keys| {
162162
keys.into_iter()
163163
.filter_map(|key_str| get_api_boundary_node_record_node_id(&key_str))
164164
.map(NodeId::from)
165165
.collect()
166-
});
167-
168-
api_bn_ids
166+
})
169167
}
170168

171169
/// Returns node record from the snapshot corresponding to a key.
@@ -185,7 +183,7 @@ pub(crate) fn get_node_record_from_snapshot(
185183
.transpose()
186184
}
187185

188-
pub(crate) fn get_subnet_ids_from_snapshot(snapshot: &RegistrySnapshot) -> Vec<SubnetId> {
186+
pub(crate) fn get_subnet_ids_from_snapshot(snapshot: &RegistrySnapshot) -> BTreeSet<SubnetId> {
189187
get_value_from_snapshot::<SubnetListRecord>(snapshot, make_subnet_list_record_key())
190188
.map(|r| {
191189
r.subnets

rs/registry/canister/src/invariants/replica_version.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,9 @@ pub(crate) fn check_replica_version_invariants(
4242
}
4343
versions_in_use.append(&mut get_all_api_boundary_node_versions(snapshot));
4444

45-
let elected_version_ids = get_all_replica_version_records(snapshot)
46-
.into_iter()
47-
.map(|(k, _)| k);
48-
49-
let num_elected = elected_version_ids.len();
50-
let elected_set = BTreeSet::from_iter(elected_version_ids);
51-
assert!(
52-
elected_set.len() == num_elected,
53-
"A version was elected multiple times."
54-
);
45+
let elected_set: BTreeSet<_> = get_all_replica_version_records(snapshot)
46+
.into_keys()
47+
.collect();
5548
assert!(
5649
elected_set.is_superset(&versions_in_use),
5750
"Using a version that isn't elected. Elected versions: {elected_set:?}, in use: {versions_in_use:?}."

0 commit comments

Comments
 (0)