Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Module 0x1::chunky_dkg_config

Structs and functions for on-chain chunky DKG configurations.

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

Resource ChunkyDKGConfig

The configuration of the on-chain chunky DKG feature.

struct ChunkyDKGConfig 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 - ConfigShadowV1

Struct ConfigOff

A chunky DKG config variant indicating the feature is disabled.

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

Struct ConfigV1

A chunky DKG 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.

Struct ConfigShadowV1

A chunky DKG config variant for shadow mode: chunky DKG runs alongside regular DKG, but epoch change is forced after grace_period_secs if chunky DKG hasn’t completed.

struct ConfigShadowV1 has copy, drop, store
Fields
secrecy_threshold: fixed_point64::FixedPoint64
reconstruction_threshold: fixed_point64::FixedPoint64
grace_period_secs: u64

Function initialize

Initialize the configuration. Used in genesis or governance.

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

Function 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: chunky_dkg_config::ChunkyDKGConfig)
Implementation
public fun set_for_next_epoch(
    framework: &signer, new_config: ChunkyDKGConfig
) {
    system_addresses::assert_aptos_framework(framework);
    config_buffer::upsert(new_config);
}

Function on_new_epoch

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

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

Function enabled

Check whether on-chain chunky DKG main logic is enabled.

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

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

Function new_off

Create a ConfigOff variant.

public fun new_off(): chunky_dkg_config::ChunkyDKGConfig
Implementation
public fun new_off(): ChunkyDKGConfig {
    ChunkyDKGConfig {
        variant: copyable_any::pack(ConfigOff {})
    }
}

Function new_v1

Create a ConfigV1 variant.

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

Function new_shadow_v1

Create a ConfigShadowV1 variant for shadow mode.

public fun new_shadow_v1(secrecy_threshold: fixed_point64::FixedPoint64, reconstruction_threshold: fixed_point64::FixedPoint64, grace_period_secs: u64): chunky_dkg_config::ChunkyDKGConfig
Implementation
public fun new_shadow_v1(
    secrecy_threshold: FixedPoint64,
    reconstruction_threshold: FixedPoint64,
    grace_period_secs: u64
): ChunkyDKGConfig {
    ChunkyDKGConfig {
        variant: copyable_any::pack(
            ConfigShadowV1 { secrecy_threshold, reconstruction_threshold, grace_period_secs }
        )
    }
}

Function grace_period_secs

Return the grace period in seconds if configured (i.e. shadow mode), or none otherwise.

public fun grace_period_secs(): option::Option<u64>
Implementation
public fun grace_period_secs(): Option<u64> acquires ChunkyDKGConfig {
    if (exists<ChunkyDKGConfig>(@aptos_framework)) {
        let config = borrow_global<ChunkyDKGConfig>(@aptos_framework);
        let variant_type_name = *config.variant.type_name().bytes();
        if (variant_type_name == b"0x1::chunky_dkg_config::ConfigShadowV1") {
            let shadow_v1 = copyable_any::unpack<ConfigShadowV1>(config.variant);
            return std::option::some(shadow_v1.grace_period_secs)
        }
    };
    option::none()
}

Function current

Get the currently effective chunky DKG configuration object.

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