Skip to content

Commit

Permalink
one other edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
max-dfinity committed Oct 25, 2024
1 parent 040deb5 commit a79201e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
17 changes: 13 additions & 4 deletions rs/nns/governance/src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4213,15 +4213,24 @@ impl Governance {
reward_to_neuron.dissolve_delay_seconds,
MAX_DISSOLVE_DELAY_SECONDS,
);

let dissolve_state_and_age = if dissolve_delay_seconds > 0 {
DissolveStateAndAge::NotDissolving {
dissolve_delay_seconds,
aging_since_timestamp_seconds: now,
}
} else {
DissolveStateAndAge::DissolvingOrDissolved {
when_dissolved_timestamp_seconds: now,
}
};

// Transfer successful.
let neuron = NeuronBuilder::new(
nid,
to_subaccount,
*np_principal,
DissolveStateAndAge::NotDissolving {
dissolve_delay_seconds,
aging_since_timestamp_seconds: now,
},
dissolve_state_and_age,
now,
)
.with_followees(self.heap_data.default_followees.clone())
Expand Down
27 changes: 15 additions & 12 deletions rs/nns/governance/src/neuron/dissolve_state_and_age.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{governance::MAX_DISSOLVE_DELAY_SECONDS, pb::v1::NeuronState};
use crate::{
governance::MAX_DISSOLVE_DELAY_SECONDS, neuron::StoredDissolveStateAndAge, pb::v1::NeuronState,
};

/// An enum to represent different combinations of a neurons dissolve_state and
/// aging_since_timestamp_seconds. Currently, the back-and-forth conversions should make sure the
Expand All @@ -21,17 +23,18 @@ pub enum DissolveStateAndAge {

impl DissolveStateAndAge {
pub fn validate(self) -> Result<Self, String> {
match self {
DissolveStateAndAge::NotDissolving {
dissolve_delay_seconds,
aging_since_timestamp_seconds: _,
} => {
if dissolve_delay_seconds == 0 {
return Err("Dissolve delay must be greater than 0".to_string());
}
}
DissolveStateAndAge::DissolvingOrDissolved { .. } => {}
};
let original = self;
let stored_dissolve_state_and_age = StoredDissolveStateAndAge::from(self);

let validated_dissolve_state_and_age = Self::try_from(stored_dissolve_state_and_age)
.map_err(|e| format!("Invalid dissolve state and age: {}", e))?;

if validated_dissolve_state_and_age != original {
return Err( format!(
"Dissolve state and age is not valid, as it does not serialize/deserialize symmetrically. In: {:?}, Out: {:?}",
original, validated_dissolve_state_and_age
));
}

Ok(self)
}
Expand Down
22 changes: 1 addition & 21 deletions rs/nns/governance/src/neuron_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,31 +476,11 @@ impl NeuronStore {
}

fn validate_neuron(&self, neuron: &Neuron) -> Result<(), NeuronStoreError> {
let dissolve_state_and_age = neuron.dissolve_state_and_age();

let stored_dissolve_state_and_age = StoredDissolveStateAndAge::from(dissolve_state_and_age);

let validated_dissolve_state_and_age =
DissolveStateAndAge::try_from(stored_dissolve_state_and_age).map_err(|e| {
NeuronStoreError::InvalidData {
reason: format!("Invalid dissolve state and age: {}", e),
}
})?;

if validated_dissolve_state_and_age != dissolve_state_and_age {
return Err(NeuronStoreError::InvalidData {
reason: format!(
"Dissolve state and age is not valid, as it reads and writes in a different shape. In: {:?}, Out: {:?}",
dissolve_state_and_age, validated_dissolve_state_and_age
),
});
}

neuron
.dissolve_state_and_age()
.validate()
.map_err(|reason| NeuronStoreError::InvalidData {
reason: format!("Neuron is invalid: {}", reason),
reason: format!("Neuron cannot be saved: {}", reason),
})?;

Ok(())
Expand Down

0 comments on commit a79201e

Please sign in to comment.