diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index f8dea97..1fbb067 100644 --- a/pallets/slow-clap/Cargo.toml +++ b/pallets/slow-clap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-slow-clap" -version = "0.3.44" +version = "0.3.45" 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 a8c77b5..e28a04e 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -35,7 +35,11 @@ use sp_staking::{ offence::{Kind, Offence, ReportOffence}, SessionIndex, }; -use sp_std::{collections::btree_map::BTreeMap, prelude::*, vec::Vec}; +use sp_std::{ + collections::{btree_map::BTreeMap, btree_set::BTreeSet}, + prelude::*, + vec::Vec, +}; use ghost_networks::{ NetworkData, NetworkDataBasicHandler, NetworkDataInspectHandler, NetworkDataMutateHandler, @@ -238,7 +242,6 @@ pub mod pallet { #[pallet::error] pub enum Error { NotEnoughClaps, - NotAnAuthority, CurrentValidatorIsDisabled, AlreadyClapped, UnregisteredClapRemove, @@ -340,7 +343,7 @@ pub mod pallet { pub fn self_applause( origin: OriginFor, network_id: NetworkIdOf, - session_index: SessionIndex, + prev_session_index: SessionIndex, transaction_hash: H256, receiver: T::AccountId, amount: BalanceOf, @@ -348,7 +351,7 @@ pub mod pallet { let _ = ensure_signed(origin)?; Self::applause_if_posible( network_id, - session_index, + prev_session_index, transaction_hash, receiver, amount, @@ -614,29 +617,35 @@ impl Pallet { ) -> DispatchResult { let curr_session_index = prev_session_index.saturating_add(1); let clap_unique_hash = Self::generate_unique_hash(&receiver, &amount, &network_id); - let prev_received_claps_key = (prev_session_index, &transaction_hash, &clap_unique_hash); - let curr_received_claps_key = (curr_session_index, &transaction_hash, &clap_unique_hash); let prev_authorities = Authorities::::get(&prev_session_index); let curr_authorities = Authorities::::get(&curr_session_index); - let prev_received_claps = ReceivedClaps::::get(&prev_received_claps_key).into_inner(); - let curr_received_claps = ReceivedClaps::::get(&curr_received_claps_key).into_inner(); + let prev_received_claps_key = (prev_session_index, &transaction_hash, &clap_unique_hash); + let curr_received_claps_key = (curr_session_index, &transaction_hash, &clap_unique_hash); + + let prev_received_claps = ReceivedClaps::::get(&prev_received_claps_key) + .into_iter() + .filter_map(|auth_index| prev_authorities.get(auth_index as usize)) + .cloned() + .collect::>(); + + let curr_received_claps = ReceivedClaps::::get(&curr_received_claps_key) + .into_iter() + .filter_map(|auth_index| curr_authorities.get(auth_index as usize)) + .cloned() + .collect::>(); + + let disabled_authorites = ClapsInSession::::get(&prev_session_index) + .values() + .filter(|info| info.disabled) + .count(); + + let active_authorities = prev_authorities.len().saturating_sub(disabled_authorites); let summary_authority_claps_length = curr_received_claps - .difference(&prev_received_claps) - .filter_map(|&index| { - curr_authorities - .get(index as usize) - .map(|curr_authority| { - prev_authorities - .iter() - .position(|prev_authority| curr_authority == prev_authority) - }) - .flatten() - }) - .count() - .saturating_add(curr_received_claps.len()); + .symmetric_difference(&prev_received_claps) + .count(); let clap = Clap { authority_index: Default::default(), @@ -651,7 +660,7 @@ impl Pallet { let enough_authorities = Perbill::from_rational( summary_authority_claps_length as u32, - Authorities::::get(prev_session_index).len() as u32, + active_authorities as u32, ) > Perbill::from_percent(T::ApplauseThreshold::get()); ensure!(enough_authorities, Error::::NotEnoughClaps);