Lines
89.12 %
Functions
11.65 %
Branches
100 %
// Copyright 2019-2025 PureStake Inc.
// This file is part of Moonbeam.
// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
//! Scheduled requests functionality for delegators
use crate::pallet::{
BalanceOf, CandidateInfo, Config, DelegationScheduledRequests,
DelegationScheduledRequestsPerCollator, DelegatorState, Error, Event, Pallet, Round,
RoundIndex, Total,
};
use crate::weights::WeightInfo;
use crate::{auto_compound::AutoCompoundDelegations, Delegator};
use frame_support::dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo};
use frame_support::ensure;
use frame_support::traits::Get;
use frame_support::BoundedVec;
use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode};
use scale_info::TypeInfo;
use sp_runtime::{traits::Saturating, RuntimeDebug};
/// An action that can be performed upon a delegation
#[derive(
Clone,
Eq,
PartialEq,
Encode,
Decode,
RuntimeDebug,
TypeInfo,
PartialOrd,
Ord,
DecodeWithMemTracking,
)]
pub enum DelegationAction<Balance> {
Revoke(Balance),
Decrease(Balance),
}
impl<Balance: Copy> DelegationAction<Balance> {
/// Returns the wrapped amount value.
pub fn amount(&self) -> Balance {
match self {
DelegationAction::Revoke(amount) => *amount,
DelegationAction::Decrease(amount) => *amount,
/// Represents a scheduled request that define a [DelegationAction]. The request is executable
/// iff the provided [RoundIndex] is achieved.
pub struct ScheduledRequest<AccountId, Balance> {
pub delegator: AccountId,
pub when_executable: RoundIndex,
pub action: DelegationAction<Balance>,
/// Represents a cancelled scheduled request for emitting an event.
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, DecodeWithMemTracking)]
pub struct CancelledScheduledRequest<Balance> {
impl<A, B> From<ScheduledRequest<A, B>> for CancelledScheduledRequest<B> {
fn from(request: ScheduledRequest<A, B>) -> Self {
CancelledScheduledRequest {
when_executable: request.when_executable,
action: request.action,
impl<T: Config> Pallet<T> {
/// Schedules a [DelegationAction::Revoke] for the delegator, towards a given collator.
pub(crate) fn delegation_schedule_revoke(
collator: T::AccountId,
delegator: T::AccountId,
) -> DispatchResultWithPostInfo {
let mut state = <DelegatorState<T>>::get(&delegator).ok_or(<Error<T>>::DelegatorDNE)?;
let mut scheduled_requests = <DelegationScheduledRequests<T>>::get(&collator, &delegator);
let actual_weight =
<T as Config>::WeightInfo::schedule_revoke_delegation(scheduled_requests.len() as u32);
// If this is the first scheduled request for this delegator towards this collator,
// ensure we do not exceed the maximum number of delegators that can have pending
// requests for the collator.
let is_new_delegator =
!<DelegationScheduledRequests<T>>::contains_key(&collator, &delegator);
if is_new_delegator {
let current = <DelegationScheduledRequestsPerCollator<T>>::get(&collator);
if current >= Pallet::<T>::max_delegators_per_candidate() {
return Err(DispatchErrorWithPostInfo {
post_info: Some(actual_weight).into(),
error: Error::<T>::ExceedMaxDelegationsPerDelegator.into(),
});
ensure!(
scheduled_requests.is_empty(),
DispatchErrorWithPostInfo {
error: <Error<T>>::PendingDelegationRequestAlreadyExists.into(),
},
);
let bonded_amount = state
.get_bond_amount(&collator)
.ok_or(<Error<T>>::DelegationDNE)?;
let now = <Round<T>>::get().current;
let when = now.saturating_add(T::RevokeDelegationDelay::get());
scheduled_requests
.try_push(ScheduledRequest {
delegator: delegator.clone(),
action: DelegationAction::Revoke(bonded_amount),
when_executable: when,
})
.map_err(|_| DispatchErrorWithPostInfo {
})?;
state.less_total = state.less_total.saturating_add(bonded_amount);
<DelegationScheduledRequestsPerCollator<T>>::mutate(&collator, |c| {
*c = c.saturating_add(1);
<DelegationScheduledRequests<T>>::insert(
collator.clone(),
delegator.clone(),
scheduled_requests,
<DelegatorState<T>>::insert(delegator.clone(), state);
Self::deposit_event(Event::DelegationRevocationScheduled {
round: now,
delegator,
candidate: collator,
scheduled_exit: when,
Ok(().into())
/// Schedules a [DelegationAction::Decrease] for the delegator, towards a given collator.
pub(crate) fn delegation_schedule_bond_decrease(
decrease_amount: BalanceOf<T>,
let actual_weight = <T as Config>::WeightInfo::schedule_delegator_bond_less(
scheduled_requests.len() as u32,
let max_delegators = Pallet::<T>::max_delegators_per_candidate();
if current >= max_delegators {
!scheduled_requests
.iter()
.any(|req| matches!(req.action, DelegationAction::Revoke(_))),
.ok_or(DispatchErrorWithPostInfo {
error: <Error<T>>::DelegationDNE.into(),
bonded_amount > decrease_amount,
error: <Error<T>>::DelegatorBondBelowMin.into(),
let new_amount: BalanceOf<T> = (bonded_amount - decrease_amount).into();
new_amount >= T::MinDelegation::get(),
error: <Error<T>>::DelegationBelowMin.into(),
// Net Total is total after pending orders are executed
let net_total = state.total().saturating_sub(state.less_total);
// Net Total is always >= MinDelegation
let max_subtracted_amount = net_total.saturating_sub(T::MinDelegation::get().into());
decrease_amount <= max_subtracted_amount,
let when = now.saturating_add(T::DelegationBondLessDelay::get());
action: DelegationAction::Decrease(decrease_amount),
state.less_total = state.less_total.saturating_add(decrease_amount);
Self::deposit_event(Event::DelegationDecreaseScheduled {
amount_to_decrease: decrease_amount,
execute_round: when,
Ok(Some(actual_weight).into())
/// Cancels the delegator's existing [ScheduledRequest] towards a given collator.
pub(crate) fn delegation_cancel_request(
<T as Config>::WeightInfo::cancel_delegation_request(scheduled_requests.len() as u32);
let request = Self::cancel_request_with_state(&mut state, &mut scheduled_requests).ok_or(
error: <Error<T>>::PendingDelegationRequestDNE.into(),
)?;
if scheduled_requests.is_empty() {
*c = c.saturating_sub(1);
<DelegationScheduledRequests<T>>::remove(&collator, &delegator);
} else {
Self::deposit_event(Event::CancelledDelegationRequest {
collator,
cancelled_request: request.into(),
fn cancel_request_with_state(
state: &mut Delegator<T::AccountId, BalanceOf<T>>,
scheduled_requests: &mut BoundedVec<
ScheduledRequest<T::AccountId, BalanceOf<T>>,
T::MaxScheduledRequestsPerDelegator,
>,
) -> Option<ScheduledRequest<T::AccountId, BalanceOf<T>>> {
let request = scheduled_requests.get(0).cloned()?;
scheduled_requests.remove(0);
let amount = request.action.amount();
state.less_total = state.less_total.saturating_sub(amount);
Some(request)
/// Executes the delegator's existing [ScheduledRequest] towards a given collator.
pub(crate) fn delegation_execute_scheduled_request(
let request = scheduled_requests
.first()
.ok_or(<Error<T>>::PendingDelegationRequestDNE)?;
request.when_executable <= now,
<Error<T>>::PendingDelegationRequestNotDueYet
match request.action {
DelegationAction::Revoke(amount) => {
<T as Config>::WeightInfo::execute_delegator_revoke_delegation_worst();
// revoking last delegation => leaving set of delegators
let leaving = if state.delegations.0.len() == 1usize {
true
state.total().saturating_sub(T::MinDelegation::get().into()) >= amount,
false
// remove from pending requests
let amount = scheduled_requests.remove(0).action.amount();
// remove delegation from delegator state
state.rm_delegation::<T>(&collator);
// remove delegation from auto-compounding info
<AutoCompoundDelegations<T>>::remove_auto_compound(&collator, &delegator);
// remove delegation from collator state delegations
Self::delegator_leaves_candidate(collator.clone(), delegator.clone(), amount)
.map_err(|err| DispatchErrorWithPostInfo {
error: err,
Self::deposit_event(Event::DelegationRevoked {
candidate: collator.clone(),
unstaked_amount: amount,
if leaving {
<DelegatorState<T>>::remove(&delegator);
Self::deposit_event(Event::DelegatorLeft {
<DelegatorState<T>>::insert(&delegator, state);
DelegationAction::Decrease(_) => {
// decrease delegation
for bond in &mut state.delegations.0 {
if bond.owner == collator {
return if bond.amount > amount {
let amount_before: BalanceOf<T> = bond.amount.into();
bond.amount = bond.amount.saturating_sub(amount);
let mut collator_info = <CandidateInfo<T>>::get(&collator)
.ok_or(<Error<T>>::CandidateDNE)
error: err.into(),
state
.total_sub_if::<T, _>(amount, |total| {
let new_total: BalanceOf<T> = total.into();
new_total >= T::MinDelegation::get(),
<Error<T>>::DelegationBelowMin
Ok(())
// need to go into decrease_delegation
let in_top = collator_info
.decrease_delegation::<T>(
&collator,
amount_before,
amount,
)
<CandidateInfo<T>>::insert(&collator, collator_info);
let new_total_staked = <Total<T>>::get().saturating_sub(amount);
<Total<T>>::put(new_total_staked);
<DelegationScheduledRequestsPerCollator<T>>::mutate(
|c| {
Self::deposit_event(Event::DelegationDecreased {
in_top,
// must rm entire delegation if bond.amount <= less or cancel request
Err(DispatchErrorWithPostInfo {
/// Removes the delegator's existing [ScheduledRequest] towards a given collator, if exists.
/// The state needs to be persisted by the caller of this function.
pub(crate) fn delegation_remove_request_with_state(
collator: &T::AccountId,
delegator: &T::AccountId,
) {
let scheduled_requests = <DelegationScheduledRequests<T>>::get(collator, delegator);
return;
let mut total_amount: BalanceOf<T> = Default::default();
for request in scheduled_requests.iter() {
total_amount = total_amount.saturating_add(amount);
state.less_total = state.less_total.saturating_sub(total_amount);
<DelegationScheduledRequests<T>>::remove(collator, delegator);
<DelegationScheduledRequestsPerCollator<T>>::mutate(collator, |c| {
/// Returns true if a [ScheduledRequest] exists for a given delegation
pub fn delegation_request_exists(collator: &T::AccountId, delegator: &T::AccountId) -> bool {
!<DelegationScheduledRequests<T>>::get(collator, delegator).is_empty()
/// Returns true if a [DelegationAction::Revoke] [ScheduledRequest] exists for a given delegation
pub fn delegation_request_revoke_exists(
) -> bool {
<DelegationScheduledRequests<T>>::get(collator, delegator)
.any(|req| matches!(req.action, DelegationAction::Revoke(_)))
#[cfg(test)]
mod tests {
use super::*;
use crate::{mock::Test, set::OrderedSet, Bond};
#[test]
fn test_cancel_request_with_state_removes_request_for_correct_delegator_and_updates_state() {
let mut state = Delegator {
id: 1,
delegations: OrderedSet::from(vec![Bond {
amount: 100,
owner: 2,
}]),
total: 100,
less_total: 150,
status: crate::DelegatorStatus::Active,
let mut scheduled_requests = vec![
ScheduledRequest {
delegator: 1,
when_executable: 1,
action: DelegationAction::Revoke(100),
delegator: 2,
action: DelegationAction::Decrease(50),
]
.try_into()
.expect("must succeed");
let removed_request =
<Pallet<Test>>::cancel_request_with_state(&mut state, &mut scheduled_requests);
assert_eq!(
removed_request,
Some(ScheduledRequest {
vec![ScheduledRequest {
},]
state.less_total, 50,
"less_total should be reduced by the amount of the cancelled request"
fn test_cancel_request_with_state_does_nothing_when_request_does_not_exist() {
less_total: 100,
let mut scheduled_requests: BoundedVec<
ScheduledRequest<u64, u128>,
<Test as crate::pallet::Config>::MaxScheduledRequestsPerDelegator,
> = BoundedVec::default();
assert_eq!(removed_request, None,);
scheduled_requests.len(),
0,
"scheduled_requests should remain empty"
state.less_total, 100,
"less_total should remain unchanged when there is nothing to cancel"