From 42638eff2ab8af84e750c1b7bedc80140c32fe80 Mon Sep 17 00:00:00 2001 From: Rivers Yang Date: Tue, 4 Jan 2022 15:18:48 +0800 Subject: [PATCH] Add sudo functions `pause_rewards_withdrawal` and `resume_rewards_withdrawal`. --- appchain-anchor/Cargo.toml | 2 +- appchain-anchor/src/interfaces.rs | 4 ++++ appchain-anchor/src/lib.rs | 10 +++++++++ appchain-anchor/src/staking.rs | 2 ++ appchain-anchor/src/storage_migration.rs | 25 +++++------------------ appchain-anchor/src/sudo_actions.rs | 26 ++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 21 deletions(-) diff --git a/appchain-anchor/Cargo.toml b/appchain-anchor/Cargo.toml index 4cf00b9..51053e7 100644 --- a/appchain-anchor/Cargo.toml +++ b/appchain-anchor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "appchain-anchor" -version = "1.0.3" +version = "1.0.4" authors = ["Octopus Network"] edition = "2018" diff --git a/appchain-anchor/src/interfaces.rs b/appchain-anchor/src/interfaces.rs index 45f9d6b..67d5644 100644 --- a/appchain-anchor/src/interfaces.rs +++ b/appchain-anchor/src/interfaces.rs @@ -314,6 +314,10 @@ pub trait SudoActions { fn resume_asset_transfer(&mut self); /// fn remove_staking_history_at(&mut self, index: U64); + /// + fn pause_rewards_withdrawal(&mut self); + /// + fn resume_rewards_withdrawal(&mut self); } pub trait ValidatorActions { diff --git a/appchain-anchor/src/lib.rs b/appchain-anchor/src/lib.rs index f0064a8..bfcf53f 100644 --- a/appchain-anchor/src/lib.rs +++ b/appchain-anchor/src/lib.rs @@ -167,6 +167,8 @@ pub struct AppchainAnchor { asset_transfer_is_paused: bool, /// The staking histories organized by account id user_staking_histories: LazyOption, + /// Whether the rewards withdrawal is paused + rewards_withdrawal_is_paused: bool, } #[near_bindgen] @@ -263,6 +265,7 @@ impl AppchainAnchor { StorageKey::UserStakingHistories.into_bytes(), Some(&UserStakingHistories::new()), ), + rewards_withdrawal_is_paused: false, } } // Assert that the contract called by the owner. @@ -338,6 +341,13 @@ impl AppchainAnchor { "Asset transfer is now paused." ); } + /// + fn assert_rewards_withdrawal_is_not_paused(&self) { + assert!( + !self.rewards_withdrawal_is_paused, + "Rewards withdrawal is now paused." + ); + } /// Set the price (in USD) of OCT token pub fn set_price_of_oct_token(&mut self, price: U128) { let anchor_settings = self.anchor_settings.get().unwrap(); diff --git a/appchain-anchor/src/staking.rs b/appchain-anchor/src/staking.rs index e58f909..a429694 100644 --- a/appchain-anchor/src/staking.rs +++ b/appchain-anchor/src/staking.rs @@ -514,6 +514,7 @@ impl StakingManager for AppchainAnchor { // fn withdraw_validator_rewards(&mut self, validator_id: AccountId) { self.assert_asset_transfer_is_not_paused(); + self.assert_rewards_withdrawal_is_not_paused(); let end_era = self .validator_set_histories .get() @@ -552,6 +553,7 @@ impl StakingManager for AppchainAnchor { // fn withdraw_delegator_rewards(&mut self, delegator_id: AccountId, validator_id: AccountId) { self.assert_asset_transfer_is_not_paused(); + self.assert_rewards_withdrawal_is_not_paused(); let end_era = self .validator_set_histories .get() diff --git a/appchain-anchor/src/storage_migration.rs b/appchain-anchor/src/storage_migration.rs index 9c85c35..30b5777 100644 --- a/appchain-anchor/src/storage_migration.rs +++ b/appchain-anchor/src/storage_migration.rs @@ -55,6 +55,8 @@ pub struct OldAppchainAnchor { reward_distribution_records: LazyOption, /// Whether the asset transfer is paused asset_transfer_is_paused: bool, + /// The staking histories organized by account id + user_staking_histories: LazyOption, } #[near_bindgen] @@ -72,7 +74,7 @@ impl AppchainAnchor { ); // // Create the new contract using the data from the old contract. - let mut new_contract = AppchainAnchor { + let new_contract = AppchainAnchor { appchain_id: old_contract.appchain_id, appchain_registry: old_contract.appchain_registry, owner: old_contract.owner, @@ -96,28 +98,11 @@ impl AppchainAnchor { beefy_light_client_state: old_contract.beefy_light_client_state, reward_distribution_records: old_contract.reward_distribution_records, asset_transfer_is_paused: old_contract.asset_transfer_is_paused, - user_staking_histories: LazyOption::new( - StorageKey::UserStakingHistories.into_bytes(), - Some(&UserStakingHistories::new()), - ), + user_staking_histories: old_contract.user_staking_histories, + rewards_withdrawal_is_paused: false, }; // - new_contract.initialize_user_staking_histories(); // new_contract } } - -impl AppchainAnchor { - pub fn initialize_user_staking_histories(&mut self) { - let staking_histories = self.staking_histories.get().unwrap(); - let index_range = staking_histories.index_range(); - let mut user_staking_histories = self.user_staking_histories.get().unwrap(); - for index in index_range.start_index.0..index_range.end_index.0 + 1 { - if let Some(staking_history) = staking_histories.get(&index) { - user_staking_histories.add_staking_history(&staking_history); - } - } - self.user_staking_histories.set(&user_staking_histories); - } -} diff --git a/appchain-anchor/src/sudo_actions.rs b/appchain-anchor/src/sudo_actions.rs index 2dc69fc..fc0446b 100644 --- a/appchain-anchor/src/sudo_actions.rs +++ b/appchain-anchor/src/sudo_actions.rs @@ -168,11 +168,19 @@ impl SudoActions for AppchainAnchor { // fn pause_asset_transfer(&mut self) { self.assert_owner(); + assert!( + !self.asset_transfer_is_paused, + "Asset transfer is already paused." + ); self.asset_transfer_is_paused = true; } // fn resume_asset_transfer(&mut self) { self.assert_owner(); + assert!( + self.asset_transfer_is_paused, + "Asset transfer is already resumed." + ); self.asset_transfer_is_paused = false; } // @@ -181,4 +189,22 @@ impl SudoActions for AppchainAnchor { let mut staking_histories = self.staking_histories.get().unwrap(); staking_histories.remove_at(&index.0); } + // + fn pause_rewards_withdrawal(&mut self) { + self.assert_owner(); + assert!( + !self.rewards_withdrawal_is_paused, + "Rewards withdrawal is already paused." + ); + self.rewards_withdrawal_is_paused = true; + } + // + fn resume_rewards_withdrawal(&mut self) { + self.assert_owner(); + assert!( + self.rewards_withdrawal_is_paused, + "Rewards withdrawal is already resumed." + ); + self.rewards_withdrawal_is_paused = false; + } }