Skip to content

Commit

Permalink
Remove the switch_domain extrinsic from pallet-domains
Browse files Browse the repository at this point in the history
switch_domain is not supported currently due to incompatible with lazily slashing, this
commit only remove the entry of switch_domain and comment out related tests/benchmark to
avoid invasive change to the rest of the code and also we may support it in the future.

Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P committed Mar 1, 2024
1 parent 918a72f commit 812f9f1
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 230 deletions.
46 changes: 24 additions & 22 deletions crates/pallet-domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,28 +308,30 @@ mod benchmarks {
);
}

#[benchmark]
fn switch_domain() {
let domain1_id = register_domain::<T>();
let domain2_id = register_domain::<T>();

let (operator_owner, operator_id) =
register_helper_operator::<T>(domain1_id, T::Currency::minimum_balance());

#[extrinsic_call]
_(
RawOrigin::Signed(operator_owner.clone()),
operator_id,
domain2_id,
);

let operator = Operators::<T>::get(operator_id).expect("operator must exist");
assert_eq!(operator.next_domain_id, domain2_id);

let pending_switch =
PendingOperatorSwitches::<T>::get(domain1_id).expect("pending switch must exist");
assert!(pending_switch.contains(&operator_id));
}
// TODO: `switch_domain` is not supported currently due to incompatible with lazily slashing
// enable this test when `switch_domain` is ready
// #[benchmark]
// fn switch_domain() {
// let domain1_id = register_domain::<T>();
// let domain2_id = register_domain::<T>();

// let (operator_owner, operator_id) =
// register_helper_operator::<T>(domain1_id, T::Currency::minimum_balance());

// #[extrinsic_call]
// _(
// RawOrigin::Signed(operator_owner.clone()),
// operator_id,
// domain2_id,
// );

// let operator = Operators::<T>::get(operator_id).expect("operator must exist");
// assert_eq!(operator.next_domain_id, domain2_id);

// let pending_switch =
// PendingOperatorSwitches::<T>::get(domain1_id).expect("pending switch must exist");
// assert!(pending_switch.contains(&operator_id));
// }

#[benchmark]
fn deregister_operator() {
Expand Down
25 changes: 2 additions & 23 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@ mod pallet {
use crate::staking::do_reward_operators;
use crate::staking::{
do_deregister_operator, do_nominate_operator, do_register_operator, do_slash_operators,
do_switch_operator_domain, do_unlock_funds, do_unlock_operator, do_withdraw_stake, Deposit,
DomainEpoch, Error as StakingError, Operator, OperatorConfig, SharePrice, StakingSummary,
Withdrawal,
do_unlock_funds, do_unlock_operator, do_withdraw_stake, Deposit, DomainEpoch,
Error as StakingError, Operator, OperatorConfig, SharePrice, StakingSummary, Withdrawal,
};
use crate::staking_epoch::{do_finalize_domain_current_epoch, Error as StakingEpochError};
use crate::weights::WeightInfo;
Expand Down Expand Up @@ -1186,26 +1185,6 @@ mod pallet {
Ok(())
}

#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::switch_domain())]
pub fn switch_domain(
origin: OriginFor<T>,
operator_id: OperatorId,
new_domain_id: DomainId,
) -> DispatchResult {
let who = ensure_signed(origin)?;

let old_domain_id = do_switch_operator_domain::<T>(who, operator_id, new_domain_id)
.map_err(Error::<T>::from)?;

Self::deposit_event(Event::OperatorSwitchedDomain {
old_domain_id,
new_domain_id,
});

Ok(())
}

#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::deregister_operator())]
pub fn deregister_operator(
Expand Down
200 changes: 97 additions & 103 deletions crates/pallet-domains/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,9 @@ pub(crate) fn hold_deposit<T: Config>(
Ok(())
}

pub(crate) fn do_switch_operator_domain<T: Config>(
// TODO: `switch_domain` is not supported currently due to incompatible with lazily slashing
#[allow(dead_code)]
fn do_switch_operator_domain<T: Config>(
operator_owner: T::AccountId,
operator_id: OperatorId,
new_domain_id: DomainId,
Expand Down Expand Up @@ -1284,8 +1286,7 @@ pub(crate) mod tests {
use crate::domain_registry::{DomainConfig, DomainObject};
use crate::pallet::{
Config, Deposits, DomainRegistry, DomainStakingSummary, LatestConfirmedDomainBlock,
NextOperatorId, NominatorCount, OperatorIdOwner, Operators, PendingOperatorSwitches,
PendingSlashes, Withdrawals,
NextOperatorId, NominatorCount, OperatorIdOwner, Operators, PendingSlashes, Withdrawals,
};
use crate::staking::{
do_convert_previous_epoch_withdrawal, do_nominate_operator, do_reward_operators,
Expand Down Expand Up @@ -1705,97 +1706,99 @@ pub(crate) mod tests {
});
}

#[test]
fn switch_domain_operator() {
let old_domain_id = DomainId::new(0);
let new_domain_id = DomainId::new(1);
let operator_account = 1;
let operator_free_balance = 250 * SSC;
let operator_stake = 200 * SSC;
let pair = OperatorPair::from_seed(&U256::from(0u32).into());

let mut ext = new_test_ext();
ext.execute_with(|| {
let (operator_id, _) = register_operator(
old_domain_id,
operator_account,
operator_free_balance,
operator_stake,
SSC,
pair.public(),
BTreeMap::new(),
);

let domain_config = DomainConfig {
domain_name: String::from_utf8(vec![0; 1024]).unwrap(),
runtime_id: 0,
max_block_size: u32::MAX,
max_block_weight: Weight::MAX,
bundle_slot_probability: (0, 0),
target_bundles_per_block: 0,
operator_allow_list: OperatorAllowList::Anyone,
initial_balances: Default::default(),
};

let domain_obj = DomainObject {
owner_account_id: 0,
created_at: 0,
genesis_receipt_hash: Default::default(),
domain_config,
domain_runtime_info: Default::default(),
};

DomainRegistry::<Test>::insert(new_domain_id, domain_obj);

DomainStakingSummary::<Test>::insert(
new_domain_id,
StakingSummary {
current_epoch_index: 0,
current_total_stake: 0,
current_operators: BTreeMap::new(),
next_operators: BTreeSet::new(),
current_epoch_rewards: BTreeMap::new(),
},
);

let res = Domains::switch_domain(
RuntimeOrigin::signed(operator_account),
operator_id,
new_domain_id,
);
assert_ok!(res);

let old_domain_stake_summary =
DomainStakingSummary::<Test>::get(old_domain_id).unwrap();
assert!(!old_domain_stake_summary
.next_operators
.contains(&operator_id));

let new_domain_stake_summary =
DomainStakingSummary::<Test>::get(new_domain_id).unwrap();
assert!(!new_domain_stake_summary
.next_operators
.contains(&operator_id));

let operator = Operators::<Test>::get(operator_id).unwrap();
assert_eq!(operator.current_domain_id, old_domain_id);
assert_eq!(operator.next_domain_id, new_domain_id);
assert_eq!(
PendingOperatorSwitches::<Test>::get(old_domain_id).unwrap(),
BTreeSet::from_iter(vec![operator_id])
);

let res = Domains::switch_domain(
RuntimeOrigin::signed(operator_account),
operator_id,
new_domain_id,
);
assert_err!(
res,
Error::<Test>::Staking(crate::staking::Error::PendingOperatorSwitch)
)
});
}
// TODO: `switch_domain` is not supported currently due to incompatible with lazily slashing
// enable this test when `switch_domain` is ready
// #[test]
// fn switch_domain_operator() {
// let old_domain_id = DomainId::new(0);
// let new_domain_id = DomainId::new(1);
// let operator_account = 1;
// let operator_free_balance = 250 * SSC;
// let operator_stake = 200 * SSC;
// let pair = OperatorPair::from_seed(&U256::from(0u32).into());

// let mut ext = new_test_ext();
// ext.execute_with(|| {
// let (operator_id, _) = register_operator(
// old_domain_id,
// operator_account,
// operator_free_balance,
// operator_stake,
// SSC,
// pair.public(),
// BTreeMap::new(),
// );

// let domain_config = DomainConfig {
// domain_name: String::from_utf8(vec![0; 1024]).unwrap(),
// runtime_id: 0,
// max_block_size: u32::MAX,
// max_block_weight: Weight::MAX,
// bundle_slot_probability: (0, 0),
// target_bundles_per_block: 0,
// operator_allow_list: OperatorAllowList::Anyone,
// initial_balances: Default::default(),
// };

// let domain_obj = DomainObject {
// owner_account_id: 0,
// created_at: 0,
// genesis_receipt_hash: Default::default(),
// domain_config,
// domain_runtime_info: Default::default(),
// };

// DomainRegistry::<Test>::insert(new_domain_id, domain_obj);

// DomainStakingSummary::<Test>::insert(
// new_domain_id,
// StakingSummary {
// current_epoch_index: 0,
// current_total_stake: 0,
// current_operators: BTreeMap::new(),
// next_operators: BTreeSet::new(),
// current_epoch_rewards: BTreeMap::new(),
// },
// );

// let res = Domains::switch_domain(
// RuntimeOrigin::signed(operator_account),
// operator_id,
// new_domain_id,
// );
// assert_ok!(res);

// let old_domain_stake_summary =
// DomainStakingSummary::<Test>::get(old_domain_id).unwrap();
// assert!(!old_domain_stake_summary
// .next_operators
// .contains(&operator_id));

// let new_domain_stake_summary =
// DomainStakingSummary::<Test>::get(new_domain_id).unwrap();
// assert!(!new_domain_stake_summary
// .next_operators
// .contains(&operator_id));

// let operator = Operators::<Test>::get(operator_id).unwrap();
// assert_eq!(operator.current_domain_id, old_domain_id);
// assert_eq!(operator.next_domain_id, new_domain_id);
// assert_eq!(
// PendingOperatorSwitches::<Test>::get(old_domain_id).unwrap(),
// BTreeSet::from_iter(vec![operator_id])
// );

// let res = Domains::switch_domain(
// RuntimeOrigin::signed(operator_account),
// operator_id,
// new_domain_id,
// );
// assert_err!(
// res,
// Error::<Test>::Staking(crate::staking::Error::PendingOperatorSwitch)
// )
// });
// }

#[test]
fn operator_deregistration() {
Expand Down Expand Up @@ -1870,15 +1873,6 @@ pub(crate) mod tests {
current_epoch_rewards: BTreeMap::new(),
},
);
let res = Domains::switch_domain(
RuntimeOrigin::signed(operator_account),
operator_id,
new_domain_id,
);
assert_err!(
res,
Error::<Test>::Staking(crate::staking::Error::OperatorNotRegistered)
);

// nominations will not work since the is frozen
let nominator_account = 100;
Expand Down
Loading

0 comments on commit 812f9f1

Please sign in to comment.