Skip to content

Commit

Permalink
feat(nns): Move neuron validation to timer (#3181)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jasonz-dfinity authored Dec 14, 2024
1 parent 34c3268 commit bdb7c71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
8 changes: 8 additions & 0 deletions rs/nns/governance/canister/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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);

Expand Down
19 changes: 9 additions & 10 deletions rs/nns/governance/src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -6739,7 +6730,6 @@ impl Governance {

self.maybe_gc();
self.maybe_run_migrations();
self.maybe_run_validations();
self.maybe_increase_neuron_allowances();
}

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit bdb7c71

Please sign in to comment.