Compare commits

..

No commits in common. "3c7e51c0f8db8117fd8b02dee8acae80b4eb6cbf" and "270f3a619104297867a519465c4d9e9951334ea8" have entirely different histories.

6 changed files with 37 additions and 127 deletions

4
Cargo.lock generated
View File

@ -1186,7 +1186,7 @@ dependencies = [
[[package]]
name = "casper-runtime"
version = "3.5.37"
version = "3.5.36"
dependencies = [
"casper-runtime-constants",
"frame-benchmarking",
@ -3837,7 +3837,7 @@ dependencies = [
[[package]]
name = "ghost-slow-clap"
version = "0.4.10"
version = "0.4.7"
dependencies = [
"frame-benchmarking",
"frame-support",

View File

@ -1,6 +1,6 @@
[package]
name = "ghost-slow-clap"
version = "0.4.10"
version = "0.4.7"
description = "Applause protocol for the EVM bridge"
license.workspace = true
authors.workspace = true

View File

@ -347,7 +347,6 @@ pub mod pallet {
TimeWentBackwards,
DisabledAuthority,
ExecutedBlockIsHigher,
CommitInWrongSession,
}
#[pallet::storage]
@ -455,11 +454,13 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn offchain_worker(now: BlockNumberFor<T>) {
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),
if let Err(e) = Self::start_slow_clapping(now) {
log::info!(
target: LOG_TARGET,
"👻 Skipping slow clap at {:?}: {:?}",
now,
e,
)
}
}
}
@ -712,21 +713,14 @@ 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
);
ensure!(
new_commitment.session_index == session_index,
Error::<T>::CommitInWrongSession,
);
let block_commitments = BlockCommitments::<T>::try_mutate(
let current_commits = BlockCommitments::<T>::try_mutate(
&network_id,
|current_commitments| -> Result<BTreeMap<AuthIndex, CommitmentDetails>, DispatchError> {
|current_commitments| -> Result<u64, DispatchError> {
let mut new_commitment_details = new_commitment.commitment;
let (current_commits, current_last_updated) = current_commitments
@ -747,21 +741,19 @@ impl<T: Config> Pallet<T> {
new_commitment_details.commits = current_commits;
current_commitments.insert(authority_index, new_commitment_details);
Ok(current_commitments.clone())
Ok(current_commits)
},
)?;
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));
@ -773,7 +765,7 @@ impl<T: Config> Pallet<T> {
})
.unwrap_or_default();
if current_commits % 3 == 0 && validators.len() > 0 {
if current_commits > 2 && validators.len() > 0 {
let offence_bitmap = Self::capture_deviation_in_commitments_and_remove(
&disabled_bitmap,
&block_commitments,
@ -810,9 +802,9 @@ impl<T: Config> Pallet<T> {
log::info!(
target: LOG_TARGET,
"👻 Slow Clap #{:?} started for network #{:?}",
block_number,
"👻 Offchain worker started for network #{:?} at block #{:?}",
network_in_use.0,
block_number,
);
let network_id_encoded = network_in_use.0.encode();
@ -824,17 +816,11 @@ impl<T: Config> Pallet<T> {
.try_lock()
.map_err(|_| OffchainErr::OffchainTimeoutPeriod(network_in_use.0))?;
Self::do_evm_claps_or_save_block(
session_index,
block_number,
network_in_use.0,
&network_in_use.1,
)
Self::do_evm_claps_or_save_block(session_index, 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, ()> {
@ -911,8 +897,7 @@ impl<T: Config> Pallet<T> {
log::info!(
target: LOG_TARGET,
"👻 Slow Clap #{:?} stored block #{:?} for network {:?}",
block_number,
"👻 Stored from_block is #{:?} for network {:?}",
new_block_range.0,
network_id,
);
@ -1089,7 +1074,7 @@ impl<T: Config> Pallet<T> {
debug_assert!(cursor.maybe_cursor.is_none());
}
fn calculate_median_value(values: &mut Vec<(AuthIndex, u64)>) -> u64 {
fn calculate_weighted_median(values: &mut Vec<(AuthIndex, u64)>) -> u64 {
values.sort_by_key(|data| data.1);
let length = values.len();
@ -1137,8 +1122,8 @@ impl<T: Config> Pallet<T> {
time_updates.push((authority_index, data.last_updated));
}
let stored_block_median = Self::calculate_median_value(&mut stored_blocks);
let time_update_median = Self::calculate_median_value(&mut time_updates);
let stored_block_median = Self::calculate_weighted_median(&mut stored_blocks);
let time_update_median = Self::calculate_weighted_median(&mut time_updates);
Self::apply_median_deviation(
&mut delayed,
@ -1225,9 +1210,8 @@ 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_or_offence_bitmap.count_ones())
.saturating_sub(disabled_bitmap.bitor(offence_bitmap).count_ones())
.lt(&T::MinAuthoritiesNumber::get());
if not_enough_validators_left && offenders.len() > 0 {
@ -1262,17 +1246,7 @@ impl<T: Config> Pallet<T> {
offence_type,
};
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) {
if let Err(e) = T::ReportUnresponsiveness::report_offence(vec![], offence) {
sp_runtime::print(e);
}
}
@ -1398,10 +1372,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(Perbill::from_percent(9).mul_ceil(self.validator_set_count))
.checked_sub(self.validator_set_count / 10 + 1)
.map(|threshold| {
Perbill::from_rational(threshold.saturating_mul(4), self.validator_set_count)
.square()
Perbill::from_rational(3 * threshold, self.validator_set_count)
.saturating_mul(Perbill::from_percent(7))
})
.unwrap_or_default(),
}

View File

@ -31,7 +31,7 @@ fn should_calculate_throttling_slash_function_correctly() {
let dummy_offence = SlowClapOffence {
session_index: 0,
validator_set_count: 100,
validator_set_count: 50,
offenders: vec![()],
offence_type: OffenceType::CommitmentOffence,
};
@ -39,22 +39,13 @@ 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(15),
Perbill::from_parts(57_600_000)
dummy_offence.slash_fraction(7),
Perbill::from_parts(4_200_000)
);
assert_eq!(
dummy_offence.slash_fraction(20),
Perbill::from_parts(193_600_000)
dummy_offence.slash_fraction(17),
Perbill::from_parts(46_200_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]
@ -1300,61 +1291,6 @@ 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,

View File

@ -1,6 +1,6 @@
[package]
name = "casper-runtime"
version = "3.5.37"
version = "3.5.36"
build = "build.rs"
description = "Runtime of the Casper Network"
edition.workspace = true

View File

@ -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: 4,
impl_version: 3,
spec_version: 3,
impl_version: 2,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
state_version: 1,