Skip to content

randomness_config - [mainnet]

Structs and functions for on-chain randomness configurations.

use 0x1::config_buffer;
use 0x1::copyable_any;
use 0x1::fixed_point64;
use 0x1::string;
use 0x1::system_addresses;

Constants

const EINVALID_CONFIG_VARIANT: u64 = 1;

Structs

ConfigOff

A randomness config variant indicating the feature is disabled.

struct ConfigOff has copy, drop, store
Fields
dummy_field: bool

ConfigV1

A randomness config variant indicating the feature is enabled.

struct ConfigV1 has copy, drop, store
Fields
secrecy_threshold: fixed_point64::FixedPoint64
Any validator subset should not be able to reconstruct randomness if subset_power / total_power <= secrecy_threshold,
reconstruction_threshold: fixed_point64::FixedPoint64
Any validator subset should be able to reconstruct randomness if subset_power / total_power > reconstruction_threshold.

ConfigV2

A randomness config variant indicating the feature is enabled with fast path.

struct ConfigV2 has copy, drop, store
Fields
secrecy_threshold: fixed_point64::FixedPoint64
Any validator subset should not be able to reconstruct randomness if subset_power / total_power <= secrecy_threshold,
reconstruction_threshold: fixed_point64::FixedPoint64
Any validator subset should be able to reconstruct randomness if subset_power / total_power > reconstruction_threshold.
fast_path_secrecy_threshold: fixed_point64::FixedPoint64
Any validator subset should not be able to reconstruct randomness via the fast path if subset_power / total_power <= fast_path_secrecy_threshold,

Resources

RandomnessConfig

The configuration of the on-chain randomness feature.

struct RandomnessConfig has copy, drop, store, key
Fields
variant: copyable_any::Any
A config variant packed as an Any. Currently the variant type is one of the following. - ConfigOff - ConfigV1

Functions

initialize

Initialize the configuration. Used in genesis or governance.

public fun initialize(framework: &signer, config: randomness_config::RandomnessConfig)
Implementation
public fun initialize(framework: &signer, config: RandomnessConfig) {
system_addresses::assert_aptos_framework(framework);
if (!exists<RandomnessConfig>(@aptos_framework)) {
move_to(framework, config)
}
}

set_for_next_epoch

This can be called by on-chain governance to update on-chain consensus configs for the next epoch.

public fun set_for_next_epoch(framework: &signer, new_config: randomness_config::RandomnessConfig)
Implementation
public fun set_for_next_epoch(framework: &signer, new_config: RandomnessConfig) {
system_addresses::assert_aptos_framework(framework);
config_buffer::upsert(new_config);
}

on_new_epoch

Only used in reconfigurations to apply the pending RandomnessConfig, if there is any.

public(friend) fun on_new_epoch(framework: &signer)
Implementation
public(friend) fun on_new_epoch(framework: &signer) acquires RandomnessConfig {
system_addresses::assert_aptos_framework(framework);
if (config_buffer::does_exist<RandomnessConfig>()) {
let new_config = config_buffer::extract_v2<RandomnessConfig>();
if (exists<RandomnessConfig>(@aptos_framework)) {
*borrow_global_mut<RandomnessConfig>(@aptos_framework) = new_config;
} else {
move_to(framework, new_config);
}
}
}

enabled

Check whether on-chain randomness main logic (e.g., DKGManager, RandManager, BlockMetadataExt) is enabled.

NOTE: this returning true does not mean randomness will run. The feature works if and only if consensus_config::validator_txn_enabled() && randomness_config::enabled().

public fun enabled(): bool
Implementation
public fun enabled(): bool acquires RandomnessConfig {
if (exists<RandomnessConfig>(@aptos_framework)) {
let config = borrow_global<RandomnessConfig>(@aptos_framework);
let variant_type_name = *string::bytes(copyable_any::type_name(&config.variant));
variant_type_name != b"0x1::randomness_config::ConfigOff"
} else {
false
}
}

new_off

Create a ConfigOff variant.

public fun new_off(): randomness_config::RandomnessConfig
Implementation
public fun new_off(): RandomnessConfig {
RandomnessConfig {
variant: copyable_any::pack( ConfigOff {} )
}
}

new_v1

Create a ConfigV1 variant.

public fun new_v1(secrecy_threshold: fixed_point64::FixedPoint64, reconstruction_threshold: fixed_point64::FixedPoint64): randomness_config::RandomnessConfig
Implementation
public fun new_v1(secrecy_threshold: FixedPoint64, reconstruction_threshold: FixedPoint64): RandomnessConfig {
RandomnessConfig {
variant: copyable_any::pack( ConfigV1 {
secrecy_threshold,
reconstruction_threshold
} )
}
}

new_v2

Create a ConfigV2 variant.

public fun new_v2(secrecy_threshold: fixed_point64::FixedPoint64, reconstruction_threshold: fixed_point64::FixedPoint64, fast_path_secrecy_threshold: fixed_point64::FixedPoint64): randomness_config::RandomnessConfig
Implementation
public fun new_v2(
secrecy_threshold: FixedPoint64,
reconstruction_threshold: FixedPoint64,
fast_path_secrecy_threshold: FixedPoint64,
): RandomnessConfig {
RandomnessConfig {
variant: copyable_any::pack( ConfigV2 {
secrecy_threshold,
reconstruction_threshold,
fast_path_secrecy_threshold,
} )
}
}

current

Get the currently effective randomness configuration object.

public fun current(): randomness_config::RandomnessConfig
Implementation
public fun current(): RandomnessConfig acquires RandomnessConfig {
if (exists<RandomnessConfig>(@aptos_framework)) {
*borrow_global<RandomnessConfig>(@aptos_framework)
} else {
new_off()
}
}

Specification

on_new_epoch

public(friend) fun on_new_epoch(framework: &signer)
requires @aptos_framework == std::signer::address_of(framework);
include config_buffer::OnNewEpochRequirement<RandomnessConfig>;
aborts_if false;

current

public fun current(): randomness_config::RandomnessConfig
aborts_if false;