more optimized version for try_offend_validators
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
parent
ba7d2a8222
commit
bd8d7145af
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ghost-slow-clap"
|
||||
version = "0.4.24"
|
||||
version = "0.4.25"
|
||||
description = "Applause protocol for the EVM bridge"
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
@ -1441,41 +1441,53 @@ impl<T: Config> Pallet<T> {
|
||||
disabled_bitmap: BitMap,
|
||||
offence_type: OffenceType,
|
||||
) -> Weight {
|
||||
let validator_set_count = validators.len() as u32;
|
||||
let mut weight = T::DbWeight::get().reads_writes(1, 1);
|
||||
let validator_set_count = validators.len();
|
||||
|
||||
let offenders = validators
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, id)| {
|
||||
(offence_bitmap.exists(&(index as AuthIndex))).then(|| {
|
||||
<T::ValidatorSet as ValidatorSetWithIdentification<T::AccountId>>::IdentificationOf::convert(
|
||||
id.clone(),
|
||||
).map(|full_id| (id.clone(), full_id))
|
||||
})
|
||||
.flatten()
|
||||
})
|
||||
.collect::<Vec<IdentificationTuple<T>>>();
|
||||
let mut offenders = Vec::with_capacity(offence_bitmap.count_ones() as usize);
|
||||
let mut reporter_indexes = Vec::with_capacity(validator_set_count);
|
||||
|
||||
let disabled_or_offence_bitmap = disabled_bitmap.bitor(offence_bitmap);
|
||||
let disabled_or_offence_bitmap = disabled_bitmap.bitor(offence_bitmap.clone());
|
||||
let validator_set_count = validator_set_count as u32;
|
||||
|
||||
for (index, id) in validators.iter().enumerate() {
|
||||
let authority_index = index as AuthIndex;
|
||||
|
||||
if offence_bitmap.exists(&authority_index) {
|
||||
weight.saturating_accrue(T::DbWeight::get().reads(1));
|
||||
if let Some(full_id) = <T::ValidatorSet as ValidatorSetWithIdentification<
|
||||
T::AccountId,
|
||||
>>::IdentificationOf::convert(id.clone())
|
||||
{
|
||||
offenders.push((id.clone(), full_id));
|
||||
}
|
||||
} else if !disabled_or_offence_bitmap.exists(&authority_index) {
|
||||
reporter_indexes.push(index);
|
||||
}
|
||||
}
|
||||
|
||||
let offenders_len = offenders.len() as u32;
|
||||
let not_enough_validators_left = validator_set_count
|
||||
.saturating_sub(disabled_or_offence_bitmap.count_ones())
|
||||
.lt(&T::MinAuthoritiesNumber::get());
|
||||
|
||||
if not_enough_validators_left && offenders.len() > 0 {
|
||||
if not_enough_validators_left && offenders_len > 0 {
|
||||
Self::deposit_event(Event::<T>::BlackSwan);
|
||||
return T::DbWeight::get().writes(1);
|
||||
return weight;
|
||||
}
|
||||
|
||||
let offenders_len = offenders.len() as u32;
|
||||
if offenders_len == 0 {
|
||||
let equilibrium_event = match offence_type {
|
||||
OffenceType::CommitmentOffence => Event::<T>::AuthoritiesCommitmentEquilibrium,
|
||||
OffenceType::ThrottlingOffence(_) => Event::<T>::AuthoritiesApplauseEquilibrium,
|
||||
};
|
||||
Self::deposit_event(equilibrium_event);
|
||||
return T::DbWeight::get().writes(1);
|
||||
return weight;
|
||||
}
|
||||
|
||||
weight.saturating_accrue(T::DbWeight::get().reads(1));
|
||||
let reporters = T::ExposureListener::get_accounts_by_indexes(reporter_indexes.into_iter());
|
||||
|
||||
let offence_event = match offence_type {
|
||||
OffenceType::CommitmentOffence => Event::<T>::SomeAuthoritiesDelayed {
|
||||
delayed: offenders.clone(),
|
||||
@ -1494,21 +1506,13 @@ impl<T: Config> Pallet<T> {
|
||||
offence_type,
|
||||
};
|
||||
|
||||
let reporters = validators
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, _)| {
|
||||
(!disabled_or_offence_bitmap.exists(&(index as AuthIndex)))
|
||||
.then(|| T::ExposureListener::get_account_by_index(index))
|
||||
.flatten()
|
||||
})
|
||||
.collect();
|
||||
|
||||
if let Err(e) = T::ReportUnresponsiveness::report_offence(reporters, offence) {
|
||||
sp_runtime::print(e);
|
||||
}
|
||||
|
||||
T::WeightInfo::try_offend_validators(offenders_len)
|
||||
let extra_weight = T::WeightInfo::try_offend_validators(offenders_len);
|
||||
|
||||
weight.saturating_add(extra_weight)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -177,6 +177,15 @@ impl ExposureListener<Balance, u64> for TestExposureListener
|
||||
where
|
||||
Balance: AtLeast32BitUnsigned + From<u64>,
|
||||
{
|
||||
fn get_accounts_by_indexes(
|
||||
indexes_iterator: impl Iterator<Item = usize>,
|
||||
) -> sp_std::prelude::Vec<u64> {
|
||||
let all_validators = FixedValidators::get();
|
||||
indexes_iterator
|
||||
.filter_map(|i| all_validators.get(i).copied())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_account_by_index(index: usize) -> Option<u64> {
|
||||
FixedValidators::get().get(index).copied()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user