Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove usage of RandomNumberGenerator #1143

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions modules/cdp-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v
support = { package = "module-support", path = "../support", default-features = false }
loans = { package = "module-loans", path = "../loans", default-features = false }
primitives = { package = "acala-primitives", path = "../../primitives", default-features = false }
rand_chacha = { version = "0.2", default-features = false }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.5" }
Expand Down
20 changes: 14 additions & 6 deletions modules/cdp-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ use loans::Position;
use orml_traits::Change;
use orml_utilities::{IterableStorageDoubleMapExtended, OffchainErr};
use primitives::{Amount, Balance, CurrencyId};
use rand_chacha::{
rand_core::{RngCore, SeedableRng},
ChaChaRng,
};
use sp_runtime::{
offchain::{
storage::StorageValueRef,
storage_lock::{StorageLock, Time},
Duration,
},
traits::{BlakeTwo256, Bounded, Convert, Hash, One, Saturating, StaticLookup, UniqueSaturatedInto, Zero},
traits::{Bounded, Convert, One, Saturating, StaticLookup, UniqueSaturatedInto, Zero},
transaction_validity::{
InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity, ValidTransaction,
},
DispatchError, DispatchResult, FixedPointNumber, RandomNumberGenerator, RuntimeDebug,
DispatchError, DispatchResult, FixedPointNumber, RuntimeDebug,
};
use sp_std::prelude::*;
use support::{
Expand Down Expand Up @@ -616,10 +620,9 @@ impl<T: Config> Pallet<T> {
{
(last_collateral_position, maybe_last_iterator_previous_key)
} else {
let random_seed = sp_io::offchain::random_seed();
let mut rng = RandomNumberGenerator::<BlakeTwo256>::new(BlakeTwo256::hash(&random_seed[..]));
let mut rng = ChaChaRng::from_seed(sp_io::offchain::random_seed());
(
rng.pick_u32(collateral_currency_ids.len().saturating_sub(1) as u32),
pick_u32(&mut rng, collateral_currency_ids.len().saturating_sub(1) as u32),
wangjj9219 marked this conversation as resolved.
Show resolved Hide resolved
None,
)
};
Expand All @@ -629,7 +632,7 @@ impl<T: Config> Pallet<T> {
.get::<u32>()
.unwrap_or(Some(DEFAULT_MAX_ITERATIONS));

let currency_id = collateral_currency_ids[(collateral_position as usize)];
let currency_id = collateral_currency_ids[collateral_position as usize];
let is_shutdown = T::EmergencyShutdown::is_shutdown();
let mut map_iterator = <loans::Positions<T> as IterableStorageDoubleMapExtended<_, _, _>>::iter_prefix(
currency_id,
Expand Down Expand Up @@ -941,3 +944,8 @@ impl<T: Config> RiskManager<T::AccountId, CurrencyId, Balance, Balance> for Pall
Ok(())
}
}

/// Pick a new PRN, in the range [0, `max`] (inclusive).
fn pick_u32<R: RngCore>(rng: &mut R, max: u32) -> u32 {
rng.next_u32() % (max + 1)
}