Skip to content

Commit

Permalink
Change design of 'AppchainState'.
Browse files Browse the repository at this point in the history
  • Loading branch information
riversyang committed Nov 18, 2022
1 parent c9f4934 commit 58fafd0
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 44 deletions.
4 changes: 2 additions & 2 deletions appchain-anchor/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion appchain-anchor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
48 changes: 46 additions & 2 deletions appchain-anchor/src/storage_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -43,7 +71,7 @@ pub struct OldAppchainAnchor {
/// The protocol settings for appchain anchor.
protocol_settings: LazyOption<ProtocolSettings>,
/// The state of the corresponding appchain.
appchain_state: AppchainState,
appchain_state: OldAppchainState,
/// The staking history data happened in this contract.
staking_histories: LazyOption<LookupArray<StakingHistory>>,
/// The appchain notification history data.
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
}
}
18 changes: 6 additions & 12 deletions appchain-anchor/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
12 changes: 8 additions & 4 deletions appchain-anchor/src/user_actions/appchain_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(),
);
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 9 additions & 9 deletions appchain-anchor/src/user_actions/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/anchor-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
20 changes: 10 additions & 10 deletions tests/simulator/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/simulator/contract_interfaces/lifecycle_actions.rs
Original file line number Diff line number Diff line change
@@ -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<Sandbox>,
signer: &Account,
anchor: &Contract,
) -> anyhow::Result<CallExecutionDetails> {
signer
.call(worker, anchor.id(), "go_booting")
.call(worker, anchor.id(), "generate_initial_validator_set")
.gas(200_000_000_000_000)
.transact()
.await
Expand Down

0 comments on commit 58fafd0

Please sign in to comment.