diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index 969cd34..6e581c4 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.19" +version = "0.4.20" 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 119c9e6..4a24e77 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -80,6 +80,7 @@ const LOCK_BLOCK_EXPIRATION: u64 = 20; const ONE_HOUR_MILLIS: u64 = 3_600_000; +const BLOCK_CHECK_CYCLES: u64 = 8; const CHECK_BLOCK_INTERVAL: u64 = 300; const BLOCK_COMMITMENT_DELAY: u64 = 100; @@ -317,6 +318,9 @@ pub mod pallet { #[pallet::constant] type MinAuthoritiesNumber: Get; + #[pallet::constant] + type EpochDuration: Get; + type WeightInfo: WeightInfo; } @@ -479,15 +483,15 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { fn on_initialize(current_block: BlockNumberFor) -> Weight { - // TODO: what about start of the session??? let mut weight = T::DbWeight::get().reads(1); - let networks_count = T::NetworkDataHandler::count(); let current_block_number: u64 = current_block.unique_saturated_into(); - //let cycle_start = (current_block_number / CHECK_BLOCK_INTERVAL) * CHECK_BLOCK_INTERVAL; - let cycle_offset = current_block_number % CHECK_BLOCK_INTERVAL; - if cycle_offset >= networks_count.into() { + let check_block_interval = T::EpochDuration::get().saturating_div(BLOCK_CHECK_CYCLES); + let cycle_offset = current_block_number % check_block_interval; + let block_in_epoch = current_block_number % T::EpochDuration::get(); + + if cycle_offset >= networks_count.into() || block_in_epoch < check_block_interval { return Default::default(); } diff --git a/pallets/slow-clap/src/mock.rs b/pallets/slow-clap/src/mock.rs index f1e379c..17fe112 100644 --- a/pallets/slow-clap/src/mock.rs +++ b/pallets/slow-clap/src/mock.rs @@ -159,6 +159,7 @@ parameter_types! { pub static ExistentialDeposit: u64 = 2; pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; pub const HistoryDepth: u32 = 10; + pub const EpochDuration: u64 = 80; } #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] @@ -213,6 +214,7 @@ impl Config for Runtime { type UnsignedPriority = ConstU64<{ 1 << 20 }>; type HistoryDepth = HistoryDepth; type MinAuthoritiesNumber = ConstU32<1>; + type EpochDuration = EpochDuration; type WeightInfo = (); } diff --git a/pallets/slow-clap/src/tests.rs b/pallets/slow-clap/src/tests.rs index cd0870f..ffa3869 100644 --- a/pallets/slow-clap/src/tests.rs +++ b/pallets/slow-clap/src/tests.rs @@ -1442,8 +1442,15 @@ fn should_check_different_networks_during_on_initialize() { prepare_evm_network(Some(network_id), None); } - let check_interval = times * CHECK_BLOCK_INTERVAL; - for check_block in 0..check_interval { + let expected_events_count: u64 = BLOCK_CHECK_CYCLES + .saturating_sub(1) + .saturating_mul(networks_count as u64) + .saturating_mul(times); + + let expected_events_count: usize = expected_events_count.try_into().unwrap(); + + let epochs_passed = times * EpochDuration::get(); + for check_block in 0..epochs_passed { SlowClap::on_initialize(check_block); } @@ -1468,12 +1475,13 @@ fn should_check_different_networks_during_on_initialize() { } }); - let expected_events = networks_count * (times as u32); - assert_eq!(total_number_of_events, expected_events as usize); + assert_eq!(total_number_of_events, expected_events_count); assert_eq!(check_commitment_events.len() as u32, networks_count); for network_id in 0..networks_count { let network_id_count = check_commitment_events.get(&network_id).unwrap(); - assert_eq!(*network_id_count, times); + let expected_events_count = + expected_events_count.saturating_div(networks_count as usize); + assert_eq!(*network_id_count, expected_events_count); } }); }