6

I have an Extrinsic which modifies some storage variables and then returns either Err or Ok.
Will it revert the changes when I return Err?
How can I make sure that it only commits to changes when I return Ok?

2 Answers 2

5

Update: All storage changes are now reverted when an error from a call is returned. This makes writing code much easier.

Original answer:
Currently you need to make sure to never return an error after you modified storage.
In your case you need to do all checks that can error before modifying something.
Returning Err after a modification will most likely put your pallet in an undefined state.

This is not the most ergonomic and will gladly change in the future.
An annotation à la #[storage_layer] or something similar will be added.
This would allow to write code more easily as it would revert all changes upon error.

Will update this post once its done.

2
  • 1
    In the case that something that could return an Err is done after modifying state , even after doing preliminary checks is it okay to handle that with a ".expect()"? I know the runtime shouldn't panic, but the behaviour shouldn't occur since the checks have already been made, yet I'm a bit paranoid around the Result type I get back at that point.
    – islami00
    Feb 18, 2022 at 2:33
  • @islami00 will be answered here substrate.stackexchange.com/questions/317/when-to-use-expect Feb 18, 2022 at 11:08
1

Extrinsic marked as #[transactional] will get discarded if it returns Err.

But there're some issues about transactional annotation, see paritytech/substrate#10806

1
  • Yes right! The whole issue with discarded state will be solved when #[transactional] becomes the default for Substrate. Then it will be as simple as in Solidity. Feb 21, 2022 at 11:10

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.