mirror of
https://git.ghostchain.io/proxmio/ghost-node.git
synced 2025-12-27 03:09:56 +00:00
tests updated and small tweaks of the lib
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
@@ -13,7 +13,7 @@ use frame_support::{
|
||||
EstimateNextSessionRotation, ValidatorSet, ValidatorSetWithIdentification,
|
||||
OneSessionHandler, Get,
|
||||
},
|
||||
PalletId, BoundedSlice, WeakBoundedVec,
|
||||
BoundedSlice, WeakBoundedVec,
|
||||
|
||||
};
|
||||
use frame_system::{
|
||||
@@ -188,12 +188,6 @@ pub struct Clap<AccountId, NetworkId, Balance> {
|
||||
pub amount: Balance,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
|
||||
pub struct AuthorityClapsInfo {
|
||||
pub total: u32,
|
||||
pub individual: BTreeMap<AuthIndex, SessionAuthorityInfo>,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
|
||||
pub struct SessionAuthorityInfo {
|
||||
pub claps: u32,
|
||||
@@ -305,9 +299,6 @@ pub mod pallet {
|
||||
#[pallet::constant]
|
||||
type UnsignedPriority: Get<TransactionPriority>;
|
||||
|
||||
#[pallet::constant]
|
||||
type TreasuryPalletId: Get<PalletId>;
|
||||
|
||||
type WeightInfo: WeightInfo;
|
||||
}
|
||||
|
||||
@@ -333,12 +324,11 @@ pub mod pallet {
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
NotAnAuthority,
|
||||
ClapForThePastSession,
|
||||
ClapForWrongSession,
|
||||
CurrentValidatorIsDisabled,
|
||||
AlreadyClapped,
|
||||
UnregisteredClapRemove,
|
||||
TooMuchAuthorities,
|
||||
NotEnoughToApplause,
|
||||
CouldNotAccumulateCommission,
|
||||
CouldNotIncreaseGatekeeperAmount,
|
||||
}
|
||||
@@ -369,25 +359,13 @@ pub mod pallet {
|
||||
ValueQuery
|
||||
>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn authorities_claps)]
|
||||
pub(super) type AuthoritiesClaps<T: Config> = StorageDoubleMap<
|
||||
_,
|
||||
Twox64Concat,
|
||||
T::AuthorityId,
|
||||
Twox64Concat,
|
||||
H256,
|
||||
bool,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn claps_in_session)]
|
||||
pub(super) type ClapsInSession<T: Config> = StorageMap<
|
||||
_,
|
||||
Twox64Concat,
|
||||
SessionIndex,
|
||||
AuthorityClapsInfo,
|
||||
BTreeMap<AuthIndex, SessionAuthorityInfo>,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
@@ -479,7 +457,7 @@ pub mod pallet {
|
||||
let authorities = Authorities::<T>::get();
|
||||
let authority = match authorities.get(clap.authority_index as usize) {
|
||||
Some(authority) => authority,
|
||||
None => return InvalidTransaction::BadProof.into(),
|
||||
None => return InvalidTransaction::BadSigner.into(),
|
||||
};
|
||||
|
||||
let signature_valid = clap.using_encoded(|encoded_clap| {
|
||||
@@ -518,7 +496,7 @@ impl<T: Config> Pallet<T> {
|
||||
|
||||
fn try_slow_clap(clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>) -> DispatchResult {
|
||||
let current_session_index = T::ValidatorSet::session_index();
|
||||
ensure!(current_session_index == clap.session_index, Error::<T>::ClapForThePastSession);
|
||||
ensure!(current_session_index == clap.session_index, Error::<T>::ClapForWrongSession);
|
||||
|
||||
let authorities = Authorities::<T>::get();
|
||||
ensure!(authorities.get(clap.authority_index as usize).is_some(), Error::<T>::NotAnAuthority);
|
||||
@@ -543,15 +521,14 @@ impl<T: Config> Pallet<T> {
|
||||
})?;
|
||||
|
||||
ClapsInSession::<T>::try_mutate(&clap.session_index, |claps_details| {
|
||||
if claps_details.individual.get(&clap.authority_index).map(|x| x.disabled).unwrap_or_default() {
|
||||
if claps_details.get(&clap.authority_index).map(|x| x.disabled).unwrap_or_default() {
|
||||
return Err(Error::<T>::CurrentValidatorIsDisabled);
|
||||
}
|
||||
|
||||
(*claps_details).total.saturating_inc();
|
||||
(*claps_details).individual
|
||||
(*claps_details)
|
||||
.entry(clap.authority_index)
|
||||
.and_modify(|individual| (*individual).claps.saturating_inc())
|
||||
.or_default();
|
||||
.or_insert(SessionAuthorityInfo { claps: 1u32, disabled: false });
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
@@ -888,7 +865,6 @@ impl<T: Config> Pallet<T> {
|
||||
}
|
||||
|
||||
let number_of_claps = ClapsInSession::<T>::get(session_index)
|
||||
.individual
|
||||
.entry(authority_index as AuthIndex)
|
||||
.or_default()
|
||||
.claps;
|
||||
@@ -905,10 +881,17 @@ impl<T: Config> Pallet<T> {
|
||||
if !authorities.is_empty() {
|
||||
assert!(Authorities::<T>::get().is_empty(), "Authorities are already initilized!");
|
||||
let bounded_authorities = BoundedSlice::<'_, _, T::MaxAuthorities>::try_from(authorities)
|
||||
.expect("more than the maximum number of clappers");
|
||||
.expect("more than the maximum number of authorities");
|
||||
Authorities::<T>::put(bounded_authorities);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn set_test_authorities(authorities: Vec<T::AuthorityId>) {
|
||||
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities)
|
||||
.expect("more than the maximum number of authorities");
|
||||
Authorities::<T>::put(bounded_authorities);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
|
||||
@@ -948,17 +931,16 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
|
||||
let validators = T::ValidatorSet::validators();
|
||||
let authorities = Authorities::<T>::get();
|
||||
|
||||
let claps_details = ClapsInSession::<T>::get(session_index);
|
||||
let total_claps_in_session = claps_details.total;
|
||||
let average_claps = claps_details
|
||||
.individual
|
||||
let (sum_claps, total_claps) = ClapsInSession::<T>::get(&session_index)
|
||||
.iter()
|
||||
.filter(|(_, value)| !value.disabled)
|
||||
.fold(0u32, |acc, (_, value)| acc.saturating_add(value.claps))
|
||||
.checked_div(total_claps_in_session)
|
||||
.unwrap_or_default();
|
||||
.fold((0u32, 0u32), |(sum, total), (_, value)|
|
||||
(sum.saturating_add(value.claps), total.saturating_add(1))
|
||||
);
|
||||
|
||||
// TODO: seems like it's not working
|
||||
let average_claps = sum_claps
|
||||
.checked_div(total_claps)
|
||||
.unwrap_or_default();
|
||||
|
||||
let offenders = validators
|
||||
.into_iter()
|
||||
@@ -988,7 +970,6 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
|
||||
let session_index = T::ValidatorSet::session_index();
|
||||
ClapsInSession::<T>::mutate(&session_index, |claps_details| {
|
||||
(*claps_details)
|
||||
.individual
|
||||
.entry(validator_index as AuthIndex)
|
||||
.and_modify(|individual| (*individual).disabled = true)
|
||||
.or_insert(SessionAuthorityInfo { claps: 0u32, disabled: true });
|
||||
|
||||
Reference in New Issue
Block a user