fixes for self_applause function

Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
Uncle Stinky 2025-11-05 20:52:00 +03:00
parent c872eca8ac
commit ebae9fadbe
Signed by: st1nky
GPG Key ID: 016064BD97603B40
2 changed files with 32 additions and 23 deletions

View File

@ -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

View File

@ -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<T> {
NotEnoughClaps,
NotAnAuthority,
CurrentValidatorIsDisabled,
AlreadyClapped,
UnregisteredClapRemove,
@ -340,7 +343,7 @@ pub mod pallet {
pub fn self_applause(
origin: OriginFor<T>,
network_id: NetworkIdOf<T>,
session_index: SessionIndex,
prev_session_index: SessionIndex,
transaction_hash: H256,
receiver: T::AccountId,
amount: BalanceOf<T>,
@ -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<T: Config> Pallet<T> {
) -> 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::<T>::get(&prev_session_index);
let curr_authorities = Authorities::<T>::get(&curr_session_index);
let prev_received_claps = ReceivedClaps::<T>::get(&prev_received_claps_key).into_inner();
let curr_received_claps = ReceivedClaps::<T>::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::<T>::get(&prev_received_claps_key)
.into_iter()
.filter_map(|auth_index| prev_authorities.get(auth_index as usize))
.cloned()
.collect::<BTreeSet<T::AuthorityId>>();
let curr_received_claps = ReceivedClaps::<T>::get(&curr_received_claps_key)
.into_iter()
.filter_map(|auth_index| curr_authorities.get(auth_index as usize))
.cloned()
.collect::<BTreeSet<T::AuthorityId>>();
let disabled_authorites = ClapsInSession::<T>::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<T: Config> Pallet<T> {
let enough_authorities = Perbill::from_rational(
summary_authority_claps_length as u32,
Authorities::<T>::get(prev_session_index).len() as u32,
active_authorities as u32,
) > Perbill::from_percent(T::ApplauseThreshold::get());
ensure!(enough_authorities, Error::<T>::NotEnoughClaps);