From c963f7816bade371c9693e8b3a7f3c475d142c4e Mon Sep 17 00:00:00 2001 From: Uncle Stinky Date: Sun, 22 Feb 2026 13:25:10 +0300 Subject: [PATCH] slightly optimise try_slow_clap Signed-off-by: Uncle Stinky --- pallets/slow-clap/Cargo.toml | 2 +- pallets/slow-clap/src/lib.rs | 45 +++++++++++++++--------------------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index 6faf2ae..a7e81ee 100644 --- a/pallets/slow-clap/Cargo.toml +++ b/pallets/slow-clap/Cargo.toml @@ -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 diff --git a/pallets/slow-clap/src/lib.rs b/pallets/slow-clap/src/lib.rs index 7aa2180..1b85692 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -662,7 +662,7 @@ impl Pallet { let clap_unique_hash = Self::generate_unique_hash(&clap); let session_index = - if ApplauseDetails::::get(&prev_session_index, &clap_unique_hash).is_some() { + if ApplauseDetails::::contains_key(&prev_session_index, &clap_unique_hash) { prev_session_index } else { clap.session_index @@ -674,44 +674,35 @@ impl Pallet { fn try_slow_clap(clap: &Clap, BalanceOf>) -> DispatchResult { let network_id = clap.network_id; ensure!( - T::NetworkDataHandler::get(&network_id).is_some(), + T::NetworkDataHandler::contains_key(&network_id), Error::::UnregistedNetwork ); + ensure!( + LatestExecutedBlock::::get(&network_id) <= clap.block_number, + Error::::ExecutedBlockIsHigher, + ); let (session_index, clap_unique_hash) = Self::mended_session_index(&clap); let authorities = Authorities::::get(&session_index); let authorities_length = authorities.len(); - let is_disabled = DisabledAuthorityIndexes::::get(&session_index) - .map(|bitmap| bitmap.exists(&clap.authority_index)) - .unwrap_or(true); + let not_disabled = DisabledAuthorityIndexes::::get(&session_index) + .map(|bitmap| !bitmap.exists(&clap.authority_index)) + .unwrap_or_default(); - ensure!(!is_disabled, Error::::DisabledAuthority); + ensure!(not_disabled, Error::::DisabledAuthority); let applause_threshold = Perbill::from_parts(T::ApplauseThreshold::get()); let threshold_amount = applause_threshold.mul_floor(TotalExposure::::get()); - let maybe_account_id = - T::ExposureListener::get_account_by_index(clap.authority_index as usize); - ensure!( - maybe_account_id.is_some(), - Error::::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::::NonExistentAuthorityIndex)?; let new_clapped_amount = T::ExposureListener::get_validator_exposure(&account_id); let mut applause_details = - match ApplauseDetails::::take(&session_index, &clap_unique_hash) { - Some(applause_details) => applause_details, - None => { - ensure!( - LatestExecutedBlock::::get(&network_id) <= clap.block_number, - Error::::ExecutedBlockIsHigher, - ); - ApplauseDetail::new(network_id, clap.block_number, authorities_length) - } - }; + ApplauseDetails::::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 Pallet { .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), } }