Skip to content

Commit

Permalink
Add 2 sudo functions and change a view function.
Browse files Browse the repository at this point in the history
  • Loading branch information
riversyang committed Jan 9, 2022
1 parent ec17b38 commit 38a39f6
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 11 deletions.
2 changes: 1 addition & 1 deletion appchain-anchor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "appchain-anchor"
version = "1.0.4"
version = "1.0.5"
authors = ["Octopus Network"]
edition = "2018"

Expand Down
2 changes: 2 additions & 0 deletions appchain-anchor/src/anchor_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ impl AnchorViewer for AppchainAnchor {
.index_range(),
index_range_of_staking_history: self.staking_histories.get().unwrap().index_range(),
permissionless_actions_status: self.permissionless_actions_status.get().unwrap(),
asset_transfer_is_paused: self.asset_transfer_is_paused,
rewards_withdrawal_is_paused: self.rewards_withdrawal_is_paused,
}
}
//
Expand Down
12 changes: 12 additions & 0 deletions appchain-anchor/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ pub trait SudoActions {
fn pause_rewards_withdrawal(&mut self);
///
fn resume_rewards_withdrawal(&mut self);
///
fn change_account_id_in_appchain_of_validator(
&mut self,
validator_id: AccountId,
account_id_in_appchain: String,
);
///
fn force_change_account_id_in_appchain_of_staking_history(
&mut self,
index: U64,
account_id_in_appchain: String,
);
}

pub trait ValidatorActions {
Expand Down
41 changes: 41 additions & 0 deletions appchain-anchor/src/sudo_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,45 @@ impl SudoActions for AppchainAnchor {
);
self.rewards_withdrawal_is_paused = false;
}
//
fn change_account_id_in_appchain_of_validator(
&mut self,
validator_id: AccountId,
account_id_in_appchain: String,
) {
self.assert_owner();
self.internal_change_account_id_in_appchain_of_validator(
&validator_id,
&account_id_in_appchain,
);
}
//
fn force_change_account_id_in_appchain_of_staking_history(
&mut self,
index: U64,
account_id_in_appchain: String,
) {
self.assert_owner();
let mut staking_histories = self.staking_histories.get().unwrap();
if let Some(mut staking_history) = staking_histories.get(&index.0) {
match staking_history.staking_fact {
StakingFact::ValidatorRegistered {
validator_id,
validator_id_in_appchain: _,
amount,
can_be_delegated_to,
} => {
staking_history.staking_fact = StakingFact::ValidatorRegistered {
validator_id,
validator_id_in_appchain: account_id_in_appchain,
amount,
can_be_delegated_to,
};
staking_histories.insert(&index.0, &staking_history);
self.staking_histories.set(&staking_histories);
}
_ => (),
}
}
}
}
7 changes: 6 additions & 1 deletion appchain-anchor/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ impl AccountIdInAppchain {
///
fn is_valid(&self) -> bool {
if self.raw_string.len() > 2 {
hex::decode(&self.raw_string.as_str()[2..self.raw_string.len()]).is_ok()
match hex::decode(&self.raw_string.as_str()[2..self.raw_string.len()]) {
Ok(bytes) => bytes.len() == 32,
Err(_) => false,
}
} else {
false
}
Expand Down Expand Up @@ -410,6 +413,8 @@ pub struct AnchorStatus {
pub index_range_of_anchor_event_history: IndexRange,
pub index_range_of_staking_history: IndexRange,
pub permissionless_actions_status: PermissionlessActionsStatus,
pub asset_transfer_is_paused: bool,
pub rewards_withdrawal_is_paused: bool,
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down
40 changes: 31 additions & 9 deletions appchain-anchor/src/validator_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ impl ValidatorActions for AppchainAnchor {
//
fn set_validator_id_in_appchain(&mut self, account_id_in_appchain: String) {
let validator_id = env::predecessor_account_id();
let next_validator_set = self.next_validator_set.get().unwrap();
self.assert_validator_id(&validator_id, &next_validator_set);
let validator_id_in_appchain = AccountIdInAppchain::new(Some(account_id_in_appchain));
validator_id_in_appchain.assert_valid();
let mut validator_profiles = self.validator_profiles.get().unwrap();
let mut validator_profile = validator_profiles.get(&validator_id).unwrap();
validator_profile.validator_id_in_appchain = validator_id_in_appchain.to_string();
validator_profiles.insert(validator_profile);
self.validator_profiles.set(&validator_profiles);
self.internal_change_account_id_in_appchain_of_validator(
&validator_id,
&account_id_in_appchain,
);
}
//
fn set_validator_profile(&mut self, profile: HashMap<String, String>) {
Expand All @@ -29,3 +24,30 @@ impl ValidatorActions for AppchainAnchor {
self.validator_profiles.set(&validator_profiles);
}
}

impl AppchainAnchor {
///
pub fn internal_change_account_id_in_appchain_of_validator(
&mut self,
validator_id: &AccountId,
account_id_in_appchain: &String,
) {
let mut next_validator_set = self.next_validator_set.get().unwrap();
self.assert_validator_id(validator_id, &next_validator_set);
let validator_id_in_appchain =
AccountIdInAppchain::new(Some(account_id_in_appchain.clone()));
validator_id_in_appchain.assert_valid();
//
let mut validator_profiles = self.validator_profiles.get().unwrap();
let mut validator_profile = validator_profiles.get(validator_id).unwrap();
validator_profile.validator_id_in_appchain = validator_id_in_appchain.to_string();
validator_profiles.insert(validator_profile);
self.validator_profiles.set(&validator_profiles);
//
let mut validator = next_validator_set.validators.get(validator_id).unwrap();
validator.validator_id_in_appchain = validator_id_in_appchain.to_string();
next_validator_set
.validators
.insert(validator_id, &validator);
}
}

0 comments on commit 38a39f6

Please sign in to comment.