referrerpolicy=no-referrer-when-downgrade

frame_support/traits/
randomness.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Traits for dealing with on-chain randomness.
19
20/// A trait that is able to provide randomness.
21///
22/// Being a deterministic blockchain, real randomness is difficult to come by, different
23/// implementations of this trait will provide different security guarantees. At best,
24/// this will be randomness which was hard to predict a long time ago, but that has become
25/// easy to predict recently.
26pub trait Randomness<Output, BlockNumber> {
27	/// Get the most recently determined random seed, along with the time in the past
28	/// since when it was determinable by chain observers.
29	///
30	/// `subject` is a context identifier and allows you to get a different result to
31	/// other callers of this function; use it like `random(&b"my context"[..])`.
32	///
33	/// NOTE: The returned seed should only be used to distinguish commitments made before
34	/// the returned block number. If the block number is too early (i.e. commitments were
35	/// made afterwards), then ensure no further commitments may be made and repeatedly
36	/// call this on later blocks until the block number returned is later than the latest
37	/// commitment.
38	fn random(subject: &[u8]) -> (Output, BlockNumber);
39
40	/// Get the basic random seed.
41	///
42	/// In general you won't want to use this, but rather `Self::random` which allows
43	/// you to give a subject for the random result and whose value will be
44	/// independently low-influence random from any other such seeds.
45	///
46	/// NOTE: The returned seed should only be used to distinguish commitments made before
47	/// the returned block number. If the block number is too early (i.e. commitments were
48	/// made afterwards), then ensure no further commitments may be made and repeatedly
49	/// call this on later blocks until the block number returned is later than the latest
50	/// commitment.
51	fn random_seed() -> (Output, BlockNumber) {
52		Self::random(&[][..])
53	}
54}