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 2 commits
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
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