diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index 3cc1748..722ea73 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.1" +version = "0.4.2" 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 d550004..e26a924 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -35,7 +35,7 @@ use sp_runtime::{ }; use sp_staking::{ offence::{Kind, Offence, ReportOffence}, - EraIndex, SessionIndex, + SessionIndex, }; use sp_std::{collections::btree_map::BTreeMap, prelude::*, vec::Vec}; @@ -281,7 +281,7 @@ pub mod pallet { SlowClapOffence>, >; type DisabledValidators: DisabledValidators; - type ExposureListener: ExposureListener, EraIndex, AuthIndex>; + type ExposureListener: ExposureListener, Self::AccountId>; #[pallet::constant] type MaxAuthorities: Get; @@ -597,7 +597,8 @@ impl Pallet { ); let (session_index, clap_unique_hash) = Self::mended_session_index(&clap); - let authorities_length = Authorities::::get(&session_index).len(); + let validators = Validators::::get(&session_index); + let validators_length = validators.len(); let is_disabled = DisabledAuthorityIndexes::::get(&session_index) .map(|bitmap| bitmap.exists(&clap.authority_index)) @@ -607,7 +608,12 @@ impl Pallet { let applause_threshold = Perbill::from_parts(T::ApplauseThreshold::get()); let threshold_amount = applause_threshold.mul_floor(TotalExposure::::get()); - let new_clapped_amount = T::ExposureListener::get_validator_exposure(clap.authority_index); + + let maybe_account_id = T::ExposureListener::get_account_by_index(clap.authority_index as usize); + ensure!(maybe_account_id.is_some(), Error::::NonExistentAuthorityIndex); + + let account_id = maybe_account_id.unwrap(); + let new_clapped_amount = T::ExposureListener::get_validator_exposure(&account_id); let mut applause_details = match ApplauseDetails::::take(&session_index, &clap_unique_hash) { @@ -617,7 +623,7 @@ impl Pallet { LatestExecutedBlock::::get(&network_id) <= clap.block_number, Error::::ExecutedBlockIsHigher, ); - ApplauseDetail::new(network_id, clap.block_number, authorities_length) + ApplauseDetail::new(network_id, clap.block_number, validators_length) } }; @@ -655,7 +661,7 @@ impl Pallet { let is_enough = applause_details .authorities .count_ones() - .gt(&(authorities_length as u32 / 2)); + .gt(&(validators_length as u32 / 2)); if total_clapped > threshold_amount && is_enough && !applause_details.finalized { applause_details.finalized = true; @@ -1265,9 +1271,7 @@ impl OneSessionHandler for Pallet { BlockCommitments::::remove(network_id); } - let current_era = T::ExposureListener::get_current_era(); - let total_exposure = T::ExposureListener::get_total_exposure(current_era); - + let total_exposure = T::ExposureListener::get_total_exposure(); TotalExposure::::set(total_exposure); let authorities = validators.map(|x| x.1).collect::>(); diff --git a/pallets/slow-clap/src/mock.rs b/pallets/slow-clap/src/mock.rs index cc7df50..365acc5 100644 --- a/pallets/slow-clap/src/mock.rs +++ b/pallets/slow-clap/src/mock.rs @@ -19,8 +19,8 @@ use sp_staking::{ use sp_runtime::BuildStorage; -use crate::{self as slow_clap, AuthIndex}; -use crate::{Config, EraIndex, ExposureListener}; +use crate::{self as slow_clap}; +use crate::{Config, ExposureListener}; type Block = frame_system::mocking::MockBlock; @@ -170,20 +170,20 @@ impl pallet_balances::Config for Runtime { type Balance = u64; pub struct TestExposureListener; -impl ExposureListener for TestExposureListener +impl ExposureListener for TestExposureListener where Balance: AtLeast32BitUnsigned + From, { - fn get_current_era() -> EraIndex { - 1 + fn get_account_by_index(index: usize) -> Option { + FixedValidators::get().get(index).copied() } - fn get_total_exposure(_era: EraIndex) -> Balance { + fn get_total_exposure() -> Balance { 1_000_000_000u64.into() } - fn get_validator_exposure(index: AuthIndex) -> Balance { - match index { + fn get_validator_exposure(validator: &u64) -> Balance { + match validator { 0 => 250_000_000u64, 1 => 200_000_000u64, 2 => 250_000_000u64, diff --git a/pallets/slow-clap/src/tests.rs b/pallets/slow-clap/src/tests.rs index 5a56dac..137a33c 100644 --- a/pallets/slow-clap/src/tests.rs +++ b/pallets/slow-clap/src/tests.rs @@ -531,8 +531,10 @@ fn should_applause_and_take_next_claps() { assert_eq!(Balances::total_balance(&receiver), 0); for i in 0..=3 { assert_ok!(do_clap_from(session_index, network_id, i, false)); - clapped_amount = - TestExposureListener::get_validator_exposure(i).saturating_add(clapped_amount); + let account_id = TestExposureListener::get_account_by_index(i as usize) + .unwrap_or_default(); + clapped_amount = TestExposureListener::get_validator_exposure(&account_id) + .saturating_add(clapped_amount); assert_clapped_amount(&session_index, &unique_hash, clapped_amount); } assert_applaused(&session_index, &unique_hash);