Skip to content

Commit

Permalink
Compute report_fork_voting() weight
Browse files Browse the repository at this point in the history
  • Loading branch information
serban300 committed Jul 30, 2024
1 parent 8e0ea6f commit e6599ea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
5 changes: 0 additions & 5 deletions substrate/frame/beefy/src/default_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ impl crate::WeightInfo for () {
.saturating_add(DbWeight::get().reads(2))
}

// TODO: Calculate
fn report_fork_voting(_validator_count: u32, _max_nominators_per_validator: u32) -> Weight {
Weight::MAX
}

fn set_new_genesis() -> Weight {
DbWeight::get().writes(1)
}
Expand Down
37 changes: 32 additions & 5 deletions substrate/frame/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,10 @@ pub mod pallet {
/// and validate the given key ownership proof against the extracted offender.
/// If both are valid, the offence will be reported.
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::report_fork_voting(
#[pallet::weight(T::WeightInfo::report_fork_voting::<T>(
key_owner_proof.validator_count(),
T::MaxNominators::get(),
&equivocation_proof.ancestry_proof
))]
pub fn report_fork_voting(
origin: OriginFor<T>,
Expand Down Expand Up @@ -328,9 +329,10 @@ pub mod pallet {
/// if the block author is defined it will be defined as the equivocation
/// reporter.
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::report_fork_voting(
#[pallet::weight(T::WeightInfo::report_fork_voting::<T>(
key_owner_proof.validator_count(),
T::MaxNominators::get(),
&equivocation_proof.ancestry_proof
))]
pub fn report_fork_voting_unsigned(
origin: OriginFor<T>,
Expand Down Expand Up @@ -740,11 +742,36 @@ pub trait WeightInfo {
max_nominators_per_validator: u32,
) -> Weight;

fn set_new_genesis() -> Weight;
}

pub(crate) trait WeightInfoExt: WeightInfo {
fn report_double_voting(validator_count: u32, max_nominators_per_validator: u32) -> Weight {
Self::report_voting_equivocation(2, validator_count, max_nominators_per_validator)
}

fn report_fork_voting(validator_count: u32, max_nominators_per_validator: u32) -> Weight;
fn report_fork_voting<T: Config>(
validator_count: u32,
max_nominators_per_validator: u32,
ancestry_proof: &<T::AncestryHelper as AncestryHelper<HeaderFor<T>>>::Proof,
) -> Weight {
let _weight = <T::AncestryHelper as AncestryHelperWeightInfo<HeaderFor<T>>>::extract_validation_context()
.saturating_add(
<T::AncestryHelper as AncestryHelperWeightInfo<HeaderFor<T>>>::is_non_canonical(
ancestry_proof,
),
)
.saturating_add(Self::report_voting_equivocation(
1,
validator_count,
max_nominators_per_validator,
));

// TODO: return `_weight` here.
// We return `Weight::MAX` currently in order to disallow this extrinsic for the moment.
// We need to check that the proof is optimal.
Weight::MAX
}

fn report_future_block_voting(
validator_count: u32,
Expand All @@ -760,6 +787,6 @@ pub trait WeightInfo {
max_nominators_per_validator,
))
}

fn set_new_genesis() -> Weight;
}

impl<T> WeightInfoExt for T where T: WeightInfo {}
10 changes: 7 additions & 3 deletions substrate/frame/beefy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sp_consensus_beefy::{
use sp_runtime::DigestItem;
use sp_session::MembershipProof;

use crate::{self as beefy, mock::*, Call, Config, Error, WeightInfo};
use crate::{self as beefy, mock::*, Call, Config, Error, WeightInfoExt};

fn init_block(block: u64) {
System::set_block_number(block);
Expand Down Expand Up @@ -765,15 +765,19 @@ fn report_double_voting_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(|validators| <Test as Config>::WeightInfo::report_double_voting(validators, 1000))
.map(|validators| <<Test as Config>::WeightInfo as WeightInfoExt>::report_double_voting(
validators, 1000
))
.collect::<Vec<_>>()
.windows(2)
.all(|w| w[0] == w[1]));

// after 100 validators the weight should keep increasing
// with every extra validator.
assert!((100..=1000)
.map(|validators| <Test as Config>::WeightInfo::report_double_voting(validators, 1000))
.map(|validators| <<Test as Config>::WeightInfo as WeightInfoExt>::report_double_voting(
validators, 1000
))
.collect::<Vec<_>>()
.windows(2)
.all(|w| w[0].ref_time() < w[1].ref_time()));
Expand Down

0 comments on commit e6599ea

Please sign in to comment.