3

I was using substrate-node-template, and created some benchmarking functions.

I was able to use bounded_vec macro in tests.rs but, can't able to use it in the benchmarking.rs file

my code

#![cfg(feature = "runtime-benchmarks")]
use super::*;

#[allow(unused)]
use crate::Pallet as Template;
use frame_benchmarking::v2::*;
use frame_support::bounded_vec;
use frame_system::RawOrigin;

#[benchmarks]
mod benchmarks {
    use super::*;

    #[benchmark]
    fn store() {
        let caller: T::AccountId = whitelisted_caller();
        #[extrinsic_call]
        store(
            RawOrigin::Signed(caller),
            b"1".to_vec().try_into().unwrap(),
            b"1".to_vec().try_into().unwrap(),
            b"1".to_vec().try_into().unwrap(),
            bounded_vec![Types{ key: b"1".to_vec().try_into().unwrap(), is_accepted: false }],
        );
    }
    impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);
}

it was working fine when trying to compile cargo t -p pallet-template -F runtime-benchmarks

but, it was throwing errors while trying to compile with cargo t -p node-template-runtime -F runtime-benchmarks

error

error: cannot determine resolution for the macro `bounded_vec`
   --> /mnt/c/Users/pavan/PROJECTS/substrate-node-template/pallets/template/src/benchmarking.rs:24:4
    |
 24 |             bounded_vec![Types{ key: b"1".to_vec().try_into().unwrap(), is_accepted: false }],
3
  • Try importing it like this: use sp_runtime::bounded_vec;
    – Alex Bean
    Commented Oct 4, 2023 at 7:28
  • I have tried using it but again triggers another error error[E0432]: unresolved import sp_runtime`` --> pallets/template/src/benchmarking.rs:9:5 | 9 | use sp_runtime::bounded_vec; | ^^^^^^^^^^ help: a similar path exists: frame_support::sp_runtime`` `
    – Mojo
    Commented Oct 4, 2023 at 10:18
  • @AlexBean can you pls help me out
    – Mojo
    Commented Oct 4, 2023 at 13:27

2 Answers 2

2

The bounded_vec macro is only available in std context - and therefore not in benchmarks.
You can replace it with vec![ ... ].try_into().unwrap().
It was done this way since it hides an unwrap, which could cause a panic in your runtime code. So it is hidden behind the std feature.

1

Not sure why the bounded_vec import is not working, see an example in the Substrate repo.

However I am not sure you are doing the conversion to bounded_vec propertly anyway as you can't just do a cast from a Vec into a BoundedVec.

See this way of doing it: How to create a bounded vec from a normal vec?. In your case it will look something like that:

use frame_support::BoundedVec;

let your_bounded_vec = BoundedVec::<u8, ConstU32<5>>::try_from(your_unbounded_vec);

See another example in this previous StackExchange answer: frame_benchmarking convert Vec to BoundedVec

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.