Skip to content

Commit

Permalink
optimizations & cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Sep 16, 2023
1 parent b0c47cc commit 2ad4ca2
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 142 deletions.
8 changes: 7 additions & 1 deletion substrate/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,13 @@ impl Contains<RuntimeCall> for CallFilter {
}

if let RuntimeCall::Staking(call) = call {
return matches!(call, staking::Call::stake { .. } | staking::Call::unstake { .. });
return matches!(
call,
staking::Call::stake { .. } |
staking::Call::unstake { .. } |
staking::Call::allocate { .. } |
staking::Call::deallocate { .. }
);
}

if let RuntimeCall::Session(call) = call {
Expand Down
34 changes: 24 additions & 10 deletions substrate/staking/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,28 @@ pub mod pallet {

Ok(())
}

/// Allocate `amount` to a given validator set.
#[pallet::call_index(3)]
#[pallet::weight((0, DispatchClass::Operational))] // TODO
pub fn deallocate(
origin: OriginFor<T>,
network: NetworkId,
#[pallet::compact] amount: u64,
) -> DispatchResult {
let account = ensure_signed(origin)?;

// remove participant if we necessary
// we can't directly deallocate here, since the leaving validator
// will be removed after the next session. We only deallocate then
// on `end_session` for the right index.
VsPallet::<T>::maybe_remove_participant(account, Amount(amount), network)
}
}

/// Call order is new_session(i) -> end_session(i - 1) -> start_session(i)
/// Call order is end_session(i - 1) -> start_session(i) -> new_session(i + 1)
/// new_session(i + 1) is called immediately after start_session(i) returns then
/// we wait until the session ends then get a call to end_session(i) and so on.
impl<T: Config> SessionManager<T::ValidatorId> for Pallet<T> {
fn new_session(new_index: u32) -> Option<Vec<T::ValidatorId>> {
let next_validators = VsPallet::<T>::next_validator_set(new_index, NetworkId::Serai);
Expand All @@ -167,20 +186,15 @@ pub mod pallet {
}

fn end_session(end_index: u32) {
// get the validators that are gonna be leaving
// or deallocating the next session
let key = ValidatorSet { session: Session(end_index + 1), network: NetworkId::Serai };
let mut deallocating_validators = VsPallet::<T>::deallocating_validators(key);
let leaving_validators = VsPallet::<T>::leaving_validators(key);
deallocating_validators.extend(leaving_validators);

// do the deallocation of those validator funds
for (account, amount) in deallocating_validators {
let key = ValidatorSet { session: Session(end_index + 1), network: NetworkId::Serai };
let deallocating_validators = VsPallet::<T>::deallocating_validators(key);
for (account, amount, _) in deallocating_validators {
// we can unwrap because we are not deallocating more than allocated.
<Self as AllocatedStaking<T>>::deallocate(&account, amount.0).unwrap();
}

VsPallet::<T>::end_session(NetworkId::Serai);
VsPallet::<T>::end_session(end_index, NetworkId::Serai);
}

fn start_session(start_index: u32) {
Expand Down
Loading

0 comments on commit 2ad4ca2

Please sign in to comment.