Skip to content

Commit

Permalink
Bench
Browse files Browse the repository at this point in the history
  • Loading branch information
serban300 committed Dec 2, 2024
1 parent 21f1ee0 commit f58d423
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 7 deletions.
4 changes: 4 additions & 0 deletions polkadot/runtime/rococo/src/weights/pallet_beefy_mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ use core::marker::PhantomData;
/// Weight functions for `pallet_beefy_mmr`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_beefy_mmr::WeightInfo for WeightInfo<T> {
fn n_leafs_proof_is_optimal(n: u32) -> Weight {
todo!()
}

/// Storage: `System::BlockHash` (r:1 w:0)
/// Proof: `System::BlockHash` (`max_values`: None, `max_size`: Some(44), added: 2519, mode: `MaxEncodedLen`)
fn extract_validation_context() -> Weight {
Expand Down
4 changes: 4 additions & 0 deletions polkadot/runtime/westend/src/weights/pallet_beefy_mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ use core::marker::PhantomData;
/// Weight functions for `pallet_beefy_mmr`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_beefy_mmr::WeightInfo for WeightInfo<T> {
fn n_leafs_proof_is_optimal(n: u32) -> Weight {
todo!()
}

/// Storage: `System::BlockHash` (r:1 w:0)
/// Proof: `System::BlockHash` (`max_values`: None, `max_size`: Some(44), added: 2519, mode: `MaxEncodedLen`)
fn extract_validation_context() -> Weight {
Expand Down
18 changes: 18 additions & 0 deletions substrate/frame/beefy-mmr/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ fn init_block<T: Config>(block_num: u32) {
mod benchmarks {
use super::*;

/// Generate ancestry proofs with `n` leafs and benchmark the logic that checks
/// if the proof is optimal.
#[benchmark]
fn n_leafs_proof_is_optimal(n: Linear<2, 512>) {
pallet_mmr::UseLocalStorage::<T>::set(true);

for block_num in 1..=n {
init_block::<T>(block_num);
}
let proof = Mmr::<T>::generate_mock_ancestry_proof().unwrap();
assert_eq!(proof.leaf_count, n as u64);

#[block]
{
<BeefyMmr<T> as AncestryHelper<HeaderFor<T>>>::is_proof_optimal(&proof);
};
}

#[benchmark]
fn extract_validation_context() {
pallet_mmr::UseLocalStorage::<T>::set(true);
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/beefy-mmr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ impl<T: Config> AncestryHelperWeightInfo<HeaderFor<T>> for Pallet<T>
where
T: pallet_mmr::Config<Hashing = sp_consensus_beefy::MmrHashing>,
{
fn is_proof_optimal(proof: &<Self as AncestryHelper<HeaderFor<T>>>::Proof) -> Weight {
<T as Config>::WeightInfo::n_leafs_proof_is_optimal(proof.leaf_count.saturated_into())
}

fn extract_validation_context() -> Weight {
<T as Config>::WeightInfo::extract_validation_context()
}
Expand Down
9 changes: 9 additions & 0 deletions substrate/frame/beefy-mmr/src/weights.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions substrate/frame/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,8 @@ pub(crate) trait WeightInfoExt: WeightInfo {
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()
<T::AncestryHelper as AncestryHelperWeightInfo<HeaderFor<T>>>::is_proof_optimal(&ancestry_proof)
.saturating_add(<T::AncestryHelper as AncestryHelperWeightInfo<HeaderFor<T>>>::extract_validation_context())
.saturating_add(
<T::AncestryHelper as AncestryHelperWeightInfo<HeaderFor<T>>>::is_non_canonical(
ancestry_proof,
Expand All @@ -765,12 +766,7 @@ pub(crate) trait WeightInfoExt: WeightInfo {
1,
validator_count,
max_nominators_per_validator,
));

// TODO: https://github.com/paritytech/polkadot-sdk/issues/4523 - 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(
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/beefy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ impl<Header: HeaderT> AncestryHelper<Header> for MockAncestryHelper {
}

impl<Header: HeaderT> AncestryHelperWeightInfo<Header> for MockAncestryHelper {
fn is_proof_optimal(_proof: &<Self as AncestryHelper<HeaderFor<Test>>>::Proof) -> Weight {
unimplemented!()
}

fn extract_validation_context() -> Weight {
unimplemented!()
}
Expand Down
3 changes: 3 additions & 0 deletions substrate/primitives/consensus/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ pub trait AncestryHelper<Header: HeaderT> {

/// Weight information for the logic in `AncestryHelper`.
pub trait AncestryHelperWeightInfo<Header: HeaderT>: AncestryHelper<Header> {
/// Weight info for the `AncestryHelper::is_proof_optimal()` method.
fn is_proof_optimal(proof: &<Self as AncestryHelper<Header>>::Proof) -> Weight;

/// Weight info for the `AncestryHelper::extract_validation_context()` method.
fn extract_validation_context() -> Weight;

Expand Down

0 comments on commit f58d423

Please sign in to comment.