From 09444539fd8d4f3babb813a5e00577aa7fc85440 Mon Sep 17 00:00:00 2001 From: marcellorigotti Date: Fri, 25 Oct 2024 15:07:33 +0200 Subject: [PATCH 1/3] keep into account only active suspensions --- state-chain/runtime/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/state-chain/runtime/src/lib.rs b/state-chain/runtime/src/lib.rs index e885b094f0..c8a63bd8d0 100644 --- a/state-chain/runtime/src/lib.rs +++ b/state-chain/runtime/src/lib.rs @@ -2147,7 +2147,8 @@ impl_runtime_apis! { } fn cf_suspended_validators() -> Vec<(Offence, u32)> { - pallet_cf_reputation::Suspensions::::iter().map(|(key, elem)| (key, elem.len() as u32)).collect() + let current_block = >::block_number(); + pallet_cf_reputation::Suspensions::::iter().map(|(key, elem)| (key, elem.iter().filter(|(suspended_until, _)| *suspended_until < current_block).collect::>().len() as u32)).collect() } fn cf_epoch_state() -> EpochState { let auction_params = Validator::auction_parameters(); From 2355ba4ffa1de093fa1e205646f944a4b6dc9a93 Mon Sep 17 00:00:00 2001 From: marcellorigotti Date: Fri, 25 Oct 2024 17:19:49 +0200 Subject: [PATCH 2/3] filter out duplicates and properly count keygen suspensions --- .../cf-validator/src/rotation_state.rs | 2 +- state-chain/runtime/src/lib.rs | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/state-chain/pallets/cf-validator/src/rotation_state.rs b/state-chain/pallets/cf-validator/src/rotation_state.rs index e0e94fbe3a..f30b4c6271 100644 --- a/state-chain/pallets/cf-validator/src/rotation_state.rs +++ b/state-chain/pallets/cf-validator/src/rotation_state.rs @@ -6,7 +6,7 @@ use sp_std::collections::btree_set::BTreeSet; pub struct RotationState { primary_candidates: Vec, secondary_candidates: Vec, - banned: BTreeSet, + pub banned: BTreeSet, pub bond: Amount, pub new_epoch_index: EpochIndex, } diff --git a/state-chain/runtime/src/lib.rs b/state-chain/runtime/src/lib.rs index c8a63bd8d0..5fae26c7b2 100644 --- a/state-chain/runtime/src/lib.rs +++ b/state-chain/runtime/src/lib.rs @@ -79,7 +79,7 @@ use pallet_cf_swapping::SwapLegInfo; use pallet_cf_validator::SetSizeMaximisingAuctionResolver; use pallet_transaction_payment::{ConstFeeMultiplier, Multiplier}; use scale_info::prelude::string::String; -use sp_std::collections::btree_map::BTreeMap; +use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet}; pub use frame_support::{ debug, parameter_types, @@ -2148,7 +2148,24 @@ impl_runtime_apis! { fn cf_suspended_validators() -> Vec<(Offence, u32)> { let current_block = >::block_number(); - pallet_cf_reputation::Suspensions::::iter().map(|(key, elem)| (key, elem.iter().filter(|(suspended_until, _)| *suspended_until < current_block).collect::>().len() as u32)).collect() + let suspended_for_keygen = match pallet_cf_validator::Pallet::::current_rotation_phase() { + pallet_cf_validator::RotationPhase::KeygensInProgress(rotation_state) | + pallet_cf_validator::RotationPhase::KeyHandoversInProgress(rotation_state) | + pallet_cf_validator::RotationPhase::ActivatingKeys(rotation_state) | + pallet_cf_validator::RotationPhase::NewKeysActivated(rotation_state) => { rotation_state.banned.len() as u32 }, + _ => {0u32} + }; + pallet_cf_reputation::Suspensions::::iter().map(|(key, elem)| { + if key == pallet_cf_threshold_signature::PalletOffence::FailedKeygen.into() { + return (key, suspended_for_keygen); + } + (key, elem.iter().filter_map(|(suspended_until, validator)| { + if *suspended_until > current_block { + return Some(validator); + } + None + }).collect::>().len() as u32) + }).collect() } fn cf_epoch_state() -> EpochState { let auction_params = Validator::auction_parameters(); From 3ac5deb13a53fc1a006ef4bf285dc11a6cf4e765 Mon Sep 17 00:00:00 2001 From: marcellorigotti Date: Mon, 28 Oct 2024 10:21:16 +0100 Subject: [PATCH 3/3] addressed comments --- state-chain/runtime/src/lib.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/state-chain/runtime/src/lib.rs b/state-chain/runtime/src/lib.rs index 5fae26c7b2..c03e76c804 100644 --- a/state-chain/runtime/src/lib.rs +++ b/state-chain/runtime/src/lib.rs @@ -79,7 +79,7 @@ use pallet_cf_swapping::SwapLegInfo; use pallet_cf_validator::SetSizeMaximisingAuctionResolver; use pallet_transaction_payment::{ConstFeeMultiplier, Multiplier}; use scale_info::prelude::string::String; -use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet}; +use sp_std::collections::btree_map::BTreeMap; pub use frame_support::{ debug, parameter_types, @@ -2147,7 +2147,6 @@ impl_runtime_apis! { } fn cf_suspended_validators() -> Vec<(Offence, u32)> { - let current_block = >::block_number(); let suspended_for_keygen = match pallet_cf_validator::Pallet::::current_rotation_phase() { pallet_cf_validator::RotationPhase::KeygensInProgress(rotation_state) | pallet_cf_validator::RotationPhase::KeyHandoversInProgress(rotation_state) | @@ -2155,16 +2154,11 @@ impl_runtime_apis! { pallet_cf_validator::RotationPhase::NewKeysActivated(rotation_state) => { rotation_state.banned.len() as u32 }, _ => {0u32} }; - pallet_cf_reputation::Suspensions::::iter().map(|(key, elem)| { + pallet_cf_reputation::Suspensions::::iter().map(|(key, _)| { if key == pallet_cf_threshold_signature::PalletOffence::FailedKeygen.into() { return (key, suspended_for_keygen); } - (key, elem.iter().filter_map(|(suspended_until, validator)| { - if *suspended_until > current_block { - return Some(validator); - } - None - }).collect::>().len() as u32) + (key, pallet_cf_reputation::Pallet::::validators_suspended_for(&[key]).len() as u32) }).collect() } fn cf_epoch_state() -> EpochState {