Skip to content

Commit a8dc041

Browse files
chore(nns): Refactor NeuronId::from_subaccount to test_utils since it's only used in tests (dfinity#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.
1 parent 0ac8a60 commit a8dc041

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

rs/nns/common/src/lib.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::pb::v1::{NeuronId, ProposalId};
2-
use ic_crypto_sha2::Sha256;
32
use ic_stable_structures::{storable::Bound, Storable};
43
use num_traits::bounds::{LowerBounded, UpperBounded};
5-
use std::{borrow::Cow, convert::TryInto};
4+
use std::borrow::Cow;
65

76
pub mod access_control;
87
pub mod init;
@@ -14,18 +13,6 @@ impl NeuronId {
1413
pub const MIN: Self = Self { id: u64::MIN };
1514
pub const MAX: Self = Self { id: u64::MAX };
1615

17-
pub fn from_subaccount(subaccount: &[u8; 32]) -> Self {
18-
Self {
19-
id: {
20-
let mut state = Sha256::new();
21-
state.write(subaccount);
22-
// TODO(NNS1-192) We should just store the Sha256, but for now
23-
// we convert it to a number
24-
u64::from_ne_bytes(state.finish()[0..8].try_into().unwrap())
25-
},
26-
}
27-
}
28-
2916
pub fn next(&self) -> Option<NeuronId> {
3017
self.id.checked_add(1).map(|id| NeuronId { id })
3118
}

rs/nns/test_utils/src/gtc_helpers.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,14 @@ fn make_neuron(
252252
)
253253
};
254254

255+
let subaccount_hash = Sha256::hash(&subaccount);
256+
let neuron_id = NeuronId {
257+
// We just need a unique ID for the neuron, so we use the first 8 bytes to create a u64.
258+
id: u64::from_ne_bytes(subaccount_hash[0..8].try_into().unwrap()),
259+
};
260+
255261
Neuron {
256-
id: Some(NeuronId::from_subaccount(&subaccount)),
262+
id: Some(neuron_id),
257263
account: subaccount.to_vec(),
258264
controller: Some(GENESIS_TOKEN_CANISTER_ID.get()),
259265
cached_neuron_stake_e8s: stake_e8s,

0 commit comments

Comments
 (0)