mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 16:11:05 +00:00
Don't use fixed nominator count for report_equivocation weight calculation (#14471)
* babe: fix report_equivocation weight calculation * grandpa: fix report_equivocation weight calculation * beefy: fix report_equivocation weight calculation * runtime: add missing MaxNominators constant
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 = ();
|
||||
|
||||
@@ -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()));
|
||||
|
||||
Reference in New Issue
Block a user