From bdb7c71b590e33040fa721eac1936ab108a65a96 Mon Sep 17 00:00:00 2001 From: jasonz-dfinity <133917836+jasonz-dfinity@users.noreply.github.com> Date: Sat, 14 Dec 2024 09:16:20 -0800 Subject: [PATCH] feat(nns): Move neuron validation to timer (#3181) We prefer timer to heartbeat in general, and the neuron validation can be scheduled in a fixed interval. Therefore we should just move it to timer as a low hanging fruit. --- rs/nns/governance/canister/canister.rs | 8 ++++++++ rs/nns/governance/src/governance.rs | 19 +++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/rs/nns/governance/canister/canister.rs b/rs/nns/governance/canister/canister.rs index 3052eab193b..d2e351bf410 100644 --- a/rs/nns/governance/canister/canister.rs +++ b/rs/nns/governance/canister/canister.rs @@ -168,6 +168,7 @@ fn schedule_timers() { schedule_prune_following(Duration::from_secs(0), Bound::Unbounded); schedule_spawn_neurons(); schedule_unstake_maturity_of_dissolved_neurons(); + schedule_neuron_data_validation(); schedule_vote_processing(); // TODO(NNS1-3446): Delete. (This only needs to be run once, but can safely be run multiple times). @@ -314,6 +315,13 @@ fn schedule_unstake_maturity_of_dissolved_neurons() { }); } +const NEURON_DATA_VALIDATION_INTERNVAL: Duration = Duration::from_secs(5); +fn schedule_neuron_data_validation() { + ic_cdk_timers::set_timer_interval(NEURON_DATA_VALIDATION_INTERNVAL, || { + governance_mut().maybe_run_validations(); + }); +} + /// The interval at which the voting state machines are processed. const VOTE_PROCESSING_INTERVAL: Duration = Duration::from_secs(3); diff --git a/rs/nns/governance/src/governance.rs b/rs/nns/governance/src/governance.rs index 04e81860318..f92ddad4de0 100644 --- a/rs/nns/governance/src/governance.rs +++ b/rs/nns/governance/src/governance.rs @@ -6632,15 +6632,6 @@ impl Governance { )); } - fn maybe_run_validations(&mut self) { - // Running validations might increase heap size. Do not run it when heap should not grow. - if self.check_heap_can_grow().is_err() { - return; - } - self.neuron_data_validator - .maybe_validate(self.env.now(), &self.neuron_store); - } - /// Increment neuron allowances if enough time has passed. fn maybe_increase_neuron_allowances(&mut self) { // We increase the allowance over the maximum per hour to account @@ -6739,7 +6730,6 @@ impl Governance { self.maybe_gc(); self.maybe_run_migrations(); - self.maybe_run_validations(); self.maybe_increase_neuron_allowances(); } @@ -6810,6 +6800,15 @@ impl Governance { Ok(()) } + pub fn maybe_run_validations(&mut self) { + // Running validations might increase heap size. Do not run it when heap should not grow. + if self.check_heap_can_grow().is_err() { + return; + } + self.neuron_data_validator + .maybe_validate(self.env.now(), &self.neuron_store); + } + /// Returns the 30-day average of the ICP/XDR conversion rate. /// /// Returns `None` if the data has not been fetched from the CMC canister yet.