pub trait Randomness<Output, BlockNumber> {
    // Required method
    fn random(subject: &[u8]) -> (Output, BlockNumber);

    // Provided method
    fn random_seed() -> (Output, BlockNumber) { ... }
}
Expand description

A trait that is able to provide randomness.

Being a deterministic blockchain, real randomness is difficult to come by, different implementations of this trait will provide different security guarantees. At best, this will be randomness which was hard to predict a long time ago, but that has become easy to predict recently.

Required Methods§

fn random(subject: &[u8]) -> (Output, BlockNumber)

Get the most recently determined random seed, along with the time in the past since when it was determinable by chain observers.

subject is a context identifier and allows you to get a different result to other callers of this function; use it like random(&b"my context"[..]).

NOTE: The returned seed should only be used to distinguish commitments made before the returned block number. If the block number is too early (i.e. commitments were made afterwards), then ensure no further commitments may be made and repeatedly call this on later blocks until the block number returned is later than the latest commitment.

Provided Methods§

fn random_seed() -> (Output, BlockNumber)

Get the basic random seed.

In general you won’t want to use this, but rather Self::random which allows you to give a subject for the random result and whose value will be independently low-influence random from any other such seeds.

NOTE: The returned seed should only be used to distinguish commitments made before the returned block number. If the block number is too early (i.e. commitments were made afterwards), then ensure no further commitments may be made and repeatedly call this on later blocks until the block number returned is later than the latest commitment.

Implementations on Foreign Types§

§

impl<T> Randomness<<T as Config>::Hash, <T as Config>::BlockNumber> for Pallet<T>where T: Config,

§

fn random(subject: &[u8]) -> (<T as Config>::Hash, <T as Config>::BlockNumber)

This randomness uses a low-influence function, drawing upon the block hashes from the previous 81 blocks. Its result for any given subject will be known far in advance by anyone observing the chain. Any block producer has significant influence over their block hashes bounded only by their computational resources. Our low-influence function reduces the actual block producer’s influence over the randomness, but increases the influence of small colluding groups of recent block producers.

WARNING: Hashing the result of this function will remove any low-influence properties it has and mean that all bits of the resulting value are entirely manipulatable by the author of the parent block, who can determine the value of parent_hash.

Implementors§