From 147542f10cd0408760261580e02de6a4c615f02d Mon Sep 17 00:00:00 2001 From: Uncle Stinky Date: Fri, 28 Nov 2025 00:02:26 +0300 Subject: [PATCH] correct median; on genesis validators list is empty Signed-off-by: Uncle Stinky --- pallets/slow-clap/Cargo.toml | 2 +- pallets/slow-clap/src/lib.rs | 23 +++++++++--------- pallets/slow-clap/src/tests.rs | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index fe41a18..b0b5118 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.4" +version = "0.4.5" 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 18a91d2..656a80d 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -597,8 +597,8 @@ impl Pallet { ); let (session_index, clap_unique_hash) = Self::mended_session_index(&clap); - let validators = Validators::::get(&session_index); - let validators_length = validators.len(); + let authorities = Authorities::::get(&session_index); + let authorities_length = authorities.len(); let is_disabled = DisabledAuthorityIndexes::::get(&session_index) .map(|bitmap| bitmap.exists(&clap.authority_index)) @@ -627,7 +627,7 @@ impl Pallet { LatestExecutedBlock::::get(&network_id) <= clap.block_number, Error::::ExecutedBlockIsHigher, ); - ApplauseDetail::new(network_id, clap.block_number, validators_length) + ApplauseDetail::new(network_id, clap.block_number, authorities_length) } }; @@ -665,7 +665,7 @@ impl Pallet { let is_enough = applause_details .authorities .count_ones() - .gt(&(validators_length as u32 / 2)); + .gt(&(authorities_length as u32 / 2)); if total_clapped > threshold_amount && is_enough && !applause_details.finalized { applause_details.finalized = true; @@ -760,7 +760,7 @@ impl Pallet { }) .unwrap_or_default(); - if current_commits > 1 { + if current_commits > 2 && validators.len() > 0 { let offence_bitmap = Self::capture_deviation_in_commitments_and_remove( &disabled_bitmap, &block_commitments, @@ -899,9 +899,8 @@ impl Pallet { log::info!( target: LOG_TARGET, - "👻 Stored from_block is #{:?} from authority #{:?} for network {:?}", + "👻 Stored from_block is #{:?} for network {:?}", new_block_range.0, - authority_index, network_id, ); @@ -1080,11 +1079,13 @@ impl Pallet { fn calculate_weighted_median(values: &mut Vec<(AuthIndex, u64)>) -> u64 { values.sort_by_key(|data| data.1); - let mid = values.len() / 2; - if values.len() % 2 == 1 { - values[mid].1 + let length = values.len(); + if length % 2 == 0 { + let mid_left = values[length / 2 - 1].1; + let mid_right = values[length / 2].1; + (mid_left + mid_right) / 2 } else { - values[mid - 1].1 + values[length / 2].1 } } diff --git a/pallets/slow-clap/src/tests.rs b/pallets/slow-clap/src/tests.rs index 99bc6aa..d0111aa 100644 --- a/pallets/slow-clap/src/tests.rs +++ b/pallets/slow-clap/src/tests.rs @@ -1182,6 +1182,50 @@ fn should_not_slash_by_applause_if_disabled_by_commitment() { }); } +#[test] +fn should_not_nullify_on_incorrect_block_commitment() { + let (network_id, _, _) = generate_unique_hash(None, None, None, None, None); + + new_test_ext().execute_with(|| { + let session_index = advance_session_and_get_index(); + prepare_evm_network(None, None); + + let timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Time went backwards") + .as_millis() as u64; + + let mut block_commitment = CommitmentDetails { + last_stored_block: 9_500_000, + commits: 420, + last_updated: timestamp, + }; + + for i in 0..4 { + assert_ok!(do_block_commitment( + session_index, + network_id, + i, + &block_commitment + )); + } + + block_commitment.last_updated = 0; + assert_err!(do_block_commitment( + session_index, + network_id, + 3, + &block_commitment), + Error::::TimeWentBackwards); + + for commitment_details in BlockCommitments::::get(network_id).values() { + assert_eq!(commitment_details.last_stored_block, 9_500_000); + assert_eq!(commitment_details.last_updated, timestamp); + assert_eq!(commitment_details.commits, 1); + } + }); +} + fn assert_clapped_amount( session_index: &SessionIndex, unique_hash: &H256,