Skip to content

Commit

Permalink
Merge branch 'jason/NNS1-2935-8' into 'master'
Browse files Browse the repository at this point in the history
refactor(nns): NNS1-2935 Make neuron state & age fields private

We'd like to make neuron state & age fields private so that the relevant invariants can be guaranteed

Closes NNS1-2935 

Closes NNS1-2935

See merge request dfinity-lab/public/ic!19056
  • Loading branch information
jasonz-dfinity committed May 7, 2024
2 parents 64e8f52 + 06f14a7 commit baf21f5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 140 deletions.
22 changes: 22 additions & 0 deletions rs/nns/governance/src/neuron/dissolve_state_and_age.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ impl DissolveStateAndAge {
}
}

/// Returns the timestamp when the neuron will be dissolved. If the neuron is not dissolving, it
/// returns None. Note that when self == LegacyDissolved {..}, even though the Neuron is
/// dissolved, we do not know when that happened. This tends to happen when Neurons are first
/// created. We will clean up this case soon.
pub fn dissolved_at_timestamp_seconds(self) -> Option<u64> {
match self {
Self::NotDissolving { .. } => None,
Self::DissolvingOrDissolved {
when_dissolved_timestamp_seconds,
} => Some(when_dissolved_timestamp_seconds),
Self::LegacyDissolvingOrDissolved {
when_dissolved_timestamp_seconds,
..
} => Some(when_dissolved_timestamp_seconds),
// The dissolved neurons in this case have DissolveDelaySeconds(0), which are created
// when the neurons are first claimed. We don't know exactly when they are dissolved
// from their dissolve state.
Self::LegacyDissolved { .. } => None,
Self::LegacyNoneDissolveState { .. } => None,
}
}

/// Increases the dissolve delay of the neuron by the given number of seconds. If the neuron is
/// already dissolved, it transitions to a non-dissolving state with the new dissolve delay. If
/// the neuron is dissolving, the dissolve timestamp is increased by the given number of
Expand Down
23 changes: 2 additions & 21 deletions rs/nns/governance/src/neuron/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,27 +769,8 @@ impl Neuron {
/// WhenDissolvedTimestampSeconds(now()), but we didn't. This could be changed for new Neurons,
/// but there is no intention to do that (yet).
pub fn dissolved_at_timestamp_seconds(&self) -> Option<u64> {
use DissolveState::{DissolveDelaySeconds, WhenDissolvedTimestampSeconds};
match self.dissolve_state {
None => None,
Some(WhenDissolvedTimestampSeconds(result)) => Some(result),
Some(DissolveDelaySeconds(_)) => None,
}
}

/// Returns an enum representing the dissolve state and age of a neuron.
pub fn dissolve_state_and_age(&self) -> DissolveStateAndAge {
DissolveStateAndAge::from(StoredDissolvedStateAndAge {
dissolve_state: self.dissolve_state.clone(),
aging_since_timestamp_seconds: self.aging_since_timestamp_seconds,
})
}

/// Sets a neuron's dissolve state and age.
pub fn set_dissolve_state_and_age(&mut self, state_and_age: DissolveStateAndAge) {
let stored = StoredDissolvedStateAndAge::from(state_and_age);
self.dissolve_state = stored.dissolve_state;
self.aging_since_timestamp_seconds = stored.aging_since_timestamp_seconds;
self.dissolve_state_and_age()
.dissolved_at_timestamp_seconds()
}

pub fn subtract_staked_maturity(&mut self, amount_e8s: u64) {
Expand Down
19 changes: 17 additions & 2 deletions rs/nns/governance/src/neuron/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct Neuron {
/// This value is meaningless when the neuron is dissolving, since a
/// dissolving neurons always has age zero. The canonical value of
/// this field for a dissolving neuron is `u64::MAX`.
pub aging_since_timestamp_seconds: u64,
aging_since_timestamp_seconds: u64,
/// The timestamp, in seconds from the Unix epoch, at which this
/// neuron should be spawned and its maturity converted to ICP
/// according to <https://wiki.internetcomputer.org/wiki/Maturity_modulation.>
Expand Down Expand Up @@ -120,7 +120,7 @@ pub struct Neuron {
/// (b) `dissolve_delay` is set to zero, (c) neither value is set.
///
/// Cf. \[Neuron::stop_dissolving\] and \[Neuron::start_dissolving\].
pub dissolve_state: Option<NeuronDissolveState>,
dissolve_state: Option<NeuronDissolveState>,
}

impl Neuron {
Expand All @@ -144,6 +144,21 @@ impl Neuron {
self.controller = new_controller;
}

/// Returns an enum representing the dissolve state and age of a neuron.
pub fn dissolve_state_and_age(&self) -> DissolveStateAndAge {
DissolveStateAndAge::from(StoredDissolvedStateAndAge {
dissolve_state: self.dissolve_state.clone(),
aging_since_timestamp_seconds: self.aging_since_timestamp_seconds,
})
}

/// Sets a neuron's dissolve state and age.
pub fn set_dissolve_state_and_age(&mut self, state_and_age: DissolveStateAndAge) {
let stored = StoredDissolvedStateAndAge::from(state_and_age);
self.dissolve_state = stored.dissolve_state;
self.aging_since_timestamp_seconds = stored.aging_since_timestamp_seconds;
}

/// Returns the dissolve state. Deprecated and only used by the old merge neurons flow and will
/// be cleaned up.
pub fn depregated_dissolve_state(&self) -> Option<NeuronDissolveState> {
Expand Down
Loading

0 comments on commit baf21f5

Please sign in to comment.