From 58fafd0ff7faa6d2c5c7dea8e5586665738288ce Mon Sep 17 00:00:00 2001 From: Rivers Yang Date: Fri, 18 Nov 2022 22:21:42 +0800 Subject: [PATCH] Change design of 'AppchainState'. --- appchain-anchor/src/interfaces.rs | 4 +- appchain-anchor/src/lib.rs | 2 +- appchain-anchor/src/storage_migration.rs | 48 ++++++++++++++++++- appchain-anchor/src/types.rs | 18 +++---- .../src/user_actions/appchain_lifecycle.rs | 12 +++-- appchain-anchor/src/user_actions/staking.rs | 18 +++---- e2e-tests/anchor-methods.js | 2 +- e2e-tests/main.test.js | 2 +- tests/simulator/common/mod.rs | 20 ++++---- .../contract_interfaces/lifecycle_actions.rs | 4 +- 10 files changed, 86 insertions(+), 44 deletions(-) diff --git a/appchain-anchor/src/interfaces.rs b/appchain-anchor/src/interfaces.rs index 97c0d7a..7e82c2a 100644 --- a/appchain-anchor/src/interfaces.rs +++ b/appchain-anchor/src/interfaces.rs @@ -159,8 +159,8 @@ pub trait AnchorViewer { } pub trait AppchainLifecycleManager { - /// Verify and change the state of corresponding appchain to `booting`. - fn go_booting(&mut self); + /// Generate the initial validator set for the appchain. + fn generate_initial_validator_set(&mut self); /// Verify and change the state of corresponding appchain to `active`. fn go_live(&mut self); /// Initialize the beefy light client diff --git a/appchain-anchor/src/lib.rs b/appchain-anchor/src/lib.rs index c12d40c..ee5ec85 100644 --- a/appchain-anchor/src/lib.rs +++ b/appchain-anchor/src/lib.rs @@ -265,7 +265,7 @@ impl AppchainAnchor { StorageKey::ProtocolSettings.into_bytes(), Some(&ProtocolSettings::default()), ), - appchain_state: AppchainState::Staging, + appchain_state: AppchainState::Booting, staking_histories: LazyOption::new( StorageKey::StakingHistories.into_bytes(), Some(&LookupArray::new(StorageKey::StakingHistoriesMap)), diff --git a/appchain-anchor/src/storage_migration.rs b/appchain-anchor/src/storage_migration.rs index e4d2011..ff065ca 100644 --- a/appchain-anchor/src/storage_migration.rs +++ b/appchain-anchor/src/storage_migration.rs @@ -3,6 +3,34 @@ use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; use near_sdk::collections::{LazyOption, LookupMap}; use near_sdk::{env, near_bindgen, AccountId, Balance}; +#[derive(Clone, Serialize, Deserialize, BorshDeserialize, BorshSerialize, Debug, PartialEq)] +#[serde(crate = "near_sdk::serde")] +pub enum OldAppchainState { + /// The initial state of an appchain, after it is successfully registered. + /// This state is managed by appchain registry. + Registered, + /// The state while the appchain is under auditing by Octopus Network. + /// This state is managed by appchain registry. + Auditing, + /// The state while voter can upvote or downvote an appchain. + /// This state is managed by appchain registry. + InQueue, + /// The state while validator and delegator can deposit OCT tokens to this contract + /// to indicate their willing of staking for an appchain. + Staging, + /// The state while an appchain is booting. + Booting, + /// The state while an appchain is active normally. + Active, + /// The state while an appchain is under challenging, which all deposit and withdraw actions + /// are frozen. + Frozen, + /// The state which an appchain is broken for some technical or governance reasons. + Broken, + /// The state which the lifecycle of an appchain is end. + Dead, +} + #[derive(BorshDeserialize, BorshSerialize)] pub struct OldAppchainAnchor { /// The id of corresponding appchain. @@ -43,7 +71,7 @@ pub struct OldAppchainAnchor { /// The protocol settings for appchain anchor. protocol_settings: LazyOption, /// The state of the corresponding appchain. - appchain_state: AppchainState, + appchain_state: OldAppchainState, /// The staking history data happened in this contract. staking_histories: LazyOption>, /// The appchain notification history data. @@ -98,7 +126,7 @@ impl AppchainAnchor { appchain_settings: old_contract.appchain_settings, anchor_settings: old_contract.anchor_settings, protocol_settings: old_contract.protocol_settings, - appchain_state: old_contract.appchain_state, + appchain_state: AppchainState::from_old_version(old_contract.appchain_state), staking_histories: old_contract.staking_histories, appchain_notification_histories: old_contract.appchain_notification_histories, permissionless_actions_status: old_contract.permissionless_actions_status, @@ -117,3 +145,19 @@ impl AppchainAnchor { new_contract } } + +impl AppchainState { + pub fn from_old_version(old_version: OldAppchainState) -> Self { + match old_version { + OldAppchainState::Registered => AppchainState::Registered, + OldAppchainState::Auditing => AppchainState::Audited, + OldAppchainState::InQueue => AppchainState::Voting, + OldAppchainState::Staging => AppchainState::Booting, + OldAppchainState::Booting => AppchainState::Booting, + OldAppchainState::Active => AppchainState::Active, + OldAppchainState::Frozen => AppchainState::Closing, + OldAppchainState::Broken => AppchainState::Closing, + OldAppchainState::Dead => AppchainState::Closed, + } + } +} diff --git a/appchain-anchor/src/types.rs b/appchain-anchor/src/types.rs index d0fac18..a64848c 100644 --- a/appchain-anchor/src/types.rs +++ b/appchain-anchor/src/types.rs @@ -82,24 +82,18 @@ pub enum AppchainState { Registered, /// The state while the appchain is under auditing by Octopus Network. /// This state is managed by appchain registry. - Auditing, - /// The state while voter can upvote or downvote an appchain. + Audited, + /// The state while members of Octopus DAO can upvote for the appchain. /// This state is managed by appchain registry. - InQueue, - /// The state while validator and delegator can deposit OCT tokens to this contract - /// to indicate their willing of staking for an appchain. - Staging, + Voting, /// The state while an appchain is booting. Booting, /// The state while an appchain is active normally. Active, - /// The state while an appchain is under challenging, which all deposit and withdraw actions - /// are frozen. - Frozen, - /// The state which an appchain is broken for some technical or governance reasons. - Broken, + /// The state which an appchain is closing for some technical or governance reasons. + Closing, /// The state which the lifecycle of an appchain is end. - Dead, + Closed, } #[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone)] diff --git a/appchain-anchor/src/user_actions/appchain_lifecycle.rs b/appchain-anchor/src/user_actions/appchain_lifecycle.rs index 0ba4e83..d897f4f 100644 --- a/appchain-anchor/src/user_actions/appchain_lifecycle.rs +++ b/appchain-anchor/src/user_actions/appchain_lifecycle.rs @@ -6,12 +6,12 @@ use crate::{ #[near_bindgen] impl AppchainLifecycleManager for AppchainAnchor { // - fn go_booting(&mut self) { + fn generate_initial_validator_set(&mut self) { self.assert_owner(); assert_eq!( self.appchain_state, - AppchainState::Staging, - "Appchain state must be 'staging'." + AppchainState::Booting, + "Appchain state must be 'Booting'." ); let protocol_settings = self.protocol_settings.get().unwrap(); let next_validator_set = self.next_validator_set.get().unwrap(); @@ -25,7 +25,6 @@ impl AppchainLifecycleManager for AppchainAnchor { >= protocol_settings.minimum_total_stake_price_for_booting.0, "Not enough stake deposited in anchor." ); - self.appchain_state = AppchainState::Booting; let mut processing_context = AppchainMessagesProcessingContext::new( self.permissionless_actions_status.get().unwrap(), ); @@ -55,6 +54,11 @@ impl AppchainLifecycleManager for AppchainAnchor { AppchainState::Booting, "Appchain state must be 'booting'." ); + let validator_set_histories = self.validator_set_histories.get().unwrap(); + assert!( + validator_set_histories.get(&0).is_some(), + "The validator set 0 has not been generated." + ); let wrapped_appchain_token = self.wrapped_appchain_token.get().unwrap(); assert!( !(wrapped_appchain_token.contract_account.is_none() diff --git a/appchain-anchor/src/user_actions/staking.rs b/appchain-anchor/src/user_actions/staking.rs index 1fec135..9f84b51 100644 --- a/appchain-anchor/src/user_actions/staking.rs +++ b/appchain-anchor/src/user_actions/staking.rs @@ -62,7 +62,7 @@ impl AppchainAnchor { can_be_delegated_to: bool, ) { match self.appchain_state { - AppchainState::Staging | AppchainState::Active => (), + AppchainState::Booting | AppchainState::Active => (), _ => panic!( "Cannot register validator while appchain state is '{}'.", serde_json::to_string(&self.appchain_state).unwrap() @@ -166,7 +166,7 @@ impl AppchainAnchor { // fn increase_stake(&mut self, validator_id: AccountId, amount: U128) { match self.appchain_state { - AppchainState::Staging | AppchainState::Active => (), + AppchainState::Booting | AppchainState::Active => (), _ => panic!( "Cannot increase stake while appchain state is '{}'.", serde_json::to_string(&self.appchain_state).unwrap() @@ -209,7 +209,7 @@ impl AppchainAnchor { deposit_amount: U128, ) { match self.appchain_state { - AppchainState::Staging | AppchainState::Active => (), + AppchainState::Booting | AppchainState::Active => (), _ => panic!( "Cannot register delegator while appchain state is '{}'.", serde_json::to_string(&self.appchain_state).unwrap() @@ -284,7 +284,7 @@ impl AppchainAnchor { amount: U128, ) { match self.appchain_state { - AppchainState::Staging | AppchainState::Active => (), + AppchainState::Booting | AppchainState::Active => (), _ => panic!( "Cannot increase delegation while appchain state is '{}'.", serde_json::to_string(&self.appchain_state).unwrap() @@ -327,7 +327,7 @@ impl StakingManager for AppchainAnchor { // fn decrease_stake(&mut self, amount: U128) { match self.appchain_state { - AppchainState::Active => (), + AppchainState::Booting | AppchainState::Active => (), _ => panic!( "Cannot decrease stake while appchain state is '{}'.", serde_json::to_string(&self.appchain_state).unwrap() @@ -368,7 +368,7 @@ impl StakingManager for AppchainAnchor { // fn unbond_stake(&mut self) { match self.appchain_state { - AppchainState::Active | AppchainState::Broken => (), + AppchainState::Active | AppchainState::Closing => (), _ => panic!( "Cannot unbond stake while appchain state is '{}'.", serde_json::to_string(&self.appchain_state).unwrap() @@ -419,7 +419,7 @@ impl StakingManager for AppchainAnchor { // fn decrease_delegation(&mut self, validator_id: AccountId, amount: U128) { match self.appchain_state { - AppchainState::Active => (), + AppchainState::Booting | AppchainState::Active => (), _ => panic!( "Cannot decrease delegation while appchain state is '{}'.", serde_json::to_string(&self.appchain_state).unwrap() @@ -464,7 +464,7 @@ impl StakingManager for AppchainAnchor { // fn unbond_delegation(&mut self, validator_id: AccountId) { match self.appchain_state { - AppchainState::Active | AppchainState::Broken => (), + AppchainState::Active | AppchainState::Closing => (), _ => panic!( "Cannot unbond delegation while appchain state is '{}'.", serde_json::to_string(&self.appchain_state).unwrap() @@ -690,7 +690,7 @@ impl StakingManager for AppchainAnchor { new_validator_id: AccountId, ) { match self.appchain_state { - AppchainState::Active | AppchainState::Broken => (), + AppchainState::Active => (), _ => panic!( "Cannot change delegated validator while appchain state is '{}'.", serde_json::to_string(&self.appchain_state).unwrap() diff --git a/e2e-tests/anchor-methods.js b/e2e-tests/anchor-methods.js index bc63e2c..a864f90 100644 --- a/e2e-tests/anchor-methods.js +++ b/e2e-tests/anchor-methods.js @@ -25,7 +25,7 @@ module.exports = { // oct price 'set_price_of_oct_token', // appchain lifecycle - 'go_booting', + 'generate_initial_validator_set', 'go_live', // staking & delegating 'decrease_stake', diff --git a/e2e-tests/main.test.js b/e2e-tests/main.test.js index 42d061a..55cb630 100644 --- a/e2e-tests/main.test.js +++ b/e2e-tests/main.test.js @@ -272,7 +272,7 @@ test('test increase stake', async () => { }); test('test go booting', async () => { - await anchor.go_booting({}, CALL_GAS, 0); + await anchor.generate_initial_validator_set({}, CALL_GAS, 0); const appchainState = await anchor.get_appchain_state(); expect(appchainState).toEqual('Booting'); }); diff --git a/tests/simulator/common/mod.rs b/tests/simulator/common/mod.rs index 7e9427c..6f20875 100644 --- a/tests/simulator/common/mod.rs +++ b/tests/simulator/common/mod.rs @@ -119,7 +119,7 @@ pub async fn test_normal_actions( // assert_eq!( anchor_viewer::get_appchain_state(worker, &anchor).await?, - AppchainState::Staging + AppchainState::Booting ); if to_confirm_view_result { let anchor_status = anchor_viewer::get_anchor_status(worker, &anchor).await?; @@ -464,13 +464,13 @@ pub async fn test_normal_actions( complex_viewer::print_validator_list_of(worker, &anchor, None).await?; } // - // Try go_booting + // Try generate_initial_validator_set // - lifecycle_actions::go_booting(worker, &root, &anchor) + lifecycle_actions::generate_initial_validator_set(worker, &root, &anchor) .await .expect_err("Should fail"); // - // Set appchain settings and try go_booting + // Set appchain settings and try generate_initial_validator_set // settings_manager::set_rpc_endpoint(worker, &root, &anchor, "rpc_endpoint".to_string()) .await @@ -481,27 +481,27 @@ pub async fn test_normal_actions( settings_manager::set_era_reward(worker, &root, &anchor, to_actual_amount(10, 18)) .await .expect("Failed in calling 'set_era_reward'"); - lifecycle_actions::go_booting(worker, &root, &anchor) + lifecycle_actions::generate_initial_validator_set(worker, &root, &anchor) .await .expect_err("Should fail"); // - // Change protocol settings and try go_booting + // Change protocol settings and try generate_initial_validator_set // settings_manager::change_minimum_validator_count(worker, &root, &anchor, 1) .await .expect("Failed in calling 'change_minimum_validator_count'"); - lifecycle_actions::go_booting(worker, &root, &anchor) + lifecycle_actions::generate_initial_validator_set(worker, &root, &anchor) .await .expect_err("Should fail"); // - // Change price of OCT token and try go_booting + // Change price of OCT token and try generate_initial_validator_set // settings_manager::set_price_of_oct_token(worker, &users[4], &anchor, 2_130_000) .await .expect("Failed in calling 'set_price_of_oct_token'"); - lifecycle_actions::go_booting(worker, &root, &anchor) + lifecycle_actions::generate_initial_validator_set(worker, &root, &anchor) .await - .expect("Failed in calling 'go_booting'"); + .expect("Failed in calling 'generate_initial_validator_set'"); assert_eq!( anchor_viewer::get_appchain_state(worker, &anchor).await?, AppchainState::Booting diff --git a/tests/simulator/contract_interfaces/lifecycle_actions.rs b/tests/simulator/contract_interfaces/lifecycle_actions.rs index bd4f542..607a518 100644 --- a/tests/simulator/contract_interfaces/lifecycle_actions.rs +++ b/tests/simulator/contract_interfaces/lifecycle_actions.rs @@ -1,13 +1,13 @@ use near_sdk::serde_json::json; use workspaces::{network::Sandbox, result::CallExecutionDetails, Account, Contract, Worker}; -pub async fn go_booting( +pub async fn generate_initial_validator_set( worker: &Worker, signer: &Account, anchor: &Contract, ) -> anyhow::Result { signer - .call(worker, anchor.id(), "go_booting") + .call(worker, anchor.id(), "generate_initial_validator_set") .gas(200_000_000_000_000) .transact() .await