Skip to content

Commit

Permalink
remove switch domain operator completely
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed May 22, 2024
1 parent 43db71a commit 697f199
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 101 deletions.
25 changes: 0 additions & 25 deletions crates/pallet-domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,31 +651,6 @@ mod benchmarks {
assert!(!operator.deposits_in_epoch.is_zero());
}

// 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() {
let domain_id = register_domain::<T>();
Expand Down
6 changes: 0 additions & 6 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,6 @@ mod pallet {
OptionQuery,
>;

/// Temporary hold of all the operators who decided to switch to another domain.
/// Once epoch is complete, these operators are added to new domains under next_operators.
#[pallet::storage]
pub(super) type PendingOperatorSwitches<T: Config> =
StorageMap<_, Identity, DomainId, BTreeSet<OperatorId>, OptionQuery>;

/// Share price for the operator pool at the end of Domain epoch.
// TODO: currently unbounded storage.
#[pallet::storage]
Expand Down
16 changes: 3 additions & 13 deletions crates/pallet-domains/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ extern crate alloc;
use crate::bundle_storage_fund::{self, deposit_reserve_for_storage_fund};
use crate::pallet::{
Deposits, DomainRegistry, DomainStakingSummary, NextOperatorId, NominatorCount,
OperatorIdOwner, OperatorSigningKey, Operators, PendingOperatorSwitches, PendingSlashes,
PendingStakingOperationCount, Withdrawals,
OperatorIdOwner, OperatorSigningKey, Operators, PendingSlashes, PendingStakingOperationCount,
Withdrawals,
};
use crate::staking_epoch::{mint_funds, mint_into_treasury};
use crate::{
Expand Down Expand Up @@ -1244,16 +1244,6 @@ pub(crate) fn do_slash_operators<T: Config>(
operator.update_status(OperatorStatus::Slashed);
stake_summary.next_operators.remove(operator_id);

// remove any current operator switches
PendingOperatorSwitches::<T>::mutate(
operator.current_domain_id,
|maybe_switching_operators| {
if let Some(switching_operators) = maybe_switching_operators.as_mut() {
switching_operators.remove(operator_id);
}
},
);

pending_slashes.insert(*operator_id);
PendingSlashes::<T>::insert(operator.current_domain_id, pending_slashes);
Pallet::<T>::deposit_event(Event::OperatorSlashed {
Expand Down Expand Up @@ -1772,7 +1762,7 @@ pub(crate) mod tests {
)
);

// domain switch will not work since the operator is frozen
// operator nomination will not work since the operator is already de-registered
let new_domain_id = DomainId::new(1);
let domain_config = DomainConfig {
domain_name: String::from_utf8(vec![0; 1024]).unwrap(),
Expand Down
58 changes: 1 addition & 57 deletions crates/pallet-domains/src/staking_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
use crate::bundle_storage_fund::deposit_reserve_for_storage_fund;
use crate::pallet::{
AccumulatedTreasuryFunds, Deposits, DomainStakingSummary, LastEpochStakingDistribution,
OperatorIdOwner, Operators, PendingOperatorSwitches, PendingSlashes,
PendingStakingOperationCount, Withdrawals,
OperatorIdOwner, Operators, PendingSlashes, PendingStakingOperationCount, Withdrawals,
};
use crate::staking::{
do_convert_previous_epoch_deposits, do_convert_previous_epoch_withdrawal, DomainEpoch,
Expand All @@ -29,7 +28,6 @@ use sp_std::collections::btree_set::BTreeSet;

#[derive(TypeInfo, Encode, Decode, PalletError, Debug, PartialEq)]
pub enum Error {
FinalizeSwitchOperatorDomain(TransitionError),
FinalizeDomainEpochStaking(TransitionError),
OperatorRewardStaking(TransitionError),
SlashOperator(TransitionError),
Expand Down Expand Up @@ -57,9 +55,6 @@ pub(crate) fn do_finalize_domain_current_epoch<T: Config>(
let slashed_nominator_count =
do_finalize_slashed_operators::<T>(domain_id).map_err(Error::SlashOperator)?;

// finalize any operator switches
do_finalize_switch_operator_domain::<T>(domain_id)?;

// finalize any withdrawals and then deposits
let (completed_epoch_index, finalized_operator_count) =
do_finalize_domain_epoch_staking::<T>(domain_id)?;
Expand Down Expand Up @@ -159,57 +154,6 @@ pub(crate) fn operator_take_reward_tax_and_stake<T: Config>(
Ok(rewarded_operator_count)
}

/// Add all the switched operators to new domain as next operators.
/// Once the new domain's epoch is complete, operators are included in the next epoch.
fn do_finalize_switch_operator_domain<T: Config>(domain_id: DomainId) -> Result<(), Error> {
if let Some(operators) = PendingOperatorSwitches::<T>::take(domain_id) {
operators.into_iter().try_for_each(|operator_id| {
switch_operator::<T>(domain_id, operator_id)
.map_err(Error::FinalizeSwitchOperatorDomain)
})?;
}

Ok(())
}

fn switch_operator<T: Config>(
domain_id: DomainId,
operator_id: OperatorId,
) -> Result<(), TransitionError> {
let previous_domain_summary =
DomainStakingSummary::<T>::get(domain_id).ok_or(TransitionError::DomainNotInitialized)?;

// finalize operator staking before moving to next domain
// this also sets the operator epoch price.
do_finalize_operator_epoch_staking::<T>(
domain_id,
operator_id,
previous_domain_summary.current_epoch_index,
)?;

Operators::<T>::try_mutate(operator_id, |maybe_operator| {
let operator = maybe_operator
.as_mut()
.ok_or(TransitionError::UnknownOperator)?;

// operator is not registered, just no-op
if *operator.status::<T>(operator_id) != OperatorStatus::Registered {
return Ok(());
}

operator.current_domain_id = operator.next_domain_id;
DomainStakingSummary::<T>::try_mutate(operator.current_domain_id, |maybe_stake_summary| {
let stake_summary = maybe_stake_summary
.as_mut()
.ok_or(TransitionError::DomainNotInitialized)?;

stake_summary.next_operators.insert(operator_id);

Ok(())
})
})
}

pub(crate) fn do_finalize_domain_epoch_staking<T: Config>(
domain_id: DomainId,
) -> Result<(EpochIndex, u32), Error> {
Expand Down

0 comments on commit 697f199

Please sign in to comment.