Module 0x3::voting_power
- Struct
VotingPowerInfo
- Struct
VotingPowerInfoV2
- Constants
- Function
set_voting_power
- Function
init_voting_power_info
- Function
total_stake
- Function
insert
- Function
adjust_voting_power
- Function
update_voting_power
- Function
check_invariants
- Function
total_voting_power
- Function
quorum_threshold
use 0x1::u64;
use 0x1::vector;
use 0x3::validator;
Struct VotingPowerInfo
Deprecated. Use VotingPowerInfoV2 instead.
struct VotingPowerInfo has drop
Fields
Struct VotingPowerInfoV2
struct VotingPowerInfoV2 has drop
Fields
Constants
const EInvalidVotingPower: u64 = 4;
const ERelativePowerMismatch: u64 = 2;
const ETotalPowerMismatch: u64 = 1;
const EVotingPowerOverThreshold: u64 = 3;
const MAX_VOTING_POWER: u64 = 1000;
Quorum threshold for our fixed voting power--any message signed by this much voting power can be trusted up to BFT assumptions
const QUORUM_THRESHOLD: u64 = 6667;
Set total_voting_power as 10_000 by convention. Individual voting powers can be interpreted as easily understandable basis points (e.g., voting_power: 100 = 1%, voting_power: 1 = 0.01%) rather than opaque quantities whose meaning changes from epoch to epoch as the total amount staked shifts. Fixing the total voting power allows clients to hardcode the quorum threshold and total_voting power rather than recomputing these.
const TOTAL_VOTING_POWER: u64 = 10000;
Function set_voting_power
Set the voting power of all validators. Each validator's voting power is initialized using their stake. We then attempt to cap their voting power at MAX_VOTING_POWER. If MAX_VOTING_POWER is not a feasible cap, we pick the lowest possible cap.
public(friend) fun set_voting_power(validators: &mut vector<validator::Validator>)
Implementation
Function init_voting_power_info
Create the initial voting power of each validator, set using their stake, but capped using threshold. We also perform insertion sort while creating the voting power list, by maintaining the list in descending order using voting power. Anything beyond the threshold is added to the remaining_power, which is also returned.
fun init_voting_power_info(validators: &vector<validator::Validator>, threshold: u64): (vector<voting_power::VotingPowerInfoV2>, u64)
Implementation
Function total_stake
Sum up the total stake of all validators.
fun total_stake(validators: &vector<validator::Validator>): u64
Implementation
Function insert
Insert new_info to info_list as part of insertion sort, such that info_list is always sorted using stake, in descending order.
fun insert(info_list: &mut vector<voting_power::VotingPowerInfoV2>, new_info: voting_power::VotingPowerInfoV2)
Implementation
Function adjust_voting_power
Distribute remaining_power to validators that are not capped at threshold.
fun adjust_voting_power(info_list: &mut vector<voting_power::VotingPowerInfoV2>, threshold: u64, remaining_power: u64)
Implementation
Function update_voting_power
Update validators with the decided voting power.
fun update_voting_power(validators: &mut vector<validator::Validator>, info_list: vector<voting_power::VotingPowerInfoV2>)
Implementation
Function check_invariants
Check a few invariants that must hold after setting the voting power.
fun check_invariants(v: &vector<validator::Validator>)
Implementation
Function total_voting_power
Return the (constant) total voting power
public fun total_voting_power(): u64
Implementation
Function quorum_threshold
Return the (constant) quorum threshold
public fun quorum_threshold(): u64