Skip to content

Commit

Permalink
a few tweaks & comments
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Sep 21, 2023
1 parent 3e73abd commit baaf96f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
3 changes: 2 additions & 1 deletion substrate/staking/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub mod pallet {
) -> DispatchResult {
let account = ensure_signed(origin)?;

// remove participant if we necessary
// remove the participant if 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.
Expand Down Expand Up @@ -187,6 +187,7 @@ pub mod pallet {

fn end_session(end_index: u32) {
// do the deallocation of those validator funds
// who will not be in the set next session.
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 {
Expand Down
48 changes: 26 additions & 22 deletions substrate/validator-sets/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ pub mod pallet {
KeyGen { set: ValidatorSet, key_pair: KeyPair },
}

#[pallet::error]
pub enum Error<T> {
/// Validator Set doesn't exist.
NonExistentValidatorSet,
/// Validator Set already generated keys.
AlreadyGeneratedKeys,
/// An invalid MuSig signature was provided.
BadSignature,
/// Not enough bond to participate in a set.
InSufficientBond,
/// Validator wasn't registered or active.
NonExistentValidator,
/// Trying to deallocate more than allocated.
InSufficientAllocation,
}

#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
Expand Down Expand Up @@ -133,22 +149,6 @@ pub mod pallet {
}
}

#[pallet::error]
pub enum Error<T> {
/// Validator Set doesn't exist.
NonExistentValidatorSet,
/// Validator Set already generated keys.
AlreadyGeneratedKeys,
/// An invalid MuSig signature was provided.
BadSignature,
/// Not enough bond to participate in a set.
InSufficientBond,
/// Validator wasn't registered or active.
NonExistentValidator,
/// Trying to deallocate more than allocated.
InSufficientAllocation,
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
/// Called when a block is initialized.
Expand Down Expand Up @@ -210,7 +210,8 @@ pub mod pallet {
*existing = Some(vec![(account, amount)]);
}
Ok::<_, Error<T>>(())
})?;
})
.unwrap();

Ok(())
}
Expand Down Expand Up @@ -264,12 +265,13 @@ pub mod pallet {
}

pub fn genesis_validator_set(network: NetworkId) -> Vec<T::AccountId> {
let mut current = Vec::new();
let key = ValidatorSet { session: Session(0), network };
for p in Self::validator_set(key).unwrap().participants {
current.push(p.0);
}
current
Self::validator_set(key)
.unwrap()
.participants
.into_iter()
.map(|(id, _)| id)
.collect::<Vec<_>>()
}

pub fn next_validator_set(new_index: u32, network: NetworkId) -> Vec<T::AccountId> {
Expand Down Expand Up @@ -397,6 +399,8 @@ pub mod pallet {
}

// delete old keys
// we don't delete the end_index itself since we still need it
// to start the next session.
let key = ValidatorSet { session: Session(end_index - 1), network };
JoiningValidators::<T>::remove(key);
DeallocatingValidators::<T>::remove(key);
Expand Down

0 comments on commit baaf96f

Please sign in to comment.