2

I have an extrinsic in one of my pallets that looks validates a signature over a message, which is separate from the actual extrinsic being submitted.

The code can be simplified to look like the below

#[pallet::call_index(0)]
pub fn do_something(
  origin: OriginFor<T>,
  msg: SignableMessage,
  user_signer: T::AccountId,
  user_sig: MultiSignature
) -> DispatchResult {
  /* -- snip -- */
  let signer_bytes = Self::account_to_bytes(&user_signer)?;
  let valid = msg.using_encoded(|encoded| {
    let msg = [DOMAIN_SEP_TAG, encoded].concat();
    user_sig.verify(msg.as_ref(), &signer_bytes.into())
  });
  ensure!(valid, <Error<T>>::InvalidSignature);
  /* -- snip -- */
}

The code works fine, but benchmarking it is challenging, because benchmarking setup runs in WASM and as such cannot create the signature. The only way I can think of to get around this is basically to create a signature for all messages I benchmark ahead of time, and write their bytes into a static map, then read that out when doing benchmarks.

Is there a better way to do this? Has anyone else run into the same problem?

For this particular usecase, it does not make sense to use the built-in proxy pallets, or something on-chain.

1 Answer 1

1

I think there are a number of options which may allow something like what you've described.

  1. Include a function feature-flagged for benchmarks which can generate the signatures. Something like this is done here: https://github.com/paritytech/polkadot/blob/2fe827411438afb323bc8c4334a1d5ec60abfec4/runtime/common/src/crowdloan/mod.rs#L847C9-L855
  2. Include a test function which can generate signatures, which you'll then include in your benchmarks plainly. There's something like this here: https://github.com/paritytech/substrate/blob/5cdb054049980eecb449c36a7ccfe4990329c494/frame/babe/src/benchmarking.rs#L80

If I had to guess, option #1 is fewer steps, but I haven't tested an approach like that.

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.