Utils

Helper Functions

Transaction Rate Limit

Gets TxRateLimit

pub fn get_tx_rate_limit() -> u64

Set Last Transaction Block

Sets LastTxBlock.

pub fn set_last_tx_block(key: &T::AccountId, block: u64)

Get Last Transaction Block

Gets LastTxBlock.

pub fn get_last_tx_block(key: &T::AccountId) -> u64

Exceeds Transaction Rate Limit

Check if the transaction rate limiter is being exceeded.

pub fn exceeds_tx_rate_limit(prev_tx_block: u64, current_block: u64

Get Current Block As u64

Gets current block as a u64.

pub fn get_current_block_as_u64() -> u64

Convert Current Block As u64

Converts current block to a u64.

pub fn convert_current_block_as_u64(block: BlockNumberFor<T>) -> u64

Get Average Score

Gets the average score for a peer during consensus while filtering outliers.

pub fn get_average_score(_values: Vec<u64>) -> u64

Filter Outliers

Using an interquartile algorithm, outliers are removed from the vector of values.

pub fn filter_outliers(_values: Vec<u64>) -> Vec<u64>

Validate IP Address

Validates if the IP address is an ipv4 or ipv6.

pub fn validate_ip_address(ip: vec<u8>) -> bool

Validate Port

Validates port.

pub fn validate_port(port: u16) -> bool

Validate Peer ID

Loosely validates a Peer ID format.

pub fn validate_peer_id(peer_id: PeerId) -> bool

Get Eligible Epoch Block

Gets the eligible block for model consensus, model removal, peer submission, peer inclusion, and stake removal based on the interval, initialization, and epochs.

pub fn get_eligible_epoch_block(
    epochs_interval: u64, 
    initialized: u64, 
    epochs: u64
) -> u64

Get Model Initialization Cost

Gets the cost to initialize a new model comparing all inclusion-eligible model peers with the cost per model peer as the total_inclusion_model_peers * model_per_init_cost based on the ModelPerPeerInitCost.

pub fn get_model_initialization_cost(
    models_count: u32, 
    model_peers_count: u32
) -> u128

Is In Consensus Steps

Check if consensus steps are currently active. This includes forming consensus and generating emissions which are the last steps before the next epoch.

pub fn is_in_consensus_steps(block: u64, interval: u64) -> bool

Can Submit Consensus Data

Check if the epoch is in the range of blocks consensus data can be submitted. The start block of being able to submit consensus follows can_remove_model_peer() up to the next epoch start block minus 1 block.

pub fn can_submit_consensus_data(block: u64, interval: u64) -> bool

Can Remove Model Peer

Check if the epoch is in the range of blocks a model peer can remove a model peer. This range follows the consensus steps specified as a percentage of the epoch based on RemoveModelPeerEpochPercentage minus the consensus steps.

pub fn can_remove_model_peer(block: u64, interval: u64) -> bool

Is Account Eligible

Check if an account is eligible by checking if its AccountPenaltyCount exceeds the MaxAccountPenaltyCount.

pub fn is_account_eligible(account_id: T::AccountId) -> bool

Do Remove Account Model Peers

Removes each account's model peer.

pub fn do_remove_account_model_peers(block: u64, account_id: T::AccountId)

Do Remove Model Peer

Removes an accounts model peer.

pub fn do_remove_model_peer(block: u64, model_id: u32, account_id: T::AccountId)

Get Account Slash Percentage

Gets the percentage to slash an account stake balance when their AccountPenaltyCount reaches the MaxAccountPenaltyCount.

pub fn get_account_slash_percentage(account_id: T::AccountId) -> u128

Get Total Submittable Model Peers

Gets the total count of model peers that can submit consensus during the current epoch based on the current block.

pub fn get_prev_epoch_total_submittable_model_peers(
    model_id: u32,
    block: u64,
    consensus_blocks_interval: u64,
    min_required_peer_consensus_submit_epochs: u64
) -> u32

Get Previous Epoch Total Submittable Model Peers

Gets the previous epoch's total count of model peers that can submit consensus during the previous epoch based on the current block.

pub fn get_prev_epoch_total_submittable_model_peers(
    model_id: u32,
    block: u64,
    consensus_blocks_interval: u64,
    min_required_peer_consensus_submit_epochs: u64
) -> u32

Last updated