diff --git a/lib/polka-storage-proofs/src/groth16/mod.rs b/lib/polka-storage-proofs/src/groth16/mod.rs index 9b676c4b7..9255c0358 100644 --- a/lib/polka-storage-proofs/src/groth16/mod.rs +++ b/lib/polka-storage-proofs/src/groth16/mod.rs @@ -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. @@ -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 diff --git a/lib/polka-storage-proofs/src/groth16/substrate.rs b/lib/polka-storage-proofs/src/groth16/substrate.rs index 9b0925974..d0e2fc984 100644 --- a/lib/polka-storage-proofs/src/groth16/substrate.rs +++ b/lib/polka-storage-proofs/src/groth16/substrate.rs @@ -68,6 +68,15 @@ where } } +impl codec::MaxEncodedLen for VerifyingKey +where + E: Engine, +{ + fn max_encoded_len() -> usize { + VERIFYINGKEY_MAX_BYTES + } +} + impl ::scale_info::TypeInfo for VerifyingKey { type Identity = Self; diff --git a/pallets/market/src/mock.rs b/pallets/market/src/mock.rs index edd2a096b..00dc12f14 100644 --- a/pallets/market/src/mock.rs +++ b/pallets/market/src/mock.rs @@ -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 = ::AccountId; diff --git a/pallets/proofs/Cargo.toml b/pallets/proofs/Cargo.toml index 21b273ad2..046bb2fea 100644 --- a/pallets/proofs/Cargo.toml +++ b/pallets/proofs/Cargo.toml @@ -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 } @@ -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 = [ diff --git a/pallets/proofs/src/benchmarking.rs b/pallets/proofs/src/benchmarking.rs new file mode 100644 index 000000000..0af2e1f22 --- /dev/null +++ b/pallets/proofs/src/benchmarking.rs @@ -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::::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::::get().is_some()); + } + + #[benchmark] + fn set_post_verifying_key() { + let mut rng = XorShiftRng::from_seed(TEST_SEED); + let vkey = VerifyingKey::::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::::get().is_some()); + } + + impl_benchmark_test_suite! { + ProofsPallet, + crate::mock::new_test_ext(), + crate::mock::Test, + } +} diff --git a/pallets/proofs/src/lib.rs b/pallets/proofs/src/lib.rs index 1a721df28..238fe188d 100644 --- a/pallets/proofs/src/lib.rs +++ b/pallets/proofs/src/lib.rs @@ -13,6 +13,7 @@ mod fr32; mod graphs; mod porep; mod post; +pub mod weights; #[cfg(test)] mod mock; @@ -20,7 +21,10 @@ 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"; @@ -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> + IsType<::RuntimeEvent>; + type WeightInfo: WeightInfo; } #[pallet::pallet] @@ -85,6 +91,8 @@ pub mod pallet { #[pallet::call] impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight((T::WeightInfo::set_porep_verifying_key(), DispatchClass::Operational))] pub fn set_porep_verifying_key( origin: OriginFor, verifying_key: crate::Vec, @@ -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, verifying_key: crate::Vec, diff --git a/pallets/proofs/src/mock.rs b/pallets/proofs/src/mock.rs index 4ffadb801..f85998557 100644 --- a/pallets/proofs/src/mock.rs +++ b/pallets/proofs/src/mock.rs @@ -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. diff --git a/pallets/proofs/src/weights.rs b/pallets/proofs/src/weights.rs new file mode 100644 index 000000000..6c94de3d9 --- /dev/null +++ b/pallets/proofs/src/weights.rs @@ -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(PhantomData); +impl WeightInfo for Weights { + /// 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)) + } +} diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 47299e3ea..96a090245 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -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", diff --git a/runtime/src/benchmarks.rs b/runtime/src/benchmarks.rs index c2e91fdd7..258786e29 100644 --- a/runtime/src/benchmarks.rs +++ b/runtime/src/benchmarks.rs @@ -35,4 +35,5 @@ frame_benchmarking::define_benchmarks!( [cumulus_pallet_xcmp_queue, XcmpQueue] // Our crates [pallet_randomness, Randomness] + [pallet_proofs, Proofs] ); diff --git a/runtime/src/configs/mod.rs b/runtime/src/configs/mod.rs index e6c811cba..712424055 100644 --- a/runtime/src/configs/mod.rs +++ b/runtime/src/configs/mod.rs @@ -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; +} + +/// Config for our randomness pallet +impl pallet_randomness::Config for Runtime { + type AuthorVrfGetter = BabeDataGetter; + type WeightInfo = pallet_randomness::weights::Weights; } #[cfg(feature = "testnet")] @@ -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; - type WeightInfo = pallet_randomness::weights::Weights; -}