Skip to content

Commit

Permalink
upgrade nimbus to get auto force-authoring (#1459)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihuiby committed May 22, 2022
1 parent 075da3f commit 7f62160
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 70 deletions.
56 changes: 28 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions pallets/parachain-staking/src/delegation_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,27 +307,25 @@ impl<T: Config> Pallet<T> {
}
}

/// Removes the delegator's existing [ScheduledRequest] towards a given collator.
/// Removes the delegator's existing [ScheduledRequest] towards a given collator, if exists.
/// The state needs to be persisted by the caller of this function.
/// Returns [Error::PendingDelegationRequestDNE] if request does not exist.
pub(crate) fn delegation_remove_request_with_state(
collator: &T::AccountId,
delegator: &T::AccountId,
state: &mut Delegator<T::AccountId, BalanceOf<T>>,
) -> DispatchResultWithPostInfo {
) {
let mut scheduled_requests = <DelegationScheduledRequests<T>>::get(collator);

let request_idx = scheduled_requests
let maybe_request_idx = scheduled_requests
.iter()
.position(|x| &x.delegator == delegator)
.ok_or(<Error<T>>::PendingDelegationRequestDNE)?;
.position(|x| &x.delegator == delegator);

let request = scheduled_requests.remove(request_idx);
let amount = request.action.amount();
state.less_total = state.less_total.saturating_sub(amount);
<DelegationScheduledRequests<T>>::insert(collator, scheduled_requests);

Ok(().into())
if let Some(request_idx) = maybe_request_idx {
let request = scheduled_requests.remove(request_idx);
let amount = request.action.amount();
state.less_total = state.less_total.saturating_sub(amount);
<DelegationScheduledRequests<T>>::insert(collator, scheduled_requests);
}
}

/// Returns true if a [ScheduledRequest] exists for a given delegation
Expand Down
18 changes: 10 additions & 8 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,15 +968,15 @@ pub mod pallet {
Delegator state also has a record. qed.",
);
if let Some(remaining) = delegator.rm_delegation(&candidate) {
Self::delegation_remove_request_with_state(
&candidate,
&bond.owner,
&mut delegator,
);

if remaining.is_zero() {
<DelegatorState<T>>::remove(&bond.owner);
} else {
Self::delegation_remove_request_with_state(
&candidate,
&bond.owner,
&mut delegator,
)
.ok(); // ignore DNE error
<DelegatorState<T>>::insert(&bond.owner, delegator);
}
}
Expand Down Expand Up @@ -1248,9 +1248,9 @@ pub mod pallet {
delegation_count: u32,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
let state = <DelegatorState<T>>::get(&delegator).ok_or(Error::<T>::DelegatorDNE)?;
let mut state = <DelegatorState<T>>::get(&delegator).ok_or(Error::<T>::DelegatorDNE)?;
state.can_execute_leave::<T>(delegation_count)?;
for bond in state.delegations.0 {
for bond in state.delegations.0.clone() {
if let Err(error) = Self::delegator_leaves_candidate(
bond.owner.clone(),
delegator.clone(),
Expand All @@ -1261,6 +1261,8 @@ pub mod pallet {
error
);
}

Self::delegation_remove_request_with_state(&bond.owner, &delegator, &mut state);
}
<DelegatorState<T>>::remove(&delegator);
Self::deposit_event(Event::DelegatorLeft {
Expand Down
Loading

0 comments on commit 7f62160

Please sign in to comment.