There are a few pallets in FRAME which provide an implementation to the Randomness
trait.
The best of which is the BABE Pallet which provides three different levels of randomness:
CurrentBlockRandomness
: Randomness produced semi-freshly with each block, but inherits limitations of RandomnessFromTwoEpochsAgo from which it derives.
RandomnessFromOneEpochAgo
: Randomness usable by on-chain code that does not depend upon finality and takes action based upon on-chain commitments made during the previous epoch.
RandomnessFromTwoEpochsAgo
: Randomness usable by consensus protocols that depend upon finality and take action based upon on-chain commitments made during the epoch before the previous epoch.
If you use the BABE pallet, you can easily integrate randomness into your other pallets by implementing this trait in your Pallet config.
/// The module's configuration trait.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Something that provides randomness in the runtime.
type Randomness: Randomness<Self::Hash, Self::BlockNumber>;
}
And then you can use this to fetch some randomness easily:
let (hash, block_number) = T::Randomness::random(&b"some_seed_phrase"[..]);
Another Pallet which provides this Randomness
implementation is the randomness-colletive-flip
pallet. However, the strength of this randomness is much weaker than the one provided by BABE.