Skip to content

Commit

Permalink
refactor(sns): Rename random_u64 to insecure_random_u64 and remove ra…
Browse files Browse the repository at this point in the history
…ndom_bytes in SNS gov (#1731)

We remove an unused random method, and rename the other method to denote
the insecurity of the random number generate. It currently is not used
in a way that needs to be secure, so no further work needs to be done.
  • Loading branch information
max-dfinity authored Sep 30, 2024
1 parent 87ed927 commit 7e78a87
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 55 deletions.
9 changes: 1 addition & 8 deletions rs/sns/governance/canister/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,10 @@ impl Environment for CanisterEnv {
}

// Returns a random u64.
fn random_u64(&mut self) -> u64 {
fn insecure_random_u64(&mut self) -> u64 {
self.rng.next_u64()
}

// Returns a random byte array.
fn random_byte_array(&mut self) -> [u8; 32] {
let mut bytes = [0u8; 32];
self.rng.fill_bytes(&mut bytes);
bytes
}

// Calls an external method (i.e., on a canister outside the nervous system) to execute a
// proposal as a result of the proposal being adopted.
//
Expand Down
38 changes: 16 additions & 22 deletions rs/sns/governance/src/governance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use crate::pb::v1::{
AddMaturityRequest, AddMaturityResponse, MintTokensRequest, MintTokensResponse,
};
use crate::{
canister_control::{
get_canister_id, perform_execute_generic_nervous_system_function_call,
Expand Down Expand Up @@ -39,18 +36,19 @@ use crate::{
proposal::Action,
proposal_data::ActionAuxiliary as ActionAuxiliaryPb,
transfer_sns_treasury_funds::TransferFrom,
Account as AccountProto, Ballot, ClaimSwapNeuronsError, ClaimSwapNeuronsRequest,
ClaimSwapNeuronsResponse, ClaimedSwapNeuronStatus, DefaultFollowees,
DeregisterDappCanisters, DisburseMaturityInProgress, Empty,
ExecuteGenericNervousSystemFunction, FailStuckUpgradeInProgressRequest,
FailStuckUpgradeInProgressResponse, GetMaturityModulationRequest,
GetMaturityModulationResponse, GetMetadataRequest, GetMetadataResponse, GetMode,
GetModeResponse, GetNeuron, GetNeuronResponse, GetProposal, GetProposalResponse,
GetSnsInitializationParametersRequest, GetSnsInitializationParametersResponse,
Governance as GovernanceProto, GovernanceError, ListNervousSystemFunctionsResponse,
ListNeurons, ListNeuronsResponse, ListProposals, ListProposalsResponse,
ManageDappCanisterSettings, ManageLedgerParameters, ManageNeuron, ManageNeuronResponse,
ManageSnsMetadata, MintSnsTokens, NervousSystemFunction, NervousSystemParameters,
Account as AccountProto, AddMaturityRequest, AddMaturityResponse, Ballot,
ClaimSwapNeuronsError, ClaimSwapNeuronsRequest, ClaimSwapNeuronsResponse,
ClaimedSwapNeuronStatus, DefaultFollowees, DeregisterDappCanisters,
DisburseMaturityInProgress, Empty, ExecuteGenericNervousSystemFunction,
FailStuckUpgradeInProgressRequest, FailStuckUpgradeInProgressResponse,
GetMaturityModulationRequest, GetMaturityModulationResponse, GetMetadataRequest,
GetMetadataResponse, GetMode, GetModeResponse, GetNeuron, GetNeuronResponse,
GetProposal, GetProposalResponse, GetSnsInitializationParametersRequest,
GetSnsInitializationParametersResponse, Governance as GovernanceProto, GovernanceError,
ListNervousSystemFunctionsResponse, ListNeurons, ListNeuronsResponse, ListProposals,
ListProposalsResponse, ManageDappCanisterSettings, ManageLedgerParameters,
ManageNeuron, ManageNeuronResponse, ManageSnsMetadata, MintSnsTokens,
MintTokensRequest, MintTokensResponse, NervousSystemFunction, NervousSystemParameters,
Neuron, NeuronId, NeuronPermission, NeuronPermissionList, NeuronPermissionType,
Proposal, ProposalData, ProposalDecisionStatus, ProposalId, ProposalRewardStatus,
RegisterDappCanisters, RewardEvent, Tally, TransferSnsTreasuryFunds,
Expand Down Expand Up @@ -1462,7 +1460,7 @@ impl Governance {
0, // Minting transfer don't pay a fee
None, // This is a minting transfer, no 'from' account is needed
self.neuron_account_id(subaccount), // The account of the neuron on the ledger
self.env.random_u64(), // Random memo(nonce) for the ledger's transaction
self.env.insecure_random_u64(), // Random memo(nonce) for the ledger's transaction
)
.await?;

Expand Down Expand Up @@ -5458,7 +5456,7 @@ impl Governance {
.expect("recipient must be set")
.try_into()
.unwrap(), // The account of the neuron on the ledger
self.env.random_u64(), // Random memo(nonce) for the ledger's transaction
self.env.insecure_random_u64(), // Random memo(nonce) for the ledger's transaction
)
.await
.unwrap();
Expand Down Expand Up @@ -6511,11 +6509,7 @@ mod tests {
unimplemented!();
}

fn random_u64(&mut self) -> u64 {
unimplemented!();
}

fn random_byte_array(&mut self) -> [u8; 32] {
fn insecure_random_u64(&mut self) -> u64 {
unimplemented!();
}

Expand Down
17 changes: 3 additions & 14 deletions rs/sns/governance/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1958,12 +1958,7 @@ pub trait Environment: Send + Sync {
/// Returns a random number.
///
/// This number is the same in all replicas.
fn random_u64(&mut self) -> u64;

/// Returns a random byte array with 32 bytes.
///
/// This byte array is the same in all replicas.
fn random_byte_array(&mut self) -> [u8; 32];
fn insecure_random_u64(&mut self) -> u64;

/// Calls another canister. The return value indicates whether the call can be successfully
/// initiated. If initiating the call is successful, the call could later be rejected by the
Expand Down Expand Up @@ -2531,7 +2526,7 @@ impl From<NeuronRecipes> for Vec<NeuronRecipe> {

pub mod test_helpers {
use super::*;
use rand::{Rng, RngCore};
use rand::Rng;
use std::{
borrow::BorrowMut,
collections::HashMap,
Expand Down Expand Up @@ -2677,16 +2672,10 @@ pub mod test_helpers {
self.now
}

fn random_u64(&mut self) -> u64 {
fn insecure_random_u64(&mut self) -> u64 {
rand::thread_rng().gen()
}

fn random_byte_array(&mut self) -> [u8; 32] {
let mut result = [0_u8; 32];
rand::thread_rng().fill_bytes(&mut result[..]);
result
}

async fn call_canister(
&self,
canister_id: CanisterId,
Expand Down
12 changes: 1 addition & 11 deletions rs/sns/governance/tests/fixtures/environment_fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,14 @@ impl Environment for EnvironmentFixture {
self.environment_fixture_state.try_lock().unwrap().now
}

fn random_u64(&mut self) -> u64 {
fn insecure_random_u64(&mut self) -> u64 {
self.environment_fixture_state
.try_lock()
.unwrap()
.rng
.next_u64()
}

fn random_byte_array(&mut self) -> [u8; 32] {
let mut bytes = [0u8; 32];
self.environment_fixture_state
.try_lock()
.unwrap()
.rng
.fill_bytes(&mut bytes);
bytes
}

async fn call_canister(
&self,
canister_id: CanisterId,
Expand Down

0 comments on commit 7e78a87

Please sign in to comment.