Skip to content

Commit

Permalink
[Aptos Framework][Governance] Update voting power to take into accoun…
Browse files Browse the repository at this point in the history
…t pending_inactive stake (aptos-labs#2532)
  • Loading branch information
movekevin authored Aug 5, 2022
1 parent 162bed6 commit bf8ab40
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ module aptos_framework::aptos_governance {

// The proposer's stake needs to be at least the required bond amount.
let governance_config = borrow_global<GovernanceConfig>(@aptos_framework);
let stake_balance = stake::get_active_staked_balance(stake_pool);
let stake_balance = stake::get_current_epoch_voting_power(stake_pool);
assert!(
stake_balance >= governance_config.required_proposer_stake,
error::invalid_argument(EINSUFFICIENT_PROPOSER_STAKE),
Expand Down Expand Up @@ -233,7 +233,7 @@ module aptos_framework::aptos_governance {
// Voting power does not include pending_active or pending_inactive balances.
// In general, the stake pool should not have pending_inactive balance if it still has lockup (required to vote)
// And if pending_active will be added to active in the next epoch.
let voting_power = stake::get_active_staked_balance(stake_pool);
let voting_power = stake::get_current_epoch_voting_power(stake_pool);
// Short-circuit if the voter has no voting power.
assert!(voting_power > 0, error::invalid_argument(ENO_VOTING_POWER));

Expand Down Expand Up @@ -406,12 +406,11 @@ module aptos_framework::aptos_governance {
stake::create_validator_set(aptos_framework, active_validators);

let (mint_cap, burn_cap) = aptos_coin::initialize(aptos_framework, core_resources);
let proposer_stake = coin::mint(100, &mint_cap);
let yes_voter_stake = coin::mint(20, &mint_cap);
let no_voter_stake = coin::mint(10, &mint_cap);
stake::create_stake_pool(proposer, proposer_stake, 10000);
stake::create_stake_pool(yes_voter, yes_voter_stake, 10000);
stake::create_stake_pool(no_voter, no_voter_stake, 10000);
// Spread stake among active and pending_inactive because both need to be accounted for when computing voting
// power.
stake::create_stake_pool(proposer, coin::mint(50, &mint_cap), coin::mint(50, &mint_cap), 10000);
stake::create_stake_pool(yes_voter, coin::mint(10, &mint_cap), coin::mint(10, &mint_cap), 10000);
stake::create_stake_pool(no_voter, coin::mint(5, &mint_cap), coin::mint(5, &mint_cap), 10000);
coin::destroy_mint_cap<AptosCoin>(mint_cap);
coin::destroy_burn_cap<AptosCoin>(burn_cap);
}
Expand Down
21 changes: 13 additions & 8 deletions aptos-move/framework/aptos-framework/sources/stake.move
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ module aptos_framework::stake {
)
}

/// Returns the validator's state.
public fun get_validator_state(pool_address: address): u64 acquires ValidatorSet {
let validator_set = borrow_global<ValidatorSet>(@aptos_framework);
if (option::is_some(&find_validator(&validator_set.pending_active, pool_address))) {
Expand All @@ -275,15 +276,17 @@ module aptos_framework::stake {
}
}

/// Return the active staked balance of the stake pool at `pool_address`. Any pending_inactive and pending_active
/// stake are not considered.
///
/// If the stake pool is not yet active, the voting power will be 0.
public fun get_active_staked_balance(pool_address: address): u64 acquires StakePool, ValidatorSet {
if (get_validator_state(pool_address) == VALIDATOR_STATUS_INACTIVE) {
0
/// Return the voting power of the validator in the current epoch.
/// This is the same as the validator's total active and pending_inactive stake.
public fun get_current_epoch_voting_power(pool_address: address): u64 acquires StakePool, ValidatorSet {
let validator_state = get_validator_state(pool_address);
// Both active and pending inactive validators can still vote in the current epoch.
if (validator_state == VALIDATOR_STATUS_ACTIVE || validator_state == VALIDATOR_STATUS_PENDING_INACTIVE) {
let active_stake = coin::value<AptosCoin>(&borrow_global<StakePool>(pool_address).active);
let pending_inactive_stake = coin::value<AptosCoin>(&borrow_global<StakePool>(pool_address).pending_inactive);
active_stake + pending_inactive_stake
} else {
coin::value<AptosCoin>(&borrow_global<StakePool>(pool_address).active)
0
}
}

Expand Down Expand Up @@ -1908,12 +1911,14 @@ module aptos_framework::stake {
public fun create_stake_pool(
account: &signer,
active: Coin<AptosCoin>,
pending_inactive: Coin<AptosCoin>,
locked_until_secs: u64,
) acquires OwnerCapability, StakePool {
let account_address = signer::address_of(account);
initialize_owner_only(account, account_address, account_address);
let stake_pool = borrow_global_mut<StakePool>(account_address);
coin::merge(&mut stake_pool.active, active);
coin::merge(&mut stake_pool.pending_inactive, pending_inactive);
stake_pool.locked_until_secs = locked_until_secs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"eventRootHash": "YPVlJld+KeJV8TgWig4e7McIG/o+rxMn60yg554WVK8=",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "+frUKK1tqTQDiSDscNcfEnCgRHXRX3WzYPGZFbegB0c=",
"accumulatorRootHash": "DJwD1vywXfYH81YFO+4eRwl/8qx5qHRtX82owmq5Rhk=",
"changes": [
{
"type": "WRITE_RESOURCE",
Expand Down Expand Up @@ -96,7 +96,7 @@
"gasUsed": "41112",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "Raok5XaHEuw/DgmG++27qw3R6aMHAOGM/eVJ994si3c=",
"accumulatorRootHash": "oN+6BcMKulsaHxaMg+0dBIkVVn/Z10ElLFyxvPZdkac=",
"changes": [
{
"type": "WRITE_RESOURCE",
Expand Down Expand Up @@ -225,7 +225,7 @@
"eventRootHash": "QUNDVU1VTEFUT1JfUExBQ0VIT0xERVJfSEFTSAAAAAA=",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "eTMY0bnqb1UA9vJ6mIW7lnaYcPEiWM3RX/mX8cn+PkQ="
"accumulatorRootHash": "QPYcjat45egxozSCjeSZKFyUuBeK/imrqmmNyuG/Sp8="
},
"blockHeight": "1",
"type": "STATE_CHECKPOINT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"eventRootHash": "YPVlJld+KeJV8TgWig4e7McIG/o+rxMn60yg554WVK8=",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "+frUKK1tqTQDiSDscNcfEnCgRHXRX3WzYPGZFbegB0c=",
"accumulatorRootHash": "DJwD1vywXfYH81YFO+4eRwl/8qx5qHRtX82owmq5Rhk=",
"changes": [
{
"type": "WRITE_RESOURCE",
Expand Down Expand Up @@ -96,7 +96,7 @@
"gasUsed": "41112",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "Raok5XaHEuw/DgmG++27qw3R6aMHAOGM/eVJ994si3c=",
"accumulatorRootHash": "oN+6BcMKulsaHxaMg+0dBIkVVn/Z10ElLFyxvPZdkac=",
"changes": [
{
"type": "WRITE_RESOURCE",
Expand Down Expand Up @@ -225,7 +225,7 @@
"eventRootHash": "QUNDVU1VTEFUT1JfUExBQ0VIT0xERVJfSEFTSAAAAAA=",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "eTMY0bnqb1UA9vJ6mIW7lnaYcPEiWM3RX/mX8cn+PkQ="
"accumulatorRootHash": "QPYcjat45egxozSCjeSZKFyUuBeK/imrqmmNyuG/Sp8="
},
"blockHeight": "1",
"type": "STATE_CHECKPOINT",
Expand All @@ -240,7 +240,7 @@
"eventRootHash": "xQa4qACFHx4YpbLjBVf8UKi4CXw95t7b2M90GQXvP8Y=",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "7l+4R74xY84/TYdh6f/miZ7pn4TOMC1FnkaWZJ4LvHc=",
"accumulatorRootHash": "EatHQ3WGg/CkfAyRrG/RzFmYyd8hvZhvDSwQUP/1UuM=",
"changes": [
{
"type": "WRITE_RESOURCE",
Expand Down Expand Up @@ -328,7 +328,7 @@
"gasUsed": "4496",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "Ii62AdxObVnTHk6GCmKy2QajuQMXcLYpYPv4ch3uUFI=",
"accumulatorRootHash": "vxIEHhTn0UOxVczHStRzEWoyt2Hx8TTVNWrI5M2DXxU=",
"changes": [
{
"type": "WRITE_RESOURCE",
Expand Down Expand Up @@ -947,7 +947,7 @@
"eventRootHash": "QUNDVU1VTEFUT1JfUExBQ0VIT0xERVJfSEFTSAAAAAA=",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "LKrTIC/x+CYcbDxr83jHzsOKP8l3tqt8AiZra4uC3lQ="
"accumulatorRootHash": "hOGrtkW6IhsI8ZVRpTWwaeJvSJFn3o/ShjY1ONuVNqs="
},
"blockHeight": "2",
"type": "STATE_CHECKPOINT",
Expand All @@ -962,7 +962,7 @@
"eventRootHash": "dliD7Dy093TZN3Zy5Vpvn9Jb7Q3dr30c4Jc4Sa/Z7Ew=",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "lgrNeOB1v3saEUOMX7rkvqUN1s776kUMgxL1dAxmhzI=",
"accumulatorRootHash": "BW5hZslxLNDMwrREY9UK6wbihuaI+jDuxj4MwyPBvVY=",
"changes": [
{
"type": "WRITE_RESOURCE",
Expand Down Expand Up @@ -1050,7 +1050,7 @@
"gasUsed": "28029",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "GwkxYMlJ49S8cRn9Ih0i35oiPTI+cJ0JEv/ZE0hssjA=",
"accumulatorRootHash": "rj3VtsTHq7EMnCsSYW3Aoc+S3hok1q5gs5bnB/12S4M=",
"changes": [
{
"type": "WRITE_RESOURCE",
Expand Down Expand Up @@ -1290,7 +1290,7 @@
"eventRootHash": "QUNDVU1VTEFUT1JfUExBQ0VIT0xERVJfSEFTSAAAAAA=",
"success": true,
"vmStatus": "Executed successfully",
"accumulatorRootHash": "Fqyx7GdOAQ96rkoKDBo2eadPPist4CXL0via9IicvzQ="
"accumulatorRootHash": "kXbcwzNhfvbbxtio8ghrof5gU166DneLyh74pYqXf3k="
},
"blockHeight": "3",
"type": "STATE_CHECKPOINT",
Expand Down

0 comments on commit bf8ab40

Please sign in to comment.