diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index e074cca..51157de 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.7" +version = "0.4.8" 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 a32c0c5..2e40665 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -347,6 +347,7 @@ pub mod pallet { TimeWentBackwards, DisabledAuthority, ExecutedBlockIsHigher, + CommitInWrongSession, } #[pallet::storage] @@ -713,14 +714,21 @@ impl Pallet { fn try_commit_block(new_commitment: &BlockCommitment>) -> DispatchResult { let authority_index = new_commitment.authority_index; let network_id = new_commitment.network_id; + let session_index = T::ValidatorSet::session_index(); + ensure!( T::NetworkDataHandler::get(&network_id).is_some(), Error::::UnregistedNetwork ); - let current_commits = BlockCommitments::::try_mutate( + ensure!( + new_commitment.session_index == session_index, + Error::::CommitInWrongSession, + ); + + let block_commitments = BlockCommitments::::try_mutate( &network_id, - |current_commitments| -> Result { + |current_commitments| -> Result, DispatchError> { let mut new_commitment_details = new_commitment.commitment; let (current_commits, current_last_updated) = current_commitments @@ -741,19 +749,21 @@ impl Pallet { new_commitment_details.commits = current_commits; 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::::BlockCommited { network_id, authority_id: authority_index, }); - let session_index = T::ValidatorSet::session_index(); let validators = Validators::::get(&session_index); - let block_commitments = BlockCommitments::::get(&network_id); - let disabled_bitmap = DisabledAuthorityIndexes::::get(&session_index) .unwrap_or(BitMap::new(validators.len() as u32)); @@ -765,7 +775,7 @@ impl Pallet { }) .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( &disabled_bitmap, &block_commitments, @@ -1074,7 +1084,7 @@ impl Pallet { 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); let length = values.len(); @@ -1122,8 +1132,8 @@ impl Pallet { time_updates.push((authority_index, data.last_updated)); } - let stored_block_median = Self::calculate_weighted_median(&mut stored_blocks); - let time_update_median = Self::calculate_weighted_median(&mut time_updates); + let stored_block_median = Self::calculate_median_value(&mut stored_blocks); + let time_update_median = Self::calculate_median_value(&mut time_updates); Self::apply_median_deviation( &mut delayed, @@ -1373,10 +1383,7 @@ impl Offence for SlowClapOffence { } OffenceType::CommitmentOffence => offenders_count .checked_sub(self.validator_set_count / 10 + 1) - .map(|threshold| { - Perbill::from_rational(3 * threshold, self.validator_set_count) - .saturating_mul(Perbill::from_percent(7)) - }) + .map(|threshold| Perbill::from_rational(3 * threshold, self.validator_set_count)) .unwrap_or_default(), } } diff --git a/pallets/slow-clap/src/tests.rs b/pallets/slow-clap/src/tests.rs index fea336f..0dc1d1a 100644 --- a/pallets/slow-clap/src/tests.rs +++ b/pallets/slow-clap/src/tests.rs @@ -31,7 +31,7 @@ fn should_calculate_throttling_slash_function_correctly() { let dummy_offence = SlowClapOffence { session_index: 0, - validator_set_count: 50, + validator_set_count: 100, offenders: vec![()], 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(5), Perbill::zero()); assert_eq!( - dummy_offence.slash_fraction(7), - Perbill::from_parts(4_200_000) + dummy_offence.slash_fraction(15), + Perbill::from_parts(120_000_000) ); assert_eq!( - dummy_offence.slash_fraction(17), - Perbill::from_parts(46_200_000) + dummy_offence.slash_fraction(20), + Perbill::from_parts(270_000_000) + ); + assert_eq!( + dummy_offence.slash_fraction(30), + Perbill::from_parts(570_000_000) ); }