From 638d31ba289ecc757ac3201ceba92122da863473 Mon Sep 17 00:00:00 2001 From: Uncle Stinky Date: Thu, 27 Nov 2025 19:07:59 +0300 Subject: [PATCH] update casper runtime based on ghost pallets Signed-off-by: Uncle Stinky --- runtime/casper/Cargo.toml | 1 + runtime/casper/src/impls.rs | 32 ++++++++++++++++++++ runtime/casper/src/lib.rs | 4 +-- runtime/casper/src/weights/ghost_networks.rs | 12 ++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/runtime/casper/Cargo.toml b/runtime/casper/Cargo.toml index bf9c70b..1ce0dc4 100644 --- a/runtime/casper/Cargo.toml +++ b/runtime/casper/Cargo.toml @@ -88,6 +88,7 @@ ghost-networks = { workspace = true } ghost-claims = { workspace = true } ghost-slow-clap = { workspace = true } ghost-sudo = { workspace = true } +ghost-traits = { workspace = true } casper-runtime-constants = { workspace = true } runtime-common = { workspace = true } primitives = { workspace = true } diff --git a/runtime/casper/src/impls.rs b/runtime/casper/src/impls.rs index c3ed55d..f2f3e8e 100644 --- a/runtime/casper/src/impls.rs +++ b/runtime/casper/src/impls.rs @@ -1,6 +1,8 @@ use super::*; use frame_support::{dispatch::DispatchResultWithPostInfo, traits::PrivilegeCmp}; +use ghost_traits::exposure::ExposureListener; use pallet_alliance::{ProposalIndex, ProposalProvider}; +use primitives::Balance; use sp_runtime::DispatchError; use sp_std::{cmp::Ordering, marker::PhantomData}; @@ -76,6 +78,36 @@ impl PrivilegeCmp for EqualOrGreatestRootCmp { } } +/// Used to get the exposure information out of staking pallet directly. +pub struct StakingExposureListener(PhantomData); +impl ExposureListener> for StakingExposureListener +where + T: pallet_session::Config + pallet_staking::Config + frame_system::Config, + u128: From, + AccountIdOf: From<::ValidatorId>, +{ + fn get_account_by_index(index: usize) -> Option> { + pallet_session::Pallet::::validators() + .get(index) + .map(|validator| { + let account_id: AccountIdOf = validator.clone().into(); + account_id + }) + } + + fn get_total_exposure() -> Balance { + let era = pallet_staking::Pallet::::current_era().unwrap_or_default(); + pallet_staking::Pallet::::eras_total_stake(era).into() + } + + fn get_validator_exposure(validator: &AccountIdOf) -> Balance { + let era = pallet_staking::Pallet::::current_era().unwrap_or_default(); + pallet_staking::EraInfo::::get_full_exposure(era, validator) + .total + .into() + } +} + #[cfg(feature = "runtime-benchmarks")] pub mod benchmarks { use super::*; diff --git a/runtime/casper/src/lib.rs b/runtime/casper/src/lib.rs index 5f3f590..9369293 100644 --- a/runtime/casper/src/lib.rs +++ b/runtime/casper/src/lib.rs @@ -78,7 +78,7 @@ mod genesis_config_presets; mod impls; mod weights; -pub use impls::{AllianceProposalProvider, EqualOrGreatestRootCmp}; +pub use impls::{AllianceProposalProvider, EqualOrGreatestRootCmp, StakingExposureListener}; // Governance configuration. pub mod cult; @@ -1076,10 +1076,10 @@ impl ghost_slow_clap::Config for Runtime { type BlockNumberProvider = System; type ReportUnresponsiveness = Offences; type DisabledValidators = Session; + type ExposureListener = StakingExposureListener; type MaxAuthorities = MaxAuthorities; type ApplauseThreshold = ApplauseThreshold; - type OffenceThreshold = OffenceThreshold; type UnsignedPriority = SlowClapUnsignedPriority; type HistoryDepth = SlowClapHistoryDepth; type MinAuthoritiesNumber = MinAuthoritiesNumber; diff --git a/runtime/casper/src/weights/ghost_networks.rs b/runtime/casper/src/weights/ghost_networks.rs index 1bbc205..7f52e21 100644 --- a/runtime/casper/src/weights/ghost_networks.rs +++ b/runtime/casper/src/weights/ghost_networks.rs @@ -191,6 +191,18 @@ impl ghost_networks::WeightInfo for WeightInfo { } /// Storage: `GhostNetworks::Networks` (r:1 w:1) /// Proof: `GhostNetworks::Networks` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn update_avg_block_speed() -> Weight { + // Proof Size summary in bytes: + // Measured: `302` + // Estimated: `3767` + // Minimum execution time: 49_579_000 picoseconds. + Weight::from_parts(51_126_000, 0) + .saturating_add(Weight::from_parts(0, 3767)) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: `GhostNetworks::Networks` (r:1 w:1) + /// Proof: `GhostNetworks::Networks` (`max_values`: None, `max_size`: None, mode: `Measured`) fn remove_network() -> Weight { // Proof Size summary in bytes: // Measured: `302`