2

I have some code that benchmarks one of my extrinsic.

benchmarks! {
    create_supersig {
        let z in 0 .. 10;

        let alice: T::AccountId = get_account::<T>("ALICE");
        let bob: T::AccountId = get_account::<T>("BOB");
        let charlie: T::AccountId = get_account::<T>("CHARLIE");
        let val: BalanceOf<T> = 4_000_000_000u32.into();
        T::Currency::make_free_balance_be(&alice, val.saturating_mul(4_000_000_000u32.into()));

        let mut vec_members = vec!{(alice.clone(), Role::Standard), (bob, Role::Standard), (charlie, Role::Standard)};
        let oui = "oui";
        for i in 0 .. z {
            let acc = benchmark_account(oui, i, 0);
            vec_members.push((acc, Role::Standard));
        }
        let members: BoundedVec<_, _> = vec_members.try_into().unwrap();
    }: _(RawOrigin::Signed(alice.clone()), members.clone())
    verify {
        assert_eq!(Pallet::<T>::nonce_supersig(), 1);
    }
}

members is a bounded Vec of the members of the organization I'm creating. The code (and other variations I tried) panic when I try to build the BoundedVec, at:

        let members: BoundedVec<_, _> = vec_members.try_into().unwrap();

If I remove the loop filling the Vec, which means that the bounded Vec will always be the same length (aka the same type), it doesn't panic anymore.

Is it because:

  • I'm not using BoundedVec the right way
  • I cannot use BoundedVec like this in benchmarks (probably due to the work done by the macro)

What should I do?

  1. change the type expected by my extrinsic from BoundedVec to Vec ?
  2. find a way to specify the size/type of my BoundedVec in the benchmarks

It's defined in my pallet config:

    #[pallet::config]
    pub trait Config: frame_system::Config {
            ...
        /// The maximum number of account that can added or removed in a single call
        #[pallet::constant]
        type MaxAccountsPerTransaction: Get<u32>;
    }

And use in the extrinsic signature:

        #[pallet::weight(T::WeightInfo::create_supersig(members.len() as u32))]
        pub fn create_supersig(
            origin: OriginFor<T>,
            members: BoundedVec<(T::AccountId, Role), T::MaxAccountsPerTransaction>,
        ) -> DispatchResult {

1 Answer 1

3

You could use BoundedVec anywhere.

What is the value of MaxAccountsPerTransaction you used in the mock runtime?

If z + 3 > MaxAccountsPerTransaction, then you got this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.