pallet_insecure_randomness_collective_flip/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
70
71use safe_mix::TripletMix;
72
73use codec::Encode;
74use frame::{prelude::*, traits::Randomness};
75
76const RANDOM_MATERIAL_LEN: u32 = 81;
77
78fn block_number_to_index<T: Config>(block_number: BlockNumberFor<T>) -> usize {
79	let index = (block_number - 1u32.into()) % RANDOM_MATERIAL_LEN.into();
81	index.try_into().ok().expect("Something % 81 is always smaller than usize; qed")
82}
83
84pub use pallet::*;
85
86#[frame::pallet]
87pub mod pallet {
88	use super::*;
89
90	#[pallet::pallet]
91	pub struct Pallet<T>(_);
92
93	#[pallet::config]
94	pub trait Config: frame_system::Config {}
95
96	#[pallet::hooks]
97	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
98		fn on_initialize(block_number: BlockNumberFor<T>) -> Weight {
99			let parent_hash = frame_system::Pallet::<T>::parent_hash();
100
101			RandomMaterial::<T>::mutate(|ref mut values| {
102				if values.try_push(parent_hash).is_err() {
103					let index = block_number_to_index::<T>(block_number);
104					values[index] = parent_hash;
105				}
106			});
107
108			T::DbWeight::get().reads_writes(1, 1)
109		}
110	}
111
112	#[pallet::storage]
116	pub type RandomMaterial<T: Config> =
117		StorageValue<_, BoundedVec<T::Hash, ConstU32<RANDOM_MATERIAL_LEN>>, ValueQuery>;
118
119	impl<T: Config> Pallet<T> {
120		pub fn random_material() -> BoundedVec<T::Hash, ConstU32<RANDOM_MATERIAL_LEN>> {
122			RandomMaterial::<T>::get()
123		}
124	}
125}
126
127impl<T: Config> Randomness<T::Hash, BlockNumberFor<T>> for Pallet<T> {
128	fn random(subject: &[u8]) -> (T::Hash, BlockNumberFor<T>) {
139		let block_number = frame_system::Pallet::<T>::block_number();
140		let index = block_number_to_index::<T>(block_number);
141
142		let hash_series = RandomMaterial::<T>::get();
143		let seed = if !hash_series.is_empty() {
144			hash_series
146				.iter()
147				.cycle()
148				.skip(index)
149				.take(RANDOM_MATERIAL_LEN as usize)
150				.enumerate()
151				.map(|(i, h)| (i as i8, subject, h).using_encoded(T::Hashing::hash))
152				.triplet_mix()
153		} else {
154			T::Hash::default()
155		};
156
157		(seed, block_number.saturating_sub(RANDOM_MATERIAL_LEN.into()))
158	}
159}
160
161#[cfg(test)]
162mod tests {
163	use super::*;
164	use crate as pallet_insecure_randomness_collective_flip;
165	use frame::{
166		testing_prelude::{frame_system::limits, *},
167		traits::Header as _,
168	};
169
170	type Block = frame_system::mocking::MockBlock<Test>;
171
172	construct_runtime!(
173		pub enum Test
174		{
175			System: frame_system,
176			CollectiveFlip: pallet_insecure_randomness_collective_flip,
177		}
178	);
179
180	parameter_types! {
181		pub BlockLength: limits::BlockLength = limits::BlockLength
182			::max(2 * 1024);
183	}
184
185	#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
186	impl frame_system::Config for Test {
187		type Block = Block;
188	}
189
190	impl pallet_insecure_randomness_collective_flip::Config for Test {}
191
192	fn new_test_ext() -> TestExternalities {
193		let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
194		t.into()
195	}
196
197	#[test]
198	fn test_block_number_to_index() {
199		for i in 1..1000 {
200			assert_eq!((i - 1) as usize % 81, block_number_to_index::<Test>(i));
201		}
202	}
203
204	fn setup_blocks(blocks: u64) {
205		let mut parent_hash = System::parent_hash();
206
207		for i in 1..(blocks + 1) {
208			System::reset_events();
209			System::initialize(&i, &parent_hash, &Default::default());
210			CollectiveFlip::on_initialize(i);
211
212			let header = System::finalize();
213			parent_hash = header.hash();
214			System::set_block_number(*header.number());
215		}
216	}
217
218	#[test]
219	fn test_random_material_partial() {
220		new_test_ext().execute_with(|| {
221			let genesis_hash = System::parent_hash();
222
223			setup_blocks(38);
224
225			let random_material = RandomMaterial::<Test>::get();
226
227			assert_eq!(random_material.len(), 38);
228			assert_eq!(random_material[0], genesis_hash);
229		});
230	}
231
232	#[test]
233	fn test_random_material_filled() {
234		new_test_ext().execute_with(|| {
235			let genesis_hash = System::parent_hash();
236
237			setup_blocks(81);
238
239			let random_material = RandomMaterial::<Test>::get();
240
241			assert_eq!(random_material.len(), 81);
242			assert_ne!(random_material[0], random_material[1]);
243			assert_eq!(random_material[0], genesis_hash);
244		});
245	}
246
247	#[test]
248	fn test_random_material_filled_twice() {
249		new_test_ext().execute_with(|| {
250			let genesis_hash = System::parent_hash();
251
252			setup_blocks(162);
253
254			let random_material = RandomMaterial::<Test>::get();
255
256			assert_eq!(random_material.len(), 81);
257			assert_ne!(random_material[0], random_material[1]);
258			assert_ne!(random_material[0], genesis_hash);
259		});
260	}
261
262	#[test]
263	fn test_random() {
264		new_test_ext().execute_with(|| {
265			setup_blocks(162);
266
267			assert_eq!(System::block_number(), 162);
268			assert_eq!(CollectiveFlip::random_seed(), CollectiveFlip::random_seed());
269			assert_ne!(CollectiveFlip::random(b"random_1"), CollectiveFlip::random(b"random_2"));
270
271			let (random, known_since) = CollectiveFlip::random_seed();
272
273			assert_eq!(known_since, 162 - RANDOM_MATERIAL_LEN as u64);
274			assert_ne!(random, H256::zero());
275			assert!(!RandomMaterial::<Test>::get().contains(&random));
276		});
277	}
278}