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 2 commits
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
74 changes: 74 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 { which: String },
}

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

#[pallet::call_index(7)]
#[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();

// Track the weights
let mut weight = Weight::zero();

// Execute the main call first
let main_result = if is_root {
main.clone().dispatch_bypass_filter(origin.clone())
} else {
main.clone().dispatch(origin.clone())
};

// Add weight of the main call
let info = main.get_dispatch_info();
weight = weight.saturating_add(extract_actual_weight(&main_result, &info));

if let Err(main_call_error) = main_result {
// Both calls have failed
Self::deposit_event(Event::IfElseFailure {
which: "Main call".to_string(),
error: main_call_error.error,
});

// If the main call failed, execute the fallback call
let fallback_result = if is_root {
fallback.clone().dispatch_bypass_filter(origin.clone())
} else {
fallback.clone().dispatch(origin.clone())
};

// Add weight of the fallback call
let fallback_info = fallback.get_dispatch_info();
weight = weight.saturating_add(extract_actual_weight(&fallback_result, &fallback_info));

if let Err(fallback_error) = fallback_result {
// Both calls have failed
Self::deposit_event(Event::IfElseFailure {
which: "Both calls have failed".to_string(),
error: fallback_error.error,
});

// Calculate base weight and return
let base_weight = T::WeightInfo::batch(2u32);
return Ok(Some(base_weight.saturating_add(weight)).into());
}

// Fallback succeeded.
Self::deposit_event(Event::IfElseCompleted {
which: "Fallback".to_string(),
});
}
// Main call succesed.
Self::deposit_event(Event::IfElseCompleted {
which: "Main".to_string()
});
Ok(Some(weight).into())
}
}

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