From 8a5a7eb077adc256a32c9380e9798fcf852eb2d6 Mon Sep 17 00:00:00 2001 From: Emma Zhong Date: Mon, 23 Jan 2023 12:32:54 -0800 Subject: [PATCH] switch order of processing delegation and withdraw --- .../empirical_transaction_cost__good_snapshot-2.snap | 12 ++++++------ crates/sui-framework/docs/staking_pool.md | 8 ++++---- crates/sui-framework/docs/validator.md | 3 ++- .../sources/governance/staking_pool.move | 8 ++++---- .../sui-framework/sources/governance/validator.move | 3 ++- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/crates/sui-cost/tests/snapshots/empirical_transaction_cost__good_snapshot-2.snap b/crates/sui-cost/tests/snapshots/empirical_transaction_cost__good_snapshot-2.snap index 0dd99ed572222..1de41a37ed2eb 100644 --- a/crates/sui-cost/tests/snapshots/empirical_transaction_cost__good_snapshot-2.snap +++ b/crates/sui-cost/tests/snapshots/empirical_transaction_cost__good_snapshot-2.snap @@ -4,13 +4,13 @@ expression: common_costs_estimate --- { "MergeCoin": { - "computation_cost": 7823, - "storage_cost": 11262, + "computation_cost": 7824, + "storage_cost": 11264, "storage_rebate": 0 }, "Publish": { - "computation_cost": 8682, - "storage_cost": 12466, + "computation_cost": 8683, + "storage_cost": 12467, "storage_rebate": 0 }, "SharedCounterAssertValue": { @@ -29,8 +29,8 @@ expression: common_costs_estimate "storage_rebate": 0 }, "SplitCoin": { - "computation_cost": 7801, - "storage_cost": 11230, + "computation_cost": 7802, + "storage_cost": 11232, "storage_rebate": 0 }, "TransferPortionSuiCoin": { diff --git a/crates/sui-framework/docs/staking_pool.md b/crates/sui-framework/docs/staking_pool.md index c9d76b8c8e32d..b3bdb655a8285 100644 --- a/crates/sui-framework/docs/staking_pool.md +++ b/crates/sui-framework/docs/staking_pool.md @@ -48,6 +48,7 @@ use 0x2::epoch_time_lock; use 0x2::linked_table; use 0x2::locked_coin; +use 0x2::math; use 0x2::object; use 0x2::sui; use 0x2::table_vec; @@ -961,10 +962,9 @@ time in request_withdraw_stake. total_sui_withdraw_amount - principal_withdraw_amount else 0; // This may happen when we are withdrawing everything from the pool and - // the rewards pool balance may be 1 less than reward_withdraw_amount. - if (balance::value(&pool.rewards_pool) < reward_withdraw_amount) { - reward_withdraw_amount = balance::value(&pool.rewards_pool); - }; + // the rewards pool balance may be less than reward_withdraw_amount. + // TODO: FIGURE OUT EXACTLY WHY THIS CAN HAPPEN. + reward_withdraw_amount = math::min(reward_withdraw_amount, balance::value(&pool.rewards_pool)); balance::decrease_supply( &mut pool.delegation_token_supply, withdrawn_pool_tokens diff --git a/crates/sui-framework/docs/validator.md b/crates/sui-framework/docs/validator.md index 7325639f297ed..336237f955229 100644 --- a/crates/sui-framework/docs/validator.md +++ b/crates/sui-framework/docs/validator.md @@ -732,10 +732,11 @@ Process pending delegations and withdraws, called at the end of the epoch.
public(friend) fun process_pending_delegations_and_withdraws(self: &mut Validator, ctx: &mut TxContext) {
-    staking_pool::process_pending_delegations(&mut self.delegation_staking_pool, ctx);
     let reward_withdraw_amount = staking_pool::process_pending_delegation_withdraws(
         &mut self.delegation_staking_pool, ctx);
     self.metadata.next_epoch_delegation = self.metadata.next_epoch_delegation - reward_withdraw_amount;
+    staking_pool::process_pending_delegations(&mut self.delegation_staking_pool, ctx);
+    // TODO: consider bringing this assert back when we are more confident.
     // assert!(delegate_amount(self) == self.metadata.next_epoch_delegation, 0);
 }
 
diff --git a/crates/sui-framework/sources/governance/staking_pool.move b/crates/sui-framework/sources/governance/staking_pool.move index 9a1d27d6a026b..40b933963ab37 100644 --- a/crates/sui-framework/sources/governance/staking_pool.move +++ b/crates/sui-framework/sources/governance/staking_pool.move @@ -14,6 +14,7 @@ module sui::staking_pool { use std::vector; use sui::table_vec::{Self, TableVec}; use sui::linked_table::{Self, LinkedTable}; + use sui::math; friend sui::validator; friend sui::validator_set; @@ -353,10 +354,9 @@ module sui::staking_pool { total_sui_withdraw_amount - principal_withdraw_amount else 0; // This may happen when we are withdrawing everything from the pool and - // the rewards pool balance may be 1 less than reward_withdraw_amount. - if (balance::value(&pool.rewards_pool) < reward_withdraw_amount) { - reward_withdraw_amount = balance::value(&pool.rewards_pool); - }; + // the rewards pool balance may be less than reward_withdraw_amount. + // TODO: FIGURE OUT EXACTLY WHY THIS CAN HAPPEN. + reward_withdraw_amount = math::min(reward_withdraw_amount, balance::value(&pool.rewards_pool)); balance::decrease_supply( &mut pool.delegation_token_supply, withdrawn_pool_tokens diff --git a/crates/sui-framework/sources/governance/validator.move b/crates/sui-framework/sources/governance/validator.move index ea3ab195e4e6f..2120bd9af1334 100644 --- a/crates/sui-framework/sources/governance/validator.move +++ b/crates/sui-framework/sources/governance/validator.move @@ -284,10 +284,11 @@ module sui::validator { /// Process pending delegations and withdraws, called at the end of the epoch. public(friend) fun process_pending_delegations_and_withdraws(self: &mut Validator, ctx: &mut TxContext) { - staking_pool::process_pending_delegations(&mut self.delegation_staking_pool, ctx); let reward_withdraw_amount = staking_pool::process_pending_delegation_withdraws( &mut self.delegation_staking_pool, ctx); self.metadata.next_epoch_delegation = self.metadata.next_epoch_delegation - reward_withdraw_amount; + staking_pool::process_pending_delegations(&mut self.delegation_staking_pool, ctx); + // TODO: consider bringing this assert back when we are more confident. // assert!(delegate_amount(self) == self.metadata.next_epoch_delegation, 0); }