diff --git a/substrate/bin/node-template/runtime/src/lib.rs b/substrate/bin/node-template/runtime/src/lib.rs index 2347d37330be47e7cbd386da97e7c84933b5b0b4..8fae73ef085718119664479cdb949828e3b10321 100644 --- a/substrate/bin/node-template/runtime/src/lib.rs +++ b/substrate/bin/node-template/runtime/src/lib.rs @@ -214,6 +214,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/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 6a093996e8e52d6bf0381715513f94923944d77b..e6c52216b7910315ae4e92b2d536c1d8630ae55e 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -417,6 +417,7 @@ impl pallet_babe::Config for Runtime { type DisabledValidators = Session; type WeightInfo = (); type MaxAuthorities = MaxAuthorities; + type MaxNominators = MaxNominatorRewardedPerValidator; type KeyOwnerProof = <Historical as KeyOwnerProofSystem<(KeyTypeId, pallet_babe::AuthorityId)>>::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 = <Historical as KeyOwnerProofSystem<(KeyTypeId, GrandpaId)>>::Proof; type EquivocationReportSystem = diff --git a/substrate/frame/babe/src/default_weights.rs b/substrate/frame/babe/src/default_weights.rs index 2e880fd67cc2234281b86dc04781efac1bb34058..1f7de2b28c2522b52f9a9a0c0723c613d3c80e62 100644 --- a/substrate/frame/babe/src/default_weights.rs +++ b/substrate/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/substrate/frame/babe/src/lib.rs b/substrate/frame/babe/src/lib.rs index eff56515a38d03ad680aa1f833988077f489e3a4..9549fac9fe2b629ce3968b80c8d2e424ae252dd5 100644 --- a/substrate/frame/babe/src/lib.rs +++ b/substrate/frame/babe/src/lib.rs @@ -72,7 +72,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. @@ -153,6 +153,10 @@ pub mod pallet { #[pallet::constant] type MaxAuthorities: Get<u32>; + /// The maximum number of nominators for each validator. + #[pallet::constant] + type MaxNominators: Get<u32>; + /// 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. @@ -407,6 +411,7 @@ pub mod pallet { #[pallet::call_index(0)] #[pallet::weight(<T as Config>::WeightInfo::report_equivocation( key_owner_proof.validator_count(), + T::MaxNominators::get(), ))] pub fn report_equivocation( origin: OriginFor<T>, @@ -433,6 +438,7 @@ pub mod pallet { #[pallet::call_index(1)] #[pallet::weight(<T as Config>::WeightInfo::report_equivocation( key_owner_proof.validator_count(), + T::MaxNominators::get(), ))] pub fn report_equivocation_unsigned( origin: OriginFor<T>, diff --git a/substrate/frame/babe/src/mock.rs b/substrate/frame/babe/src/mock.rs index b4a7e89ceb04ee1a8e5d10e00179ea63f8c3538b..dc8e8e3499fad719db21ee1a3486d9e96c6ab11b 100644 --- a/substrate/frame/babe/src/mock.rs +++ b/substrate/frame/babe/src/mock.rs @@ -223,6 +223,7 @@ impl Config for Test { type DisabledValidators = Session; type WeightInfo = (); type MaxAuthorities = ConstU32<10>; + type MaxNominators = ConstU32<100>; type KeyOwnerProof = <Historical as KeyOwnerProofSystem<(KeyTypeId, AuthorityId)>>::Proof; type EquivocationReportSystem = super::EquivocationReportSystem<Self, Offences, Historical, ReportLongevity>; diff --git a/substrate/frame/babe/src/tests.rs b/substrate/frame/babe/src/tests.rs index 38edc2af7f272359088f9e0dd233de47f7f093e5..ae0c3e3873c5023f25c6ab6c6d3f1e4bdceb6d39 100644 --- a/substrate/frame/babe/src/tests.rs +++ b/substrate/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(<Test as Config>::WeightInfo::report_equivocation) + .map(|validators| <Test as Config>::WeightInfo::report_equivocation(validators, 1000)) .collect::<Vec<_>>() .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(<Test as Config>::WeightInfo::report_equivocation) + .map(|validators| <Test as Config>::WeightInfo::report_equivocation(validators, 1000)) .collect::<Vec<_>>() .windows(2) .all(|w| w[0].ref_time() < w[1].ref_time())); diff --git a/substrate/frame/beefy-mmr/src/mock.rs b/substrate/frame/beefy-mmr/src/mock.rs index 8594184ad71ab6a8de8440273f6a9470b0b55e4d..b17550653d5ea41cc267698d2c79d0b5199b2e7e 100644 --- a/substrate/frame/beefy-mmr/src/mock.rs +++ b/substrate/frame/beefy-mmr/src/mock.rs @@ -118,6 +118,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/substrate/frame/beefy/src/default_weights.rs b/substrate/frame/beefy/src/default_weights.rs index b15f1c88f9611ccdccd23d91c6c5d4917b782800..091d58f47f97888b43259bcb62c44905fae568e3 100644 --- a/substrate/frame/beefy/src/default_weights.rs +++ b/substrate/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/substrate/frame/beefy/src/lib.rs b/substrate/frame/beefy/src/lib.rs index da469c5b9ca023cfd8966c5e745311d2712018db..35d3273e1ef762576854ac19f7a815c06b312896 100644 --- a/substrate/frame/beefy/src/lib.rs +++ b/substrate/frame/beefy/src/lib.rs @@ -78,6 +78,10 @@ pub mod pallet { #[pallet::constant] type MaxAuthorities: Get<u32>; + /// The maximum number of nominators for each validator. + #[pallet::constant] + type MaxNominators: Get<u32>; + /// 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<T>, 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<T>, equivocation_proof: Box< @@ -441,5 +451,5 @@ impl<T: Config> IsMember<T::BeefyId> for Pallet<T> { } 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/substrate/frame/beefy/src/mock.rs b/substrate/frame/beefy/src/mock.rs index 40dcf4d7e70d3c4a43ccc23b7352051eb514a557..26a13fe16ca41a0d808c2af2e2c3b95671f5de55 100644 --- a/substrate/frame/beefy/src/mock.rs +++ b/substrate/frame/beefy/src/mock.rs @@ -110,6 +110,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/substrate/frame/beefy/src/tests.rs b/substrate/frame/beefy/src/tests.rs index f9da20e90dc74504ee5548332fc78ce430b134d1..8a8c6ae1092b4bec456eccf6d295794604382915 100644 --- a/substrate/frame/beefy/src/tests.rs +++ b/substrate/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(<Test as Config>::WeightInfo::report_equivocation) + .map(|validators| <Test as Config>::WeightInfo::report_equivocation(validators, 1000)) .collect::<Vec<_>>() .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(<Test as Config>::WeightInfo::report_equivocation) + .map(|validators| <Test as Config>::WeightInfo::report_equivocation(validators, 1000)) .collect::<Vec<_>>() .windows(2) .all(|w| w[0].ref_time() < w[1].ref_time())); diff --git a/substrate/frame/grandpa/src/default_weights.rs b/substrate/frame/grandpa/src/default_weights.rs index 3afd714f47e57c3524a601994551d988cf2e4f2e..5ccf3794880eb4cc89608aa14a1957aec1509aa7 100644 --- a/substrate/frame/grandpa/src/default_weights.rs +++ b/substrate/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/substrate/frame/grandpa/src/lib.rs b/substrate/frame/grandpa/src/lib.rs index 204bcfa826b7741ccce8ea60b7ac62b5141d49e3..2a0e707ac4148c81777a99014fb907a23ee0ca67 100644 --- a/substrate/frame/grandpa/src/lib.rs +++ b/substrate/frame/grandpa/src/lib.rs @@ -95,6 +95,10 @@ pub mod pallet { #[pallet::constant] type MaxAuthorities: Get<u32>; + /// The maximum number of nominators for each validator. + #[pallet::constant] + type MaxNominators: Get<u32>; + /// 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 @@ -189,7 +193,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<T>, equivocation_proof: Box<EquivocationProof<T::Hash, BlockNumberFor<T>>>, @@ -215,7 +222,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<T>, equivocation_proof: Box<EquivocationProof<T::Hash, BlockNumberFor<T>>>, @@ -365,7 +375,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/substrate/frame/grandpa/src/mock.rs b/substrate/frame/grandpa/src/mock.rs index 35a0d99c566c0c9a44fa06f63a2dc971ffafb500..86f91ba8032300567edeeff84eafa0917f7ab9cb 100644 --- a/substrate/frame/grandpa/src/mock.rs +++ b/substrate/frame/grandpa/src/mock.rs @@ -223,6 +223,7 @@ impl Config for Test { type RuntimeEvent = RuntimeEvent; type WeightInfo = (); type MaxAuthorities = ConstU32<100>; + type MaxNominators = ConstU32<1000>; type MaxSetIdSessionEntries = MaxSetIdSessionEntries; type KeyOwnerProof = <Historical as KeyOwnerProofSystem<(KeyTypeId, AuthorityId)>>::Proof; type EquivocationReportSystem = diff --git a/substrate/frame/grandpa/src/tests.rs b/substrate/frame/grandpa/src/tests.rs index 16d89307bb71f03ee0ccb1274cb5a30ddfc8dabd..59d73ee729ee8c9c9b9c04672ba05568986d5987 100644 --- a/substrate/frame/grandpa/src/tests.rs +++ b/substrate/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(<Test as Config>::WeightInfo::report_equivocation) + .map(|validators| <Test as Config>::WeightInfo::report_equivocation(validators, 1000)) .collect::<Vec<_>>() .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(<Test as Config>::WeightInfo::report_equivocation) + .map(|validators| <Test as Config>::WeightInfo::report_equivocation(validators, 1000)) .collect::<Vec<_>>() .windows(2) .all(|w| w[0].ref_time() < w[1].ref_time())); diff --git a/substrate/test-utils/runtime/src/lib.rs b/substrate/test-utils/runtime/src/lib.rs index ee391a6ec2147a9d3ad3afb213dad30634f4e192..b17c31fb3ea041b9dc4bb93a5a1bcd1b27558710 100644 --- a/substrate/test-utils/runtime/src/lib.rs +++ b/substrate/test-utils/runtime/src/lib.rs @@ -419,6 +419,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.