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 22 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b83a8e8
extrinsic outline
rainbow-promise Oct 31, 2024
1b01dc9
new: outline utility_if_else
rainbow-promise Nov 1, 2024
430a791
do not clone calls
rainbow-promise Nov 6, 2024
2caf95e
compiler needs to know the size, put in Box
rainbow-promise Nov 6, 2024
4183c6b
siimple test cases
rainbow-promise Nov 6, 2024
e70a607
if_else::benchmarking
rainbow-promise Nov 7, 2024
a93b142
add weights substrate
rainbow-promise Nov 7, 2024
cc79306
add weight to all runtimes
rainbow-promise Nov 7, 2024
007d1c3
add prdoc
rainbow-promise Nov 12, 2024
7f2c600
Merge branch 'master' into if-else-utility
rainbow-promise Nov 12, 2024
f2bd937
check CI event not emitted
rainbow-promise Nov 13, 2024
ad9e68b
DispatchErrorWithPostInfo
rainbow-promise Nov 14, 2024
5ef6752
conditional Dispatch class
rainbow-promise Nov 15, 2024
1d01444
nit DispatchClass
rainbow-promise Nov 15, 2024
975ac65
all calls fail return just error, events don't fire in the same block
rainbow-promise Nov 21, 2024
aa1fdc7
Merge branch 'master' into if-else-utility
rainbow-promise Nov 21, 2024
caf067b
include fallback error in DispatchErrorWithPostInfo
rainbow-promise Nov 22, 2024
cd7d48f
Merge branch 'master' into if-else-utility
rainbow-promise Nov 27, 2024
ca8c072
nested if_else test
rainbow-promise Nov 27, 2024
898b5e1
Merge branch 'master' into if-else-utility
rainbow-promise Nov 29, 2024
be2f1c2
Merge branch 'master' into if-else-utility
gui1117 Dec 3, 2024
f65f270
Merge branch 'master' into if-else-utility
rainbow-promise Dec 4, 2024
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