Make Dispatchable return the actual weight consumed (#5458)

* Make Dispatchable return the actual weight consumed

Add PostInfo associated type to Dispatchable and have frame implement
Dispatchable { type PostInfo = PostDispatchInfo } where PostDispatchInfo
contains the actual weight consumed.

* Fix whitespace issues in docs
This commit is contained in:
Alexander Theißen
2020-04-03 16:45:30 +02:00
committed by GitHub
parent 14505471ee
commit 8f819f4ba6
11 changed files with 202 additions and 23 deletions
@@ -79,7 +79,7 @@ where
(None, pre)
};
let res = self.function.dispatch(Origin::from(maybe_who));
Extra::post_dispatch(pre, info.clone(), len);
Ok(res.map_err(Into::into))
Extra::post_dispatch(pre, info, len);
Ok(res.map(|_| ()).map_err(|e| e.error))
}
}
+49 -2
View File
@@ -353,10 +353,15 @@ impl From<DispatchError> for DispatchOutcome {
}
}
/// Result of a module function call; either nothing (functions are only called for "side effects")
/// or an error message.
/// This is the legacy return type of `Dispatchable`. It is still exposed for compatibilty
/// reasons. The new return type is `DispatchResultWithInfo`.
/// FRAME runtimes should use frame_support::dispatch::DispatchResult
pub type DispatchResult = sp_std::result::Result<(), DispatchError>;
/// Return type of a `Dispatchable` which contains the `DispatchResult` and additional information
/// about the `Dispatchable` that is only known post dispatch.
pub type DispatchResultWithInfo<T> = sp_std::result::Result<T, DispatchErrorWithPostInfo<T>>;
/// Reason why a dispatch call failed
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize))]
@@ -379,6 +384,18 @@ pub enum DispatchError {
},
}
/// Result of a `Dispatchable` which contains the `DispatchResult` and additional information
/// about the `Dispatchable` that is only known post dispatch.
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, RuntimeDebug)]
pub struct DispatchErrorWithPostInfo<Info> where
Info: Eq + PartialEq + Clone + Copy + Encode + Decode + traits::Printable
{
/// Addditional information about the `Dispatchable` which is only known post dispatch.
pub post_info: Info,
/// The actual `DispatchResult` indicating whether the dispatch was succesfull.
pub error: DispatchError,
}
impl DispatchError {
/// Return the same error but without the attached message.
pub fn stripped(self) -> Self {
@@ -390,6 +407,18 @@ impl DispatchError {
}
}
impl<T, E> From<E> for DispatchErrorWithPostInfo<T> where
T: Eq + PartialEq + Clone + Copy + Encode + Decode + traits::Printable + Default,
E: Into<DispatchError>
{
fn from(error: E) -> Self {
Self {
post_info: Default::default(),
error: error.into(),
}
}
}
impl From<crate::traits::LookupError> for DispatchError {
fn from(_: crate::traits::LookupError) -> Self {
Self::CannotLookup
@@ -419,6 +448,14 @@ impl From<DispatchError> for &'static str {
}
}
impl<T> From<DispatchErrorWithPostInfo<T>> for &'static str where
T: Eq + PartialEq + Clone + Copy + Encode + Decode + traits::Printable
{
fn from(err: DispatchErrorWithPostInfo<T>) -> &'static str {
err.error.into()
}
}
impl traits::Printable for DispatchError {
fn print(&self) {
"DispatchError".print();
@@ -437,6 +474,16 @@ impl traits::Printable for DispatchError {
}
}
impl<T> traits::Printable for DispatchErrorWithPostInfo<T> where
T: Eq + PartialEq + Clone + Copy + Encode + Decode + traits::Printable
{
fn print(&self) {
self.error.print();
"PostInfo: ".print();
self.post_info.print();
}
}
/// This type specifies the outcome of dispatching a call to a module.
///
/// In case of failure an error specific to the module is returned.
+1 -1
View File
@@ -379,6 +379,6 @@ impl<Origin, Call, Extra, Info> Applyable for TestXt<Call, Extra> where
None
};
Ok(self.call.dispatch(maybe_who.into()).map_err(Into::into))
Ok(self.call.dispatch(maybe_who.into()).map(|_| ()).map_err(|e| e.error))
}
}
+4 -1
View File
@@ -642,8 +642,11 @@ pub trait Dispatchable {
type Origin;
/// ...
type Trait;
/// Additional information that is returned by `dispatch`. Can be used to supply the caller
/// with information about a `Dispatchable` that is ownly known post dispatch.
type PostInfo: Eq + PartialEq + Clone + Copy + Encode + Decode + Printable;
/// Actually dispatch this call and result the result of it.
fn dispatch(self, origin: Self::Origin) -> crate::DispatchResult;
fn dispatch(self, origin: Self::Origin) -> crate::DispatchResultWithInfo<Self::PostInfo>;
}
/// Means by which a transaction may be extended. This type embodies both the data and the logic