RandomMaterial

Storage Plain v101 → current

Ring buffer of the last 81 block hashes used as random seed material.

Explore chain
Queried by: developersvalidators

The Big Picture

This is Substrate's legacy randomness solution. It collects block hashes as a ring buffer and uses them as random seed material. While simple, it's somewhat predictable (block producers know future hashes) and is being replaced by more secure randomness sources like Drand in modern Bittensor.

Why This Matters

On-chain randomness is tricky. This pallet provides a simple solution using block hashes, but it's predictable by block producers. For most casual uses it's fine, but security-critical randomness should use Drand or commit-reveal schemes.

Example Scenario

A subnet needs to randomly select a validator for a task. It queries RandomMaterial to get the seed, then hashes it with some unique input to produce a pseudo-random selection. The result is deterministic but hard to predict for users who don't produce blocks.

Common Questions

Why 81 blocks?
A somewhat arbitrary choice that provides a buffer of recent hashes. 81 blocks (~16 minutes) gives variety while keeping storage bounded.
Is this secure for lotteries?
Not really. Block producers can manipulate the random seed by choosing which block to produce. For high-stakes randomness, use Drand or commit-reveal patterns.
Why is this called 'CollectiveFlip'?
Historical naming from early Substrate. 'Collective' because all validators contribute hashes, 'Flip' as in coin flip for randomness. The name stuck even though it's legacy.

Use Cases

  • Generate pseudo-random values for on-chain logic
  • Seed lottery or selection mechanisms
  • Provide unpredictable values for game mechanics

From Chain Metadata

Series of block headers from the last 81 blocks that acts as random seed material. This is arranged as a ring buffer with `block_number % 81` being the index into the `Vec` of the oldest hash.

Purpose & Usage

Purpose

Provides on-chain randomness derived from recent block headers.

Common Query Patterns

  • Query for current random seed material
  • Used internally by pallets needing randomness

Notes

  • Ring buffer indexed by block_number % 81
  • Oldest hash is at current index, newest is at index-1
  • Not cryptographically secure - block producers can influence
  • Legacy pallet - prefer Drand for new applications

Stored Value

value (BoundedVec)

Code Examples

import { ApiPromise, WsProvider } from "@polkadot/api";
import { stringCamelCase } from "@polkadot/util";

const provider = new WsProvider("wss://entrypoint-finney.opentensor.ai:443");
const api = await ApiPromise.create({ provider });

// Query RandomMaterial storage (no keys - plain value)
const result = await api.query
  [stringCamelCase("RandomnessCollectiveFlip")]
  [stringCamelCase("RandomMaterial")]();
console.log("RandomMaterial:", result.toHuman());

Runtime Info

Pallet
RandomnessCollectiveFlip
Storage Kind
Plain
First Version
v101
Current Version
v393