3

I am building a pallet which uses assets in a loosely-coupled manner. In the pallet's Config, there's an associated type Assets with Inspect and Transfer bounds.

Now, when writing benchmarks, I need some way to create and mint assets. Otherwise, my pallet won't be able to perform any transfers. I know that assets pallet implements both Create and Mutate traits, but as far as I understand, I cannot use any methods from these traits in my pallet's benchmarks, as long as they're not explicitly added to the Config. And I don't want to do that, because it seems really bad design to add trait bounds for the sake of benchmarks only.

So, how could I create and mint assets for my benchmarks??

3 Answers 3

3

This could be a good use for feature flags since you have the following requirement:
Depend on a trait only in benchmarking.

You can see this multiple times already being used for example in the config of the Uniques pallet:

#[cfg(feature = "runtime-benchmarks")]
/// A set of helper functions for benchmarking.
type Helper: BenchmarkHelper<Self::CollectionId, Self::ItemId>;

The BenchmarkHelper trait then provides everything you need for benchmarking.

In the runtime config this can be easily configured via:

#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
2

One way of achieving this would be a setup similar to pallet-xcm-benchmarks.

Here you can find the benchmarking pallet's Config that contains a Config of whatever pallet should be tested. This approach gives you freedom to implement any necessary helpers you might need. You can find some sample benchmarking code here. Of course it doesn't necessarily need to be a separate pallet.

EDIT: Oliver's answer might actually be an easier solution for this kind of thing.

2

Turns out, there's a really simple solution to this problem. The benchmarking framework allows to introduce additional trait bounds in a where_clause block. So, in order to be able to create and mint assets, I just need to add the following code:

benchmarks! {
    where_clause {
        where
            T::Assets: Create<T::AssetId> + Mutate<T::AssetId>
    }

    ...
}

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.