Skip to content

Commit ba7edac

Browse files
committed
ensure to create share price when operator de-registers
1 parent cce3d82 commit ba7edac

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

crates/pallet-domains/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,12 @@ mod pallet {
771771
pub type InvalidBundleAuthors<T: Config> =
772772
StorageMap<_, Identity, DomainId, BTreeSet<OperatorId>, ValueQuery>;
773773

774+
/// Storage for operators who de-registered in the current epoch.
775+
/// Will be cleared once epoch is transitioned.
776+
#[pallet::storage]
777+
pub type DeregisteredOperators<T: Config> =
778+
StorageMap<_, Identity, DomainId, BTreeSet<OperatorId>, ValueQuery>;
779+
774780
#[derive(TypeInfo, Encode, Decode, PalletError, Debug, PartialEq)]
775781
pub enum BundleError {
776782
/// Can not find the operator for given operator id.

crates/pallet-domains/src/staking.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use crate::pallet::{
1212
};
1313
use crate::staking_epoch::{mint_funds, mint_into_treasury};
1414
use crate::{
15-
BalanceOf, Config, DepositOnHold, DomainBlockNumberFor, DomainHashingFor, Event,
16-
ExecutionReceiptOf, HoldIdentifier, InvalidBundleAuthors, NominatorId, OperatorEpochSharePrice,
17-
OperatorHighestSlot, Pallet, ReceiptHashFor, SlashedReason,
15+
BalanceOf, Config, DepositOnHold, DeregisteredOperators, DomainBlockNumberFor,
16+
DomainHashingFor, Event, ExecutionReceiptOf, HoldIdentifier, InvalidBundleAuthors, NominatorId,
17+
OperatorEpochSharePrice, OperatorHighestSlot, Pallet, ReceiptHashFor, SlashedReason,
1818
};
1919
use frame_support::traits::fungible::{Inspect, MutateHold};
2020
use frame_support::traits::tokens::{Fortitude, Precision, Preservation};
@@ -698,6 +698,10 @@ pub(crate) fn do_deregister_operator<T: Config>(
698698
operator.update_status(OperatorStatus::Deregistered(operator_deregister_info));
699699

700700
stake_summary.next_operators.remove(&operator_id);
701+
702+
DeregisteredOperators::<T>::mutate(operator.current_domain_id, |operators| {
703+
operators.insert(operator_id)
704+
});
701705
Ok(())
702706
},
703707
)
@@ -1131,36 +1135,35 @@ pub(crate) fn do_unlock_nominator<T: Config>(
11311135
let mut deposit = Deposits::<T>::take(operator_id, nominator_id.clone())
11321136
.ok_or(Error::UnknownNominator)?;
11331137

1134-
// convert any deposits from the previous epoch to shares
1135-
match do_convert_previous_epoch_deposits::<T>(
1138+
// convert any deposits from the previous epoch to shares.
1139+
// share prices will always be present because
1140+
// - if there are any deposits before operator de-registered, we ensure to create a
1141+
// share price for them at the time of epoch transition.
1142+
// - if the operator got rewarded after the being de-registered and due to nomination tax
1143+
// operator self deposits said tax amount, we calculate share price at the time of epoch transition.
1144+
do_convert_previous_epoch_deposits::<T>(
11361145
operator_id,
11371146
&mut deposit,
11381147
current_domain_epoch_index,
1139-
) {
1140-
// Share price may be missing if there is deposit happen in the same epoch as de-register
1141-
Ok(()) | Err(Error::MissingOperatorEpochSharePrice) => {}
1142-
Err(err) => return Err(err),
1143-
}
1148+
)?;
11441149

11451150
// if there are any withdrawals from this operator, account for them
11461151
// if the withdrawals has share price noted, then convert them to AI3
1147-
// if no share price, then it must be intitated in the epoch before operator de-registered,
1148-
// so get the shares as is and include them in the total staked shares.
11491152
let (
11501153
amount_ready_to_withdraw,
11511154
total_storage_fee_withdrawal,
11521155
shares_withdrew_in_current_epoch,
11531156
) = Withdrawals::<T>::take(operator_id, nominator_id.clone())
11541157
.map(|mut withdrawal| {
1155-
match do_convert_previous_epoch_withdrawal::<T>(
1158+
// convert any withdrawals from the previous epoch to stake.
1159+
// share prices will always be present because
1160+
// - if there are any withdrawals before operator de-registered, we ensure to create a
1161+
// share price for them at the time of epoch transition.
1162+
do_convert_previous_epoch_withdrawal::<T>(
11561163
operator_id,
11571164
&mut withdrawal,
11581165
current_domain_epoch_index,
1159-
) {
1160-
// Share price may be missing if there is withdrawal happen in the same epoch as de-register
1161-
Ok(()) | Err(Error::MissingOperatorEpochSharePrice) => {}
1162-
Err(err) => return Err(err),
1163-
}
1166+
)?;
11641167
Ok((
11651168
withdrawal.total_withdrawal_amount,
11661169
withdrawal.total_storage_fee_withdrawal,

crates/pallet-domains/src/staking_epoch.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use crate::staking::{
99
do_cleanup_operator, do_convert_previous_epoch_deposits, do_convert_previous_epoch_withdrawal,
1010
};
1111
use crate::{
12-
BalanceOf, Config, DepositOnHold, DomainChainRewards, ElectionVerificationParams, Event,
13-
HoldIdentifier, InvalidBundleAuthors, OperatorEpochSharePrice, Pallet, bundle_storage_fund,
12+
BalanceOf, Config, DepositOnHold, DeregisteredOperators, DomainChainRewards,
13+
ElectionVerificationParams, Event, HoldIdentifier, InvalidBundleAuthors,
14+
OperatorEpochSharePrice, Pallet, bundle_storage_fund,
1415
};
1516
use frame_support::traits::fungible::{Inspect, Mutate, MutateHold};
1617
use frame_support::traits::tokens::{
@@ -271,6 +272,8 @@ pub(crate) fn do_finalize_domain_epoch_staking<T: Config>(
271272
let mut operators_to_calculate_share_price = operators_with_self_deposits;
272273
// include invalid bundle authors
273274
operators_to_calculate_share_price.extend(InvalidBundleAuthors::<T>::take(domain_id));
275+
// include operators who de-registered in this epoch
276+
operators_to_calculate_share_price.extend(DeregisteredOperators::<T>::take(domain_id));
274277
// exclude operators who have already been processed as they are in next operator set.
275278
operators_to_calculate_share_price.retain(|&x| !stake_summary.next_operators.contains(&x));
276279

@@ -785,8 +788,8 @@ mod tests {
785788
vec![(2, 10 * AI3), (4, 10 * AI3)],
786789
vec![(1, 20 * AI3), (2, 10 * AI3)],
787790
vec![
788-
(1, 164285714285714285666),
789-
(2, 64761904761904761888),
791+
(1, 164285714285714285665),
792+
(2, 64761904761904761879),
790793
(3, 10952380952380952377),
791794
(4, 10 * AI3),
792795
],

0 commit comments

Comments
 (0)