Skip to content

Commit

Permalink
🤖 cargo-fmt auto-update
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Oct 18, 2024
1 parent 8145da8 commit 0fce48f
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/internet_identity/src/storage/registration_rates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,16 @@ impl<M: Memory> RegistrationRates<M> {
};

let now = time();
self.reference_rate_data
.push(&now)
.expect("out of memory");
self.current_rate_data
.push(&now)
.expect("out of memory");
self.reference_rate_data.push(&now).expect("out of memory");
self.current_rate_data.push(&now).expect("out of memory");
}

pub fn registration_rates(&self) -> Option<NormalizedRegistrationRates> {
let config = dynamic_captcha_config()?;
let now = time();

let reference_rate_per_second = calculate_registration_rate(
now,
&self.reference_rate_data,
);
let current_rate_per_second = calculate_registration_rate(
now,
&self.current_rate_data,
);
let reference_rate_per_second = calculate_registration_rate(now, &self.reference_rate_data);
let current_rate_per_second = calculate_registration_rate(now, &self.current_rate_data);
let captcha_threshold_rate = reference_rate_per_second * config.threshold_multiplier;
let rates = NormalizedRegistrationRates {
reference_rate_per_second,
Expand All @@ -87,9 +77,18 @@ impl<M: Memory> RegistrationRates<M> {
return;
};
println!("in da prun_expired");
println!("reference_rate {}", data_retention.reference_rate_retention_ns.to_string());
prune_data(&mut self.reference_rate_data, data_retention.reference_rate_retention_ns);
prune_data(&mut self.current_rate_data, data_retention.current_rate_retention_ns);
println!(
"reference_rate {}",
data_retention.reference_rate_retention_ns.to_string()
);
prune_data(
&mut self.reference_rate_data,
data_retention.reference_rate_retention_ns,
);
prune_data(
&mut self.current_rate_data,
data_retention.current_rate_retention_ns,
);
}
}

Expand All @@ -112,10 +111,7 @@ impl<M: Memory> RegistrationRates<M> {
/// However, because the data is not actually spanning 3 weeks, this underestimates the actual rate.
/// Taking into account that the data is only spanning 3 days we get the following:
/// 3 registrations / 259200 seconds = 0.00001157407407 registrations / second
fn calculate_registration_rate<M: Memory>(
now: u64,
data: &MinHeap<Timestamp, M>,
) -> f64 {
fn calculate_registration_rate<M: Memory>(now: u64, data: &MinHeap<Timestamp, M>) -> f64 {
data
// get the oldest value
.peek()
Expand Down Expand Up @@ -339,7 +335,7 @@ mod test {
NormalizedRegistrationRates {
current_rate_per_second: 2.0,
reference_rate_per_second: 2.0, // increases too, but more slowly
captcha_threshold_rate: 2.2, // reference rate * 1.1 (as per config)
captcha_threshold_rate: 2.2, // reference rate * 1.1 (as per config)
}
);
}
Expand Down

0 comments on commit 0fce48f

Please sign in to comment.