apply changes from ghost-traits for ExposureListener

Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
Uncle Stinky 2025-11-27 19:04:24 +03:00
parent fbc58e350b
commit 911db3b974
Signed by: st1nky
GPG Key ID: 016064BD97603B40
4 changed files with 26 additions and 20 deletions

View File

@ -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

View File

@ -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<IdentificationTuple<Self>>,
>;
type DisabledValidators: DisabledValidators;
type ExposureListener: ExposureListener<BalanceOf<Self>, EraIndex, AuthIndex>;
type ExposureListener: ExposureListener<BalanceOf<Self>, Self::AccountId>;
#[pallet::constant]
type MaxAuthorities: Get<u32>;
@ -597,7 +597,8 @@ impl<T: Config> Pallet<T> {
);
let (session_index, clap_unique_hash) = Self::mended_session_index(&clap);
let authorities_length = Authorities::<T>::get(&session_index).len();
let validators = Validators::<T>::get(&session_index);
let validators_length = validators.len();
let is_disabled = DisabledAuthorityIndexes::<T>::get(&session_index)
.map(|bitmap| bitmap.exists(&clap.authority_index))
@ -607,7 +608,12 @@ impl<T: Config> Pallet<T> {
let applause_threshold = Perbill::from_parts(T::ApplauseThreshold::get());
let threshold_amount = applause_threshold.mul_floor(TotalExposure::<T>::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::<T>::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::<T>::take(&session_index, &clap_unique_hash) {
@ -617,7 +623,7 @@ impl<T: Config> Pallet<T> {
LatestExecutedBlock::<T>::get(&network_id) <= clap.block_number,
Error::<T>::ExecutedBlockIsHigher,
);
ApplauseDetail::new(network_id, clap.block_number, authorities_length)
ApplauseDetail::new(network_id, clap.block_number, validators_length)
}
};
@ -655,7 +661,7 @@ impl<T: Config> Pallet<T> {
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<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
BlockCommitments::<T>::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::<T>::set(total_exposure);
let authorities = validators.map(|x| x.1).collect::<Vec<_>>();

View File

@ -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<Runtime>;
@ -170,20 +170,20 @@ impl pallet_balances::Config for Runtime {
type Balance = u64;
pub struct TestExposureListener;
impl ExposureListener<Balance, EraIndex, AuthIndex> for TestExposureListener
impl ExposureListener<Balance, u64> for TestExposureListener
where
Balance: AtLeast32BitUnsigned + From<u64>,
{
fn get_current_era() -> EraIndex {
1
fn get_account_by_index(index: usize) -> Option<u64> {
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,

View File

@ -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);