Skip to content

Commit

Permalink
chore(nns): Refactor NeuronId::from_subaccount to test_utils since it…
Browse files Browse the repository at this point in the history
…'s only used in tests (#3611)

This is a trivial inlining, since `NeuronId::from_subaccount` is only
used in one place. Putting it together with production code creates
confusion about how NNS neuron ids are created in production.
  • Loading branch information
jasonz-dfinity authored Jan 28, 2025
1 parent 0ac8a60 commit a8dc041
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
15 changes: 1 addition & 14 deletions rs/nns/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::pb::v1::{NeuronId, ProposalId};
use ic_crypto_sha2::Sha256;
use ic_stable_structures::{storable::Bound, Storable};
use num_traits::bounds::{LowerBounded, UpperBounded};
use std::{borrow::Cow, convert::TryInto};
use std::borrow::Cow;

pub mod access_control;
pub mod init;
Expand All @@ -14,18 +13,6 @@ impl NeuronId {
pub const MIN: Self = Self { id: u64::MIN };
pub const MAX: Self = Self { id: u64::MAX };

pub fn from_subaccount(subaccount: &[u8; 32]) -> Self {
Self {
id: {
let mut state = Sha256::new();
state.write(subaccount);
// TODO(NNS1-192) We should just store the Sha256, but for now
// we convert it to a number
u64::from_ne_bytes(state.finish()[0..8].try_into().unwrap())
},
}
}

pub fn next(&self) -> Option<NeuronId> {
self.id.checked_add(1).map(|id| NeuronId { id })
}
Expand Down
8 changes: 7 additions & 1 deletion rs/nns/test_utils/src/gtc_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,14 @@ fn make_neuron(
)
};

let subaccount_hash = Sha256::hash(&subaccount);
let neuron_id = NeuronId {
// We just need a unique ID for the neuron, so we use the first 8 bytes to create a u64.
id: u64::from_ne_bytes(subaccount_hash[0..8].try_into().unwrap()),
};

Neuron {
id: Some(NeuronId::from_subaccount(&subaccount)),
id: Some(neuron_id),
account: subaccount.to_vec(),
controller: Some(GENESIS_TOKEN_CANISTER_ID.get()),
cached_neuron_stake_e8s: stake_e8s,
Expand Down

0 comments on commit a8dc041

Please sign in to comment.