Skip to content

Commit

Permalink
feat(pallet-proofs): add benchmarks (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte authored Jan 3, 2025
1 parent 1f404ac commit 81d899e
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 7 deletions.
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))
}
}
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ runtime-benchmarks = [
"pallet-collator-selection/runtime-benchmarks",
"pallet-market/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"pallet-proofs/runtime-benchmarks",
"pallet-randomness/runtime-benchmarks",
"pallet-storage-provider/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
Expand Down
1 change: 1 addition & 0 deletions runtime/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ frame_benchmarking::define_benchmarks!(
[cumulus_pallet_xcmp_queue, XcmpQueue]
// Our crates
[pallet_randomness, Randomness]
[pallet_proofs, Proofs]
);
13 changes: 7 additions & 6 deletions runtime/src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ impl pallet_market::Config for Runtime {

impl pallet_proofs::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_proofs::weights::Weights<Runtime>;
}

/// Config for our randomness pallet
impl pallet_randomness::Config for Runtime {
type AuthorVrfGetter = BabeDataGetter<Runtime>;
type WeightInfo = pallet_randomness::weights::Weights<Runtime>;
}

#[cfg(feature = "testnet")]
Expand All @@ -420,9 +427,3 @@ impl pallet_faucet::Config for Runtime {
type FaucetDripAmount = FaucetDripAmount;
type FaucetDripDelay = FaucetDripDelay;
}

/// Config for our randomness pallet
impl pallet_randomness::Config for Runtime {
type AuthorVrfGetter = BabeDataGetter<Runtime>;
type WeightInfo = pallet_randomness::weights::Weights<Runtime>;
}

0 comments on commit 81d899e

Please sign in to comment.