rustfmt ghost slow clap pallet
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
parent
16b933f3ba
commit
b692959369
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ghost-slow-clap"
|
name = "ghost-slow-clap"
|
||||||
version = "0.4.0"
|
version = "0.4.1"
|
||||||
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
|
||||||
|
|||||||
@ -24,7 +24,11 @@ impl BitMap {
|
|||||||
buckets.insert(bucket_id, 0);
|
buckets.insert(bucket_id, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
BitMap { buckets, max_length, max_value }
|
BitMap {
|
||||||
|
buckets,
|
||||||
|
max_length,
|
||||||
|
max_value,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn size_of_bucket() -> u32 {
|
pub fn size_of_bucket() -> u32 {
|
||||||
@ -66,7 +70,8 @@ impl BitMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn count_ones(&self) -> u32 {
|
pub fn count_ones(&self) -> u32 {
|
||||||
let zeros: u32 = self.iter_buckets()
|
let zeros: u32 = self
|
||||||
|
.iter_buckets()
|
||||||
.map(|(_, bucket)| bucket.count_zeros())
|
.map(|(_, bucket)| bucket.count_zeros())
|
||||||
.sum();
|
.sum();
|
||||||
let total_bits = self.total_bits();
|
let total_bits = self.total_bits();
|
||||||
|
|||||||
@ -39,11 +39,11 @@ use sp_staking::{
|
|||||||
};
|
};
|
||||||
use sp_std::{collections::btree_map::BTreeMap, prelude::*, vec::Vec};
|
use sp_std::{collections::btree_map::BTreeMap, prelude::*, vec::Vec};
|
||||||
|
|
||||||
use ghost_traits::exposure::ExposureListener;
|
|
||||||
use ghost_networks::{
|
use ghost_networks::{
|
||||||
NetworkData, NetworkDataBasicHandler, NetworkDataInspectHandler, NetworkDataMutateHandler,
|
NetworkData, NetworkDataBasicHandler, NetworkDataInspectHandler, NetworkDataMutateHandler,
|
||||||
NetworkType,
|
NetworkType,
|
||||||
};
|
};
|
||||||
|
use ghost_traits::exposure::ExposureListener;
|
||||||
|
|
||||||
pub mod weights;
|
pub mod weights;
|
||||||
pub use crate::weights::WeightInfo;
|
pub use crate::weights::WeightInfo;
|
||||||
@ -51,12 +51,12 @@ mod benchmarking;
|
|||||||
mod mock;
|
mod mock;
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
mod bitmap;
|
||||||
mod deserialisations;
|
mod deserialisations;
|
||||||
mod evm_types;
|
mod evm_types;
|
||||||
mod bitmap;
|
|
||||||
|
|
||||||
|
use bitmap::{BitMap, Bucket};
|
||||||
use evm_types::{EvmResponse, EvmResponseType};
|
use evm_types::{EvmResponse, EvmResponseType};
|
||||||
use bitmap::{Bucket, BitMap};
|
|
||||||
|
|
||||||
pub mod sr25519 {
|
pub mod sr25519 {
|
||||||
mod app_sr25519 {
|
mod app_sr25519 {
|
||||||
@ -355,13 +355,8 @@ pub mod pallet {
|
|||||||
|
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
#[pallet::getter(fn latest_executed_block)]
|
#[pallet::getter(fn latest_executed_block)]
|
||||||
pub(super) type LatestExecutedBlock<T: Config> = StorageMap<
|
pub(super) type LatestExecutedBlock<T: Config> =
|
||||||
_,
|
StorageMap<_, Twox64Concat, NetworkIdOf<T>, u64, ValueQuery>;
|
||||||
Twox64Concat,
|
|
||||||
NetworkIdOf<T>,
|
|
||||||
u64,
|
|
||||||
ValueQuery,
|
|
||||||
>;
|
|
||||||
|
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
#[pallet::getter(fn block_commitments)]
|
#[pallet::getter(fn block_commitments)]
|
||||||
@ -382,18 +377,13 @@ pub mod pallet {
|
|||||||
Twox64Concat,
|
Twox64Concat,
|
||||||
H256,
|
H256,
|
||||||
ApplauseDetail<NetworkIdOf<T>, BalanceOf<T>>,
|
ApplauseDetail<NetworkIdOf<T>, BalanceOf<T>>,
|
||||||
OptionQuery
|
OptionQuery,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
#[pallet::getter(fn disabled_authority_indexes)]
|
#[pallet::getter(fn disabled_authority_indexes)]
|
||||||
pub(super) type DisabledAuthorityIndexes<T: Config> = StorageMap<
|
pub(super) type DisabledAuthorityIndexes<T: Config> =
|
||||||
_,
|
StorageMap<_, Twox64Concat, SessionIndex, BitMap, OptionQuery>;
|
||||||
Twox64Concat,
|
|
||||||
SessionIndex,
|
|
||||||
BitMap,
|
|
||||||
OptionQuery,
|
|
||||||
>;
|
|
||||||
|
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
#[pallet::getter(fn authorities)]
|
#[pallet::getter(fn authorities)]
|
||||||
@ -480,16 +470,21 @@ pub mod pallet {
|
|||||||
type Call = Call<T>;
|
type Call = Call<T>;
|
||||||
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
|
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
|
||||||
match call {
|
match call {
|
||||||
Call::commit_block { block_commitment, signature } => {
|
Call::commit_block {
|
||||||
|
block_commitment,
|
||||||
|
signature,
|
||||||
|
} => {
|
||||||
let session_index = block_commitment.session_index;
|
let session_index = block_commitment.session_index;
|
||||||
let authority_index = block_commitment.authority_index;
|
let authority_index = block_commitment.authority_index;
|
||||||
|
|
||||||
match Authorities::<T>::get(&session_index).get(authority_index as usize) {
|
match Authorities::<T>::get(&session_index).get(authority_index as usize) {
|
||||||
Some(authority) => {
|
Some(authority) => {
|
||||||
if !block_commitment.using_encoded(|encoded| authority.verify(&encoded, signature)) {
|
if !block_commitment
|
||||||
|
.using_encoded(|encoded| authority.verify(&encoded, signature))
|
||||||
|
{
|
||||||
return InvalidTransaction::BadProof.into();
|
return InvalidTransaction::BadProof.into();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => return InvalidTransaction::BadSigner.into(),
|
None => return InvalidTransaction::BadSigner.into(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -505,10 +500,11 @@ pub mod pallet {
|
|||||||
|
|
||||||
match Authorities::<T>::get(&session_index).get(clap.authority_index as usize) {
|
match Authorities::<T>::get(&session_index).get(clap.authority_index as usize) {
|
||||||
Some(authority) => {
|
Some(authority) => {
|
||||||
if !clap.using_encoded(|encoded| authority.verify(&encoded, signature)) {
|
if !clap.using_encoded(|encoded| authority.verify(&encoded, signature))
|
||||||
|
{
|
||||||
return InvalidTransaction::BadProof.into();
|
return InvalidTransaction::BadProof.into();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => return InvalidTransaction::BadSigner.into(),
|
None => return InvalidTransaction::BadSigner.into(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -583,7 +579,8 @@ impl<T: Config> Pallet<T> {
|
|||||||
let prev_session_index = clap.session_index.saturating_sub(1);
|
let prev_session_index = clap.session_index.saturating_sub(1);
|
||||||
let clap_unique_hash = Self::generate_unique_hash(&clap);
|
let clap_unique_hash = Self::generate_unique_hash(&clap);
|
||||||
|
|
||||||
let session_index = if ApplauseDetails::<T>::get(&prev_session_index, &clap_unique_hash).is_some() {
|
let session_index =
|
||||||
|
if ApplauseDetails::<T>::get(&prev_session_index, &clap_unique_hash).is_some() {
|
||||||
prev_session_index
|
prev_session_index
|
||||||
} else {
|
} else {
|
||||||
clap.session_index
|
clap.session_index
|
||||||
@ -594,7 +591,10 @@ impl<T: Config> Pallet<T> {
|
|||||||
|
|
||||||
fn try_slow_clap(clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>) -> DispatchResult {
|
fn try_slow_clap(clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>) -> DispatchResult {
|
||||||
let network_id = clap.network_id;
|
let network_id = clap.network_id;
|
||||||
ensure!(T::NetworkDataHandler::get(&network_id).is_some(), Error::<T>::UnregistedNetwork);
|
ensure!(
|
||||||
|
T::NetworkDataHandler::get(&network_id).is_some(),
|
||||||
|
Error::<T>::UnregistedNetwork
|
||||||
|
);
|
||||||
|
|
||||||
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 authorities_length = Authorities::<T>::get(&session_index).len();
|
||||||
@ -607,11 +607,10 @@ 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(
|
let new_clapped_amount = T::ExposureListener::get_validator_exposure(clap.authority_index);
|
||||||
clap.authority_index,
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut applause_details = match ApplauseDetails::<T>::take(&session_index, &clap_unique_hash) {
|
let mut applause_details =
|
||||||
|
match ApplauseDetails::<T>::take(&session_index, &clap_unique_hash) {
|
||||||
Some(applause_details) => applause_details,
|
Some(applause_details) => applause_details,
|
||||||
None => {
|
None => {
|
||||||
ensure!(
|
ensure!(
|
||||||
@ -628,14 +627,18 @@ impl<T: Config> Pallet<T> {
|
|||||||
Error::<T>::UnregisteredClapRemove,
|
Error::<T>::UnregisteredClapRemove,
|
||||||
);
|
);
|
||||||
applause_details.authorities.unset(clap.authority_index);
|
applause_details.authorities.unset(clap.authority_index);
|
||||||
applause_details.clapped_amount.saturating_sub(new_clapped_amount)
|
applause_details
|
||||||
|
.clapped_amount
|
||||||
|
.saturating_sub(new_clapped_amount)
|
||||||
} else {
|
} else {
|
||||||
ensure!(
|
ensure!(
|
||||||
!applause_details.authorities.exists(&clap.authority_index),
|
!applause_details.authorities.exists(&clap.authority_index),
|
||||||
Error::<T>::AlreadyClapped,
|
Error::<T>::AlreadyClapped,
|
||||||
);
|
);
|
||||||
applause_details.authorities.set(clap.authority_index);
|
applause_details.authorities.set(clap.authority_index);
|
||||||
applause_details.clapped_amount.saturating_add(new_clapped_amount)
|
applause_details
|
||||||
|
.clapped_amount
|
||||||
|
.saturating_add(new_clapped_amount)
|
||||||
};
|
};
|
||||||
|
|
||||||
applause_details.clapped_amount = total_clapped;
|
applause_details.clapped_amount = total_clapped;
|
||||||
@ -649,7 +652,8 @@ impl<T: Config> Pallet<T> {
|
|||||||
removed: clap.removed,
|
removed: clap.removed,
|
||||||
});
|
});
|
||||||
|
|
||||||
let is_enough = applause_details.authorities
|
let is_enough = applause_details
|
||||||
|
.authorities
|
||||||
.count_ones()
|
.count_ones()
|
||||||
.gt(&(authorities_length as u32 / 2));
|
.gt(&(authorities_length as u32 / 2));
|
||||||
|
|
||||||
@ -690,7 +694,7 @@ impl<T: Config> Pallet<T> {
|
|||||||
network_id: clap.network_id,
|
network_id: clap.network_id,
|
||||||
receiver: clap.receiver.clone(),
|
receiver: clap.receiver.clone(),
|
||||||
received_amount: final_amount,
|
received_amount: final_amount,
|
||||||
block_number: clap.block_number
|
block_number: clap.block_number,
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -699,9 +703,14 @@ impl<T: Config> Pallet<T> {
|
|||||||
fn try_commit_block(new_commitment: &BlockCommitment<NetworkIdOf<T>>) -> DispatchResult {
|
fn try_commit_block(new_commitment: &BlockCommitment<NetworkIdOf<T>>) -> DispatchResult {
|
||||||
let authority_index = new_commitment.authority_index;
|
let authority_index = new_commitment.authority_index;
|
||||||
let network_id = new_commitment.network_id;
|
let network_id = new_commitment.network_id;
|
||||||
ensure!(T::NetworkDataHandler::get(&network_id).is_some(), Error::<T>::UnregistedNetwork);
|
ensure!(
|
||||||
|
T::NetworkDataHandler::get(&network_id).is_some(),
|
||||||
|
Error::<T>::UnregistedNetwork
|
||||||
|
);
|
||||||
|
|
||||||
let current_commits = BlockCommitments::<T>::try_mutate(&network_id, |current_commitments| -> Result<u64, DispatchError> {
|
let current_commits = BlockCommitments::<T>::try_mutate(
|
||||||
|
&network_id,
|
||||||
|
|current_commitments| -> Result<u64, DispatchError> {
|
||||||
let mut new_commitment_details = new_commitment.commitment;
|
let mut new_commitment_details = new_commitment.commitment;
|
||||||
|
|
||||||
let (current_commits, current_last_updated) = current_commitments
|
let (current_commits, current_last_updated) = current_commitments
|
||||||
@ -718,11 +727,12 @@ impl<T: Config> Pallet<T> {
|
|||||||
current_commitments.insert(authority_index, new_commitment_details);
|
current_commitments.insert(authority_index, new_commitment_details);
|
||||||
|
|
||||||
Ok(current_commits)
|
Ok(current_commits)
|
||||||
})?;
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
Self::deposit_event(Event::<T>::BlockCommited {
|
Self::deposit_event(Event::<T>::BlockCommited {
|
||||||
network_id,
|
network_id,
|
||||||
authority_id: authority_index
|
authority_id: authority_index,
|
||||||
});
|
});
|
||||||
|
|
||||||
let session_index = T::ValidatorSet::session_index();
|
let session_index = T::ValidatorSet::session_index();
|
||||||
@ -733,10 +743,11 @@ impl<T: Config> Pallet<T> {
|
|||||||
.unwrap_or(BitMap::new(validators.len() as u32));
|
.unwrap_or(BitMap::new(validators.len() as u32));
|
||||||
|
|
||||||
let max_block_deviation = T::NetworkDataHandler::get(&network_id)
|
let max_block_deviation = T::NetworkDataHandler::get(&network_id)
|
||||||
.map(|network| ONE_HOUR_MILLIS
|
.map(|network| {
|
||||||
|
ONE_HOUR_MILLIS
|
||||||
.saturating_mul(6)
|
.saturating_mul(6)
|
||||||
.saturating_div(network.avg_block_speed)
|
.saturating_div(network.avg_block_speed)
|
||||||
)
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
if current_commits > 1 {
|
if current_commits > 1 {
|
||||||
@ -1086,7 +1097,10 @@ impl<T: Config> Pallet<T> {
|
|||||||
let mut time_updates: Vec<(AuthIndex, u64)> = Vec::new();
|
let mut time_updates: Vec<(AuthIndex, u64)> = Vec::new();
|
||||||
|
|
||||||
for authority_index in 0..validators_len {
|
for authority_index in 0..validators_len {
|
||||||
let data = block_commitments.get(&authority_index).copied().unwrap_or_default();
|
let data = block_commitments
|
||||||
|
.get(&authority_index)
|
||||||
|
.copied()
|
||||||
|
.unwrap_or_default();
|
||||||
stored_blocks.push((authority_index, data.last_stored_block));
|
stored_blocks.push((authority_index, data.last_stored_block));
|
||||||
time_updates.push((authority_index, data.last_updated));
|
time_updates.push((authority_index, data.last_updated));
|
||||||
}
|
}
|
||||||
@ -1094,8 +1108,20 @@ impl<T: Config> Pallet<T> {
|
|||||||
let stored_block_median = Self::calculate_weighted_median(&mut stored_blocks);
|
let stored_block_median = Self::calculate_weighted_median(&mut stored_blocks);
|
||||||
let time_update_median = Self::calculate_weighted_median(&mut time_updates);
|
let time_update_median = Self::calculate_weighted_median(&mut time_updates);
|
||||||
|
|
||||||
Self::apply_median_deviation(&mut delayed, disabled_bitmap, &stored_blocks, stored_block_median, max_block_deviation);
|
Self::apply_median_deviation(
|
||||||
Self::apply_median_deviation(&mut delayed, disabled_bitmap, &time_updates, time_update_median, ONE_HOUR_MILLIS);
|
&mut delayed,
|
||||||
|
disabled_bitmap,
|
||||||
|
&stored_blocks,
|
||||||
|
stored_block_median,
|
||||||
|
max_block_deviation,
|
||||||
|
);
|
||||||
|
Self::apply_median_deviation(
|
||||||
|
&mut delayed,
|
||||||
|
disabled_bitmap,
|
||||||
|
&time_updates,
|
||||||
|
time_update_median,
|
||||||
|
ONE_HOUR_MILLIS,
|
||||||
|
);
|
||||||
|
|
||||||
delayed
|
delayed
|
||||||
}
|
}
|
||||||
@ -1134,8 +1160,7 @@ impl<T: Config> Pallet<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
missed_validators.set(position.into());
|
missed_validators.set(position.into());
|
||||||
missed_percent = missed_percent
|
missed_percent = missed_percent.saturating_add(Perbill::from_rational(
|
||||||
.saturating_add(Perbill::from_rational(
|
|
||||||
total_exposure.saturating_sub(clapped_amount),
|
total_exposure.saturating_sub(clapped_amount),
|
||||||
total_exposure,
|
total_exposure,
|
||||||
));
|
));
|
||||||
@ -1169,10 +1194,8 @@ impl<T: Config> Pallet<T> {
|
|||||||
.collect::<Vec<IdentificationTuple<T>>>();
|
.collect::<Vec<IdentificationTuple<T>>>();
|
||||||
|
|
||||||
let not_enough_validators_left = validator_set_count
|
let not_enough_validators_left = validator_set_count
|
||||||
.saturating_sub(disabled_bitmap
|
.saturating_sub(disabled_bitmap.bitor(offence_bitmap).count_ones())
|
||||||
.bitor(offence_bitmap)
|
.lt(&T::MinAuthoritiesNumber::get());
|
||||||
.count_ones()
|
|
||||||
).lt(&T::MinAuthoritiesNumber::get());
|
|
||||||
|
|
||||||
if not_enough_validators_left && offenders.len() > 0 {
|
if not_enough_validators_left && offenders.len() > 0 {
|
||||||
Self::deposit_event(Event::<T>::BlackSwan);
|
Self::deposit_event(Event::<T>::BlackSwan);
|
||||||
@ -1189,10 +1212,12 @@ impl<T: Config> Pallet<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let offence_event = match offence_type {
|
let offence_event = match offence_type {
|
||||||
OffenceType::CommitmentOffence =>
|
OffenceType::CommitmentOffence => Event::<T>::SomeAuthoritiesDelayed {
|
||||||
Event::<T>::SomeAuthoritiesDelayed { delayed: offenders.clone() },
|
delayed: offenders.clone(),
|
||||||
OffenceType::ThrottlingOffence(_) =>
|
},
|
||||||
Event::<T>::SomeAuthoritiesTrottling { throttling: offenders.clone() },
|
OffenceType::ThrottlingOffence(_) => Event::<T>::SomeAuthoritiesTrottling {
|
||||||
|
throttling: offenders.clone(),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
Self::deposit_event(offence_event);
|
Self::deposit_event(offence_event);
|
||||||
@ -1328,13 +1353,15 @@ impl<Offender: Clone> Offence<Offender> for SlowClapOffence<Offender> {
|
|||||||
|
|
||||||
fn slash_fraction(&self, offenders_count: u32) -> Perbill {
|
fn slash_fraction(&self, offenders_count: u32) -> Perbill {
|
||||||
match self.offence_type {
|
match self.offence_type {
|
||||||
OffenceType::ThrottlingOffence(missed_percent) => missed_percent
|
OffenceType::ThrottlingOffence(missed_percent) => {
|
||||||
.saturating_mul(Perbill::from_rational(1, offenders_count)),
|
missed_percent.saturating_mul(Perbill::from_rational(1, offenders_count))
|
||||||
|
}
|
||||||
OffenceType::CommitmentOffence => offenders_count
|
OffenceType::CommitmentOffence => offenders_count
|
||||||
.checked_sub(self.validator_set_count / 10 + 1)
|
.checked_sub(self.validator_set_count / 10 + 1)
|
||||||
.map(|threshold| Perbill::from_rational(3 * threshold, self.validator_set_count)
|
.map(|threshold| {
|
||||||
|
Perbill::from_rational(3 * threshold, self.validator_set_count)
|
||||||
.saturating_mul(Perbill::from_percent(7))
|
.saturating_mul(Perbill::from_percent(7))
|
||||||
)
|
})
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,7 +20,7 @@ use sp_staking::{
|
|||||||
use sp_runtime::BuildStorage;
|
use sp_runtime::BuildStorage;
|
||||||
|
|
||||||
use crate::{self as slow_clap, AuthIndex};
|
use crate::{self as slow_clap, AuthIndex};
|
||||||
use crate::{ExposureListener, Config, EraIndex};
|
use crate::{Config, EraIndex, ExposureListener};
|
||||||
|
|
||||||
type Block = frame_system::mocking::MockBlock<Runtime>;
|
type Block = frame_system::mocking::MockBlock<Runtime>;
|
||||||
|
|
||||||
|
|||||||
@ -38,8 +38,14 @@ fn should_calculate_throttling_slash_function_correctly() {
|
|||||||
|
|
||||||
assert_eq!(dummy_offence.slash_fraction(1), Perbill::zero());
|
assert_eq!(dummy_offence.slash_fraction(1), Perbill::zero());
|
||||||
assert_eq!(dummy_offence.slash_fraction(5), Perbill::zero());
|
assert_eq!(dummy_offence.slash_fraction(5), Perbill::zero());
|
||||||
assert_eq!(dummy_offence.slash_fraction(7), Perbill::from_parts(4_200_000));
|
assert_eq!(
|
||||||
assert_eq!(dummy_offence.slash_fraction(17), Perbill::from_parts(46_200_000));
|
dummy_offence.slash_fraction(7),
|
||||||
|
Perbill::from_parts(4_200_000)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
dummy_offence.slash_fraction(17),
|
||||||
|
Perbill::from_parts(46_200_000)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -307,8 +313,8 @@ fn should_emit_offence_on_missed_clap() {
|
|||||||
|
|
||||||
System::assert_has_event(RuntimeEvent::SlowClap(
|
System::assert_has_event(RuntimeEvent::SlowClap(
|
||||||
crate::Event::SomeAuthoritiesTrottling {
|
crate::Event::SomeAuthoritiesTrottling {
|
||||||
throttling: vec![(0, 0)]
|
throttling: vec![(0, 0)],
|
||||||
}
|
},
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -329,8 +335,8 @@ fn should_emit_offence_on_fake_clap() {
|
|||||||
|
|
||||||
System::assert_has_event(RuntimeEvent::SlowClap(
|
System::assert_has_event(RuntimeEvent::SlowClap(
|
||||||
crate::Event::SomeAuthoritiesTrottling {
|
crate::Event::SomeAuthoritiesTrottling {
|
||||||
throttling: vec![(0, 0), (1, 1)]
|
throttling: vec![(0, 0), (1, 1)],
|
||||||
}
|
},
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -352,8 +358,8 @@ fn should_not_emit_offence_if_already_disabled() {
|
|||||||
|
|
||||||
System::assert_has_event(RuntimeEvent::SlowClap(
|
System::assert_has_event(RuntimeEvent::SlowClap(
|
||||||
crate::Event::SomeAuthoritiesTrottling {
|
crate::Event::SomeAuthoritiesTrottling {
|
||||||
throttling: vec![(1, 1)]
|
throttling: vec![(1, 1)],
|
||||||
}
|
},
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -406,7 +412,8 @@ fn should_emit_authorities_equilibrium_if_no_deviation() {
|
|||||||
advance_session();
|
advance_session();
|
||||||
|
|
||||||
let binding = System::events();
|
let binding = System::events();
|
||||||
let number_of_events = binding.iter()
|
let number_of_events = binding
|
||||||
|
.iter()
|
||||||
.filter(|x| {
|
.filter(|x| {
|
||||||
x.event == RuntimeEvent::SlowClap(crate::Event::AuthoritiesApplauseEquilibrium)
|
x.event == RuntimeEvent::SlowClap(crate::Event::AuthoritiesApplauseEquilibrium)
|
||||||
})
|
})
|
||||||
@ -425,18 +432,27 @@ fn should_clear_sesions_based_on_history_depth() {
|
|||||||
let session_index = advance_session_and_get_index();
|
let session_index = advance_session_and_get_index();
|
||||||
prepare_evm_network(None, None);
|
prepare_evm_network(None, None);
|
||||||
|
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(&session_index, &unique_hash), None);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(&session_index, &unique_hash),
|
||||||
|
None
|
||||||
|
);
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 1, false));
|
assert_ok!(do_clap_from(session_index, network_id, 1, false));
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 2, false));
|
assert_ok!(do_clap_from(session_index, network_id, 2, false));
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 3, false));
|
assert_ok!(do_clap_from(session_index, network_id, 3, false));
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(&session_index, &unique_hash).is_some(), true);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(&session_index, &unique_hash).is_some(),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
for _ in 0..history_depth {
|
for _ in 0..history_depth {
|
||||||
advance_session();
|
advance_session();
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(&session_index, &unique_hash), None);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(&session_index, &unique_hash),
|
||||||
|
None
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -515,8 +531,8 @@ 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 = TestExposureListener::get_validator_exposure(i)
|
clapped_amount =
|
||||||
.saturating_add(clapped_amount);
|
TestExposureListener::get_validator_exposure(i).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);
|
||||||
@ -561,13 +577,17 @@ fn should_remove_clap_by_authority() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_throw_error_if_executed_block_number_is_higher() {
|
fn should_throw_error_if_executed_block_number_is_higher() {
|
||||||
let (network_id, block_number, unique_hash) = generate_unique_hash(None, None, None, None, None);
|
let (network_id, block_number, unique_hash) =
|
||||||
|
generate_unique_hash(None, None, None, None, None);
|
||||||
|
|
||||||
new_test_ext().execute_with(|| {
|
new_test_ext().execute_with(|| {
|
||||||
let session_index = advance_session_and_get_index();
|
let session_index = advance_session_and_get_index();
|
||||||
prepare_evm_network(None, None);
|
prepare_evm_network(None, None);
|
||||||
|
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(session_index, unique_hash), None);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(session_index, unique_hash),
|
||||||
|
None
|
||||||
|
);
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 1, false));
|
assert_ok!(do_clap_from(session_index, network_id, 1, false));
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 2, false));
|
assert_ok!(do_clap_from(session_index, network_id, 2, false));
|
||||||
@ -591,13 +611,19 @@ fn should_throw_error_if_validator_disabled_and_ignore_later() {
|
|||||||
let session_index = advance_session_and_get_index();
|
let session_index = advance_session_and_get_index();
|
||||||
prepare_evm_network(None, None);
|
prepare_evm_network(None, None);
|
||||||
|
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(&session_index, &unique_hash), None);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(&session_index, &unique_hash),
|
||||||
|
None
|
||||||
|
);
|
||||||
assert_eq!(Session::disable_index(0), true);
|
assert_eq!(Session::disable_index(0), true);
|
||||||
assert_err!(
|
assert_err!(
|
||||||
do_clap_from(session_index, network_id, 0, false),
|
do_clap_from(session_index, network_id, 0, false),
|
||||||
Error::<Runtime>::DisabledAuthority,
|
Error::<Runtime>::DisabledAuthority,
|
||||||
);
|
);
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(&session_index, &unique_hash), None);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(&session_index, &unique_hash),
|
||||||
|
None
|
||||||
|
);
|
||||||
|
|
||||||
advance_session();
|
advance_session();
|
||||||
let session_index = session_index + 1;
|
let session_index = session_index + 1;
|
||||||
@ -607,7 +633,10 @@ fn should_throw_error_if_validator_disabled_and_ignore_later() {
|
|||||||
do_clap_from(session_index, network_id, 0, false),
|
do_clap_from(session_index, network_id, 0, false),
|
||||||
Error::<Runtime>::DisabledAuthority,
|
Error::<Runtime>::DisabledAuthority,
|
||||||
);
|
);
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(&session_index, &unique_hash), None);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(&session_index, &unique_hash),
|
||||||
|
None
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -777,7 +806,10 @@ fn should_avoid_session_overlap_on_mended_session_index() {
|
|||||||
|
|
||||||
assert_ok!(do_clap_from(new_session_index, network_id, 2, false));
|
assert_ok!(do_clap_from(new_session_index, network_id, 2, false));
|
||||||
assert_eq!(Balances::total_balance(&receiver), amount);
|
assert_eq!(Balances::total_balance(&receiver), amount);
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(new_session_index, unique_hash), None);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(new_session_index, unique_hash),
|
||||||
|
None
|
||||||
|
);
|
||||||
|
|
||||||
assert_applaused(&session_index, &unique_hash);
|
assert_applaused(&session_index, &unique_hash);
|
||||||
assert_clapped(&session_index, &unique_hash, 0, true);
|
assert_clapped(&session_index, &unique_hash, 0, true);
|
||||||
@ -810,7 +842,10 @@ fn should_emit_event_on_each_clap_and_on_applause() {
|
|||||||
let _ = prepare_evm_network(None, Some(100_000_000));
|
let _ = prepare_evm_network(None, Some(100_000_000));
|
||||||
let session_index = advance_session_and_get_index();
|
let session_index = advance_session_and_get_index();
|
||||||
|
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(&session_index, &unique_hash), None);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(&session_index, &unique_hash),
|
||||||
|
None
|
||||||
|
);
|
||||||
for index in 0..2 {
|
for index in 0..2 {
|
||||||
assert_ok!(do_clap_from(session_index, network_id, index, false));
|
assert_ok!(do_clap_from(session_index, network_id, index, false));
|
||||||
System::assert_last_event(RuntimeEvent::SlowClap(crate::Event::Clapped {
|
System::assert_last_event(RuntimeEvent::SlowClap(crate::Event::Clapped {
|
||||||
@ -875,7 +910,10 @@ fn should_not_fail_on_sub_existential_balance() {
|
|||||||
assert_eq!(Networks::bridged_imbalance().bridged_in, 0);
|
assert_eq!(Networks::bridged_imbalance().bridged_in, 0);
|
||||||
assert_eq!(Networks::bridged_imbalance().bridged_out, 0);
|
assert_eq!(Networks::bridged_imbalance().bridged_out, 0);
|
||||||
assert_eq!(Balances::total_balance(&receiver), 0);
|
assert_eq!(Balances::total_balance(&receiver), 0);
|
||||||
assert_eq!(ApplauseDetails::<Runtime>::get(session_index, unique_hash), None);
|
assert_eq!(
|
||||||
|
ApplauseDetails::<Runtime>::get(session_index, unique_hash),
|
||||||
|
None
|
||||||
|
);
|
||||||
|
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 1, false));
|
assert_ok!(do_clap_from(session_index, network_id, 1, false));
|
||||||
@ -910,7 +948,12 @@ fn should_register_block_commitments() {
|
|||||||
last_updated: 1337,
|
last_updated: 1337,
|
||||||
};
|
};
|
||||||
for i in 0..=3 {
|
for i in 0..=3 {
|
||||||
assert_ok!(do_block_commitment(session_index, network_id, i, &block_commitment));
|
assert_ok!(do_block_commitment(
|
||||||
|
session_index,
|
||||||
|
network_id,
|
||||||
|
i,
|
||||||
|
&block_commitment
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
block_commitment.last_updated = 1000;
|
block_commitment.last_updated = 1000;
|
||||||
@ -966,17 +1009,25 @@ fn should_disable_on_commitment_inactivity() {
|
|||||||
for extra_time in 0..=2 {
|
for extra_time in 0..=2 {
|
||||||
block_commitment.last_updated += extra_time;
|
block_commitment.last_updated += extra_time;
|
||||||
for i in 0..3 {
|
for i in 0..3 {
|
||||||
assert_ok!(do_block_commitment(session_index, network_id, i, &block_commitment));
|
assert_ok!(do_block_commitment(
|
||||||
|
session_index,
|
||||||
|
network_id,
|
||||||
|
i,
|
||||||
|
&block_commitment
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System::assert_has_event(RuntimeEvent::SlowClap(
|
System::assert_has_event(RuntimeEvent::SlowClap(
|
||||||
crate::Event::SomeAuthoritiesDelayed {
|
crate::Event::SomeAuthoritiesDelayed {
|
||||||
delayed: vec![(3, 3)]
|
delayed: vec![(3, 3)],
|
||||||
}
|
},
|
||||||
));
|
));
|
||||||
|
|
||||||
for commitment_details in BlockCommitments::<Runtime>::get(network_id).values().take(2) {
|
for commitment_details in BlockCommitments::<Runtime>::get(network_id)
|
||||||
|
.values()
|
||||||
|
.take(2)
|
||||||
|
{
|
||||||
assert_eq!(commitment_details.commits, 3);
|
assert_eq!(commitment_details.commits, 3);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -1017,15 +1068,25 @@ fn should_disable_on_commitment_block_deviation() {
|
|||||||
block_commitment.last_updated += extra_time;
|
block_commitment.last_updated += extra_time;
|
||||||
bad_block_commitment.last_updated += extra_time;
|
bad_block_commitment.last_updated += extra_time;
|
||||||
for i in 0..3 {
|
for i in 0..3 {
|
||||||
assert_ok!(do_block_commitment(session_index, network_id, i, &block_commitment));
|
assert_ok!(do_block_commitment(
|
||||||
|
session_index,
|
||||||
|
network_id,
|
||||||
|
i,
|
||||||
|
&block_commitment
|
||||||
|
));
|
||||||
}
|
}
|
||||||
assert_ok!(do_block_commitment(session_index, network_id, 3, &bad_block_commitment));
|
assert_ok!(do_block_commitment(
|
||||||
|
session_index,
|
||||||
|
network_id,
|
||||||
|
3,
|
||||||
|
&bad_block_commitment
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
System::assert_has_event(RuntimeEvent::SlowClap(
|
System::assert_has_event(RuntimeEvent::SlowClap(
|
||||||
crate::Event::SomeAuthoritiesDelayed {
|
crate::Event::SomeAuthoritiesDelayed {
|
||||||
delayed: vec![(3, 3)]
|
delayed: vec![(3, 3)],
|
||||||
}
|
},
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -1059,7 +1120,12 @@ fn should_not_offend_disabled_authorities() {
|
|||||||
for extra_time in 0..=2 {
|
for extra_time in 0..=2 {
|
||||||
block_commitment.last_updated += extra_time;
|
block_commitment.last_updated += extra_time;
|
||||||
for i in 0..3 {
|
for i in 0..3 {
|
||||||
assert_ok!(do_block_commitment(session_index, network_id, i, &block_commitment));
|
assert_ok!(do_block_commitment(
|
||||||
|
session_index,
|
||||||
|
network_id,
|
||||||
|
i,
|
||||||
|
&block_commitment
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1099,7 +1165,12 @@ fn should_not_slash_by_applause_if_disabled_by_commitment() {
|
|||||||
for extra_time in 0..=2 {
|
for extra_time in 0..=2 {
|
||||||
block_commitment.last_updated += extra_time;
|
block_commitment.last_updated += extra_time;
|
||||||
for i in 0..3 {
|
for i in 0..3 {
|
||||||
assert_ok!(do_block_commitment(session_index, network_id, i, &block_commitment));
|
assert_ok!(do_block_commitment(
|
||||||
|
session_index,
|
||||||
|
network_id,
|
||||||
|
i,
|
||||||
|
&block_commitment
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1272,8 +1343,7 @@ fn generate_unique_hash(
|
|||||||
) -> (NetworkIdOf<Runtime>, u64, H256) {
|
) -> (NetworkIdOf<Runtime>, u64, H256) {
|
||||||
let network_id = maybe_network_id.unwrap_or(1);
|
let network_id = maybe_network_id.unwrap_or(1);
|
||||||
|
|
||||||
let (transaction_hash, receiver, amount, block_number) =
|
let (transaction_hash, receiver, amount, block_number) = get_mocked_metadata();
|
||||||
get_mocked_metadata();
|
|
||||||
|
|
||||||
let transaction_hash = maybe_transaction_hash.unwrap_or(transaction_hash);
|
let transaction_hash = maybe_transaction_hash.unwrap_or(transaction_hash);
|
||||||
let receiver = maybe_receiver.unwrap_or(receiver);
|
let receiver = maybe_receiver.unwrap_or(receiver);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user