slightly optimise try_slow_clap

Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
Uncle Stinky 2026-02-22 13:25:10 +03:00
parent 63c03ad6ce
commit c963f7816b
Signed by: st1nky
GPG Key ID: 016064BD97603B40
2 changed files with 19 additions and 28 deletions

View File

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

View File

@ -662,7 +662,7 @@ impl<T: Config> Pallet<T> {
let clap_unique_hash = Self::generate_unique_hash(&clap);
let session_index =
if ApplauseDetails::<T>::get(&prev_session_index, &clap_unique_hash).is_some() {
if ApplauseDetails::<T>::contains_key(&prev_session_index, &clap_unique_hash) {
prev_session_index
} else {
clap.session_index
@ -674,44 +674,35 @@ impl<T: Config> Pallet<T> {
fn try_slow_clap(clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>) -> DispatchResult {
let network_id = clap.network_id;
ensure!(
T::NetworkDataHandler::get(&network_id).is_some(),
T::NetworkDataHandler::contains_key(&network_id),
Error::<T>::UnregistedNetwork
);
ensure!(
LatestExecutedBlock::<T>::get(&network_id) <= clap.block_number,
Error::<T>::ExecutedBlockIsHigher,
);
let (session_index, clap_unique_hash) = Self::mended_session_index(&clap);
let authorities = Authorities::<T>::get(&session_index);
let authorities_length = authorities.len();
let is_disabled = DisabledAuthorityIndexes::<T>::get(&session_index)
.map(|bitmap| bitmap.exists(&clap.authority_index))
.unwrap_or(true);
let not_disabled = DisabledAuthorityIndexes::<T>::get(&session_index)
.map(|bitmap| !bitmap.exists(&clap.authority_index))
.unwrap_or_default();
ensure!(!is_disabled, Error::<T>::DisabledAuthority);
ensure!(not_disabled, Error::<T>::DisabledAuthority);
let applause_threshold = Perbill::from_parts(T::ApplauseThreshold::get());
let threshold_amount = applause_threshold.mul_floor(TotalExposure::<T>::get());
let maybe_account_id =
T::ExposureListener::get_account_by_index(clap.authority_index as usize);
ensure!(
maybe_account_id.is_some(),
Error::<T>::NonExistentAuthorityIndex
);
let account_id = maybe_account_id.unwrap();
let account_id = T::ExposureListener::get_account_by_index(clap.authority_index as usize)
.ok_or(Error::<T>::NonExistentAuthorityIndex)?;
let new_clapped_amount = T::ExposureListener::get_validator_exposure(&account_id);
let mut applause_details =
match ApplauseDetails::<T>::take(&session_index, &clap_unique_hash) {
Some(applause_details) => applause_details,
None => {
ensure!(
LatestExecutedBlock::<T>::get(&network_id) <= clap.block_number,
Error::<T>::ExecutedBlockIsHigher,
);
ApplauseDetail::new(network_id, clap.block_number, authorities_length)
}
};
ApplauseDetails::<T>::try_get(&session_index, &clap_unique_hash).unwrap_or(
ApplauseDetail::new(network_id, clap.block_number, authorities_length),
);
let total_clapped = if clap.removed {
ensure!(
@ -750,9 +741,9 @@ impl<T: Config> Pallet<T> {
.gt(&(authorities_length as u32 / 2));
if total_clapped > threshold_amount && is_enough && !applause_details.finalized {
applause_details.finalized = true;
if let Err(e) = Self::try_applause(&clap) {
sp_runtime::print(e);
match Self::try_applause(&clap) {
Ok(_) => applause_details.finalized = true,
Err(e) => sp_runtime::print(e),
}
}