starcoin-framework

Module 0x1::ConsensusConfig

The module provide configuration of consensus parameters.

use 0x1::Config;
use 0x1::CoreAddresses;
use 0x1::Errors;
use 0x1::Math;
use 0x1::Timestamp;

Struct ConsensusConfig

consensus configurations.

struct ConsensusConfig has copy, drop, store
Fields
uncle_rate_target: u64
Uncle block rate per epoch
base_block_time_target: u64
Average target time to calculate a block's difficulty
base_reward_per_block: u128
Rewards per block
base_reward_per_uncle_percent: u64
Percentage of base_reward_per_block to reward a uncle block
epoch_block_count: u64
Blocks each epoch
base_block_difficulty_window: u64
How many ancestor blocks which use to calculate next block's difficulty
min_block_time_target: u64
Minimum target time to calculate a block's difficulty
max_block_time_target: u64
Maximum target time to calculate a block's difficulty
base_max_uncles_per_block: u64
Maximum number of uncle block per block
base_block_gas_limit: u64
Maximum gases per block
strategy: u8
Strategy to calculate difficulty

Constants

const EINVALID_ARGUMENT: u64 = 18;

Function initialize

Initialization of the module.

public fun initialize(account: &signer, uncle_rate_target: u64, epoch_block_count: u64, base_block_time_target: u64, base_block_difficulty_window: u64, base_reward_per_block: u128, base_reward_per_uncle_percent: u64, min_block_time_target: u64, max_block_time_target: u64, base_max_uncles_per_block: u64, base_block_gas_limit: u64, strategy: u8)
Implementation
public fun initialize(
    account: &signer,
    uncle_rate_target: u64,
    epoch_block_count: u64,
    base_block_time_target: u64,
    base_block_difficulty_window: u64,
    base_reward_per_block: u128,
    base_reward_per_uncle_percent: u64,
    min_block_time_target: u64,
    max_block_time_target: u64,
    base_max_uncles_per_block: u64,
    base_block_gas_limit: u64,
    strategy: u8,
) {
    Timestamp::assert_genesis();
    CoreAddresses::assert_genesis_address(account);

    Config::publish_new_config<Self::ConsensusConfig>(
        account,
        new_consensus_config(
            uncle_rate_target,
            base_block_time_target,
            base_reward_per_block,
            base_reward_per_uncle_percent,
            epoch_block_count,
            base_block_difficulty_window,
            min_block_time_target,
            max_block_time_target,
            base_max_uncles_per_block,
            base_block_gas_limit,
            strategy,
        ),
    );
}
Specification
aborts_if !Timestamp::is_genesis();
aborts_if Signer::address_of(account) != CoreAddresses::SPEC_GENESIS_ADDRESS();
aborts_if uncle_rate_target == 0;
aborts_if epoch_block_count == 0;
aborts_if base_reward_per_block == 0;
aborts_if base_block_time_target == 0;
aborts_if base_block_difficulty_window == 0;
aborts_if min_block_time_target == 0;
aborts_if max_block_time_target < min_block_time_target;
include Config::PublishNewConfigAbortsIf<ConsensusConfig>;
include Config::PublishNewConfigEnsures<ConsensusConfig>;

Function new_consensus_config

Create a new consensus config mainly used in DAO.

public fun new_consensus_config(uncle_rate_target: u64, base_block_time_target: u64, base_reward_per_block: u128, base_reward_per_uncle_percent: u64, epoch_block_count: u64, base_block_difficulty_window: u64, min_block_time_target: u64, max_block_time_target: u64, base_max_uncles_per_block: u64, base_block_gas_limit: u64, strategy: u8): ConsensusConfig::ConsensusConfig
Implementation
public fun new_consensus_config(uncle_rate_target: u64,
                                base_block_time_target: u64,
                                base_reward_per_block: u128,
                                base_reward_per_uncle_percent: u64,
                                epoch_block_count: u64,
                                base_block_difficulty_window: u64,
                                min_block_time_target: u64,
                                max_block_time_target: u64,
                                base_max_uncles_per_block: u64,
                                base_block_gas_limit: u64,
                                strategy: u8,): ConsensusConfig {
    assert!(uncle_rate_target > 0, Errors::invalid_argument(EINVALID_ARGUMENT));
    assert!(base_block_time_target > 0, Errors::invalid_argument(EINVALID_ARGUMENT));
    assert!(base_reward_per_block > 0, Errors::invalid_argument(EINVALID_ARGUMENT));
    assert!(epoch_block_count > 0, Errors::invalid_argument(EINVALID_ARGUMENT));
    assert!(base_block_difficulty_window > 0, Errors::invalid_argument(EINVALID_ARGUMENT));
    // base_reward_per_uncle_percent can been zero.
    assert!(min_block_time_target > 0, Errors::invalid_argument(EINVALID_ARGUMENT));
    assert!(max_block_time_target >= min_block_time_target, Errors::invalid_argument(EINVALID_ARGUMENT));

    ConsensusConfig {
        uncle_rate_target,
        base_block_time_target,
        base_reward_per_block,
        epoch_block_count,
        base_block_difficulty_window,
        base_reward_per_uncle_percent,
        min_block_time_target,
        max_block_time_target,
        base_max_uncles_per_block,
        base_block_gas_limit,
        strategy,
    }
}
Specification
aborts_if uncle_rate_target == 0;
aborts_if epoch_block_count == 0;
aborts_if base_reward_per_block == 0;
aborts_if base_block_time_target == 0;
aborts_if base_block_difficulty_window == 0;
aborts_if min_block_time_target == 0;
aborts_if max_block_time_target < min_block_time_target;

Function get_config

Get config data.

public fun get_config(): ConsensusConfig::ConsensusConfig
Implementation
public fun get_config(): ConsensusConfig {
    Config::get_by_address<ConsensusConfig>(CoreAddresses::GENESIS_ADDRESS())
}
Specification
aborts_if !exists<Config::Config<ConsensusConfig>>(CoreAddresses::SPEC_GENESIS_ADDRESS());
fun spec_get_config(): ConsensusConfig {
   global<Config::Config<ConsensusConfig>>(CoreAddresses::SPEC_GENESIS_ADDRESS()).payload
}

Function uncle_rate_target

Get uncle_rate_target

public fun uncle_rate_target(config: &ConsensusConfig::ConsensusConfig): u64
Implementation
public fun uncle_rate_target(config: &ConsensusConfig): u64 {
    config.uncle_rate_target
}

Function base_block_time_target

Get base_block_time_target

public fun base_block_time_target(config: &ConsensusConfig::ConsensusConfig): u64
Implementation
public fun base_block_time_target(config: &ConsensusConfig): u64 {
    config.base_block_time_target
}

Function base_reward_per_block

Get base_reward_per_block

public fun base_reward_per_block(config: &ConsensusConfig::ConsensusConfig): u128
Implementation
public fun base_reward_per_block(config: &ConsensusConfig): u128 {
    config.base_reward_per_block
}

Function epoch_block_count

Get epoch_block_count

public fun epoch_block_count(config: &ConsensusConfig::ConsensusConfig): u64
Implementation
public fun epoch_block_count(config: &ConsensusConfig): u64 {
    config.epoch_block_count
}

Function base_block_difficulty_window

Get base_block_difficulty_window

public fun base_block_difficulty_window(config: &ConsensusConfig::ConsensusConfig): u64
Implementation
public fun base_block_difficulty_window(config: &ConsensusConfig): u64 {
    config.base_block_difficulty_window
}

Function base_reward_per_uncle_percent

Get base_reward_per_uncle_percent

public fun base_reward_per_uncle_percent(config: &ConsensusConfig::ConsensusConfig): u64
Implementation
public fun base_reward_per_uncle_percent(config: &ConsensusConfig): u64 {
    config.base_reward_per_uncle_percent
}

Function min_block_time_target

Get min_block_time_target

public fun min_block_time_target(config: &ConsensusConfig::ConsensusConfig): u64
Implementation
public fun min_block_time_target(config: &ConsensusConfig): u64 {
    config.min_block_time_target
}

Function max_block_time_target

Get max_block_time_target

public fun max_block_time_target(config: &ConsensusConfig::ConsensusConfig): u64
Implementation
public fun max_block_time_target(config: &ConsensusConfig): u64 {
    config.max_block_time_target
}

Function base_max_uncles_per_block

Get base_max_uncles_per_block

public fun base_max_uncles_per_block(config: &ConsensusConfig::ConsensusConfig): u64
Implementation
public fun base_max_uncles_per_block(config: &ConsensusConfig): u64 {
    config.base_max_uncles_per_block
}

Function base_block_gas_limit

Get base_block_gas_limit

public fun base_block_gas_limit(config: &ConsensusConfig::ConsensusConfig): u64
Implementation
public fun base_block_gas_limit(config: &ConsensusConfig): u64 {
    config.base_block_gas_limit
}

Function strategy

Get strategy

public fun strategy(config: &ConsensusConfig::ConsensusConfig): u8
Implementation
public fun strategy(config: &ConsensusConfig): u8 {
    config.strategy
}

Function compute_reward_per_block

Compute block reward given the new_epoch_block_time_target.

public fun compute_reward_per_block(new_epoch_block_time_target: u64): u128
Implementation
public fun compute_reward_per_block(new_epoch_block_time_target: u64): u128 {
    let config = get_config();
    do_compute_reward_per_block(&config, new_epoch_block_time_target)
}
Specification
aborts_if !exists<Config::Config<ConsensusConfig>>(CoreAddresses::SPEC_GENESIS_ADDRESS());
include Math::MulDivAbortsIf{x: spec_get_config().base_reward_per_block, y: new_epoch_block_time_target, z: spec_get_config().base_block_time_target};

Function do_compute_reward_per_block

Compute block reward given the new_epoch_block_time_target, and the consensus config.

public fun do_compute_reward_per_block(config: &ConsensusConfig::ConsensusConfig, new_epoch_block_time_target: u64): u128
Implementation
public fun do_compute_reward_per_block(config: &ConsensusConfig, new_epoch_block_time_target: u64): u128 {
    Math::mul_div(config.base_reward_per_block, (new_epoch_block_time_target as u128), (config.base_block_time_target as u128))
}
Specification
include Math::MulDivAbortsIf{x: config.base_reward_per_block, y: new_epoch_block_time_target, z: config.base_block_time_target};

Module Specification

pragma verify = false;
pragma aborts_if_is_strict;