Skip to content

Commit

Permalink
feat(fc-pallet-pass): first benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
pandres95 committed Sep 27, 2024
1 parent 9077ddf commit 4b9dace
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
44 changes: 38 additions & 6 deletions pallets/pass/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,56 @@
use super::*;
use crate::Pallet;
use frame_benchmarking::v2::*;
use sp_runtime::traits::Hash;

fn assert_last_event<T: Config<I>, I: 'static>(generic_event: <T as Config<I>>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
type RuntimeEventFor<T, I> = <T as Config<I>>::RuntimeEvent;

fn assert_has_event<T: Config<I>, I: 'static>(generic_event: RuntimeEventFor<T, I>) {
frame_system::Pallet::<T>::assert_has_event(generic_event.into());
}

fn setup_signers<T: frame_system::Config>() -> (T::AccountId, T::AccountId) {
(
frame_benchmarking::account("signer", 0, 0),
frame_benchmarking::account("signer", 1, 0),
)
}

#[benchmarks]
fn hash<T: frame_system::Config>(b: &[u8]) -> HashedUserId
where
T::Hash: Into<HashedUserId>,
{
T::Hashing::hash(b).into()
}

#[instance_benchmarks(
where
T: frame_system::Config + crate::Config<I>,
T::Hash: Into<HashedUserId>,
RuntimeEventFor<T, I>: From<frame_system::Event<T>>,
)]

Check warning on line 33 in pallets/pass/src/benchmarking.rs

View workflow job for this annotation

GitHub Actions / clippy

bound is defined in more than one place

warning: bound is defined in more than one place --> pallets/pass/src/benchmarking.rs:28:1 | 28 | / #[instance_benchmarks( 29 | | where 30 | | T: frame_system::Config + crate::Config<I>, | | ^ 31 | | T::Hash: Into<HashedUserId>, 32 | | RuntimeEventFor<T, I>: From<frame_system::Event<T>>, 33 | | )] | |__^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_bound_locations = note: `#[warn(clippy::multiple_bound_locations)]` on by default = note: this warning originates in the attribute macro `instance_benchmarks` (in Nightly builds, run with -Z macro-backtrace for more info)
mod benchmarks {
use super::*;

#[benchmark]
pub fn register() {
pub fn register() -> Result<(), BenchmarkError> {
// Setup code
let (one, _) = setup_signers::<T>();
let user_id = hash::<T>(&*b"my-account");

Check warning on line 41 in pallets/pass/src/benchmarking.rs

View workflow job for this annotation

GitHub Actions / clippy

deref on an immutable reference

warning: deref on an immutable reference --> pallets/pass/src/benchmarking.rs:41:33 | 41 | let user_id = hash::<T>(&*b"my-account"); | ^^^^^^^^^^^^^^^ help: if you would like to reborrow, try removing `&*`: `b"my-account"` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_deref_ref = note: `#[warn(clippy::borrow_deref_ref)]` on by default
let account_id = Pallet::<T, I>::account_id_for(user_id)?;
let device_id = [0u8; 32];

#[extrinsic_call]
_(RawOrigin::Root);
_(
RawOrigin::Signed(one),
user_id,
T::BenchmarkHelper::device_attestation(device_id),
);

// Verification code
assert_last_event::<T>(Event::Success.into());
assert_has_event::<T, I>(Event::Registered { who: account_id }.into());

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
Expand Down
3 changes: 3 additions & 0 deletions pallets/pass/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub mod pallet {
type MaxSessionDuration: Get<BlockNumberFor<Self>>;

type RegisterOrigin: EnsureOriginWithArg<Self::RuntimeOrigin, HashedUserId>;

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: BenchmarkHelper<Self, I>;
}

#[pallet::pallet]

Check warning on line 67 in pallets/pass/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

using `map_err` over `inspect_err`

warning: using `map_err` over `inspect_err` --> pallets/pass/src/lib.rs:67:15 | 67 | #[pallet::pallet] | ^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_inspect = note: `#[warn(clippy::manual_inspect)]` on by default help: try | 67 - #[pallet::pallet] 67 + #[pallet::&inspect_err] |
Expand Down
24 changes: 23 additions & 1 deletion pallets/pass/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Test environment for pallet pass.
use crate::{self as pallet_pass, Config};
use crate::{self as pallet_pass, Config, CredentialOf, DeviceAttestationOf};
pub use authenticators::*;
use codec::{Decode, Encode, MaxEncodedLen};
use fc_traits_authn::{composite_authenticators, util::AuthorityFromPalletId, Challenger};
Expand Down Expand Up @@ -100,6 +100,28 @@ impl Config for Test {
type PalletId = PassPalletId;
type PalletsOrigin = OriginCaller;
type MaxSessionDuration = ConstU64<10>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = BenchmarkHelper;
}

#[cfg(feature = "runtime-benchmarks")]
pub struct BenchmarkHelper;

#[cfg(feature = "runtime-benchmarks")]
impl pallet_pass::BenchmarkHelper<Test> for BenchmarkHelper {
fn device_attestation(device_id: DeviceId) -> DeviceAttestationOf<Test, ()> {
PassDeviceAttestation::AuthenticatorA(authenticator_a::DeviceAttestation {
device_id,
challenge: AuthenticatorA::generate(&()),
})
}

fn credential(user_id: HashedUserId) -> CredentialOf<Test, ()> {
PassCredential::AuthenticatorA(authenticator_a::Credential {
user_id,
challenge: AuthenticatorA::generate(&()),
})
}
}

pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
13 changes: 13 additions & 0 deletions pallets/pass/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
use crate::Config;
use fc_traits_authn::HashedUserId;
use sp_runtime::traits::StaticLookup;

// pub type HashedUserId<T> = <T as frame_system::Config>::Hash;
pub type ContextOf<T, I> =
<<<T as Config<I>>::Authenticator as fc_traits_authn::Authenticator>::Challenger as fc_traits_authn::Challenger>::Context;
pub type DeviceOf<T, I> =
<<T as Config<I>>::Authenticator as fc_traits_authn::Authenticator>::Device;
pub type CredentialOf<T, I> = <DeviceOf<T, I> as fc_traits_authn::UserAuthenticator>::Credential;
pub type DeviceAttestationOf<T, I> =
<<T as Config<I>>::Authenticator as fc_traits_authn::Authenticator>::DeviceAttestation;
pub type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;

#[cfg(feature = "runtime-benchmarks")]
pub trait BenchmarkHelper<T, I = ()>
where
T: Config<I>,
I: 'static,
{
fn device_attestation(device_id: fc_traits_authn::DeviceId) -> DeviceAttestationOf<T, I>;
fn credential(user_id: HashedUserId) -> CredentialOf<T, I>;
}

0 comments on commit 4b9dace

Please sign in to comment.