If we have the storage map DummyStorageMap in pallet-example

And we read its value under the key key by calling DummyStorageMap::<T>::get(key) in the runtime chain extension (https://paritytech.github.io/substrate/master/pallet_contracts/chain_extension/trait.ChainExtension.html#) , should we be charging weight (https://paritytech.github.io/substrate/master/pallet_contracts/chain_extension/struct.Environment.html#method.charge_weight)?

I know we are obviously supposed to charge weight if we execute an extrinsic of a pallet in the runtime chain extension, but what about if we read a storage map / storage value?


@bkchr :)


1 Answer 1


If you are not doing complex calculations in your runtime, most of the time the storage reading/writing should dominate your weight. With 2D weights which also include the increase of the proof size, reading storage will be more important to your weight calculation.

So, yes you need to charge weight for only reading.

  • so if we read a query a single key in a StorageMap - are we supposed to charge this weight manually by doing env.charge_weight(T::DbWeight::get().reads(1))?? Thanks
    – aang114
    Commented Feb 8, 2023 at 11:38
  • Furthermore, I noticed that it says here that we can benchmark our chain extension functions on Substrate: "In order to be able to charge the correct weight for the functions defined by a chain extension benchmarks must be written, too" (crates.parity.io/pallet_contracts/chain_extension/… and substrate.stackexchange.com/a/1910/684)? Is this true? I thought we could only benchmark pallets calls in Substrate currently?
    – aang114
    Commented Feb 8, 2023 at 11:39
  • You can basically benchmark any kind of code.
    – bkchr
    Commented Feb 9, 2023 at 11:27

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.