Skip to content

Commit

Permalink
chore: simplify names
Browse files Browse the repository at this point in the history
  • Loading branch information
IDX GitLab Automation committed Nov 23, 2024
1 parent bb8450b commit 6b0a377
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 89 deletions.
64 changes: 32 additions & 32 deletions rs/boundary_node/rate_limits/canister/add_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
storage::StorableIncidentMetadata,
storage::StorableIncident,
types::{self, AddConfigError, IncidentId, Timestamp},
};
use anyhow::{anyhow, Context};
Expand All @@ -10,7 +10,7 @@ use uuid::Uuid;

use crate::{
state::CanisterApi,
storage::{StorableConfig, StorableRuleMetadata},
storage::{StorableConfig, StorableRule},
types::{InputConfig, RuleId, Version},
};

Expand Down Expand Up @@ -94,8 +94,8 @@ impl<A: CanisterApi> AddsConfig for ConfigAdder<A> {

// Ordered IDs of all rules in the submitted config
let mut rule_ids = Vec::<RuleId>::new();
// Metadata of the newly submitted rules
let mut new_rules_metadata = Vec::<(RuleId, StorableRuleMetadata)>::new();
// Newly submitted rules
let mut new_rules = Vec::<(RuleId, StorableRule)>::new();
// Hashmap of the submitted incident IDs
let mut incidents_map = HashMap::<IncidentId, HashSet<RuleId>>::new();

Expand Down Expand Up @@ -130,7 +130,7 @@ impl<A: CanisterApi> AddsConfig for ConfigAdder<A> {
}
}

let rule_metadata = StorableRuleMetadata {
let rule = StorableRule {
incident_id: input_rule.incident_id,
rule_raw: input_rule.rule_raw.clone(),
description: input_rule.description.clone(),
Expand All @@ -139,7 +139,7 @@ impl<A: CanisterApi> AddsConfig for ConfigAdder<A> {
removed_in_version: None,
};

new_rules_metadata.push((rule_id, rule_metadata));
new_rules.push((rule_id, rule));

rule_id
};
Expand Down Expand Up @@ -172,7 +172,7 @@ impl<A: CanisterApi> AddsConfig for ConfigAdder<A> {
next_version,
storable_config,
removed_rule_ids,
new_rules_metadata,
new_rules,
incidents_map,
);

Expand All @@ -194,38 +194,38 @@ fn commit_changes(
next_version: u64,
storable_config: StorableConfig,
removed_rules: Vec<RuleId>,
new_rules_metadata: Vec<(RuleId, StorableRuleMetadata)>,
new_rules: Vec<(RuleId, StorableRule)>,
incidents_map: HashMap<IncidentId, HashSet<RuleId>>,
) {
// Update metadata of the removed rules in the stable memory
for rule_id in removed_rules {
// Rule should exist, it was already checked before.
let mut rule_metadata = canister_api
let mut rule = canister_api
.get_rule(&rule_id)
.expect("inconsistent state, rule_id={rule_id} not found");
rule_metadata.removed_in_version = Some(next_version);
canister_api.upsert_rule(rule_id, rule_metadata);
rule.removed_in_version = Some(next_version);
canister_api.upsert_rule(rule_id, rule);
}

// Add new rules to the stable memory
for (rule_id, rule_metadata) in new_rules_metadata {
canister_api.upsert_rule(rule_id, rule_metadata);
for (rule_id, rule) in new_rules {
canister_api.upsert_rule(rule_id, rule);
}

// Upsert incidents to the stable memory, some of the incidents can be new, some already existed before
for (incident_id, rule_ids) in incidents_map {
let incident_metadata = canister_api
let incident = canister_api
.get_incident(&incident_id)
.map(|mut metadata| {
metadata.rule_ids.extend(rule_ids.clone());
metadata
.map(|mut stored_incident| {
stored_incident.rule_ids.extend(rule_ids.clone());
stored_incident
})
.unwrap_or_else(|| StorableIncidentMetadata {
.unwrap_or_else(|| StorableIncident {
is_disclosed: false,
rule_ids: rule_ids.clone(),
});

canister_api.upsert_incident(incident_id, incident_metadata);
canister_api.upsert_incident(incident_id, incident);
}

// Add a new config to the stable memory
Expand All @@ -243,7 +243,7 @@ mod tests {
struct FullConfig {
schema_version: api::SchemaVersion,
active_since: api::Timestamp,
rules: Vec<StorableRuleMetadata>,
rules: Vec<StorableRule>,
}

fn retrieve_full_config(canister_api: impl CanisterApi, version: u64) -> FullConfig {
Expand Down Expand Up @@ -408,15 +408,15 @@ mod tests {
schema_version: 1,
active_since: current_time,
rules: vec![
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_1,
rule_raw: b"{\"a\": 1, \"b\": 2}".to_vec(),
description: "best rule #1 ever".to_string(),
disclosed_at: None,
added_in_version: 2,
removed_in_version: Some(5),
},
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_1,
rule_raw: b"{\"c\": 3, \"d\": 4}".to_vec(),
description: "best rule #2 ever".to_string(),
Expand All @@ -435,15 +435,15 @@ mod tests {
schema_version: 2,
active_since: current_time + 1,
rules: vec![
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_1,
rule_raw: b"{\"c\": 3, \"d\": 4}".to_vec(),
description: "best rule #2 ever".to_string(),
disclosed_at: None,
added_in_version: 2,
removed_in_version: Some(6),
},
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_1,
rule_raw: b"{\"a\": 1, \"b\": 2}".to_vec(),
description: "best rule #1 ever".to_string(),
Expand All @@ -462,23 +462,23 @@ mod tests {
schema_version: 2,
active_since: current_time + 2,
rules: vec![
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_1,
rule_raw: b"{\"a\": 1, \"b\": 2}".to_vec(),
description: "best rule #1 ever".to_string(),
disclosed_at: None,
added_in_version: 2,
removed_in_version: Some(5),
},
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_2,
rule_raw: b"{}".to_vec(),
description: "best rule #3 ever".to_string(),
disclosed_at: None,
added_in_version: 4,
removed_in_version: Some(5),
},
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_1,
rule_raw: b"{\"c\": 3, \"d\": 4}".to_vec(),
description: "best rule #2 ever".to_string(),
Expand All @@ -497,23 +497,23 @@ mod tests {
schema_version: 2,
active_since: current_time + 3,
rules: vec![
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_2,
rule_raw: b"{\"e\": 5, \"f\": 6}".to_vec(),
description: "best rate-limit rule #4 ever".to_string(),
disclosed_at: None,
added_in_version: 5,
removed_in_version: Some(6),
},
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_1,
rule_raw: b"{\"c\": 3, \"d\": 4}".to_vec(),
description: "best rule #2 ever".to_string(),
disclosed_at: None,
added_in_version: 2,
removed_in_version: Some(6),
},
StorableRuleMetadata {
StorableRule {
incident_id: incident_id_3,
rule_raw: b"{\"g\": 7, \"e\": 8}".to_vec(),
description: "best rate-limit rule #5 ever".to_string(),
Expand Down Expand Up @@ -615,12 +615,12 @@ mod tests {
);
let incident_id_1 = IncidentId(Uuid::new_v4());
let incident_id_2 = IncidentId(Uuid::new_v4());
let storable_incident_1 = StorableIncidentMetadata {
let storable_incident_1 = StorableIncident {
is_disclosed: false,
rule_ids: HashSet::new(),
};
// This incident is disclosed, new rules can't be linked to it anymore. \
let storable_incident_2 = StorableIncidentMetadata {
let storable_incident_2 = StorableIncident {
is_disclosed: true,
rule_ids: HashSet::new(),
};
Expand Down
54 changes: 27 additions & 27 deletions rs/boundary_node/rate_limits/canister/disclose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ fn disclose_rules(
) -> Result<(), DiscloseRulesError> {
let mut rules = Vec::with_capacity(rule_ids.len());

// Validate and collect rule metadata, failing fast on first missing rule
// Validate and collect rules, failing fast on first missing rule
for rule_id in rule_ids.iter() {
let rule_metadata = canister_api
let rule = canister_api
.get_rule(rule_id)
.ok_or_else(|| DiscloseRulesError::RuleIdNotFound(*rule_id))?;
rules.push((rule_id, rule_metadata));
rules.push((rule_id, rule));
}

// Update disclosed rules, marking them with current timestamp
for (rule_id, mut metadata) in rules {
if metadata.disclosed_at.is_none() {
metadata.disclosed_at = Some(time);
canister_api.upsert_rule(*rule_id, metadata);
for (rule_id, mut rule) in rules {
if rule.disclosed_at.is_none() {
rule.disclosed_at = Some(time);
canister_api.upsert_rule(*rule_id, rule);
}
}

Expand All @@ -85,25 +85,25 @@ fn disclose_incidents(
time: Timestamp,
incident_ids: &[IncidentId],
) -> Result<(), DiscloseRulesError> {
let mut incidents_metadata: Vec<(_, _)> = Vec::with_capacity(incident_ids.len());
let mut incidents: Vec<(_, _)> = Vec::with_capacity(incident_ids.len());

// Validate and collect incident metadata, failing fast on first missing incident
// Validate and collect incidents, failing fast on first missing incident
for incident_id in incident_ids.iter() {
let incident_metadata = canister_api
let incident = canister_api
.get_incident(incident_id)
.ok_or_else(|| DiscloseRulesError::IncidentIdNotFound(*incident_id))?;
incidents_metadata.push((*incident_id, incident_metadata));
incidents.push((*incident_id, incident));
}

// Disclose incidents and their associated rules
for (incident_id, mut metadata) in incidents_metadata {
if !metadata.is_disclosed {
for (incident_id, mut incident) in incidents {
if !incident.is_disclosed {
// Ensure all associated rules are disclosed first
let rule_ids: Vec<RuleId> = metadata.rule_ids.iter().cloned().collect();
let rule_ids: Vec<RuleId> = incident.rule_ids.iter().cloned().collect();
disclose_rules(canister_api, time, &rule_ids)?;
// Mark incident as disclosed too
metadata.is_disclosed = true;
canister_api.upsert_incident(incident_id, metadata);
incident.is_disclosed = true;
canister_api.upsert_incident(incident_id, incident);
}
}

Expand All @@ -121,11 +121,11 @@ mod tests {
use std::collections::HashSet;

use crate::state::MockCanisterApi;
use crate::storage::{StorableIncidentMetadata, StorableRuleMetadata};
use crate::storage::{StorableIncident, StorableRule};

// Helper to create a mock rule metadata
fn create_mock_rule_metadata(disclosed_at: Option<Timestamp>) -> StorableRuleMetadata {
StorableRuleMetadata {
// Helper to create a mock rule
fn create_mock_rule(disclosed_at: Option<Timestamp>) -> StorableRule {
StorableRule {
incident_id: IncidentId(Uuid::new_v4()),
rule_raw: vec![],
description: "".to_string(),
Expand All @@ -149,20 +149,20 @@ mod tests {
let rule_id_4 = RuleId(Uuid::new_v4());
let rule_id_5 = RuleId(Uuid::new_v4());
// Two rules are disclosed, and three are not
let rule_1 = create_mock_rule_metadata(None);
let rule_2 = create_mock_rule_metadata(Some(25u64));
let rule_3 = create_mock_rule_metadata(None);
let rule_4 = create_mock_rule_metadata(Some(30u64));
let rule_5 = create_mock_rule_metadata(None);
let rule_1 = create_mock_rule(None);
let rule_2 = create_mock_rule(Some(25u64));
let rule_3 = create_mock_rule(None);
let rule_4 = create_mock_rule(Some(30u64));
let rule_5 = create_mock_rule(None);
// Two incidents
let incident_id_1 = IncidentId(Uuid::new_v4());
let incident_id_2 = IncidentId(Uuid::new_v4());
// One incident is disclosed and one is not
let incident_1 = StorableIncidentMetadata {
let incident_1 = StorableIncident {
is_disclosed: false,
rule_ids: HashSet::from_iter(vec![rule_id_1, rule_id_2, rule_id_3]),
};
let incident_2 = StorableIncidentMetadata {
let incident_2 = StorableIncident {
is_disclosed: true,
rule_ids: HashSet::from_iter(vec![rule_id_4]),
};
Expand Down
14 changes: 7 additions & 7 deletions rs/boundary_node/rate_limits/canister/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod tests {
ConfigConfidentialityFormatter, RuleConfidentialityFormatter,
};
use crate::state::CanisterState;
use crate::storage::{StorableConfig, StorableIncidentMetadata, StorableRuleMetadata};
use crate::storage::{StorableConfig, StorableIncident, StorableRule};
use std::collections::HashSet;

use super::*;
Expand Down Expand Up @@ -286,7 +286,7 @@ mod tests {
// One rule is not disclosed
canister_state.upsert_rule(
rule_id_1,
StorableRuleMetadata {
StorableRule {
incident_id,
rule_raw: b"{\"a\": 1}".to_vec(),
description: "verbose description 1".to_string(),
Expand All @@ -299,7 +299,7 @@ mod tests {
// One rule is disclosed
canister_state.upsert_rule(
rule_id_2,
StorableRuleMetadata {
StorableRule {
incident_id,
rule_raw: b"{\"b\": 2}".to_vec(),
description: "verbose description 2".to_string(),
Expand Down Expand Up @@ -388,7 +388,7 @@ mod tests {

canister_state.upsert_rule(
rule_id,
StorableRuleMetadata {
StorableRule {
incident_id,
rule_raw: b"{\"a\": 1}".to_vec(),
description: "verbose description".to_string(),
Expand Down Expand Up @@ -453,7 +453,7 @@ mod tests {
// One rule is not disclosed
canister_state.upsert_rule(
rule_id_1,
StorableRuleMetadata {
StorableRule {
incident_id,
rule_raw: b"{\"a\": 1}".to_vec(),
description: "verbose description 1".to_string(),
Expand All @@ -465,7 +465,7 @@ mod tests {
// One rule is disclosed
canister_state.upsert_rule(
rule_id_2,
StorableRuleMetadata {
StorableRule {
incident_id,
rule_raw: b"{\"b\": 2}".to_vec(),
description: "verbose description 2".to_string(),
Expand All @@ -476,7 +476,7 @@ mod tests {
);
canister_state.upsert_incident(
incident_id,
StorableIncidentMetadata {
StorableIncident {
is_disclosed: false,
rule_ids: HashSet::from_iter(vec![rule_id_1, rule_id_2]),
},
Expand Down
2 changes: 1 addition & 1 deletion rs/boundary_node/rate_limits/canister/interface.did
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type DiscloseRulesArg = variant {

// Configuration containing a list of rules that replaces the current configuration
type InputConfig = record {
schema_version: SchemaVersion; // schema version used to serialized the rules
schema_version: SchemaVersion; // JSON schema version of the rate-limit rule
rules: vec InputRule;
};

Expand Down
Loading

0 comments on commit 6b0a377

Please sign in to comment.