1

Suppose I have the following cross pallet function call

// Pallet A : method that sets a storage variable
pub fn try_set_value_in_pallet_a(value: u64) -> DispatchResult {
    ensure!(!value.is_zero(), Error::<T>::CannotSetZeroValue);
    ValueA::<T>::set(value);
    Ok(())
}

// Pallet B : extrinsic leveraging functionality in pallet A
#[weights(...)]
pub fn foo(value_a: u64, value_b: u64) -> DispatchResult {
    ensure!(!b.is_zero(), Error::<T>::CannotSetZeroValue);
    ValueB::<T>::set(value_b);
    PalletA::<T>::try_set_value_in_pallet_a(value_a)?;
}

In this case if the following behavior is desired: Either foo in Pallet B succeeds or it leaves PalletA and PalletB states invariate. Where should I put the #[transactional] macro? Should I put it above foo only?

1 Answer 1

2

Putting it above fn foo only should be fine in this case. Basically the transactional macro needs to wrap all logic that should be reverted if it fails.

Since the logic in pallet A is inside of fn foo, you only need a transactional layer around fn foo, and that will also take care of any reverting of storage changes in pallet A from that call aswell.

5
  • If both fn foo and fn try_set_value_in_pallet_a are annotated with transactional, will it behaves as cascade mode ? Commented Jul 11, 2022 at 3:48
  • Each #[transactional] will wrap the logic inside of that function in its own transactional layer. I don't know if cascade is the right term, but sounds like yes.
    – Shawn Tabrizi
    Commented Jul 11, 2022 at 7:51
  • In traditional transactional architecture, there is parent-children relation between transactions. So if children transaction rolls back, so do the parents. Does substrate's transactional behave in the same way ? Commented Jul 11, 2022 at 8:26
  • The #[transactional] macro rolls back whenever an error is returned by the function, so as long as you are propagating the error up, yes, each layer will revert. However, you CAN swallow the error, and if you do and return Ok, then that layer will not revert. Check the code, it's pretty simple.
    – Shawn Tabrizi
    Commented Jul 11, 2022 at 9:15
  • Thank you, Shawn~ Commented Jul 11, 2022 at 9:28

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.