Compare commits
4 Commits
270f3a6191
...
3c7e51c0f8
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c7e51c0f8 | |||
| ddd3a42564 | |||
| dc785e30d9 | |||
| 3b80a7a94a |
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -1186,7 +1186,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "casper-runtime"
|
||||
version = "3.5.36"
|
||||
version = "3.5.37"
|
||||
dependencies = [
|
||||
"casper-runtime-constants",
|
||||
"frame-benchmarking",
|
||||
@ -3837,7 +3837,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ghost-slow-clap"
|
||||
version = "0.4.7"
|
||||
version = "0.4.10"
|
||||
dependencies = [
|
||||
"frame-benchmarking",
|
||||
"frame-support",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ghost-slow-clap"
|
||||
version = "0.4.7"
|
||||
version = "0.4.10"
|
||||
description = "Applause protocol for the EVM bridge"
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
@ -347,6 +347,7 @@ pub mod pallet {
|
||||
TimeWentBackwards,
|
||||
DisabledAuthority,
|
||||
ExecutedBlockIsHigher,
|
||||
CommitInWrongSession,
|
||||
}
|
||||
|
||||
#[pallet::storage]
|
||||
@ -454,13 +455,11 @@ pub mod pallet {
|
||||
#[pallet::hooks]
|
||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
||||
fn offchain_worker(now: BlockNumberFor<T>) {
|
||||
if let Err(e) = Self::start_slow_clapping(now) {
|
||||
log::info!(
|
||||
target: LOG_TARGET,
|
||||
"👻 Skipping slow clap at {:?}: {:?}",
|
||||
now,
|
||||
e,
|
||||
)
|
||||
match Self::start_slow_clapping(now) {
|
||||
Ok(_) => {
|
||||
log::info!(target: LOG_TARGET, "👻 Slow Clap #{:?} finished gracefully", now)
|
||||
}
|
||||
Err(e) => log::info!(target: LOG_TARGET, "👻 Slow Clap #{:?} failed: {:?}", now, e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -713,14 +712,21 @@ impl<T: Config> Pallet<T> {
|
||||
fn try_commit_block(new_commitment: &BlockCommitment<NetworkIdOf<T>>) -> DispatchResult {
|
||||
let authority_index = new_commitment.authority_index;
|
||||
let network_id = new_commitment.network_id;
|
||||
let session_index = T::ValidatorSet::session_index();
|
||||
|
||||
ensure!(
|
||||
T::NetworkDataHandler::get(&network_id).is_some(),
|
||||
Error::<T>::UnregistedNetwork
|
||||
);
|
||||
|
||||
let current_commits = BlockCommitments::<T>::try_mutate(
|
||||
ensure!(
|
||||
new_commitment.session_index == session_index,
|
||||
Error::<T>::CommitInWrongSession,
|
||||
);
|
||||
|
||||
let block_commitments = BlockCommitments::<T>::try_mutate(
|
||||
&network_id,
|
||||
|current_commitments| -> Result<u64, DispatchError> {
|
||||
|current_commitments| -> Result<BTreeMap<AuthIndex, CommitmentDetails>, DispatchError> {
|
||||
let mut new_commitment_details = new_commitment.commitment;
|
||||
|
||||
let (current_commits, current_last_updated) = current_commitments
|
||||
@ -741,19 +747,21 @@ impl<T: Config> Pallet<T> {
|
||||
new_commitment_details.commits = current_commits;
|
||||
current_commitments.insert(authority_index, new_commitment_details);
|
||||
|
||||
Ok(current_commits)
|
||||
Ok(current_commitments.clone())
|
||||
},
|
||||
)?;
|
||||
|
||||
let current_commits = block_commitments
|
||||
.get(&authority_index)
|
||||
.map(|details| details.commits)
|
||||
.unwrap_or(1);
|
||||
|
||||
Self::deposit_event(Event::<T>::BlockCommited {
|
||||
network_id,
|
||||
authority_id: authority_index,
|
||||
});
|
||||
|
||||
let session_index = T::ValidatorSet::session_index();
|
||||
let validators = Validators::<T>::get(&session_index);
|
||||
let block_commitments = BlockCommitments::<T>::get(&network_id);
|
||||
|
||||
let disabled_bitmap = DisabledAuthorityIndexes::<T>::get(&session_index)
|
||||
.unwrap_or(BitMap::new(validators.len() as u32));
|
||||
|
||||
@ -765,7 +773,7 @@ impl<T: Config> Pallet<T> {
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
if current_commits > 2 && validators.len() > 0 {
|
||||
if current_commits % 3 == 0 && validators.len() > 0 {
|
||||
let offence_bitmap = Self::capture_deviation_in_commitments_and_remove(
|
||||
&disabled_bitmap,
|
||||
&block_commitments,
|
||||
@ -802,9 +810,9 @@ impl<T: Config> Pallet<T> {
|
||||
|
||||
log::info!(
|
||||
target: LOG_TARGET,
|
||||
"👻 Offchain worker started for network #{:?} at block #{:?}",
|
||||
network_in_use.0,
|
||||
"👻 Slow Clap #{:?} started for network #{:?}",
|
||||
block_number,
|
||||
network_in_use.0,
|
||||
);
|
||||
|
||||
let network_id_encoded = network_in_use.0.encode();
|
||||
@ -816,11 +824,17 @@ impl<T: Config> Pallet<T> {
|
||||
.try_lock()
|
||||
.map_err(|_| OffchainErr::OffchainTimeoutPeriod(network_in_use.0))?;
|
||||
|
||||
Self::do_evm_claps_or_save_block(session_index, network_in_use.0, &network_in_use.1)
|
||||
Self::do_evm_claps_or_save_block(
|
||||
session_index,
|
||||
block_number,
|
||||
network_in_use.0,
|
||||
&network_in_use.1,
|
||||
)
|
||||
}
|
||||
|
||||
fn do_evm_claps_or_save_block(
|
||||
session_index: SessionIndex,
|
||||
block_number: BlockNumberFor<T>,
|
||||
network_id: NetworkIdOf<T>,
|
||||
network_data: &NetworkData,
|
||||
) -> OffchainResult<T, ()> {
|
||||
@ -897,7 +911,8 @@ impl<T: Config> Pallet<T> {
|
||||
|
||||
log::info!(
|
||||
target: LOG_TARGET,
|
||||
"👻 Stored from_block is #{:?} for network {:?}",
|
||||
"👻 Slow Clap #{:?} stored block #{:?} for network {:?}",
|
||||
block_number,
|
||||
new_block_range.0,
|
||||
network_id,
|
||||
);
|
||||
@ -1074,7 +1089,7 @@ impl<T: Config> Pallet<T> {
|
||||
debug_assert!(cursor.maybe_cursor.is_none());
|
||||
}
|
||||
|
||||
fn calculate_weighted_median(values: &mut Vec<(AuthIndex, u64)>) -> u64 {
|
||||
fn calculate_median_value(values: &mut Vec<(AuthIndex, u64)>) -> u64 {
|
||||
values.sort_by_key(|data| data.1);
|
||||
|
||||
let length = values.len();
|
||||
@ -1122,8 +1137,8 @@ impl<T: Config> Pallet<T> {
|
||||
time_updates.push((authority_index, data.last_updated));
|
||||
}
|
||||
|
||||
let stored_block_median = Self::calculate_weighted_median(&mut stored_blocks);
|
||||
let time_update_median = Self::calculate_weighted_median(&mut time_updates);
|
||||
let stored_block_median = Self::calculate_median_value(&mut stored_blocks);
|
||||
let time_update_median = Self::calculate_median_value(&mut time_updates);
|
||||
|
||||
Self::apply_median_deviation(
|
||||
&mut delayed,
|
||||
@ -1210,8 +1225,9 @@ impl<T: Config> Pallet<T> {
|
||||
})
|
||||
.collect::<Vec<IdentificationTuple<T>>>();
|
||||
|
||||
let disabled_or_offence_bitmap = disabled_bitmap.bitor(offence_bitmap);
|
||||
let not_enough_validators_left = validator_set_count
|
||||
.saturating_sub(disabled_bitmap.bitor(offence_bitmap).count_ones())
|
||||
.saturating_sub(disabled_or_offence_bitmap.count_ones())
|
||||
.lt(&T::MinAuthoritiesNumber::get());
|
||||
|
||||
if not_enough_validators_left && offenders.len() > 0 {
|
||||
@ -1246,7 +1262,17 @@ impl<T: Config> Pallet<T> {
|
||||
offence_type,
|
||||
};
|
||||
|
||||
if let Err(e) = T::ReportUnresponsiveness::report_offence(vec![], offence) {
|
||||
let reporters = validators
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, _)| {
|
||||
(!disabled_or_offence_bitmap.exists(&(index as AuthIndex)))
|
||||
.then(|| T::ExposureListener::get_account_by_index(index))
|
||||
.flatten()
|
||||
})
|
||||
.collect();
|
||||
|
||||
if let Err(e) = T::ReportUnresponsiveness::report_offence(reporters, offence) {
|
||||
sp_runtime::print(e);
|
||||
}
|
||||
}
|
||||
@ -1372,10 +1398,10 @@ impl<Offender: Clone> Offence<Offender> for SlowClapOffence<Offender> {
|
||||
missed_percent.saturating_mul(Perbill::from_rational(1, offenders_count))
|
||||
}
|
||||
OffenceType::CommitmentOffence => offenders_count
|
||||
.checked_sub(self.validator_set_count / 10 + 1)
|
||||
.checked_sub(Perbill::from_percent(9).mul_ceil(self.validator_set_count))
|
||||
.map(|threshold| {
|
||||
Perbill::from_rational(3 * threshold, self.validator_set_count)
|
||||
.saturating_mul(Perbill::from_percent(7))
|
||||
Perbill::from_rational(threshold.saturating_mul(4), self.validator_set_count)
|
||||
.square()
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ fn should_calculate_throttling_slash_function_correctly() {
|
||||
|
||||
let dummy_offence = SlowClapOffence {
|
||||
session_index: 0,
|
||||
validator_set_count: 50,
|
||||
validator_set_count: 100,
|
||||
offenders: vec![()],
|
||||
offence_type: OffenceType::CommitmentOffence,
|
||||
};
|
||||
@ -39,13 +39,22 @@ fn should_calculate_throttling_slash_function_correctly() {
|
||||
assert_eq!(dummy_offence.slash_fraction(1), Perbill::zero());
|
||||
assert_eq!(dummy_offence.slash_fraction(5), Perbill::zero());
|
||||
assert_eq!(
|
||||
dummy_offence.slash_fraction(7),
|
||||
Perbill::from_parts(4_200_000)
|
||||
dummy_offence.slash_fraction(15),
|
||||
Perbill::from_parts(57_600_000)
|
||||
);
|
||||
assert_eq!(
|
||||
dummy_offence.slash_fraction(17),
|
||||
Perbill::from_parts(46_200_000)
|
||||
dummy_offence.slash_fraction(20),
|
||||
Perbill::from_parts(193_600_000)
|
||||
);
|
||||
assert_eq!(
|
||||
dummy_offence.slash_fraction(25),
|
||||
Perbill::from_parts(409_600_000)
|
||||
);
|
||||
assert_eq!(
|
||||
dummy_offence.slash_fraction(30),
|
||||
Perbill::from_parts(705_600_000)
|
||||
);
|
||||
assert_eq!(dummy_offence.slash_fraction(50), Perbill::one());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1291,6 +1300,61 @@ fn should_not_nullify_on_incorrect_block_commitment() {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_split_commit_slash_between_active_validators() {
|
||||
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 extra_time in 1..=3 {
|
||||
if extra_time < 3 {
|
||||
let offences = Offences::get();
|
||||
assert_eq!(offences.len(), 0);
|
||||
}
|
||||
|
||||
block_commitment.last_updated += COMMITMENT_DELAY_MILLIS + extra_time;
|
||||
for i in 0..3 {
|
||||
assert_ok!(do_block_commitment(
|
||||
session_index,
|
||||
network_id,
|
||||
i,
|
||||
&block_commitment
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
System::assert_has_event(RuntimeEvent::SlowClap(
|
||||
crate::Event::SomeAuthoritiesDelayed {
|
||||
delayed: vec![(3, 3)],
|
||||
},
|
||||
));
|
||||
|
||||
let offences = Offences::get();
|
||||
assert_eq!(offences.len(), 3);
|
||||
for offence in offences {
|
||||
assert_eq!(offence.0, vec![0, 1, 2]);
|
||||
assert_eq!(offence.1.session_index, session_index);
|
||||
assert_eq!(offence.1.validator_set_count, 4);
|
||||
assert_eq!(offence.1.offenders, vec![(3, 3)]);
|
||||
assert_eq!(offence.1.validator_set_count, 4);
|
||||
assert_eq!(offence.1.offence_type, OffenceType::CommitmentOffence);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn assert_clapped_amount(
|
||||
session_index: &SessionIndex,
|
||||
unique_hash: &H256,
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "casper-runtime"
|
||||
version = "3.5.36"
|
||||
version = "3.5.37"
|
||||
build = "build.rs"
|
||||
description = "Runtime of the Casper Network"
|
||||
edition.workspace = true
|
||||
|
||||
@ -117,8 +117,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
spec_name: create_runtime_str!("casper"),
|
||||
impl_name: create_runtime_str!("casper-svengali"),
|
||||
authoring_version: 0,
|
||||
spec_version: 3,
|
||||
impl_version: 2,
|
||||
spec_version: 4,
|
||||
impl_version: 3,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
transaction_version: 1,
|
||||
state_version: 1,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user