From 0323c25d08a9e71e7c6d47d54e16484250f4e749 Mon Sep 17 00:00:00 2001 From: Uncle Stretch Date: Thu, 4 Dec 2025 17:51:29 +0300 Subject: [PATCH] avoid excessive block commitments Signed-off-by: Uncle Stretch --- Cargo.lock | 2 +- pallets/slow-clap/Cargo.toml | 2 +- pallets/slow-clap/src/lib.rs | 7 ++- pallets/slow-clap/src/tests.rs | 103 +++++++++++++++++++++++++++------ 4 files changed, 92 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fff695c..9f509b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3837,7 +3837,7 @@ dependencies = [ [[package]] name = "ghost-slow-clap" -version = "0.4.5" +version = "0.4.6" dependencies = [ "frame-benchmarking", "frame-support", diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index b0b5118..b8daee7 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.5" +version = "0.4.6" 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 656a80d..7366fc0 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -725,7 +725,12 @@ impl Pallet { let (current_commits, current_last_updated) = current_commitments .get(&authority_index) - .map(|details| (details.commits + 1, details.last_updated)) + .map(|details| { + ( + details.commits.saturating_add(1), + details.last_updated.saturating_add(COMMITMENT_DELAY_MILLIS), + ) + }) .unwrap_or((1, 0)); ensure!( diff --git a/pallets/slow-clap/src/tests.rs b/pallets/slow-clap/src/tests.rs index d0111aa..fea336f 100644 --- a/pallets/slow-clap/src/tests.rs +++ b/pallets/slow-clap/src/tests.rs @@ -1008,8 +1008,8 @@ fn should_disable_on_commitment_inactivity() { }; // two block commitments - for extra_time in 0..=2 { - block_commitment.last_updated += extra_time; + for extra_time in 1..=3 { + block_commitment.last_updated += COMMITMENT_DELAY_MILLIS + extra_time; for i in 0..3 { assert_ok!(do_block_commitment( session_index, @@ -1066,9 +1066,9 @@ fn should_disable_on_commitment_block_deviation() { }; // two block commitments - for extra_time in 0..=2 { - block_commitment.last_updated += extra_time; - bad_block_commitment.last_updated += extra_time; + for extra_time in 1..=3 { + block_commitment.last_updated += COMMITMENT_DELAY_MILLIS + extra_time; + bad_block_commitment.last_updated += COMMITMENT_DELAY_MILLIS + extra_time; for i in 0..3 { assert_ok!(do_block_commitment( session_index, @@ -1093,6 +1093,73 @@ fn should_disable_on_commitment_block_deviation() { }); } +#[test] +fn should_throw_error_on_fast_commitments() { + 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, + }; + + assert_ok!(do_block_commitment( + session_index, + network_id, + 0, + &block_commitment + )); + assert_err!( + do_block_commitment(session_index, network_id, 0, &block_commitment), + Error::::TimeWentBackwards, + ); + + block_commitment.last_updated += COMMITMENT_DELAY_MILLIS / 2; + assert_err!( + do_block_commitment(session_index, network_id, 0, &block_commitment), + Error::::TimeWentBackwards, + ); + + block_commitment.last_updated += COMMITMENT_DELAY_MILLIS / 2; + assert_err!( + do_block_commitment(session_index, network_id, 0, &block_commitment), + Error::::TimeWentBackwards, + ); + + block_commitment.last_updated += 1; + assert_ok!(do_block_commitment( + session_index, + network_id, + 0, + &block_commitment + )); + + block_commitment.last_updated = timestamp; + assert_err!( + do_block_commitment(session_index, network_id, 0, &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 + COMMITMENT_DELAY_MILLIS + 1 + ); + assert_eq!(commitment_details.commits, 2); + } + }); +} + #[test] fn should_not_offend_disabled_authorities() { let (network_id, _, _) = generate_unique_hash(None, None, None, None, None); @@ -1119,8 +1186,8 @@ fn should_not_offend_disabled_authorities() { Session::disable_index(3); // two block commitments - for extra_time in 0..=2 { - block_commitment.last_updated += extra_time; + for extra_time in 1..=3 { + block_commitment.last_updated += COMMITMENT_DELAY_MILLIS + extra_time; for i in 0..3 { assert_ok!(do_block_commitment( session_index, @@ -1164,8 +1231,8 @@ fn should_not_slash_by_applause_if_disabled_by_commitment() { Session::disable_index(3); // two block commitments - for extra_time in 0..=2 { - block_commitment.last_updated += extra_time; + for extra_time in 1..=3 { + block_commitment.last_updated += COMMITMENT_DELAY_MILLIS + extra_time; for i in 0..3 { assert_ok!(do_block_commitment( session_index, @@ -1203,20 +1270,18 @@ fn should_not_nullify_on_incorrect_block_commitment() { for i in 0..4 { assert_ok!(do_block_commitment( - session_index, - network_id, - i, - &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); + 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);