slightly optimise try_slow_clap
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
parent
63c03ad6ce
commit
c963f7816b
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ghost-slow-clap"
|
name = "ghost-slow-clap"
|
||||||
version = "0.4.17"
|
version = "0.4.18"
|
||||||
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
|
||||||
|
|||||||
@ -662,7 +662,7 @@ impl<T: Config> Pallet<T> {
|
|||||||
let clap_unique_hash = Self::generate_unique_hash(&clap);
|
let clap_unique_hash = Self::generate_unique_hash(&clap);
|
||||||
|
|
||||||
let session_index =
|
let session_index =
|
||||||
if ApplauseDetails::<T>::get(&prev_session_index, &clap_unique_hash).is_some() {
|
if ApplauseDetails::<T>::contains_key(&prev_session_index, &clap_unique_hash) {
|
||||||
prev_session_index
|
prev_session_index
|
||||||
} else {
|
} else {
|
||||||
clap.session_index
|
clap.session_index
|
||||||
@ -674,44 +674,35 @@ 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!(
|
ensure!(
|
||||||
T::NetworkDataHandler::get(&network_id).is_some(),
|
T::NetworkDataHandler::contains_key(&network_id),
|
||||||
Error::<T>::UnregistedNetwork
|
Error::<T>::UnregistedNetwork
|
||||||
);
|
);
|
||||||
|
ensure!(
|
||||||
|
LatestExecutedBlock::<T>::get(&network_id) <= clap.block_number,
|
||||||
|
Error::<T>::ExecutedBlockIsHigher,
|
||||||
|
);
|
||||||
|
|
||||||
let (session_index, clap_unique_hash) = Self::mended_session_index(&clap);
|
let (session_index, clap_unique_hash) = Self::mended_session_index(&clap);
|
||||||
let authorities = Authorities::<T>::get(&session_index);
|
let authorities = Authorities::<T>::get(&session_index);
|
||||||
let authorities_length = authorities.len();
|
let authorities_length = authorities.len();
|
||||||
|
|
||||||
let is_disabled = DisabledAuthorityIndexes::<T>::get(&session_index)
|
let not_disabled = DisabledAuthorityIndexes::<T>::get(&session_index)
|
||||||
.map(|bitmap| bitmap.exists(&clap.authority_index))
|
.map(|bitmap| !bitmap.exists(&clap.authority_index))
|
||||||
.unwrap_or(true);
|
.unwrap_or_default();
|
||||||
|
|
||||||
ensure!(!is_disabled, Error::<T>::DisabledAuthority);
|
ensure!(not_disabled, Error::<T>::DisabledAuthority);
|
||||||
|
|
||||||
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 maybe_account_id =
|
let account_id = T::ExposureListener::get_account_by_index(clap.authority_index as usize)
|
||||||
T::ExposureListener::get_account_by_index(clap.authority_index as usize);
|
.ok_or(Error::<T>::NonExistentAuthorityIndex)?;
|
||||||
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 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) {
|
ApplauseDetails::<T>::try_get(&session_index, &clap_unique_hash).unwrap_or(
|
||||||
Some(applause_details) => applause_details,
|
ApplauseDetail::new(network_id, clap.block_number, authorities_length),
|
||||||
None => {
|
|
||||||
ensure!(
|
|
||||||
LatestExecutedBlock::<T>::get(&network_id) <= clap.block_number,
|
|
||||||
Error::<T>::ExecutedBlockIsHigher,
|
|
||||||
);
|
);
|
||||||
ApplauseDetail::new(network_id, clap.block_number, authorities_length)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let total_clapped = if clap.removed {
|
let total_clapped = if clap.removed {
|
||||||
ensure!(
|
ensure!(
|
||||||
@ -750,9 +741,9 @@ impl<T: Config> Pallet<T> {
|
|||||||
.gt(&(authorities_length as u32 / 2));
|
.gt(&(authorities_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;
|
match Self::try_applause(&clap) {
|
||||||
if let Err(e) = Self::try_applause(&clap) {
|
Ok(_) => applause_details.finalized = true,
|
||||||
sp_runtime::print(e);
|
Err(e) => sp_runtime::print(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user