무작위성 적용하기
온체인 무작위성 기술과 도구에 대한 자세한 설명입니다.
Randomness 가져오기
Randomness 가져오기use frame_support::traits::Randomness;#[pallet::config] pub trait frame_system::Config { type MyRandomness: Randomness<Self::Hash, Self::BlockNumber>; }
nonce 생성 및 무작위성 구현에 사용하기
fn get_and_increment_nonce() -> Vec<u8> { let nonce = Nonce::<T>::get(); Nonce::<T>::put(nonce.wrapping_add(1)); nonce.encode() }#[pallet::weight(100)] pub fn create_unique( origin: OriginFor<T>) -> DispatchResultWithPostInfo { // 이 디스패처를 호출하는 계정. let sender = ensure_signed(origin)?; // 무작위 값. let nonce = Self::get_and_increment_nonce(); let (randomValue, _) = T::MyRandomness::random(&nonce); // 무작위 값을 스토리지에 기록합니다. <MyStorageItem<T>>::put(randomValue); Self::deposit_event(Event::UniqueCreated(randomValue)); }impl my_pallet::Config for Runtime{ type Event; type MyRandomness = RandomCollectiveFlip; }
예제
관련 자료
Last updated