Allow PostDispatchInfo to disable fees (#6749)

* initial mock

* add test

* remove unneeded clone

* Update frame/support/src/weights.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* fix compile

* Update frame/support/src/weights.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update frame/sudo/src/lib.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

Co-authored-by: Alexander Theißen <alex.theissen@me.com>
Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Shawn Tabrizi
2020-07-29 14:30:10 +02:00
committed by GitHub
parent 6bfbb7c6f1
commit 6e6568167a
6 changed files with 127 additions and 11 deletions
+47 -1
View File
@@ -263,10 +263,13 @@ pub trait GetDispatchInfo {
}
/// Weight information that is only available post dispatch.
/// NOTE: This can only be used to reduce the weight or fee, not increase it.
#[derive(Clone, Copy, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode)]
pub struct PostDispatchInfo {
/// Actual weight consumed by a call or `None` which stands for the worst case static weight.
pub actual_weight: Option<Weight>,
/// Whether this transaction should pay fees when all is said and done.
pub pays_fee: Pays,
}
impl PostDispatchInfo {
@@ -283,6 +286,20 @@ impl PostDispatchInfo {
info.weight
}
}
/// Determine if user should actually pay fees at the end of the dispatch.
pub fn pays_fee(&self, info: &DispatchInfo) -> Pays {
// If they originally were not paying fees, or the post dispatch info
// says they should not pay fees, then they don't pay fees.
// This is because the pre dispatch information must contain the
// worst case for weight and fees paid.
if info.pays_fee == Pays::No || self.pays_fee == Pays::No {
Pays::No
} else {
// Otherwise they pay.
Pays::Yes
}
}
}
/// Extract the actual weight from a dispatch result if any or fall back to the default weight.
@@ -293,10 +310,30 @@ pub fn extract_actual_weight(result: &DispatchResultWithPostInfo, info: &Dispatc
}.calc_actual_weight(info)
}
impl From<(Option<Weight>, Pays)> for PostDispatchInfo {
fn from(post_weight_info: (Option<Weight>, Pays)) -> Self {
let (actual_weight, pays_fee) = post_weight_info;
Self {
actual_weight,
pays_fee,
}
}
}
impl From<Pays> for PostDispatchInfo {
fn from(pays_fee: Pays) -> Self {
Self {
actual_weight: None,
pays_fee,
}
}
}
impl From<Option<Weight>> for PostDispatchInfo {
fn from(actual_weight: Option<Weight>) -> Self {
Self {
actual_weight,
pays_fee: Default::default(),
}
}
}
@@ -305,6 +342,7 @@ impl From<()> for PostDispatchInfo {
fn from(_: ()) -> Self {
Self {
actual_weight: None,
pays_fee: Default::default(),
}
}
}
@@ -315,6 +353,11 @@ impl sp_runtime::traits::Printable for PostDispatchInfo {
match self.actual_weight {
Some(weight) => weight.print(),
None => "max-weight".print(),
};
"pays_fee=".print();
match self.pays_fee {
Pays::Yes => "Yes".print(),
Pays::No => "No".print(),
}
}
}
@@ -338,7 +381,10 @@ impl<T> WithPostDispatchInfo for T where
{
fn with_weight(self, actual_weight: Weight) -> DispatchErrorWithPostInfo {
DispatchErrorWithPostInfo {
post_info: PostDispatchInfo { actual_weight: Some(actual_weight) },
post_info: PostDispatchInfo {
actual_weight: Some(actual_weight),
pays_fee: Default::default(),
},
error: self.into(),
}
}