-
Notifications
You must be signed in to change notification settings - Fork 707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pallet-utility: if_else
#6321
base: master
Are you sure you want to change the base?
pallet-utility: if_else
#6321
Changes from 16 commits
b83a8e8
1b01dc9
430a791
2caf95e
4183c6b
e70a607
a93b142
cc79306
007d1c3
7f2c600
f2bd937
ad9e68b
5ef6752
1d01444
975ac65
aa1fdc7
caf067b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
title: Utility call fallback | ||
|
||
doc: | ||
- audience: Runtime Dev | ||
description: This PR adds the `if_else` call to `pallet-utility` | ||
enabling an error fallback when the main call is unsuccessful. | ||
|
||
crates: | ||
- name: asset-hub-rococo-runtime | ||
bump: major | ||
- name: asset-hub-westend-runtime | ||
bump: major | ||
- name: bridge-hub-rococo-runtime | ||
bump: major | ||
- name: bridge-hub-westend-runtime | ||
bump: major | ||
- name: collectives-westend-runtime | ||
bump: major | ||
- name: coretime-rococo-runtime | ||
bump: major | ||
- name: coretime-westend-runtime | ||
bump: major | ||
- name: people-rococo-runtime | ||
bump: major | ||
- name: people-westend-runtime | ||
bump: major | ||
- name: rococo-runtime | ||
bump: major | ||
- name: westend-runtime | ||
bump: major | ||
- name: pallet-utility | ||
bump: major | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,18 @@ mod benchmark { | |
assert_last_event::<T>(Event::BatchCompleted.into()); | ||
} | ||
|
||
#[benchmark] | ||
fn if_else() { | ||
let main_call = Box::new(frame_system::Call::remark { remark: vec![0] }.into()); | ||
let fallback_call = Box::new(frame_system::Call::remark { remark: vec![1] }.into()); | ||
let caller = whitelisted_caller(); | ||
|
||
#[extrinsic_call] | ||
_(RawOrigin::Signed(caller), main_call, fallback_call); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The err branch should be slightly more costly, but difference should be negligible so it is ok. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Thanks for the review There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use a root call, it will be failing, like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...
#[benchmark]
fn if_else() {
let main_call = Box::new(frame_system::Call::set_heap_pages { pages: 0u64 }.into());
let fallback_call = Box::new(frame_system::Call::remark { remark: vec![1] }.into());
let caller = whitelisted_caller();
#[extrinsic_call]
_(RawOrigin::Signed(caller), main_call, fallback_call);
assert_last_event::<T>(Event::IfElseFallbackCalled { main_error: sp_runtime::DispatchError::Module(sp_runtime::ModuleError { index: 0, error: [5, 0, 0, 0], message: None }) }.into());
} |
||
|
||
assert_last_event::<T>(Event::IfElseMainSuccess.into()); | ||
} | ||
|
||
impl_benchmark_test_suite! { | ||
Pallet, | ||
tests::new_test_ext(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,11 @@ extern crate alloc; | |
use alloc::{boxed::Box, vec::Vec}; | ||
use codec::{Decode, Encode}; | ||
use frame_support::{ | ||
dispatch::{extract_actual_weight, GetDispatchInfo, PostDispatchInfo}, | ||
dispatch::{ | ||
extract_actual_weight, | ||
DispatchClass::{Normal, Operational}, | ||
GetDispatchInfo, PostDispatchInfo, | ||
}, | ||
traits::{IsSubType, OriginTrait, UnfilteredDispatchable}, | ||
}; | ||
use sp_core::TypeId; | ||
|
@@ -120,6 +124,10 @@ pub mod pallet { | |
ItemFailed { error: DispatchError }, | ||
/// A call was dispatched. | ||
DispatchedAs { result: DispatchResult }, | ||
/// Main call was dispatched. | ||
IfElseMainSuccess, | ||
/// The fallback call was dispatched. | ||
IfElseFallbackCalled { main_error: DispatchError }, | ||
} | ||
|
||
// Align the call size to 1KB. As we are currently compiling the runtime for native/wasm | ||
|
@@ -159,6 +167,8 @@ pub mod pallet { | |
pub enum Error<T> { | ||
/// Too many calls batched. | ||
TooManyCalls, | ||
/// Main and fallback call failure. | ||
IfElseBothFailure, | ||
} | ||
|
||
#[pallet::call] | ||
|
@@ -454,6 +464,102 @@ pub mod pallet { | |
let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into()); | ||
res.map(|_| ()).map_err(|e| e.error) | ||
} | ||
|
||
/// Dispatch a fallback call in the event the main call fails to execute. | ||
/// May be called from any origin except `None`. | ||
/// | ||
/// This function first attempts to dispatch the `main` call. | ||
/// If the `main` call fails, the `fallback` is attemted. | ||
/// if the fallback is successfully dispatched, the weights of both calls | ||
/// are accumulated and an event containing the main call error is deposited. | ||
/// | ||
/// In the event of a fallback failure the whole call fails | ||
/// with the weights returned. | ||
/// | ||
/// - `main`: The main call to be dispatched. This is the primary action to execute. | ||
/// - `fallback`: The fallback call to be dispatched in case the `main` call fails. | ||
/// | ||
/// ## Dispatch Logic | ||
/// - If the origin is `root`, both the main and fallback calls are executed without | ||
/// applying any origin filters. | ||
/// - If the origin is not `root`, the origin filter is applied to both the `main` and | ||
/// `fallback` calls. | ||
/// | ||
/// ## Use Case | ||
/// - Some use cases might involve submitting a `batch` type call in either main, fallback | ||
/// or both. | ||
/// | ||
/// ## Weight | ||
/// The weight of this call is calculated as the sum of: | ||
/// `WeightInfo::if_else()` + `main` | | ||
/// `WeightInfo::if_else()` + `main` + `fallback`. | ||
#[pallet::call_index(6)] | ||
rainbow-promise marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[pallet::weight({ | ||
let main = main.get_dispatch_info(); | ||
let fallback = fallback.get_dispatch_info(); | ||
( | ||
T::WeightInfo::if_else() | ||
.saturating_add(main.call_weight) | ||
.saturating_add(fallback.call_weight), | ||
if main.class == Operational && fallback.class == Operational { Operational } else { Normal }, | ||
) | ||
})] | ||
pub fn if_else( | ||
origin: OriginFor<T>, | ||
main: Box<<T as Config>::RuntimeCall>, | ||
fallback: Box<<T as Config>::RuntimeCall>, | ||
) -> DispatchResultWithPostInfo { | ||
// Do not allow the `None` origin. | ||
if ensure_none(origin.clone()).is_ok() { | ||
return Err(BadOrigin.into()); | ||
} | ||
|
||
let is_root = ensure_root(origin.clone()).is_ok(); | ||
|
||
// Track the weights | ||
let mut weight = T::WeightInfo::if_else(); | ||
|
||
let info = main.get_dispatch_info(); | ||
|
||
// Execute the main call first | ||
let main_result = if is_root { | ||
main.dispatch_bypass_filter(origin.clone()) | ||
} else { | ||
main.dispatch(origin.clone()) | ||
}; | ||
|
||
// Add weight of the main call | ||
weight = weight.saturating_add(extract_actual_weight(&main_result, &info)); | ||
|
||
if let Err(main_error) = main_result { | ||
// If the main call failed, execute the fallback call | ||
let fallback_info = fallback.get_dispatch_info(); | ||
|
||
let fallback_result = if is_root { | ||
fallback.dispatch_bypass_filter(origin.clone()) | ||
} else { | ||
fallback.dispatch(origin) | ||
}; | ||
|
||
// Add weight of the fallback call | ||
weight = | ||
weight.saturating_add(extract_actual_weight(&fallback_result, &fallback_info)); | ||
|
||
if let Err(_fallback_error) = fallback_result { | ||
// Both calls have faild. | ||
return Err(sp_runtime::DispatchErrorWithPostInfo { | ||
error: Error::<T>::IfElseBothFailure.into(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not 100% sure about this as it mask the underlying error and makes debugging harder There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can alternatively return the fallback error here return Err(sp_runtime::DispatchErrorWithPostInfo {
error: _fallback_error.error,
post_info: Some(weight).into(),
}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah that will be better than not having anything |
||
post_info: Some(weight).into(), | ||
}) | ||
} | ||
// Fallback succeeded. | ||
Self::deposit_event(Event::IfElseFallbackCalled { main_error: main_error.error }); | ||
return Ok(Some(weight).into()); | ||
} | ||
// Main call succeeded. | ||
Self::deposit_event(Event::IfElseMainSuccess); | ||
Ok(Some(weight).into()) | ||
} | ||
} | ||
|
||
impl<T: Config> Pallet<T> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a breaking change so should be a minor bump?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes the call enum
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but the change is not in a breaking way? i.e. existing enum encode/decode as it was
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ggwpez noted it here