more optimized version for try_offend_validators

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2026-02-26 14:44:36 +03:00
parent ba7d2a8222
commit bd8d7145af
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
3 changed files with 43 additions and 30 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ghost-slow-clap" name = "ghost-slow-clap"
version = "0.4.24" version = "0.4.25"
description = "Applause protocol for the EVM bridge" description = "Applause protocol for the EVM bridge"
license.workspace = true license.workspace = true
authors.workspace = true authors.workspace = true

View File

@ -1441,41 +1441,53 @@ impl<T: Config> Pallet<T> {
disabled_bitmap: BitMap, disabled_bitmap: BitMap,
offence_type: OffenceType, offence_type: OffenceType,
) -> Weight { ) -> 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 let mut offenders = Vec::with_capacity(offence_bitmap.count_ones() as usize);
.into_iter() let mut reporter_indexes = Vec::with_capacity(validator_set_count);
.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 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 let not_enough_validators_left = validator_set_count
.saturating_sub(disabled_or_offence_bitmap.count_ones()) .saturating_sub(disabled_or_offence_bitmap.count_ones())
.lt(&T::MinAuthoritiesNumber::get()); .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); 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 { if offenders_len == 0 {
let equilibrium_event = match offence_type { let equilibrium_event = match offence_type {
OffenceType::CommitmentOffence => Event::<T>::AuthoritiesCommitmentEquilibrium, OffenceType::CommitmentOffence => Event::<T>::AuthoritiesCommitmentEquilibrium,
OffenceType::ThrottlingOffence(_) => Event::<T>::AuthoritiesApplauseEquilibrium, OffenceType::ThrottlingOffence(_) => Event::<T>::AuthoritiesApplauseEquilibrium,
}; };
Self::deposit_event(equilibrium_event); 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 { let offence_event = match offence_type {
OffenceType::CommitmentOffence => Event::<T>::SomeAuthoritiesDelayed { OffenceType::CommitmentOffence => Event::<T>::SomeAuthoritiesDelayed {
delayed: offenders.clone(), delayed: offenders.clone(),
@ -1494,21 +1506,13 @@ impl<T: Config> Pallet<T> {
offence_type, 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) { if let Err(e) = T::ReportUnresponsiveness::report_offence(reporters, offence) {
sp_runtime::print(e); 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)
} }
} }

View File

@ -177,6 +177,15 @@ impl ExposureListener<Balance, u64> for TestExposureListener
where where
Balance: AtLeast32BitUnsigned + From<u64>, 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> { fn get_account_by_index(index: usize) -> Option<u64> {
FixedValidators::get().get(index).copied() FixedValidators::get().get(index).copied()
} }