correct median; on genesis validators list is empty
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
parent
d45cc9bd13
commit
147542f10c
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ghost-slow-clap"
|
||||
version = "0.4.4"
|
||||
version = "0.4.5"
|
||||
description = "Applause protocol for the EVM bridge"
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
@ -597,8 +597,8 @@ impl<T: Config> Pallet<T> {
|
||||
);
|
||||
|
||||
let (session_index, clap_unique_hash) = Self::mended_session_index(&clap);
|
||||
let validators = Validators::<T>::get(&session_index);
|
||||
let validators_length = validators.len();
|
||||
let authorities = Authorities::<T>::get(&session_index);
|
||||
let authorities_length = authorities.len();
|
||||
|
||||
let is_disabled = DisabledAuthorityIndexes::<T>::get(&session_index)
|
||||
.map(|bitmap| bitmap.exists(&clap.authority_index))
|
||||
@ -627,7 +627,7 @@ impl<T: Config> Pallet<T> {
|
||||
LatestExecutedBlock::<T>::get(&network_id) <= clap.block_number,
|
||||
Error::<T>::ExecutedBlockIsHigher,
|
||||
);
|
||||
ApplauseDetail::new(network_id, clap.block_number, validators_length)
|
||||
ApplauseDetail::new(network_id, clap.block_number, authorities_length)
|
||||
}
|
||||
};
|
||||
|
||||
@ -665,7 +665,7 @@ impl<T: Config> Pallet<T> {
|
||||
let is_enough = applause_details
|
||||
.authorities
|
||||
.count_ones()
|
||||
.gt(&(validators_length as u32 / 2));
|
||||
.gt(&(authorities_length as u32 / 2));
|
||||
|
||||
if total_clapped > threshold_amount && is_enough && !applause_details.finalized {
|
||||
applause_details.finalized = true;
|
||||
@ -760,7 +760,7 @@ impl<T: Config> Pallet<T> {
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
if current_commits > 1 {
|
||||
if current_commits > 2 && validators.len() > 0 {
|
||||
let offence_bitmap = Self::capture_deviation_in_commitments_and_remove(
|
||||
&disabled_bitmap,
|
||||
&block_commitments,
|
||||
@ -899,9 +899,8 @@ impl<T: Config> Pallet<T> {
|
||||
|
||||
log::info!(
|
||||
target: LOG_TARGET,
|
||||
"👻 Stored from_block is #{:?} from authority #{:?} for network {:?}",
|
||||
"👻 Stored from_block is #{:?} for network {:?}",
|
||||
new_block_range.0,
|
||||
authority_index,
|
||||
network_id,
|
||||
);
|
||||
|
||||
@ -1080,11 +1079,13 @@ impl<T: Config> Pallet<T> {
|
||||
fn calculate_weighted_median(values: &mut Vec<(AuthIndex, u64)>) -> u64 {
|
||||
values.sort_by_key(|data| data.1);
|
||||
|
||||
let mid = values.len() / 2;
|
||||
if values.len() % 2 == 1 {
|
||||
values[mid].1
|
||||
let length = values.len();
|
||||
if length % 2 == 0 {
|
||||
let mid_left = values[length / 2 - 1].1;
|
||||
let mid_right = values[length / 2].1;
|
||||
(mid_left + mid_right) / 2
|
||||
} else {
|
||||
values[mid - 1].1
|
||||
values[length / 2].1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1182,6 +1182,50 @@ fn should_not_slash_by_applause_if_disabled_by_commitment() {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_nullify_on_incorrect_block_commitment() {
|
||||
let (network_id, _, _) = generate_unique_hash(None, None, None, None, None);
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
let session_index = advance_session_and_get_index();
|
||||
prepare_evm_network(None, None);
|
||||
|
||||
let timestamp = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Time went backwards")
|
||||
.as_millis() as u64;
|
||||
|
||||
let mut block_commitment = CommitmentDetails {
|
||||
last_stored_block: 9_500_000,
|
||||
commits: 420,
|
||||
last_updated: timestamp,
|
||||
};
|
||||
|
||||
for i in 0..4 {
|
||||
assert_ok!(do_block_commitment(
|
||||
session_index,
|
||||
network_id,
|
||||
i,
|
||||
&block_commitment
|
||||
));
|
||||
}
|
||||
|
||||
block_commitment.last_updated = 0;
|
||||
assert_err!(do_block_commitment(
|
||||
session_index,
|
||||
network_id,
|
||||
3,
|
||||
&block_commitment),
|
||||
Error::<Runtime>::TimeWentBackwards);
|
||||
|
||||
for commitment_details in BlockCommitments::<Runtime>::get(network_id).values() {
|
||||
assert_eq!(commitment_details.last_stored_block, 9_500_000);
|
||||
assert_eq!(commitment_details.last_updated, timestamp);
|
||||
assert_eq!(commitment_details.commits, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn assert_clapped_amount(
|
||||
session_index: &SessionIndex,
|
||||
unique_hash: &H256,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user