Skip to content

Commit

Permalink
[staking] process delegations withdraws at epoch boundaries to includ…
Browse files Browse the repository at this point in the history
…e rewards
  • Loading branch information
emmazzz committed Nov 14, 2022
1 parent 0998a3d commit 34a760f
Show file tree
Hide file tree
Showing 16 changed files with 767 additions and 300 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,13 @@ validators:
delegation_staking_pool:
validator_address: 21b60aa9a8cb189ccbe20461dbfad2202fdef55b
starting_epoch: 1
epoch_starting_sui_balance: 0
epoch_starting_delegation_token_supply: 0
sui_balance: 0
rewards_pool:
value: 0
delegation_token_supply:
value: 0
pending_delegations: []
pending_withdraws: []
commission_rate: 0
pending_validators: []
pending_removals: []
Expand Down Expand Up @@ -428,6 +427,8 @@ validators:
next_epoch_delegation: 0
next_epoch_gas_price: 1
next_epoch_commission_rate: 0
pending_delegation_switches:
contents: []
treasury_cap:
value: 100000000000001
storage_fund:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ expression: common_costs_estimate
---
{
"MergeCoin": {
"computation_cost": 5586,
"storage_cost": 8244,
"computation_cost": 5776,
"storage_cost": 8526,
"storage_rebate": 0
},
"Publish": {
"computation_cost": 5574,
"storage_cost": 8179,
"computation_cost": 5764,
"storage_cost": 8461,
"storage_rebate": 0
},
"SharedCounterAssertValue": {
Expand All @@ -29,8 +29,8 @@ expression: common_costs_estimate
"storage_rebate": 0
},
"SplitCoin": {
"computation_cost": 5564,
"storage_cost": 8212,
"computation_cost": 5754,
"storage_cost": 8493,
"storage_rebate": 0
},
"TransferPortionSuiCoin": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ expression: common_costs_actual
---
{
"MergeCoin": {
"computation_cost": 673,
"computation_cost": 694,
"storage_cost": 32,
"storage_rebate": 0
},
"Publish": {
"computation_cost": 729,
"computation_cost": 750,
"storage_cost": 83,
"storage_rebate": 0
},
Expand All @@ -29,7 +29,7 @@ expression: common_costs_actual
"storage_rebate": 15
},
"SplitCoin": {
"computation_cost": 799,
"computation_cost": 820,
"storage_cost": 80,
"storage_rebate": 0
},
Expand Down
403 changes: 274 additions & 129 deletions crates/sui-framework/docs/staking_pool.md

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions crates/sui-framework/docs/sui_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ If the sender represents an active validator, the request will be processed at t



<pre><code><b>public</b> entry <b>fun</b> <a href="sui_system.md#0x2_sui_system_request_switch_delegation">request_switch_delegation</a>(self: &<b>mut</b> <a href="sui_system.md#0x2_sui_system_SuiSystemState">sui_system::SuiSystemState</a>, delegation: <a href="staking_pool.md#0x2_staking_pool_Delegation">staking_pool::Delegation</a>, staked_sui: &<b>mut</b> <a href="staking_pool.md#0x2_staking_pool_StakedSui">staking_pool::StakedSui</a>, new_validator_address: <b>address</b>, ctx: &<b>mut</b> <a href="tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
<pre><code><b>public</b> entry <b>fun</b> <a href="sui_system.md#0x2_sui_system_request_switch_delegation">request_switch_delegation</a>(self: &<b>mut</b> <a href="sui_system.md#0x2_sui_system_SuiSystemState">sui_system::SuiSystemState</a>, delegation: &<b>mut</b> <a href="staking_pool.md#0x2_staking_pool_Delegation">staking_pool::Delegation</a>, staked_sui: &<b>mut</b> <a href="staking_pool.md#0x2_staking_pool_StakedSui">staking_pool::StakedSui</a>, new_validator_address: <b>address</b>, switch_pool_token_amount: u64, ctx: &<b>mut</b> <a href="tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
</code></pre>


Expand All @@ -653,12 +653,15 @@ If the sender represents an active validator, the request will be processed at t

<pre><code><b>public</b> entry <b>fun</b> <a href="sui_system.md#0x2_sui_system_request_switch_delegation">request_switch_delegation</a>(
self: &<b>mut</b> <a href="sui_system.md#0x2_sui_system_SuiSystemState">SuiSystemState</a>,
delegation: Delegation,
delegation: &<b>mut</b> Delegation,
staked_sui: &<b>mut</b> StakedSui,
new_validator_address: <b>address</b>,
switch_pool_token_amount: u64,
ctx: &<b>mut</b> TxContext,
) {
<a href="validator_set.md#0x2_validator_set_request_switch_delegation">validator_set::request_switch_delegation</a>(&<b>mut</b> self.validators, delegation, staked_sui, new_validator_address, ctx);
<a href="validator_set.md#0x2_validator_set_request_switch_delegation">validator_set::request_switch_delegation</a>(
&<b>mut</b> self.validators, delegation, staked_sui, new_validator_address, switch_pool_token_amount, ctx
);
}
</code></pre>

Expand Down
45 changes: 36 additions & 9 deletions crates/sui-framework/docs/validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
- [Function `decrease_next_epoch_delegation`](#0x2_validator_decrease_next_epoch_delegation)
- [Function `request_set_gas_price`](#0x2_validator_request_set_gas_price)
- [Function `request_set_commission_rate`](#0x2_validator_request_set_commission_rate)
- [Function `distribute_rewards_and_new_delegations`](#0x2_validator_distribute_rewards_and_new_delegations)
- [Function `distribute_rewards`](#0x2_validator_distribute_rewards)
- [Function `process_pending_delegations`](#0x2_validator_process_pending_delegations)
- [Function `get_staking_pool_mut_ref`](#0x2_validator_get_staking_pool_mut_ref)
- [Function `metadata`](#0x2_validator_metadata)
- [Function `sui_address`](#0x2_validator_sui_address)
Expand Down Expand Up @@ -456,7 +457,7 @@ Process pending stake and pending withdraws.



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x2_validator_request_add_delegation">request_add_delegation</a>(self: &<b>mut</b> <a href="validator.md#0x2_validator_Validator">validator::Validator</a>, delegated_stake: <a href="balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="sui.md#0x2_sui_SUI">sui::SUI</a>&gt;, locking_period: <a href="_Option">option::Option</a>&lt;<a href="epoch_time_lock.md#0x2_epoch_time_lock_EpochTimeLock">epoch_time_lock::EpochTimeLock</a>&gt;, ctx: &<b>mut</b> <a href="tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x2_validator_request_add_delegation">request_add_delegation</a>(self: &<b>mut</b> <a href="validator.md#0x2_validator_Validator">validator::Validator</a>, delegated_stake: <a href="balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="sui.md#0x2_sui_SUI">sui::SUI</a>&gt;, locking_period: <a href="_Option">option::Option</a>&lt;<a href="epoch_time_lock.md#0x2_epoch_time_lock_EpochTimeLock">epoch_time_lock::EpochTimeLock</a>&gt;, delegator: <b>address</b>, ctx: &<b>mut</b> <a href="tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
</code></pre>


Expand All @@ -469,11 +470,12 @@ Process pending stake and pending withdraws.
self: &<b>mut</b> <a href="validator.md#0x2_validator_Validator">Validator</a>,
delegated_stake: Balance&lt;SUI&gt;,
locking_period: Option&lt;EpochTimeLock&gt;,
delegator: <b>address</b>,
ctx: &<b>mut</b> TxContext,
) {
<b>let</b> delegate_amount = <a href="balance.md#0x2_balance_value">balance::value</a>(&delegated_stake);
<b>assert</b>!(delegate_amount &gt; 0, 0);
<a href="staking_pool.md#0x2_staking_pool_request_add_delegation">staking_pool::request_add_delegation</a>(&<b>mut</b> self.delegation_staking_pool, delegated_stake, locking_period, ctx);
<a href="staking_pool.md#0x2_staking_pool_request_add_delegation">staking_pool::request_add_delegation</a>(&<b>mut</b> self.delegation_staking_pool, delegated_stake, locking_period, delegator, ctx);

<a href="validator.md#0x2_validator_increase_next_epoch_delegation">increase_next_epoch_delegation</a>(self, delegate_amount);
}
Expand Down Expand Up @@ -529,7 +531,7 @@ Process pending stake and pending withdraws.
withdraw_pool_token_amount: u64,
ctx: &<b>mut</b> TxContext,
) {
<b>let</b> withdraw_sui_amount = <a href="staking_pool.md#0x2_staking_pool_withdraw_stake">staking_pool::withdraw_stake</a>(
<b>let</b> withdraw_sui_amount = <a href="staking_pool.md#0x2_staking_pool_request_withdraw_stake">staking_pool::request_withdraw_stake</a>(
&<b>mut</b> self.delegation_staking_pool, delegation, staked_sui, withdraw_pool_token_amount, ctx);
<a href="validator.md#0x2_validator_decrease_next_epoch_delegation">decrease_next_epoch_delegation</a>(self, withdraw_sui_amount);
}
Expand Down Expand Up @@ -611,13 +613,13 @@ Process pending stake and pending withdraws.

</details>

<a name="0x2_validator_distribute_rewards_and_new_delegations"></a>
<a name="0x2_validator_distribute_rewards"></a>

## Function `distribute_rewards_and_new_delegations`
## Function `distribute_rewards`



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x2_validator_distribute_rewards_and_new_delegations">distribute_rewards_and_new_delegations</a>(self: &<b>mut</b> <a href="validator.md#0x2_validator_Validator">validator::Validator</a>, reward: <a href="balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="sui.md#0x2_sui_SUI">sui::SUI</a>&gt;, ctx: &<b>mut</b> <a href="tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x2_validator_distribute_rewards">distribute_rewards</a>(self: &<b>mut</b> <a href="validator.md#0x2_validator_Validator">validator::Validator</a>, reward: <a href="balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="sui.md#0x2_sui_SUI">sui::SUI</a>&gt;, ctx: &<b>mut</b> <a href="tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
</code></pre>


Expand All @@ -626,9 +628,34 @@ Process pending stake and pending withdraws.
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x2_validator_distribute_rewards_and_new_delegations">distribute_rewards_and_new_delegations</a>(self: &<b>mut</b> <a href="validator.md#0x2_validator_Validator">Validator</a>, reward: Balance&lt;SUI&gt;, ctx: &<b>mut</b> TxContext) {
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x2_validator_distribute_rewards">distribute_rewards</a>(self: &<b>mut</b> <a href="validator.md#0x2_validator_Validator">Validator</a>, reward: Balance&lt;SUI&gt;, ctx: &<b>mut</b> TxContext) {
self.metadata.next_epoch_delegation = self.metadata.next_epoch_delegation + <a href="balance.md#0x2_balance_value">balance::value</a>(&reward);
<a href="staking_pool.md#0x2_staking_pool_advance_epoch">staking_pool::advance_epoch</a>(&<b>mut</b> self.delegation_staking_pool, reward, ctx);
<b>let</b> reward_withdraw = <a href="staking_pool.md#0x2_staking_pool_distribute_rewards">staking_pool::distribute_rewards</a>(&<b>mut</b> self.delegation_staking_pool, reward, ctx);
self.metadata.next_epoch_delegation = self.metadata.next_epoch_delegation - reward_withdraw;
}
</code></pre>



</details>

<a name="0x2_validator_process_pending_delegations"></a>

## Function `process_pending_delegations`



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x2_validator_process_pending_delegations">process_pending_delegations</a>(self: &<b>mut</b> <a href="validator.md#0x2_validator_Validator">validator::Validator</a>, ctx: &<b>mut</b> <a href="tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x2_validator_process_pending_delegations">process_pending_delegations</a>(self: &<b>mut</b> <a href="validator.md#0x2_validator_Validator">Validator</a>, ctx: &<b>mut</b> TxContext) {
<b>let</b> _sui_deposit = <a href="staking_pool.md#0x2_staking_pool_process_pending_delegations">staking_pool::process_pending_delegations</a>(&<b>mut</b> self.delegation_staking_pool, ctx);
<b>assert</b>!(<a href="validator.md#0x2_validator_delegate_amount">delegate_amount</a>(self) == self.metadata.next_epoch_delegation, 0);
}
</code></pre>
Expand Down
Loading

0 comments on commit 34a760f

Please sign in to comment.