Skip to content

Commit

Permalink
A0-4580: Tests for pallet committee management (#1887)
Browse files Browse the repository at this point in the history
Missing runtime only tests for pallet committee management.

---------

Co-authored-by: Michal Swietek <mike1729@users.noreply.github.com>
  • Loading branch information
mike1729 and mike1729 authored Dec 19, 2024
1 parent 9acefac commit 4ed5ab0
Show file tree
Hide file tree
Showing 12 changed files with 704 additions and 72 deletions.
7 changes: 6 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 32 additions & 32 deletions aleph-client/src/aleph_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2182,9 +2182,9 @@ pub mod api {
.hash();
runtime_metadata_hash
== [
181u8, 58u8, 203u8, 79u8, 62u8, 47u8, 91u8, 201u8, 90u8, 151u8, 65u8, 10u8, 7u8,
43u8, 151u8, 98u8, 47u8, 70u8, 191u8, 228u8, 11u8, 90u8, 230u8, 47u8, 159u8, 15u8,
195u8, 212u8, 89u8, 77u8, 211u8, 66u8,
38u8, 216u8, 17u8, 228u8, 165u8, 206u8, 209u8, 62u8, 138u8, 131u8, 22u8, 187u8,
86u8, 180u8, 170u8, 171u8, 83u8, 9u8, 71u8, 17u8, 117u8, 132u8, 198u8, 240u8, 97u8,
88u8, 240u8, 156u8, 138u8, 201u8, 116u8, 209u8,
]
}
pub mod system {
Expand Down Expand Up @@ -18561,7 +18561,7 @@ pub mod api {
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
#[doc = "Ban thresholds for the next era has changed"]
pub struct SetBanConfig(pub runtime_types::primitives::BanConfig);
pub struct SetBanConfig(pub runtime_types::primitives::ProductionBanConfig);
impl ::subxt::events::StaticEvent for SetBanConfig {
const PALLET: &'static str = "CommitteeManagement";
const EVENT: &'static str = "SetBanConfig";
Expand Down Expand Up @@ -18727,24 +18727,24 @@ pub mod api {
)
}
#[doc = " Current era config for ban functionality related to block production."]
pub fn ban_config(
pub fn production_ban_config(
&self,
) -> ::subxt::storage::address::Address<
::subxt::storage::address::StaticStorageMapKey,
runtime_types::primitives::BanConfig,
runtime_types::primitives::ProductionBanConfig,
::subxt::storage::address::Yes,
::subxt::storage::address::Yes,
(),
> {
::subxt::storage::address::Address::new_static(
"CommitteeManagement",
"BanConfig",
"ProductionBanConfig",
vec![],
[
192u8, 154u8, 113u8, 98u8, 112u8, 155u8, 249u8, 127u8, 155u8, 187u8,
221u8, 219u8, 255u8, 68u8, 176u8, 48u8, 53u8, 186u8, 213u8, 31u8,
103u8, 208u8, 118u8, 174u8, 20u8, 106u8, 70u8, 113u8, 13u8, 251u8,
52u8, 1u8,
237u8, 143u8, 142u8, 190u8, 254u8, 146u8, 35u8, 173u8, 131u8, 40u8,
223u8, 112u8, 71u8, 60u8, 241u8, 238u8, 67u8, 207u8, 176u8, 211u8,
190u8, 50u8, 232u8, 192u8, 216u8, 171u8, 185u8, 53u8, 148u8, 9u8,
214u8, 104u8,
],
)
}
Expand Down Expand Up @@ -22322,7 +22322,7 @@ pub mod api {
#[doc = "The `Error` enum of this pallet."]
pub enum Error {
#[codec(index = 0)]
#[doc = "Raised in any scenario [`BanConfig`] is invalid"]
#[doc = "Raised in any scenario [`ProductionBanConfig`] is invalid"]
#[doc = "* `performance_ratio_threshold` must be a number in range [0; 100]"]
#[doc = "* `underperformed_session_count_threshold` must be a positive number,"]
#[doc = "* `clean_session_counter_delay` must be a positive number."]
Expand Down Expand Up @@ -22352,7 +22352,7 @@ pub mod api {
pub enum Event {
#[codec(index = 0)]
#[doc = "Ban thresholds for the next era has changed"]
SetBanConfig(runtime_types::primitives::BanConfig),
SetBanConfig(runtime_types::primitives::ProductionBanConfig),
#[codec(index = 1)]
#[doc = "Ban thresholds for the next era has changed"]
SetFinalityBanConfig(runtime_types::primitives::FinalityBanConfig),
Expand Down Expand Up @@ -27559,25 +27559,6 @@ pub mod api {
# [codec (crate = :: subxt :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
pub struct BanConfig {
pub minimal_expected_performance: runtime_types::sp_arithmetic::per_things::Perbill,
pub underperformed_session_count_threshold: ::core::primitive::u32,
pub clean_session_counter_delay: ::core::primitive::u32,
pub ban_period: ::core::primitive::u32,
}
#[derive(
:: subxt :: ext :: codec :: Decode,
:: subxt :: ext :: codec :: Encode,
:: subxt :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: scale_encode :: EncodeAsType,
Clone,
Debug,
Eq,
PartialEq,
)]
# [codec (crate = :: subxt :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
pub struct BanInfo {
pub reason: runtime_types::primitives::BanReason,
pub start: ::core::primitive::u32,
Expand Down Expand Up @@ -27690,6 +27671,25 @@ pub mod api {
# [codec (crate = :: subxt :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
pub struct ProductionBanConfig {
pub minimal_expected_performance: runtime_types::sp_arithmetic::per_things::Perbill,
pub underperformed_session_count_threshold: ::core::primitive::u32,
pub clean_session_counter_delay: ::core::primitive::u32,
pub ban_period: ::core::primitive::u32,
}
#[derive(
:: subxt :: ext :: codec :: Decode,
:: subxt :: ext :: codec :: Encode,
:: subxt :: ext :: scale_decode :: DecodeAsType,
:: subxt :: ext :: scale_encode :: EncodeAsType,
Clone,
Debug,
Eq,
PartialEq,
)]
# [codec (crate = :: subxt :: ext :: codec)]
#[decode_as_type(crate_path = ":: subxt :: ext :: scale_decode")]
#[encode_as_type(crate_path = ":: subxt :: ext :: scale_encode")]
pub struct Score {
pub session_id: ::core::primitive::u32,
pub nonce: ::core::primitive::u32,
Expand Down
8 changes: 4 additions & 4 deletions aleph-client/src/pallets/committee_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
pallet_committee_management::pallet::Call::{
ban_from_committee, set_ban_config, set_lenient_threshold,
},
primitives::{BanConfig, BanInfo, BanReason},
primitives::{ProductionBanConfig, BanInfo, BanReason},
AccountId, AsConnection, BlockHash, ConnectionApi, EraIndex, RootConnection, SessionCount,
SessionIndex, SudoCall, TxInfo, TxStatus,
};
Expand All @@ -22,7 +22,7 @@ use crate::{
pub trait CommitteeManagementApi {
/// Returns `committee-management.ban_config` storage of the committee-management pallet.
/// * `at` - optional hash of a block to query state from
async fn get_ban_config(&self, at: Option<BlockHash>) -> BanConfig;
async fn get_ban_config(&self, at: Option<BlockHash>) -> ProductionBanConfig;

/// Returns `committee-management.session_validator_block_count` of a given validator.
/// * `validator` - a validator stash account id
Expand Down Expand Up @@ -115,8 +115,8 @@ pub trait CommitteeManagementSudoApi {

#[async_trait::async_trait]
impl<C: ConnectionApi + AsConnection> CommitteeManagementApi for C {
async fn get_ban_config(&self, at: Option<BlockHash>) -> BanConfig {
let addrs = api::storage().committee_management().ban_config();
async fn get_ban_config(&self, at: Option<BlockHash>) -> ProductionBanConfig {
let addrs = api::storage().committee_management().production_ban_config();

self.get_storage_entry(&addrs, at).await
}
Expand Down
4 changes: 2 additions & 2 deletions e2e-tests/src/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashSet;
use aleph_client::{
api::committee_management::events::BanValidators,
pallets::{committee_management::CommitteeManagementApi, elections::ElectionsSudoApi},
primitives::{BanConfig, BanInfo, CommitteeSeats, EraValidators},
primitives::{ProductionBanConfig, BanInfo, CommitteeSeats, EraValidators},
utility::BlocksApi,
waiting::{AlephWaiting, BlockStatus, WaitingExt},
AccountId, AsConnection, RootConnection, TxStatus,
Expand Down Expand Up @@ -82,7 +82,7 @@ pub async fn check_ban_config<C: CommitteeManagementApi>(
expected_minimal_expected_performance: Perbill,
expected_session_count_threshold: SessionCount,
expected_clean_session_counter_delay: SessionCount,
) -> BanConfig {
) -> ProductionBanConfig {
let ban_config = connection.get_ban_config(None).await;

assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion pallets/aleph/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use frame_support::sp_runtime::{traits::OpaqueKeys, RuntimeAppPublic};
use primitives::AuthorityId;
use sp_std::prelude::*;
use sp_std::{prelude::*, vec};

use crate::Config;

Expand Down
12 changes: 11 additions & 1 deletion pallets/committee-management/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-committee-management"
version = "0.2.0"
version = "0.3.0"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
Expand All @@ -16,13 +16,18 @@ log = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-authorship = { workspace = true }
pallet-balances = { workspace = true }
pallet-session = { workspace = true }
pallet-staking = { workspace = true }
pallet-timestamp = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-staking = { workspace = true }
sp-std = { workspace = true }

pallet-aleph = { workspace = true }
pallet-elections = { workspace = true }
pallets-support = { workspace = true }
primitives = { workspace = true }

Expand All @@ -37,14 +42,19 @@ std = [
"frame-support/std",
"frame-system/std",
"pallet-authorship/std",
"pallet-balances/std",
"pallet-session/std",
"pallet-staking/std",
"pallet-timestamp/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-staking/std",
"sp-std/std",
"primitives/std",
"pallets-support/std",
"pallet-aleph/std",
"pallet-elections/std",
]

try-runtime = [
Expand Down
25 changes: 14 additions & 11 deletions pallets/committee-management/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ use sp_std::{

use crate::{
pallet::{
BanConfig, Banned, Config, CurrentAndNextSessionValidatorsStorage, Event, Pallet,
Banned, Config, CurrentAndNextSessionValidatorsStorage, Event, Pallet,
SessionValidatorBlockCount, UnderperformedFinalizerSessionCount,
UnderperformedValidatorSessionCount, ValidatorEraTotalReward,
},
traits::{EraInfoProvider, ValidatorRewardsHandler},
BanConfigStruct, CurrentAndNextSessionValidators, FinalityBanConfig, LenientThreshold,
CurrentAndNextSessionValidators, LenientThreshold, ProductionBanConfigStruct,
ValidatorExtractor, ValidatorTotalRewards, LOG_TARGET,
};

Expand All @@ -33,7 +33,7 @@ impl<T: Config> BannedValidators for Pallet<T> {

fn banned() -> Vec<Self::AccountId> {
let active_era = T::EraInfoProvider::active_era().unwrap_or(0);
let ban_period = BanConfig::<T>::get().ban_period;
let ban_period = Self::production_ban_config().ban_period;

Banned::<T>::iter()
.filter(|(_, info)| !ban_expired(info.start, ban_period, active_era + 1))
Expand Down Expand Up @@ -230,7 +230,7 @@ impl<T: Config> Pallet<T> {
})
}

fn blocks_to_produce_per_session() -> u32 {
pub(crate) fn blocks_to_produce_per_session() -> u32 {
T::SessionPeriod::get()
.saturating_div(T::ValidatorProvider::current_era_committee_size().size())
}
Expand Down Expand Up @@ -353,10 +353,10 @@ impl<T: Config> Pallet<T> {
..
} = CurrentAndNextSessionValidatorsStorage::<T>::get();

let finality_ban_config = Self::finality_ban_config();
let underperformed_session_count_threshold =
FinalityBanConfig::<T>::get().underperformed_session_count_threshold;
let minimal_expected_performance =
FinalityBanConfig::<T>::get().minimal_expected_performance;
finality_ban_config.underperformed_session_count_threshold;
let minimal_expected_performance = finality_ban_config.minimal_expected_performance;

let is_underperforming = |score| score > minimal_expected_performance;

Expand All @@ -382,7 +382,7 @@ impl<T: Config> Pallet<T> {
}

pub(crate) fn calculate_underperforming_validators() {
let thresholds = BanConfig::<T>::get();
let thresholds = Self::production_ban_config();
let CurrentAndNextSessionValidators {
current: SessionValidators { producers, .. },
..
Expand All @@ -402,7 +402,10 @@ impl<T: Config> Pallet<T> {
}
}

fn mark_validator_underperformance(thresholds: &BanConfigStruct, validator: &T::AccountId) {
pub(crate) fn mark_validator_underperformance(
thresholds: &ProductionBanConfigStruct,
validator: &T::AccountId,
) {
let counter = UnderperformedValidatorSessionCount::<T>::mutate(validator, |count| {
*count += 1;
*count
Expand All @@ -415,7 +418,7 @@ impl<T: Config> Pallet<T> {
}

pub(crate) fn clear_underperformance_session_counter(session: SessionIndex) {
let clean_session_counter_delay = BanConfig::<T>::get().clean_session_counter_delay;
let clean_session_counter_delay = Self::production_ban_config().clean_session_counter_delay;
if session % clean_session_counter_delay == 0 {
info!(
target: LOG_TARGET,
Expand All @@ -428,7 +431,7 @@ impl<T: Config> Pallet<T> {
}

pub fn clear_expired_bans(active_era: EraIndex) {
let ban_period = BanConfig::<T>::get().ban_period;
let ban_period = Self::production_ban_config().ban_period;
let unban = Banned::<T>::iter().filter_map(|(v, ban_info)| {
if ban_expired(ban_info.start, ban_period, active_era) {
return Some(v);
Expand Down
Loading

0 comments on commit 4ed5ab0

Please sign in to comment.