Compare commits

...

4 Commits

Author SHA1 Message Date
eb181c7f44
fixes for the inconsistency of the slow claps
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-11-04 17:43:36 +03:00
5307afe352
bump locked version
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-08-27 15:33:26 +03:00
7edc8935b6
optimize offenders gathering with a single pass over the data
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-08-27 15:31:32 +03:00
46aa18aafe
implement cross session claim via self_applause
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-08-27 14:41:46 +03:00
4 changed files with 134 additions and 57 deletions

2
Cargo.lock generated
View File

@ -3836,7 +3836,7 @@ dependencies = [
[[package]]
name = "ghost-slow-clap"
version = "0.3.39"
version = "0.3.42"
dependencies = [
"frame-benchmarking",
"frame-support",

View File

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

View File

@ -72,7 +72,7 @@ const LOG_TARGET: &str = "runtime::ghost-slow-clap";
const DB_PREFIX: &[u8] = b"slow_clap::";
const FETCH_TIMEOUT_PERIOD: u64 = 3_000;
const LOCK_BLOCK_EXPIRATION: u64 = 10;
const LOCK_BLOCK_EXPIRATION: u64 = 20;
pub type AuthIndex = u32;
@ -378,7 +378,8 @@ pub mod pallet {
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
if let Call::slow_clap { clap, signature } = call {
let authorities = Authorities::<T>::get(&clap.session_index);
let (session_index, _) = Self::mended_session_index(&clap);
let authorities = Authorities::<T>::get(&session_index);
let authority = match authorities.get(clap.authority_index as usize) {
Some(authority) => authority,
None => return InvalidTransaction::BadSigner.into(),
@ -459,27 +460,49 @@ impl<T: Config> Pallet<T> {
hex_str
}
fn try_slow_clap(clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>) -> DispatchResult {
let authorities = Authorities::<T>::get(&clap.session_index);
ensure!(
authorities.get(clap.authority_index as usize).is_some(),
Error::<T>::NotAnAuthority
fn mended_session_index(
clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>,
) -> (SessionIndex, H256) {
let prev_session_index = clap.session_index.saturating_sub(1);
let clap_unique_hash =
Self::generate_unique_hash(&clap.receiver, &clap.amount, &clap.network_id);
let received_claps_key = (
prev_session_index,
&clap.transaction_hash,
&clap_unique_hash,
);
let session_index = ReceivedClaps::<T>::get(&received_claps_key)
.is_empty()
.then(|| clap.session_index)
.unwrap_or(prev_session_index);
(session_index, clap_unique_hash)
}
fn try_slow_clap(clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>) -> DispatchResult {
let (session_index, clap_unique_hash) = Self::mended_session_index(&clap);
let mut claps_in_session = ClapsInSession::<T>::get(&session_index);
ensure!(
ClapsInSession::<T>::get(&clap.session_index)
claps_in_session
.get(&clap.authority_index)
.map(|info| !info.disabled)
.unwrap_or(true),
Error::<T>::CurrentValidatorIsDisabled
);
let clap_unique_hash =
Self::generate_unique_hash(&clap.receiver, &clap.amount, &clap.network_id);
let received_claps_key = (
clap.session_index,
&clap.transaction_hash,
&clap_unique_hash,
);
let disabled_authorites = claps_in_session
.values()
.filter(|info| info.disabled)
.count();
let active_authorities = Authorities::<T>::get(&session_index)
.len()
.saturating_sub(disabled_authorites);
let received_claps_key = (session_index, &clap.transaction_hash, &clap_unique_hash);
let number_of_received_claps =
ReceivedClaps::<T>::try_mutate(&received_claps_key, |tree_of_claps| {
@ -498,15 +521,15 @@ impl<T: Config> Pallet<T> {
}
})?;
ClapsInSession::<T>::mutate(&clap.session_index, |claps_details| {
(*claps_details)
claps_in_session
.entry(clap.authority_index)
.and_modify(|individual| (*individual).claps.saturating_inc())
.and_modify(|individual| individual.claps.saturating_inc())
.or_insert(SessionAuthorityInfo {
claps: 1u32,
disabled: false,
});
});
ClapsInSession::<T>::insert(&session_index, claps_in_session);
Self::deposit_event(Event::<T>::Clapped {
authority_id: clap.authority_index,
@ -517,7 +540,7 @@ impl<T: Config> Pallet<T> {
});
let enough_authorities =
Perbill::from_rational(number_of_received_claps as u32, authorities.len() as u32)
Perbill::from_rational(number_of_received_claps as u32, active_authorities as u32)
> Perbill::from_percent(T::ApplauseThreshold::get());
if enough_authorities {
@ -574,32 +597,55 @@ impl<T: Config> Pallet<T> {
fn applause_if_posible(
network_id: NetworkIdOf<T>,
session_index: SessionIndex,
prev_session_index: SessionIndex,
transaction_hash: H256,
receiver: T::AccountId,
amount: BalanceOf<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 received_claps_key = (session_index, &transaction_hash, &clap_unique_hash);
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 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());
let clap = Clap {
authority_index: Default::default(),
block_number: Default::default(),
removed: false,
session_index,
removed: Default::default(),
session_index: Default::default(),
transaction_hash: Default::default(),
network_id,
receiver,
amount,
transaction_hash,
};
let enough_authorities = Perbill::from_rational(
ReceivedClaps::<T>::get(&received_claps_key).len() as u32,
Authorities::<T>::get(session_index).len() as u32,
summary_authority_claps_length as u32,
Authorities::<T>::get(prev_session_index).len() as u32,
) > Perbill::from_percent(T::ApplauseThreshold::get());
ensure!(enough_authorities, Error::<T>::NotEnoughClaps);
Self::try_applause(&clap, &received_claps_key)?;
Self::try_applause(&clap, &prev_received_claps_key)?;
Ok(())
}
@ -766,14 +812,17 @@ impl<T: Config> Pallet<T> {
Some(_) if from_block.le(&to_block) => {
let adjusted_to_block = estimated_block
.checked_sub(from_block)
.map(|current_distance| current_distance
.map(|current_distance| {
current_distance
.le(&max_block_distance)
.then(|| estimated_block)
)
})
.flatten()
.unwrap_or(from_block
.unwrap_or(
from_block
.saturating_add(max_block_distance)
.min(estimated_block));
.min(estimated_block),
);
(from_block, adjusted_to_block)
}
_ => (to_block, to_block),
@ -1087,8 +1136,8 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
}
fn on_before_session_ending() {
let session_index = T::ValidatorSet::session_index();
let validators = T::ValidatorSet::validators();
let session_index = T::ValidatorSet::session_index().saturating_sub(1);
let authorities_len = Authorities::<T>::get(&session_index).len();
let claps_in_session = ClapsInSession::<T>::get(&session_index);
@ -1097,12 +1146,14 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
let offenders = validators
.into_iter()
.enumerate()
.filter(|(index, _)| !Self::is_good_actor(*index, median_claps, &claps_in_session))
.filter_map(|(_, id)| {
.filter_map(|(index, id)| {
(!Self::is_good_actor(index, median_claps, &claps_in_session)).then(|| {
<T::ValidatorSet as ValidatorSetWithIdentification<T::AccountId>>::IdentificationOf::convert(
id.clone(),
).map(|full_id| (id, full_id))
})
.flatten()
})
.collect::<Vec<IdentificationTuple<T>>>();
if offenders.is_empty() {

View File

@ -991,7 +991,6 @@ fn should_self_applause_if_enough_received_claps() {
assert_ok!(do_clap_from(session_index, network_id, 0, false));
assert_ok!(do_clap_from(session_index, network_id, 1, false));
assert_ok!(do_clap_from(session_index, network_id, 2, false));
assert_eq!(
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
@ -999,14 +998,17 @@ fn should_self_applause_if_enough_received_claps() {
);
assert_eq!(Balances::balance(&receiver), 0);
assert_ok!(SlowClap::self_applause(
assert_err!(
SlowClap::self_applause(
RuntimeOrigin::signed(receiver),
network_id,
session_index,
transaction_hash,
receiver,
amount,
));
),
Error::<Runtime>::NotEnoughClaps
);
assert_eq!(
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
false
@ -1015,6 +1017,26 @@ fn should_self_applause_if_enough_received_claps() {
Networks::on_finalize(System::block_number());
advance_session();
let next_session_index = Session::session_index();
let next_storage_key = (
next_session_index,
transaction_hash,
unique_transaction_hash,
);
assert_ok!(do_clap_from(next_session_index, network_id, 2, false));
assert_err!(
SlowClap::self_applause(
RuntimeOrigin::signed(receiver),
network_id,
next_session_index,
transaction_hash,
receiver,
amount,
),
Error::<Runtime>::NotEnoughClaps
);
assert_ok!(SlowClap::self_applause(
RuntimeOrigin::signed(receiver),
network_id,
@ -1027,6 +1049,10 @@ fn should_self_applause_if_enough_received_claps() {
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
true
);
assert_eq!(
pallet::ApplausesForTransaction::<Runtime>::get(&next_storage_key),
false
);
assert_eq!(Balances::balance(&receiver), amount);
});
}