3

Hey Substrate Community,

is there a way to annotate a extrinsic call function with the Rust cfg condition compile execution?

#[cfg_attr(feature = "with-global-disputes", pallet::weight(T::WeightInfo::start_global_dispute(CacheSize::get())))]
#[cfg_attr(feature = "with-global-disputes", transactional)]
#[cfg(feature = "with-global-disputes")]
pub fn start_global_dispute(origin: OriginFor<T>, #[pallet::compact] market_id: MarketIdOf<T>) -> DispatchResultWithPostInfo {
   ...
}

This produces an error:

  error: Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]`
     --> /Users/tesst/dev/random/test-project.rs
      |
  963 |         pub fn start_global_dispute(origin: OriginFor<T>, #[pallet::compact] market_id: MarketIdOf<T>) -> DispatchResultWithPostInfo {
3
  • Based on a bit of experimentation, I don't believe that this is currently possible. Perhaps one of the FRAME experts can weigh in.
    – rob
    Sep 23, 2022 at 20:32
  • if you have the cfg statement as the first thing then the other two might not need to be cfg_attr ? While this might not yet be possible cfg is supported in the constuct_runtime macro now so you can now add in pallets via features.
    – Squirrel
    Oct 5, 2022 at 14:37
  • That would be useful in this case: substrate.stackexchange.com/questions/8215/… with a #[cfg(feature = "runtime-benchmarks")] Apr 25 at 15:04

2 Answers 2

3

This is not currently possible, and something I might not encourage anyway due to the intended deterministic nature of the runtime.

You can see here we have had a similar need, and there is note that this is not handled yet:

https://github.com/paritytech/substrate/blob/9476d2157a0d266677dee144a8a7bc44a6b821e1/frame/system/src/lib.rs#L371

If this is a feature you think is needed, please open an issue on Substrate, and describe how you would like to use this feature.

2
  • I think it's not so critical. It's just a nice to have. In Zeitgeist we have the main-net and test-net and the runtimes are separated. There is the prediction-markets pallet, which is used by both runtimes. Now, we'd like to include a specific feature, which affects the prediction-market pallet. But this should only be allowed for the test-net, but since both runtimes use the same code, we thought about using Rust features. Issue: github.com/paritytech/substrate/issues/12350
    – Chralt
    Sep 26, 2022 at 7:34
  • The link to lib.rs#L371 is not a permalink. Here is one for the same file at the time when you posted this answer: github.com/paritytech/substrate/blob/… (I updated the answer) Apr 25 at 15:00
2

You can use a config variable like EnableFeatureX and then require that to be true in the Extrinsic.
This should be a safer way of archiving what you want since you are not reliant on rust features, which must be handled carefully to fulfill their objective.
The extrinsic ordering also stays the same in this approach, allowing you to re-use metadata definitions.

Your Answer

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

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