change slashing logic during block commitments

Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
Uncle Stinky 2025-12-09 14:56:24 +03:00
parent 270f3a6191
commit 3b80a7a94a
Signed by: st1nky
GPG Key ID: 016064BD97603B40
3 changed files with 31 additions and 20 deletions

View File

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

View File

@ -347,6 +347,7 @@ pub mod pallet {
TimeWentBackwards, TimeWentBackwards,
DisabledAuthority, DisabledAuthority,
ExecutedBlockIsHigher, ExecutedBlockIsHigher,
CommitInWrongSession,
} }
#[pallet::storage] #[pallet::storage]
@ -713,14 +714,21 @@ impl<T: Config> Pallet<T> {
fn try_commit_block(new_commitment: &BlockCommitment<NetworkIdOf<T>>) -> DispatchResult { fn try_commit_block(new_commitment: &BlockCommitment<NetworkIdOf<T>>) -> DispatchResult {
let authority_index = new_commitment.authority_index; let authority_index = new_commitment.authority_index;
let network_id = new_commitment.network_id; let network_id = new_commitment.network_id;
let session_index = T::ValidatorSet::session_index();
ensure!( ensure!(
T::NetworkDataHandler::get(&network_id).is_some(), T::NetworkDataHandler::get(&network_id).is_some(),
Error::<T>::UnregistedNetwork Error::<T>::UnregistedNetwork
); );
let current_commits = BlockCommitments::<T>::try_mutate( ensure!(
new_commitment.session_index == session_index,
Error::<T>::CommitInWrongSession,
);
let block_commitments = BlockCommitments::<T>::try_mutate(
&network_id, &network_id,
|current_commitments| -> Result<u64, DispatchError> { |current_commitments| -> Result<BTreeMap<AuthIndex, CommitmentDetails>, DispatchError> {
let mut new_commitment_details = new_commitment.commitment; let mut new_commitment_details = new_commitment.commitment;
let (current_commits, current_last_updated) = current_commitments let (current_commits, current_last_updated) = current_commitments
@ -741,19 +749,21 @@ impl<T: Config> Pallet<T> {
new_commitment_details.commits = current_commits; new_commitment_details.commits = current_commits;
current_commitments.insert(authority_index, new_commitment_details); current_commitments.insert(authority_index, new_commitment_details);
Ok(current_commits) Ok(current_commitments.clone())
}, },
)?; )?;
let current_commits = block_commitments
.get(&authority_index)
.map(|details| details.commits)
.unwrap_or(1);
Self::deposit_event(Event::<T>::BlockCommited { Self::deposit_event(Event::<T>::BlockCommited {
network_id, network_id,
authority_id: authority_index, authority_id: authority_index,
}); });
let session_index = T::ValidatorSet::session_index();
let validators = Validators::<T>::get(&session_index); let validators = Validators::<T>::get(&session_index);
let block_commitments = BlockCommitments::<T>::get(&network_id);
let disabled_bitmap = DisabledAuthorityIndexes::<T>::get(&session_index) let disabled_bitmap = DisabledAuthorityIndexes::<T>::get(&session_index)
.unwrap_or(BitMap::new(validators.len() as u32)); .unwrap_or(BitMap::new(validators.len() as u32));
@ -765,7 +775,7 @@ impl<T: Config> Pallet<T> {
}) })
.unwrap_or_default(); .unwrap_or_default();
if current_commits > 2 && validators.len() > 0 { if current_commits % 3 == 0 && validators.len() > 0 {
let offence_bitmap = Self::capture_deviation_in_commitments_and_remove( let offence_bitmap = Self::capture_deviation_in_commitments_and_remove(
&disabled_bitmap, &disabled_bitmap,
&block_commitments, &block_commitments,
@ -1074,7 +1084,7 @@ impl<T: Config> Pallet<T> {
debug_assert!(cursor.maybe_cursor.is_none()); debug_assert!(cursor.maybe_cursor.is_none());
} }
fn calculate_weighted_median(values: &mut Vec<(AuthIndex, u64)>) -> u64 { fn calculate_median_value(values: &mut Vec<(AuthIndex, u64)>) -> u64 {
values.sort_by_key(|data| data.1); values.sort_by_key(|data| data.1);
let length = values.len(); let length = values.len();
@ -1122,8 +1132,8 @@ impl<T: Config> Pallet<T> {
time_updates.push((authority_index, data.last_updated)); time_updates.push((authority_index, data.last_updated));
} }
let stored_block_median = Self::calculate_weighted_median(&mut stored_blocks); let stored_block_median = Self::calculate_median_value(&mut stored_blocks);
let time_update_median = Self::calculate_weighted_median(&mut time_updates); let time_update_median = Self::calculate_median_value(&mut time_updates);
Self::apply_median_deviation( Self::apply_median_deviation(
&mut delayed, &mut delayed,
@ -1373,10 +1383,7 @@ impl<Offender: Clone> Offence<Offender> for SlowClapOffence<Offender> {
} }
OffenceType::CommitmentOffence => offenders_count OffenceType::CommitmentOffence => offenders_count
.checked_sub(self.validator_set_count / 10 + 1) .checked_sub(self.validator_set_count / 10 + 1)
.map(|threshold| { .map(|threshold| Perbill::from_rational(3 * threshold, self.validator_set_count))
Perbill::from_rational(3 * threshold, self.validator_set_count)
.saturating_mul(Perbill::from_percent(7))
})
.unwrap_or_default(), .unwrap_or_default(),
} }
} }

View File

@ -31,7 +31,7 @@ fn should_calculate_throttling_slash_function_correctly() {
let dummy_offence = SlowClapOffence { let dummy_offence = SlowClapOffence {
session_index: 0, session_index: 0,
validator_set_count: 50, validator_set_count: 100,
offenders: vec![()], offenders: vec![()],
offence_type: OffenceType::CommitmentOffence, offence_type: OffenceType::CommitmentOffence,
}; };
@ -39,12 +39,16 @@ fn should_calculate_throttling_slash_function_correctly() {
assert_eq!(dummy_offence.slash_fraction(1), Perbill::zero()); assert_eq!(dummy_offence.slash_fraction(1), Perbill::zero());
assert_eq!(dummy_offence.slash_fraction(5), Perbill::zero()); assert_eq!(dummy_offence.slash_fraction(5), Perbill::zero());
assert_eq!( assert_eq!(
dummy_offence.slash_fraction(7), dummy_offence.slash_fraction(15),
Perbill::from_parts(4_200_000) Perbill::from_parts(120_000_000)
); );
assert_eq!( assert_eq!(
dummy_offence.slash_fraction(17), dummy_offence.slash_fraction(20),
Perbill::from_parts(46_200_000) Perbill::from_parts(270_000_000)
);
assert_eq!(
dummy_offence.slash_fraction(30),
Perbill::from_parts(570_000_000)
); );
} }