Compare commits

..

4 Commits

Author SHA1 Message Date
3c7e51c0f8
new version of casper runtime
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
2025-12-09 17:46:48 +03:00
ddd3a42564
new slashing function for block commitment offence
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
2025-12-09 17:23:08 +03:00
dc785e30d9
more readable logs and use validators as offence reporters
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-12-09 16:24:41 +03:00
3b80a7a94a
change slashing logic during block commitments
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-12-09 14:56:24 +03:00
6 changed files with 127 additions and 37 deletions

4
Cargo.lock generated
View File

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

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ghost-slow-clap" name = "ghost-slow-clap"
version = "0.4.7" version = "0.4.10"
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

View File

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

View File

@ -31,7 +31,7 @@ fn should_calculate_throttling_slash_function_correctly() {
let dummy_offence = SlowClapOffence { let dummy_offence = SlowClapOffence {
session_index: 0, session_index: 0,
validator_set_count: 50, validator_set_count: 100,
offenders: vec![()], offenders: vec![()],
offence_type: OffenceType::CommitmentOffence, 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(1), Perbill::zero());
assert_eq!(dummy_offence.slash_fraction(5), Perbill::zero()); assert_eq!(dummy_offence.slash_fraction(5), Perbill::zero());
assert_eq!( assert_eq!(
dummy_offence.slash_fraction(7), dummy_offence.slash_fraction(15),
Perbill::from_parts(4_200_000) Perbill::from_parts(57_600_000)
); );
assert_eq!( assert_eq!(
dummy_offence.slash_fraction(17), dummy_offence.slash_fraction(20),
Perbill::from_parts(46_200_000) 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] #[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( fn assert_clapped_amount(
session_index: &SessionIndex, session_index: &SessionIndex,
unique_hash: &H256, unique_hash: &H256,

View File

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

View File

@ -117,8 +117,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("casper"), spec_name: create_runtime_str!("casper"),
impl_name: create_runtime_str!("casper-svengali"), impl_name: create_runtime_str!("casper-svengali"),
authoring_version: 0, authoring_version: 0,
spec_version: 3, spec_version: 4,
impl_version: 2, impl_version: 3,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
state_version: 1, state_version: 1,