From d3e85ee565a33a94dbe1dd478bae403b0c788b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Tue, 27 Jun 2023 15:07:35 +0100 Subject: [PATCH 1/4] babe: fix report_equivocation weight calculation --- frame/babe/src/default_weights.rs | 12 ++++-------- frame/babe/src/lib.rs | 8 +++++++- frame/babe/src/mock.rs | 1 + frame/babe/src/tests.rs | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/frame/babe/src/default_weights.rs b/frame/babe/src/default_weights.rs index 2e880fd67cc22..1f7de2b28c252 100644 --- a/frame/babe/src/default_weights.rs +++ b/frame/babe/src/default_weights.rs @@ -28,15 +28,11 @@ impl crate::WeightInfo for () { DbWeight::get().writes(1) } - fn report_equivocation(validator_count: u32) -> Weight { + fn report_equivocation(validator_count: u32, max_nominators_per_validator: u32) -> Weight { // we take the validator set count from the membership proof to // calculate the weight but we set a floor of 100 validators. let validator_count = validator_count.max(100) as u64; - // worst case we are considering is that the given offender - // is backed by 200 nominators - const MAX_NOMINATORS: u64 = 200; - // checking membership proof Weight::from_parts(35u64 * WEIGHT_REF_TIME_PER_MICROS, 0) .saturating_add( @@ -49,10 +45,10 @@ impl crate::WeightInfo for () { // report offence .saturating_add(Weight::from_parts(110u64 * WEIGHT_REF_TIME_PER_MICROS, 0)) .saturating_add(Weight::from_parts( - 25u64 * WEIGHT_REF_TIME_PER_MICROS * MAX_NOMINATORS, + 25u64 * WEIGHT_REF_TIME_PER_MICROS * max_nominators_per_validator as u64, 0, )) - .saturating_add(DbWeight::get().reads(14 + 3 * MAX_NOMINATORS)) - .saturating_add(DbWeight::get().writes(10 + 3 * MAX_NOMINATORS)) + .saturating_add(DbWeight::get().reads(14 + 3 * max_nominators_per_validator as u64)) + .saturating_add(DbWeight::get().writes(10 + 3 * max_nominators_per_validator as u64)) } } diff --git a/frame/babe/src/lib.rs b/frame/babe/src/lib.rs index 8001450b43583..52bc096cb84ff 100644 --- a/frame/babe/src/lib.rs +++ b/frame/babe/src/lib.rs @@ -71,7 +71,7 @@ pub use pallet::*; pub trait WeightInfo { fn plan_config_change() -> Weight; - fn report_equivocation(validator_count: u32) -> Weight; + fn report_equivocation(validator_count: u32, max_nominators_per_validator: u32) -> Weight; } /// Trigger an epoch change, if any should take place. @@ -152,6 +152,10 @@ pub mod pallet { #[pallet::constant] type MaxAuthorities: Get; + /// The maximum number of nominators for each validator. + #[pallet::constant] + type MaxNominators: Get; + /// The proof of key ownership, used for validating equivocation reports. /// The proof must include the session index and validator count of the /// session at which the equivocation occurred. @@ -404,6 +408,7 @@ pub mod pallet { #[pallet::call_index(0)] #[pallet::weight(::WeightInfo::report_equivocation( key_owner_proof.validator_count(), + T::MaxNominators::get(), ))] pub fn report_equivocation( origin: OriginFor, @@ -430,6 +435,7 @@ pub mod pallet { #[pallet::call_index(1)] #[pallet::weight(::WeightInfo::report_equivocation( key_owner_proof.validator_count(), + T::MaxNominators::get(), ))] pub fn report_equivocation_unsigned( origin: OriginFor, diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index c0ccc9a8acd32..7b6b2743bc5aa 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -228,6 +228,7 @@ impl Config for Test { type DisabledValidators = Session; type WeightInfo = (); type MaxAuthorities = ConstU32<10>; + type MaxNominators = ConstU32<100>; type KeyOwnerProof = >::Proof; type EquivocationReportSystem = super::EquivocationReportSystem; diff --git a/frame/babe/src/tests.rs b/frame/babe/src/tests.rs index 38edc2af7f272..ae0c3e3873c50 100644 --- a/frame/babe/src/tests.rs +++ b/frame/babe/src/tests.rs @@ -815,7 +815,7 @@ fn report_equivocation_has_valid_weight() { // the weight depends on the size of the validator set, // but there's a lower bound of 100 validators. assert!((1..=100) - .map(::WeightInfo::report_equivocation) + .map(|validators| ::WeightInfo::report_equivocation(validators, 1000)) .collect::>() .windows(2) .all(|w| w[0] == w[1])); @@ -823,7 +823,7 @@ fn report_equivocation_has_valid_weight() { // after 100 validators the weight should keep increasing // with every extra validator. assert!((100..=1000) - .map(::WeightInfo::report_equivocation) + .map(|validators| ::WeightInfo::report_equivocation(validators, 1000)) .collect::>() .windows(2) .all(|w| w[0].ref_time() < w[1].ref_time())); From 7bfdc2a086c9abb06dabc30de23fe9dae3e5609a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Tue, 27 Jun 2023 15:06:26 +0100 Subject: [PATCH 2/4] grandpa: fix report_equivocation weight calculation --- frame/grandpa/src/default_weights.rs | 12 ++++-------- frame/grandpa/src/lib.rs | 16 +++++++++++++--- frame/grandpa/src/mock.rs | 1 + frame/grandpa/src/tests.rs | 4 ++-- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/frame/grandpa/src/default_weights.rs b/frame/grandpa/src/default_weights.rs index 3afd714f47e57..5ccf3794880eb 100644 --- a/frame/grandpa/src/default_weights.rs +++ b/frame/grandpa/src/default_weights.rs @@ -24,15 +24,11 @@ use frame_support::weights::{ }; impl crate::WeightInfo for () { - fn report_equivocation(validator_count: u32) -> Weight { + fn report_equivocation(validator_count: u32, max_nominators_per_validator: u32) -> Weight { // we take the validator set count from the membership proof to // calculate the weight but we set a floor of 100 validators. let validator_count = validator_count.max(100) as u64; - // worst case we are considering is that the given offender - // is backed by 200 nominators - const MAX_NOMINATORS: u64 = 200; - // checking membership proof Weight::from_parts(35u64 * WEIGHT_REF_TIME_PER_MICROS, 0) .saturating_add( @@ -45,11 +41,11 @@ impl crate::WeightInfo for () { // report offence .saturating_add(Weight::from_parts(110u64 * WEIGHT_REF_TIME_PER_MICROS, 0)) .saturating_add(Weight::from_parts( - 25u64 * WEIGHT_REF_TIME_PER_MICROS * MAX_NOMINATORS, + 25u64 * WEIGHT_REF_TIME_PER_MICROS * max_nominators_per_validator as u64, 0, )) - .saturating_add(DbWeight::get().reads(14 + 3 * MAX_NOMINATORS)) - .saturating_add(DbWeight::get().writes(10 + 3 * MAX_NOMINATORS)) + .saturating_add(DbWeight::get().reads(14 + 3 * max_nominators_per_validator as u64)) + .saturating_add(DbWeight::get().writes(10 + 3 * max_nominators_per_validator as u64)) // fetching set id -> session index mappings .saturating_add(DbWeight::get().reads(2)) } diff --git a/frame/grandpa/src/lib.rs b/frame/grandpa/src/lib.rs index 8311131e97347..273f9dc6f1981 100644 --- a/frame/grandpa/src/lib.rs +++ b/frame/grandpa/src/lib.rs @@ -94,6 +94,10 @@ pub mod pallet { #[pallet::constant] type MaxAuthorities: Get; + /// The maximum number of nominators for each validator. + #[pallet::constant] + type MaxNominators: Get; + /// The maximum number of entries to keep in the set id to session index mapping. /// /// Since the `SetIdSession` map is only used for validating equivocations this @@ -188,7 +192,10 @@ pub mod pallet { /// against the extracted offender. If both are valid, the offence /// will be reported. #[pallet::call_index(0)] - #[pallet::weight(T::WeightInfo::report_equivocation(key_owner_proof.validator_count()))] + #[pallet::weight(T::WeightInfo::report_equivocation( + key_owner_proof.validator_count(), + T::MaxNominators::get(), + ))] pub fn report_equivocation( origin: OriginFor, equivocation_proof: Box>, @@ -214,7 +221,10 @@ pub mod pallet { /// if the block author is defined it will be defined as the equivocation /// reporter. #[pallet::call_index(1)] - #[pallet::weight(T::WeightInfo::report_equivocation(key_owner_proof.validator_count()))] + #[pallet::weight(T::WeightInfo::report_equivocation( + key_owner_proof.validator_count(), + T::MaxNominators::get(), + ))] pub fn report_equivocation_unsigned( origin: OriginFor, equivocation_proof: Box>, @@ -362,7 +372,7 @@ pub mod pallet { } pub trait WeightInfo { - fn report_equivocation(validator_count: u32) -> Weight; + fn report_equivocation(validator_count: u32, max_nominators_per_validator: u32) -> Weight; fn note_stalled() -> Weight; } diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index df012ab9dc6e8..b628f0e7aa17c 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -230,6 +230,7 @@ impl Config for Test { type RuntimeEvent = RuntimeEvent; type WeightInfo = (); type MaxAuthorities = ConstU32<100>; + type MaxNominators = ConstU32<1000>; type MaxSetIdSessionEntries = MaxSetIdSessionEntries; type KeyOwnerProof = >::Proof; type EquivocationReportSystem = diff --git a/frame/grandpa/src/tests.rs b/frame/grandpa/src/tests.rs index 16d89307bb71f..59d73ee729ee8 100644 --- a/frame/grandpa/src/tests.rs +++ b/frame/grandpa/src/tests.rs @@ -838,7 +838,7 @@ fn report_equivocation_has_valid_weight() { // the weight depends on the size of the validator set, // but there's a lower bound of 100 validators. assert!((1..=100) - .map(::WeightInfo::report_equivocation) + .map(|validators| ::WeightInfo::report_equivocation(validators, 1000)) .collect::>() .windows(2) .all(|w| w[0] == w[1])); @@ -846,7 +846,7 @@ fn report_equivocation_has_valid_weight() { // after 100 validators the weight should keep increasing // with every extra validator. assert!((100..=1000) - .map(::WeightInfo::report_equivocation) + .map(|validators| ::WeightInfo::report_equivocation(validators, 1000)) .collect::>() .windows(2) .all(|w| w[0].ref_time() < w[1].ref_time())); From 3f379c066ab8abb89482f5a741bda2a17871cf07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Tue, 27 Jun 2023 15:08:32 +0100 Subject: [PATCH 3/4] beefy: fix report_equivocation weight calculation --- frame/beefy-mmr/src/mock.rs | 1 + frame/beefy/src/default_weights.rs | 11 ++++------- frame/beefy/src/lib.rs | 16 +++++++++++++--- frame/beefy/src/mock.rs | 1 + frame/beefy/src/tests.rs | 4 ++-- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/frame/beefy-mmr/src/mock.rs b/frame/beefy-mmr/src/mock.rs index 31484aaa6be70..4742b8f9baad3 100644 --- a/frame/beefy-mmr/src/mock.rs +++ b/frame/beefy-mmr/src/mock.rs @@ -123,6 +123,7 @@ impl pallet_mmr::Config for Test { impl pallet_beefy::Config for Test { type BeefyId = BeefyId; type MaxAuthorities = ConstU32<100>; + type MaxNominators = ConstU32<1000>; type MaxSetIdSessionEntries = ConstU64<100>; type OnNewValidatorSet = BeefyMmr; type WeightInfo = (); diff --git a/frame/beefy/src/default_weights.rs b/frame/beefy/src/default_weights.rs index b15f1c88f9611..091d58f47f978 100644 --- a/frame/beefy/src/default_weights.rs +++ b/frame/beefy/src/default_weights.rs @@ -24,14 +24,11 @@ use frame_support::weights::{ }; impl crate::WeightInfo for () { - fn report_equivocation(validator_count: u32) -> Weight { + fn report_equivocation(validator_count: u32, max_nominators_per_validator: u32) -> Weight { // we take the validator set count from the membership proof to // calculate the weight but we set a floor of 100 validators. let validator_count = validator_count.max(100) as u64; - // worst case we are considering is that the given offender is backed by 200 nominators - const MAX_NOMINATORS: u64 = 200; - // checking membership proof Weight::from_parts(35u64 * WEIGHT_REF_TIME_PER_MICROS, 0) .saturating_add( @@ -44,11 +41,11 @@ impl crate::WeightInfo for () { // report offence .saturating_add(Weight::from_parts(110u64 * WEIGHT_REF_TIME_PER_MICROS, 0)) .saturating_add(Weight::from_parts( - 25u64 * WEIGHT_REF_TIME_PER_MICROS * MAX_NOMINATORS, + 25u64 * WEIGHT_REF_TIME_PER_MICROS * max_nominators_per_validator as u64, 0, )) - .saturating_add(DbWeight::get().reads(14 + 3 * MAX_NOMINATORS)) - .saturating_add(DbWeight::get().writes(10 + 3 * MAX_NOMINATORS)) + .saturating_add(DbWeight::get().reads(14 + 3 * max_nominators_per_validator as u64)) + .saturating_add(DbWeight::get().writes(10 + 3 * max_nominators_per_validator as u64)) // fetching set id -> session index mappings .saturating_add(DbWeight::get().reads(2)) } diff --git a/frame/beefy/src/lib.rs b/frame/beefy/src/lib.rs index d56d5b32f8f36..6a3422fa968d6 100644 --- a/frame/beefy/src/lib.rs +++ b/frame/beefy/src/lib.rs @@ -78,6 +78,10 @@ pub mod pallet { #[pallet::constant] type MaxAuthorities: Get; + /// The maximum number of nominators for each validator. + #[pallet::constant] + type MaxNominators: Get; + /// The maximum number of entries to keep in the set id to session index mapping. /// /// Since the `SetIdSession` map is only used for validating equivocations this @@ -203,7 +207,10 @@ pub mod pallet { /// against the extracted offender. If both are valid, the offence /// will be reported. #[pallet::call_index(0)] - #[pallet::weight(T::WeightInfo::report_equivocation(key_owner_proof.validator_count()))] + #[pallet::weight(T::WeightInfo::report_equivocation( + key_owner_proof.validator_count(), + T::MaxNominators::get(), + ))] pub fn report_equivocation( origin: OriginFor, equivocation_proof: Box< @@ -235,7 +242,10 @@ pub mod pallet { /// if the block author is defined it will be defined as the equivocation /// reporter. #[pallet::call_index(1)] - #[pallet::weight(T::WeightInfo::report_equivocation(key_owner_proof.validator_count()))] + #[pallet::weight(T::WeightInfo::report_equivocation( + key_owner_proof.validator_count(), + T::MaxNominators::get(), + ))] pub fn report_equivocation_unsigned( origin: OriginFor, equivocation_proof: Box< @@ -441,5 +451,5 @@ impl IsMember for Pallet { } pub trait WeightInfo { - fn report_equivocation(validator_count: u32) -> Weight; + fn report_equivocation(validator_count: u32, max_nominators_per_validator: u32) -> Weight; } diff --git a/frame/beefy/src/mock.rs b/frame/beefy/src/mock.rs index 7edf4d339758c..da5c787736c5f 100644 --- a/frame/beefy/src/mock.rs +++ b/frame/beefy/src/mock.rs @@ -117,6 +117,7 @@ parameter_types! { impl pallet_beefy::Config for Test { type BeefyId = BeefyId; type MaxAuthorities = ConstU32<100>; + type MaxNominators = ConstU32<1000>; type MaxSetIdSessionEntries = MaxSetIdSessionEntries; type OnNewValidatorSet = (); type WeightInfo = (); diff --git a/frame/beefy/src/tests.rs b/frame/beefy/src/tests.rs index f9da20e90dc74..8a8c6ae1092b4 100644 --- a/frame/beefy/src/tests.rs +++ b/frame/beefy/src/tests.rs @@ -716,7 +716,7 @@ fn report_equivocation_has_valid_weight() { // the weight depends on the size of the validator set, // but there's a lower bound of 100 validators. assert!((1..=100) - .map(::WeightInfo::report_equivocation) + .map(|validators| ::WeightInfo::report_equivocation(validators, 1000)) .collect::>() .windows(2) .all(|w| w[0] == w[1])); @@ -724,7 +724,7 @@ fn report_equivocation_has_valid_weight() { // after 100 validators the weight should keep increasing // with every extra validator. assert!((100..=1000) - .map(::WeightInfo::report_equivocation) + .map(|validators| ::WeightInfo::report_equivocation(validators, 1000)) .collect::>() .windows(2) .all(|w| w[0].ref_time() < w[1].ref_time())); From 9d136795054005a64e70decf7ab8cb1d6614d708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Tue, 27 Jun 2023 15:08:53 +0100 Subject: [PATCH 4/4] runtime: add missing MaxNominators constant --- bin/node-template/runtime/src/lib.rs | 1 + bin/node/runtime/src/lib.rs | 2 ++ test-utils/runtime/src/lib.rs | 1 + 3 files changed, 4 insertions(+) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 22fb01b62d0f0..3c3a606293494 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -216,6 +216,7 @@ impl pallet_grandpa::Config for Runtime { type WeightInfo = (); type MaxAuthorities = ConstU32<32>; + type MaxNominators = ConstU32<0>; type MaxSetIdSessionEntries = ConstU64<0>; type KeyOwnerProof = sp_core::Void; diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 2270cd0145353..a7e00b5fe27c9 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -418,6 +418,7 @@ impl pallet_babe::Config for Runtime { type DisabledValidators = Session; type WeightInfo = (); type MaxAuthorities = MaxAuthorities; + type MaxNominators = MaxNominatorRewardedPerValidator; type KeyOwnerProof = >::Proof; type EquivocationReportSystem = @@ -1356,6 +1357,7 @@ impl pallet_grandpa::Config for Runtime { type RuntimeEvent = RuntimeEvent; type WeightInfo = (); type MaxAuthorities = MaxAuthorities; + type MaxNominators = MaxNominatorRewardedPerValidator; type MaxSetIdSessionEntries = MaxSetIdSessionEntries; type KeyOwnerProof = >::Proof; type EquivocationReportSystem = diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 8d77439f16455..1f3b1afa81036 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -421,6 +421,7 @@ impl pallet_babe::Config for Runtime { type EquivocationReportSystem = (); type WeightInfo = (); type MaxAuthorities = ConstU32<10>; + type MaxNominators = ConstU32<100>; } /// Adds one to the given input and returns the final result.