Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pallet-proofs): add benchmarks #657

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/polka-storage-proofs/src/groth16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const G2AFFINE_UNCOMPRESSED_BYTES: usize = 192;
/// That computes to: 3 x 48 + 3 * 96 + 4 = 436.
pub const VERIFYINGKEY_MIN_BYTES: usize =
3 * G1AFFINE_COMPRESSED_BYTES + 3 * G2AFFINE_COMPRESSED_BYTES + 4;

/// This constant specifies the minimum number of bytes of a serialised `VerifyingKey` of usual
/// public implementations. Usual public implementations means similar implementations in crates
/// `bellman` or `bellperson` based on `blstrs`s `Engine` implementation.
Expand All @@ -70,6 +71,7 @@ pub const VERIFYINGKEY_MIN_BYTES: usize =
/// That computes to: 3 x 96 + 3 * 192 + 4 = 868.
pub const VERIFYINGKEY_MIN_BYTES_STD: usize =
3 * G1AFFINE_UNCOMPRESSED_BYTES + 3 * G2AFFINE_UNCOMPRESSED_BYTES + 4;

/// This constant specifies the maximum number of bytes of a serialised `VerifyingKey`.
///
/// The maximum number of parameters in field `ic` is 40 because its depedency can be resolved to
Expand Down
9 changes: 9 additions & 0 deletions lib/polka-storage-proofs/src/groth16/substrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ where
}
}

impl<E> codec::MaxEncodedLen for VerifyingKey<E>
where
E: Engine<G1Affine = G1Affine, G2Affine = G2Affine>,
{
fn max_encoded_len() -> usize {
VERIFYINGKEY_MAX_BYTES
}
}

impl<E: Engine> ::scale_info::TypeInfo for VerifyingKey<E> {
type Identity = Self;

Expand Down
1 change: 1 addition & 0 deletions pallets/market/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl pallet_storage_provider::Config for Test {

impl pallet_proofs::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
Expand Down
4 changes: 4 additions & 0 deletions pallets/proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ sha2 = { workspace = true }
sp-runtime.workspace = true
sp-std.workspace = true

# Runtime benchmarks
rand_xorshift = { workspace = true, optional = true }

[dev-dependencies]
blstrs = { workspace = true }
filecoin-hashers = { workspace = true }
Expand All @@ -54,6 +57,7 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"rand_xorshift",
"sp-runtime/runtime-benchmarks",
]
std = [
Expand Down
61 changes: 61 additions & 0 deletions pallets/proofs/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![cfg(feature = "runtime-benchmarks")]

use frame_benchmarking::v2::*;
use frame_system::RawOrigin;

use super::*;
#[allow(unused)]
use crate::Pallet as ProofsPallet;

#[frame_benchmarking::v2::benchmarks(
where T: crate::Config
)]
mod benchmarks {
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;

use super::*;
use crate::crypto::groth16::{Bls12, VerifyingKey};

// Copies from the tests module to simplify imports, etc
const TEST_SEED: [u8; 16] = [
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc,
0xe5,
];

#[benchmark]
fn set_porep_verifying_key() {
let mut rng = XorShiftRng::from_seed(TEST_SEED);
let vkey = VerifyingKey::<Bls12>::random(&mut rng);
let mut vkey_bytes = vec![0u8; vkey.serialised_bytes()];
vkey.clone()
.into_bytes(&mut vkey_bytes.as_mut_slice())
.unwrap();

#[extrinsic_call]
_(RawOrigin::Signed(whitelisted_caller()), vkey_bytes);

assert!(PoRepVerifyingKey::<T>::get().is_some());
}

#[benchmark]
fn set_post_verifying_key() {
let mut rng = XorShiftRng::from_seed(TEST_SEED);
let vkey = VerifyingKey::<Bls12>::random(&mut rng);
let mut vkey_bytes = vec![0u8; vkey.serialised_bytes()];
vkey.clone()
.into_bytes(&mut vkey_bytes.as_mut_slice())
.unwrap();

#[extrinsic_call]
_(RawOrigin::Signed(whitelisted_caller()), vkey_bytes);

assert!(PoStVerifyingKey::<T>::get().is_some());
}

impl_benchmark_test_suite! {
ProofsPallet,
crate::mock::new_test_ext(),
crate::mock::Test,
}
}
12 changes: 11 additions & 1 deletion pallets/proofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ mod fr32;
mod graphs;
mod porep;
mod post;
pub mod weights;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[frame_support::pallet(dev_mode)]
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[frame_support::pallet]
pub mod pallet {
pub const LOG_TARGET: &'static str = "runtime::proofs";

Expand All @@ -37,11 +41,13 @@ pub mod pallet {
use crate::{
crypto::groth16::{Bls12, Proof, VerifyingKey},
porep, post,
weights::WeightInfo,
};

#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;
}

#[pallet::pallet]
Expand Down Expand Up @@ -85,6 +91,8 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight((T::WeightInfo::set_porep_verifying_key(), DispatchClass::Operational))]
pub fn set_porep_verifying_key(
origin: OriginFor<T>,
verifying_key: crate::Vec<u8>,
Expand All @@ -103,6 +111,8 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight((T::WeightInfo::set_post_verifying_key(), DispatchClass::Operational))]
pub fn set_post_verifying_key(
origin: OriginFor<T>,
verifying_key: crate::Vec<u8>,
Expand Down
1 change: 1 addition & 0 deletions pallets/proofs/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl frame_system::Config for Test {

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

// Build genesis storage according to the mock runtime.
Expand Down
95 changes: 95 additions & 0 deletions pallets/proofs/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

//! Autogenerated weights for `pallet_proofs`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0
//! DATE: 2024-12-23, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `parthenon`, CPU: `12th Gen Intel(R) Core(TM) i7-12700H`
//! WASM-EXECUTION: `Compiled`, CHAIN: `None`, DB CACHE: 1024

// Executed Command:
// target/release/polka-storage-node
// benchmark
// pallet
// --wasm-execution=compiled
// --pallet
// pallet_proofs
// --extrinsic
// *
// --steps
// 50
// --repeat
// 20
// --output
// pallets/proofs/weights.rs
// --template
// node/benchmark_template.hbs

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(missing_docs)]

use frame_support::{traits::Get, weights::Weight};
use core::marker::PhantomData;

pub trait WeightInfo {
fn set_porep_verifying_key() -> Weight;
fn set_post_verifying_key() -> Weight;
}

/// Weight functions for `pallet_proofs`.
pub struct Weights<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for Weights<T> {
/// Storage: `Proofs::PoRepVerifyingKey` (r:0 w:1)
/// Proof: `Proofs::PoRepVerifyingKey` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn set_porep_verifying_key() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 9_847_752_000 picoseconds.
Weight::from_parts(10_481_480_000, 0)
.saturating_add(Weight::from_parts(0, 0))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `Proofs::PoStVerifyingKey` (r:0 w:1)
/// Proof: `Proofs::PoStVerifyingKey` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn set_post_verifying_key() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 9_854_299_000 picoseconds.
Weight::from_parts(10_455_634_000, 0)
.saturating_add(Weight::from_parts(0, 0))
.saturating_add(T::DbWeight::get().writes(1))
}
}

impl WeightInfo for () {
/// Storage: `Proofs::PoRepVerifyingKey` (r:0 w:1)
/// Proof: `Proofs::PoRepVerifyingKey` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn set_porep_verifying_key() -> Weight {
use frame_support::weights::constants::RocksDbWeight;

// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 9_847_752_000 picoseconds.
Weight::from_parts(10_481_480_000, 0)
.saturating_add(Weight::from_parts(0, 0))
.saturating_add(RocksDbWeight::get().writes(1))
}
/// Storage: `Proofs::PoStVerifyingKey` (r:0 w:1)
/// Proof: `Proofs::PoStVerifyingKey` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn set_post_verifying_key() -> Weight {
use frame_support::weights::constants::RocksDbWeight;

// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 9_854_299_000 picoseconds.
Weight::from_parts(10_455_634_000, 0)
.saturating_add(Weight::from_parts(0, 0))
.saturating_add(RocksDbWeight::get().writes(1))
}
}
88 changes: 88 additions & 0 deletions pallets/randomness/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

//! Autogenerated weights for `pallet_randomness`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0
//! DATE: 2024-12-23, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `parthenon`, CPU: `12th Gen Intel(R) Core(TM) i7-12700H`
//! WASM-EXECUTION: `Compiled`, CHAIN: `None`, DB CACHE: 1024

// Executed Command:
// target/release/polka-storage-node
// benchmark
// pallet
// --wasm-execution=compiled
// --pallet
// pallet_randomness
// --extrinsic
// *
// --steps
// 50
// --repeat
// 20
// --output
// pallets/randomness/weights.rs
// --template
// node/benchmark_template.hbs

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(missing_docs)]

use frame_support::{traits::Get, weights::Weight};
use core::marker::PhantomData;

pub trait WeightInfo {
fn set_author_vrf() -> Weight;
}

/// Weight functions for `pallet_randomness`.
pub struct Weights<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for Weights<T> {
/// Storage: `ParachainSystem::ValidationData` (r:1 w:0)
/// Proof: `ParachainSystem::ValidationData` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `ParachainSystem::RelayStateProof` (r:1 w:0)
/// Proof: `ParachainSystem::RelayStateProof` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Randomness::CounterForAuthorVrfHistory` (r:1 w:0)
/// Proof: `Randomness::CounterForAuthorVrfHistory` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `Randomness::AuthorVrfHistory` (r:1 w:1)
/// Proof: `Randomness::AuthorVrfHistory` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`)
/// Storage: `Randomness::AuthorVrf` (r:0 w:1)
/// Proof: `Randomness::AuthorVrf` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
fn set_author_vrf() -> Weight {
// Proof Size summary in bytes:
// Measured: `1052`
// Estimated: `3513`
// Minimum execution time: 16_315_000 picoseconds.
Weight::from_parts(17_210_000, 0)
.saturating_add(Weight::from_parts(0, 3513))
.saturating_add(T::DbWeight::get().reads(4))
.saturating_add(T::DbWeight::get().writes(2))
}
}

impl<T: frame_system::Config> WeightInfo for () {
/// Storage: `ParachainSystem::ValidationData` (r:1 w:0)
/// Proof: `ParachainSystem::ValidationData` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `ParachainSystem::RelayStateProof` (r:1 w:0)
/// Proof: `ParachainSystem::RelayStateProof` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Randomness::CounterForAuthorVrfHistory` (r:1 w:0)
/// Proof: `Randomness::CounterForAuthorVrfHistory` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `Randomness::AuthorVrfHistory` (r:1 w:1)
/// Proof: `Randomness::AuthorVrfHistory` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`)
/// Storage: `Randomness::AuthorVrf` (r:0 w:1)
/// Proof: `Randomness::AuthorVrf` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
fn set_author_vrf() -> Weight {
use frame_support::constants::RocksDbWeight;

// Proof Size summary in bytes:
// Measured: `1052`
// Estimated: `3513`
// Minimum execution time: 16_315_000 picoseconds.
Weight::from_parts(17_210_000, 0)
.saturating_add(Weight::from_parts(0, 3513))
.saturating_add(RocksDbWeight::get().reads(4))
.saturating_add(RocksDbWeight::get().writes(2))
}
}
Loading
Loading