Compare commits

..

4 Commits

Author SHA1 Message Date
39a6192d28
avoid re-using of to_block during rotation of block_range
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-06-18 17:20:25 +03:00
6100e79ebf
make clap regardless of fail during applause
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-06-18 17:03:09 +03:00
99c43a0c24
runtime update, with history depth for the slow clap
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-06-18 14:17:02 +03:00
417de5a7b2
clear storage based on provided history depth
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
2025-06-18 13:47:13 +03:00
7 changed files with 77 additions and 29 deletions

4
Cargo.lock generated
View File

@ -1186,7 +1186,7 @@ dependencies = [
[[package]] [[package]]
name = "casper-runtime" name = "casper-runtime"
version = "3.5.22" version = "3.5.23"
dependencies = [ dependencies = [
"casper-runtime-constants", "casper-runtime-constants",
"frame-benchmarking", "frame-benchmarking",
@ -3834,7 +3834,7 @@ dependencies = [
[[package]] [[package]]
name = "ghost-slow-clap" name = "ghost-slow-clap"
version = "0.3.24" version = "0.3.26"
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.3.24" version = "0.3.27"
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

@ -299,6 +299,9 @@ pub mod pallet {
#[pallet::constant] #[pallet::constant]
type UnsignedPriority: Get<TransactionPriority>; type UnsignedPriority: Get<TransactionPriority>;
#[pallet::constant]
type HistoryDepth: Get<SessionIndex>;
type WeightInfo: WeightInfo; type WeightInfo: WeightInfo;
} }
@ -339,11 +342,11 @@ pub mod pallet {
pub(super) type ReceivedClaps<T: Config> = StorageNMap< pub(super) type ReceivedClaps<T: Config> = StorageNMap<
_, _,
( (
NMapKey<Twox64Concat, u32>, NMapKey<Twox64Concat, SessionIndex>,
NMapKey<Twox64Concat, H256>, NMapKey<Twox64Concat, H256>,
NMapKey<Twox64Concat, H256>, NMapKey<Twox64Concat, H256>,
), ),
BoundedBTreeSet<u32, T::MaxAuthorities>, BoundedBTreeSet<AuthIndex, T::MaxAuthorities>,
ValueQuery ValueQuery
>; >;
@ -352,7 +355,7 @@ pub mod pallet {
pub(super) type ApplausesForTransaction<T: Config> = StorageNMap< pub(super) type ApplausesForTransaction<T: Config> = StorageNMap<
_, _,
( (
NMapKey<Twox64Concat, u32>, NMapKey<Twox64Concat, SessionIndex>,
NMapKey<Twox64Concat, H256>, NMapKey<Twox64Concat, H256>,
NMapKey<Twox64Concat, H256>, NMapKey<Twox64Concat, H256>,
), ),
@ -562,7 +565,13 @@ impl<T: Config> Pallet<T> {
) > Perbill::from_percent(T::ApplauseThreshold::get()); ) > Perbill::from_percent(T::ApplauseThreshold::get());
if enough_authorities { if enough_authorities {
Self::try_applause(&clap, &received_claps_key)?; if let Err(error_msg) = Self::try_applause(&clap, &received_claps_key) {
log::info!(
target: LOG_TARGET,
"👏 Could not applause because of: {:?}",
error_msg,
);
}
} }
Ok(()) Ok(())
@ -690,8 +699,8 @@ impl<T: Config> Pallet<T> {
match result_block_range { match result_block_range {
Ok(maybe_block_range) => { Ok(maybe_block_range) => {
let request_body = match maybe_block_range { let request_body = match maybe_block_range {
Some((from_block, to_block)) if from_block < to_block => Some((from_block, to_block)) if from_block < to_block.saturating_sub(1) =>
Self::prepare_request_body_for_latest_transfers(from_block, to_block, network_data), Self::prepare_request_body_for_latest_transfers(from_block, to_block.saturating_sub(1), network_data),
_ => Self::prepare_request_body_for_latest_block(network_data), _ => Self::prepare_request_body_for_latest_block(network_data),
}; };
@ -928,10 +937,23 @@ impl<T: Config> Pallet<T> {
assert!(Authorities::<T>::get(&session_index).is_empty(), "Authorities are already initilized!"); assert!(Authorities::<T>::get(&session_index).is_empty(), "Authorities are already initilized!");
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities) let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities)
.expect("more than the maximum number of authorities"); .expect("more than the maximum number of authorities");
if let Some(target_session_index) = session_index.checked_sub(T::HistoryDepth::get()) {
Self::clear_history(&target_session_index);
}
Authorities::<T>::set(&session_index, bounded_authorities); Authorities::<T>::set(&session_index, bounded_authorities);
ClapsInSession::<T>::set(&session_index, Default::default()); ClapsInSession::<T>::set(&session_index, Default::default());
} }
fn clear_history(target_session_index: &SessionIndex) {
ClapsInSession::<T>::remove(target_session_index);
let mut cursor = ReceivedClaps::<T>::clear_prefix((target_session_index,), u32::MAX, None);
debug_assert!(cursor.maybe_cursor.is_none());
cursor = ApplausesForTransaction::<T>::clear_prefix((target_session_index,), u32::MAX, None);
debug_assert!(cursor.maybe_cursor.is_none());
}
fn calculate_median_claps(session_index: &SessionIndex) -> u32 { fn calculate_median_claps(session_index: &SessionIndex) -> u32 {
let mut claps_in_session = ClapsInSession::<T>::get(session_index) let mut claps_in_session = ClapsInSession::<T>::get(session_index)
.values() .values()

View File

@ -187,6 +187,7 @@ pallet_staking_reward_curve::build! {
parameter_types! { parameter_types! {
pub static ExistentialDeposit: u64 = 2; pub static ExistentialDeposit: u64 = 2;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub const HistoryDepth: u32 = 10;
} }
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
@ -212,6 +213,7 @@ impl Config for Runtime {
type ApplauseThreshold = ConstU32<50>; type ApplauseThreshold = ConstU32<50>;
type OffenceThreshold = ConstU32<75>; type OffenceThreshold = ConstU32<75>;
type UnsignedPriority = ConstU64<{ 1 << 20 }>; type UnsignedPriority = ConstU64<{ 1 << 20 }>;
type HistoryDepth = HistoryDepth;
type WeightInfo = (); type WeightInfo = ();
} }

View File

@ -247,6 +247,35 @@ fn should_make_http_call_and_parse_logs() {
}); });
} }
#[test]
fn should_clear_sesions_based_on_history_depth() {
let (network_id, transaction_hash, unique_transaction_hash) =
generate_unique_hash(None, None, None, None);
new_test_ext().execute_with(|| {
let session_index = advance_session_and_get_index();
let history_depth = HistoryDepth::get();
let storage_key = (session_index, transaction_hash, unique_transaction_hash);
assert_claps_info_correct(&storage_key, &session_index, 0);
assert_eq!(pallet::ApplausesForTransaction::<Runtime>::get(&storage_key), false);
assert_ok!(do_clap_from(session_index, network_id, 0, false));
assert_ok!(do_clap_from(session_index, network_id, 1, false));
assert_ok!(do_clap_from(session_index, network_id, 2, false));
assert_claps_info_correct(&storage_key, &session_index, 3);
assert_eq!(pallet::ApplausesForTransaction::<Runtime>::get(&storage_key), true);
for _ in 0..history_depth {
advance_session();
}
assert_claps_info_correct(&storage_key, &session_index, 0);
assert_eq!(pallet::ApplausesForTransaction::<Runtime>::get(&storage_key), false);
});
}
#[test] #[test]
fn should_collect_commission_accordingly() { fn should_collect_commission_accordingly() {
let (network_id, _, _) = generate_unique_hash(None, None, None, None); let (network_id, _, _) = generate_unique_hash(None, None, None, None);
@ -495,7 +524,7 @@ fn should_throw_error_if_validator_disabled_and_ignore_later() {
} }
#[test] #[test]
fn should_throw_error_on_gatekeeper_amount_overflow() { fn should_clap_without_applause_on_gatekeeper_amount_overflow() {
let big_amount: u64 = u64::MAX; let big_amount: u64 = u64::MAX;
let first_receiver: u64 = 1337; let first_receiver: u64 = 1337;
let second_receiver: u64 = 420; let second_receiver: u64 = 420;
@ -540,12 +569,7 @@ fn should_throw_error_on_gatekeeper_amount_overflow() {
}; };
let authority = UintAuthorityId::from((authority_index + 1) as u64); let authority = UintAuthorityId::from((authority_index + 1) as u64);
let signature = authority.sign(&clap.encode()).unwrap(); let signature = authority.sign(&clap.encode()).unwrap();
if authority_index == 0 {
assert_ok!(SlowClap::slow_clap(RuntimeOrigin::none(), clap, signature)); assert_ok!(SlowClap::slow_clap(RuntimeOrigin::none(), clap, signature));
} else {
assert_err!(SlowClap::slow_clap(RuntimeOrigin::none(), clap, signature),
Error::<Runtime>::CouldNotIncreaseGatekeeperAmount);
}
} }
assert_eq!(Balances::balance(&first_receiver), big_amount); assert_eq!(Balances::balance(&first_receiver), big_amount);
@ -558,7 +582,7 @@ fn should_throw_error_on_gatekeeper_amount_overflow() {
} }
#[test] #[test]
fn should_throw_error_on_commission_overflow() { fn should_clap_without_applause_on_commission_overflow() {
let big_amount: u64 = u64::MAX; let big_amount: u64 = u64::MAX;
let first_receiver: u64 = 1337; let first_receiver: u64 = 1337;
let second_receiver: u64 = 420; let second_receiver: u64 = 420;
@ -606,19 +630,14 @@ fn should_throw_error_on_commission_overflow() {
}; };
let authority = UintAuthorityId::from((authority_index + 1) as u64); let authority = UintAuthorityId::from((authority_index + 1) as u64);
let signature = authority.sign(&clap.encode()).unwrap(); let signature = authority.sign(&clap.encode()).unwrap();
if authority_index == 0 {
assert_ok!(SlowClap::slow_clap(RuntimeOrigin::none(), clap, signature)); assert_ok!(SlowClap::slow_clap(RuntimeOrigin::none(), clap, signature));
} else {
assert_err!(SlowClap::slow_clap(RuntimeOrigin::none(), clap, signature),
Error::<Runtime>::CouldNotAccumulateIncomingImbalance);
}
} }
assert_eq!(Balances::balance(&first_receiver), big_amount); assert_eq!(Balances::balance(&first_receiver), big_amount);
assert_eq!(Balances::balance(&second_receiver), 0); assert_eq!(Balances::balance(&second_receiver), 0);
assert_eq!(Networks::gatekeeper_amount(network_id), big_amount); assert_eq!(Networks::gatekeeper_amount(network_id), big_amount);
assert_eq!(Networks::gatekeeper_amount(network_id_other), 0); assert_eq!(Networks::gatekeeper_amount(network_id_other), big_amount);
assert_eq!(Networks::bridged_imbalance().bridged_in, big_amount); assert_eq!(Networks::bridged_imbalance().bridged_in, big_amount);
assert_eq!(Networks::bridged_imbalance().bridged_out, 0); assert_eq!(Networks::bridged_imbalance().bridged_out, 0);
}); });

View File

@ -1,6 +1,6 @@
[package] [package]
name = "casper-runtime" name = "casper-runtime"
version = "3.5.22" version = "3.5.23"
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

@ -492,6 +492,8 @@ parameter_types! {
pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17); pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17);
pub const MaxNominations: u32 = pub const MaxNominations: u32 =
<NposCompactSolution16 as frame_election_provider_support::NposSolution>::LIMIT as u32; <NposCompactSolution16 as frame_election_provider_support::NposSolution>::LIMIT as u32;
pub const StakingHistoryDepth: u32 = 84;
pub const MaxUnlockingChunks: u32 = 32;
} }
impl pallet_staking::Config for Runtime { impl pallet_staking::Config for Runtime {
@ -520,8 +522,8 @@ impl pallet_staking::Config for Runtime {
type VoterList = VoterList; type VoterList = VoterList;
type TargetList = UseValidatorsMap<Self>; type TargetList = UseValidatorsMap<Self>;
type NominationsQuota = pallet_staking::FixedNominationsQuota<{ MaxNominations::get() }>; type NominationsQuota = pallet_staking::FixedNominationsQuota<{ MaxNominations::get() }>;
type MaxUnlockingChunks = frame_support::traits::ConstU32<32>; type MaxUnlockingChunks = MaxUnlockingChunks;
type HistoryDepth = frame_support::traits::ConstU32<84>; type HistoryDepth = StakingHistoryDepth;
type MaxControllersInDeprecationBatch = ConstU32<5314>; type MaxControllersInDeprecationBatch = ConstU32<5314>;
type BenchmarkingConfig = runtime_common::StakingBenchmarkingConfig; type BenchmarkingConfig = runtime_common::StakingBenchmarkingConfig;
type EventListeners = NominationPools; type EventListeners = NominationPools;
@ -1075,10 +1077,12 @@ impl ghost_claims::Config<CultCollectiveInstance> for Runtime {
parameter_types! { parameter_types! {
// will be used in `Perbill::from_percent()` // will be used in `Perbill::from_percent()`
pub ApplauseThreshold: u32 = 70; pub const ApplauseThreshold: u32 = 70;
// will be used in `Perbill::from_percent()` // will be used in `Perbill::from_percent()`
pub OffenceThreshold: u32 = 40; pub const OffenceThreshold: u32 = 40;
pub const SlowClapUnsignedPriority: TransactionPriority = TransactionPriority::MAX; pub const SlowClapUnsignedPriority: TransactionPriority = TransactionPriority::MAX;
pub const SlowClapHistoryDepth: sp_staking::SessionIndex =
StakingHistoryDepth::get() * SessionsPerEra::get();
} }
impl ghost_slow_clap::Config for Runtime { impl ghost_slow_clap::Config for Runtime {
@ -1096,6 +1100,7 @@ impl ghost_slow_clap::Config for Runtime {
type ApplauseThreshold = ApplauseThreshold; type ApplauseThreshold = ApplauseThreshold;
type OffenceThreshold = OffenceThreshold; type OffenceThreshold = OffenceThreshold;
type UnsignedPriority = SlowClapUnsignedPriority; type UnsignedPriority = SlowClapUnsignedPriority;
type HistoryDepth = SlowClapHistoryDepth;
type WeightInfo = weights::ghost_slow_clap::WeightInfo<Runtime>; type WeightInfo = weights::ghost_slow_clap::WeightInfo<Runtime>;
} }