Skip to content

Commit

Permalink
[staking] use vector for pending delegations
Browse files Browse the repository at this point in the history
  • Loading branch information
emmazzz committed Sep 20, 2022
1 parent 7082f1f commit 587d7da
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ validators:
value: 0
delegation_token_supply:
value: 0
pending_delegations:
contents: []
pending_delegations: []
pending_validators: []
pending_removals: []
next_epoch_validators:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ expression: common_costs
---
{
"MergeCoin": {
"computation_cost": 485,
"computation_cost": 482,
"storage_cost": 32,
"storage_rebate": 0
},
"Publish": {
"computation_cost": 543,
"computation_cost": 541,
"storage_cost": 83,
"storage_rebate": 0
},
Expand All @@ -29,7 +29,7 @@ expression: common_costs
"storage_rebate": 15
},
"SplitCoin": {
"computation_cost": 596,
"computation_cost": 593,
"storage_cost": 80,
"storage_rebate": 0
},
Expand Down
32 changes: 16 additions & 16 deletions crates/sui-framework/sources/governance/staking_pool.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module sui::staking_pool {
use sui::object::{Self, UID};
use sui::locked_coin;
use sui::coin;
use sui::vec_map::{Self, VecMap};
use std::vector;

friend sui::validator;
friend sui::validator_set;
Expand Down Expand Up @@ -43,7 +43,7 @@ module sui::staking_pool {
/// Delegations requested during the current epoch. We will activate these delegation at the end of current epoch
/// and distribute staking pool tokens at the end-of-epoch exchange rate after the rewards for the current epoch
/// have been deposited.
pending_delegations: VecMap<address, u64>,
pending_delegations: vector<PendingDelegationEntry>,
}

/// An inactive staking pool associated with an inactive validator.
Expand All @@ -56,6 +56,12 @@ module sui::staking_pool {
/// The staking pool token.
struct DelegationToken has drop {}

/// Struct representing a pending delegation.
struct PendingDelegationEntry has store, drop {
delegator: address,
sui_amount: u64,
}

/// A self-custodial delegation object, serving as evidence that the delegator
/// has delegated to a staking pool.
struct Delegation has key {
Expand Down Expand Up @@ -90,7 +96,7 @@ module sui::staking_pool {
sui_balance: 0,
rewards_pool: balance::zero(),
delegation_token_supply: balance::create_supply(DelegationToken {}),
pending_delegations: vec_map::empty(),
pending_delegations: vector::empty(),
}
}

Expand All @@ -100,16 +106,17 @@ module sui::staking_pool {
balance::join(&mut pool.rewards_pool, rewards);

// distribute pool tokens at new exchange rate.
while (!vec_map::is_empty(&pool.pending_delegations)) {
let (delegator, sui_amount) = vec_map::pop(&mut pool.pending_delegations);
distribute_delegation_tokens(pool, delegator, sui_amount, ctx);
while (!vector::is_empty(&pool.pending_delegations)) {
let PendingDelegationEntry { delegator, sui_amount } = vector::pop_back(&mut pool.pending_delegations);
mint_delegation_tokens_to_delegator(pool, delegator, sui_amount, ctx);
pool.sui_balance = pool.sui_balance + sui_amount
};

// Record the epoch starting balance.
pool.epoch_starting_sui_balance = pool.sui_balance;
}

// TODO: implement rate limiting new delegations per epoch.
/// Request to delegate to a staking pool. The delegation gets counted at the beginning of the next epoch,
/// when the delegation object containing the pool tokens is distributed to the delegator.
public(friend) fun request_add_delegation(
Expand All @@ -121,15 +128,8 @@ module sui::staking_pool {
let sui_amount = balance::value(&stake);
assert!(sui_amount > 0, 0);
let delegator = tx_context::sender(ctx);
// insert delegation info into the vec map.
let idx_opt = vec_map::get_idx_opt(&pool.pending_delegations, &delegator);
if (option::is_some(&idx_opt)) {
let (_, entry_mut_ref) =
vec_map::get_entry_by_idx_mut(&mut pool.pending_delegations, option::extract(&mut idx_opt));
*entry_mut_ref = *entry_mut_ref + sui_amount;
} else {
vec_map::insert(&mut pool.pending_delegations, delegator, sui_amount);
};
// insert delegation info into the pendng_delegations vector.
vector::push_back(&mut pool.pending_delegations, PendingDelegationEntry { delegator, sui_amount });
let staked_sui = StakedSui {
id: object::new(ctx),
principal: stake,
Expand All @@ -142,7 +142,7 @@ module sui::staking_pool {
/// `pool_tokens` field of the delegation object.
/// After activation, the delegation officially counts toward the staking power of the validator.
/// Aborts if the pool mismatches, the delegation is already activated, or the delegation cannot be activated yet.
public(friend) fun distribute_delegation_tokens(
public(friend) fun mint_delegation_tokens_to_delegator(
pool: &mut StakingPool,
delegator: address,
sui_amount: u64,
Expand Down
24 changes: 8 additions & 16 deletions crates/sui-types/src/sui_system_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ pub struct MoveOption<T> {
pub vec: Vec<T>,
}

/// Rust version of the Move sui::vec_map::VecMap type.
/// Putting it in this file because it's only used here.
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct VecMap<K, V> {
pub contents: Vec<VecMapEntry<K, V>>,
}

/// Rust version of the Move sui::vec_map::Entry type.
/// Putting it in this file because it's only used here.
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct VecMapEntry<K, V> {
pub key: K,
pub value: V,
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct ValidatorMetadata {
pub sui_address: AccountAddress,
Expand All @@ -70,6 +55,13 @@ pub struct Validator {
pub delegation_staking_pool: StakingPool,
}

/// Rust version of the Move sui::staking_pool::PendingDelegationEntry type.
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct PendingDelegationEntry {
pub delegator: AccountAddress,
pub sui_amount: u64,
}

/// Rust version of the Move sui::staking_pool::StakingPool type
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct StakingPool {
Expand All @@ -79,7 +71,7 @@ pub struct StakingPool {
pub sui_balance: u64,
pub rewards_pool: Balance,
pub delegation_token_supply: Supply,
pub pending_delegations: VecMap<AccountAddress, u64>,
pub pending_delegations: Vec<PendingDelegationEntry>,
}

/// Rust version of the Move sui::validator_set::ValidatorSet type
Expand Down

0 comments on commit 587d7da

Please sign in to comment.