Skip to content
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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions substrate/frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ pub mod pallet {
ItemFailed { error: DispatchError },
/// A call was dispatched.
DispatchedAs { result: DispatchResult },
/// one or both if_else calls failed.
IfElseFailure { which: String, error: DispatchError },
rainbow-promise marked this conversation as resolved.
Show resolved Hide resolved
/// if_else completed.
IfElseCompleted,
}

// Align the call size to 1KB. As we are currently compiling the runtime for native/wasm
Expand Down Expand Up @@ -454,6 +458,53 @@ pub mod pallet {
let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into());
res.map(|_| ()).map_err(|e| e.error)
}

#[pallet::call_index(6)]
rainbow-promise marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::weight(10_000)]
pub fn if_else(
origin: OriginFor<T>,
main: <T as Config>::RuntimeCall,
fallback: <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();
let calls: Vec<<T as Config>::RuntimeCall> = vec![main.clone(), fallback.clone()];
rainbow-promise marked this conversation as resolved.
Show resolved Hide resolved

// Track the weights
let mut weight = Weight::zero();
rainbow-promise marked this conversation as resolved.
Show resolved Hide resolved
for (index, call) in calls.into_iter().enumerate() {
rainbow-promise marked this conversation as resolved.
Show resolved Hide resolved
let info = call.get_dispatch_info();
// If origin is root, don't apply any dispatch filters; root can call anything.
let result = if is_root {
call.dispatch_bypass_filter(origin.clone())
} else {
call.dispatch(origin.clone())
};
// Add weight of this call.
weight = weight.saturating_add(extract_actual_weight(&result, &info));
if let Err(e) = result {
let which = if index == 0 { "main".to_string() } else { "both calls have failed".to_string() }; // Use descriptive names
Self::deposit_event(Event::IfElseFailure {
which,
error: e.error,

});

// Take the weight of this function into account.
let base_weight = T::WeightInfo::batch(index.saturating_add(1) as u32);
// Return the actual used weight + base_weight of this call.
return Ok(Some(base_weight.saturating_add(weight)).into())
}
Self::deposit_event(Event::ItemCompleted);
}
Self::deposit_event(Event::IfElseCompleted);
let base_weight = T::WeightInfo::batch(2u32);
Ok(Some(base_weight.saturating_add(weight)).into())
}
}

impl<T: Config> Pallet<T> {
Expand Down