apply changes from ghost-traits for ExposureListener
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
parent
fbc58e350b
commit
911db3b974
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ghost-slow-clap"
|
name = "ghost-slow-clap"
|
||||||
version = "0.4.1"
|
version = "0.4.2"
|
||||||
description = "Applause protocol for the EVM bridge"
|
description = "Applause protocol for the EVM bridge"
|
||||||
license.workspace = true
|
license.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
|
|||||||
@ -35,7 +35,7 @@ use sp_runtime::{
|
|||||||
};
|
};
|
||||||
use sp_staking::{
|
use sp_staking::{
|
||||||
offence::{Kind, Offence, ReportOffence},
|
offence::{Kind, Offence, ReportOffence},
|
||||||
EraIndex, SessionIndex,
|
SessionIndex,
|
||||||
};
|
};
|
||||||
use sp_std::{collections::btree_map::BTreeMap, prelude::*, vec::Vec};
|
use sp_std::{collections::btree_map::BTreeMap, prelude::*, vec::Vec};
|
||||||
|
|
||||||
@ -281,7 +281,7 @@ pub mod pallet {
|
|||||||
SlowClapOffence<IdentificationTuple<Self>>,
|
SlowClapOffence<IdentificationTuple<Self>>,
|
||||||
>;
|
>;
|
||||||
type DisabledValidators: DisabledValidators;
|
type DisabledValidators: DisabledValidators;
|
||||||
type ExposureListener: ExposureListener<BalanceOf<Self>, EraIndex, AuthIndex>;
|
type ExposureListener: ExposureListener<BalanceOf<Self>, Self::AccountId>;
|
||||||
|
|
||||||
#[pallet::constant]
|
#[pallet::constant]
|
||||||
type MaxAuthorities: Get<u32>;
|
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 (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)
|
let is_disabled = DisabledAuthorityIndexes::<T>::get(&session_index)
|
||||||
.map(|bitmap| bitmap.exists(&clap.authority_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 applause_threshold = Perbill::from_parts(T::ApplauseThreshold::get());
|
||||||
let threshold_amount = applause_threshold.mul_floor(TotalExposure::<T>::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 =
|
let mut applause_details =
|
||||||
match ApplauseDetails::<T>::take(&session_index, &clap_unique_hash) {
|
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,
|
LatestExecutedBlock::<T>::get(&network_id) <= clap.block_number,
|
||||||
Error::<T>::ExecutedBlockIsHigher,
|
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
|
let is_enough = applause_details
|
||||||
.authorities
|
.authorities
|
||||||
.count_ones()
|
.count_ones()
|
||||||
.gt(&(authorities_length as u32 / 2));
|
.gt(&(validators_length as u32 / 2));
|
||||||
|
|
||||||
if total_clapped > threshold_amount && is_enough && !applause_details.finalized {
|
if total_clapped > threshold_amount && is_enough && !applause_details.finalized {
|
||||||
applause_details.finalized = true;
|
applause_details.finalized = true;
|
||||||
@ -1265,9 +1271,7 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
|
|||||||
BlockCommitments::<T>::remove(network_id);
|
BlockCommitments::<T>::remove(network_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
let current_era = T::ExposureListener::get_current_era();
|
let total_exposure = T::ExposureListener::get_total_exposure();
|
||||||
let total_exposure = T::ExposureListener::get_total_exposure(current_era);
|
|
||||||
|
|
||||||
TotalExposure::<T>::set(total_exposure);
|
TotalExposure::<T>::set(total_exposure);
|
||||||
|
|
||||||
let authorities = validators.map(|x| x.1).collect::<Vec<_>>();
|
let authorities = validators.map(|x| x.1).collect::<Vec<_>>();
|
||||||
|
|||||||
@ -19,8 +19,8 @@ use sp_staking::{
|
|||||||
|
|
||||||
use sp_runtime::BuildStorage;
|
use sp_runtime::BuildStorage;
|
||||||
|
|
||||||
use crate::{self as slow_clap, AuthIndex};
|
use crate::{self as slow_clap};
|
||||||
use crate::{Config, EraIndex, ExposureListener};
|
use crate::{Config, ExposureListener};
|
||||||
|
|
||||||
type Block = frame_system::mocking::MockBlock<Runtime>;
|
type Block = frame_system::mocking::MockBlock<Runtime>;
|
||||||
|
|
||||||
@ -170,20 +170,20 @@ impl pallet_balances::Config for Runtime {
|
|||||||
type Balance = u64;
|
type Balance = u64;
|
||||||
|
|
||||||
pub struct TestExposureListener;
|
pub struct TestExposureListener;
|
||||||
impl ExposureListener<Balance, EraIndex, AuthIndex> for TestExposureListener
|
impl ExposureListener<Balance, u64> for TestExposureListener
|
||||||
where
|
where
|
||||||
Balance: AtLeast32BitUnsigned + From<u64>,
|
Balance: AtLeast32BitUnsigned + From<u64>,
|
||||||
{
|
{
|
||||||
fn get_current_era() -> EraIndex {
|
fn get_account_by_index(index: usize) -> Option<u64> {
|
||||||
1
|
FixedValidators::get().get(index).copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_total_exposure(_era: EraIndex) -> Balance {
|
fn get_total_exposure() -> Balance {
|
||||||
1_000_000_000u64.into()
|
1_000_000_000u64.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_validator_exposure(index: AuthIndex) -> Balance {
|
fn get_validator_exposure(validator: &u64) -> Balance {
|
||||||
match index {
|
match validator {
|
||||||
0 => 250_000_000u64,
|
0 => 250_000_000u64,
|
||||||
1 => 200_000_000u64,
|
1 => 200_000_000u64,
|
||||||
2 => 250_000_000u64,
|
2 => 250_000_000u64,
|
||||||
|
|||||||
@ -531,8 +531,10 @@ fn should_applause_and_take_next_claps() {
|
|||||||
assert_eq!(Balances::total_balance(&receiver), 0);
|
assert_eq!(Balances::total_balance(&receiver), 0);
|
||||||
for i in 0..=3 {
|
for i in 0..=3 {
|
||||||
assert_ok!(do_clap_from(session_index, network_id, i, false));
|
assert_ok!(do_clap_from(session_index, network_id, i, false));
|
||||||
clapped_amount =
|
let account_id = TestExposureListener::get_account_by_index(i as usize)
|
||||||
TestExposureListener::get_validator_exposure(i).saturating_add(clapped_amount);
|
.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_clapped_amount(&session_index, &unique_hash, clapped_amount);
|
||||||
}
|
}
|
||||||
assert_applaused(&session_index, &unique_hash);
|
assert_applaused(&session_index, &unique_hash);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user