mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-22 20:45:40 +00:00
Fix for 'note_applied_extrinsic' - consider also 'extract_actual_pays_fee' (#11849)
This commit is contained in:
@@ -358,6 +358,15 @@ pub fn extract_actual_weight(result: &DispatchResultWithPostInfo, info: &Dispatc
|
|||||||
.calc_actual_weight(info)
|
.calc_actual_weight(info)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extract the actual pays_fee from a dispatch result if any or fall back to the default weight.
|
||||||
|
pub fn extract_actual_pays_fee(result: &DispatchResultWithPostInfo, info: &DispatchInfo) -> Pays {
|
||||||
|
match result {
|
||||||
|
Ok(post_info) => post_info,
|
||||||
|
Err(err) => &err.post_info,
|
||||||
|
}
|
||||||
|
.pays_fee(info)
|
||||||
|
}
|
||||||
|
|
||||||
impl From<(Option<Weight>, Pays)> for PostDispatchInfo {
|
impl From<(Option<Weight>, Pays)> for PostDispatchInfo {
|
||||||
fn from(post_weight_info: (Option<Weight>, Pays)) -> Self {
|
fn from(post_weight_info: (Option<Weight>, Pays)) -> Self {
|
||||||
let (actual_weight, pays_fee) = post_weight_info;
|
let (actual_weight, pays_fee) = post_weight_info;
|
||||||
@@ -956,6 +965,34 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn extract_actual_pays_fee_works() {
|
||||||
|
let pre = DispatchInfo { weight: 1000, ..Default::default() };
|
||||||
|
assert_eq!(extract_actual_pays_fee(&Ok(Some(7).into()), &pre), Pays::Yes);
|
||||||
|
assert_eq!(extract_actual_pays_fee(&Ok(Some(1000).into()), &pre), Pays::Yes);
|
||||||
|
assert_eq!(extract_actual_pays_fee(&Ok((Some(1000), Pays::Yes).into()), &pre), Pays::Yes);
|
||||||
|
assert_eq!(extract_actual_pays_fee(&Ok((Some(1000), Pays::No).into()), &pre), Pays::No);
|
||||||
|
assert_eq!(
|
||||||
|
extract_actual_pays_fee(&Err(DispatchError::BadOrigin.with_weight(9)), &pre),
|
||||||
|
Pays::Yes
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
extract_actual_pays_fee(
|
||||||
|
&Err(DispatchErrorWithPostInfo {
|
||||||
|
post_info: PostDispatchInfo { actual_weight: None, pays_fee: Pays::No },
|
||||||
|
error: DispatchError::BadOrigin,
|
||||||
|
}),
|
||||||
|
&pre
|
||||||
|
),
|
||||||
|
Pays::No
|
||||||
|
);
|
||||||
|
|
||||||
|
let pre = DispatchInfo { weight: 1000, pays_fee: Pays::No, ..Default::default() };
|
||||||
|
assert_eq!(extract_actual_pays_fee(&Ok(Some(7).into()), &pre), Pays::No);
|
||||||
|
assert_eq!(extract_actual_pays_fee(&Ok(Some(1000).into()), &pre), Pays::No);
|
||||||
|
assert_eq!(extract_actual_pays_fee(&Ok((Some(1000), Pays::Yes).into()), &pre), Pays::No);
|
||||||
|
}
|
||||||
|
|
||||||
type Balance = u64;
|
type Balance = u64;
|
||||||
|
|
||||||
// 0.5x^3 + 2.333x^2 + 7x - 10_000
|
// 0.5x^3 + 2.333x^2 + 7x - 10_000
|
||||||
|
|||||||
@@ -91,8 +91,8 @@ use frame_support::{
|
|||||||
OriginTrait, PalletInfo, SortedMembers, StoredMap, TypedGet,
|
OriginTrait, PalletInfo, SortedMembers, StoredMap, TypedGet,
|
||||||
},
|
},
|
||||||
weights::{
|
weights::{
|
||||||
extract_actual_weight, DispatchClass, DispatchInfo, PerDispatchClass, RuntimeDbWeight,
|
extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo,
|
||||||
Weight,
|
PerDispatchClass, RuntimeDbWeight, Weight,
|
||||||
},
|
},
|
||||||
Parameter,
|
Parameter,
|
||||||
};
|
};
|
||||||
@@ -1500,6 +1500,7 @@ impl<T: Config> Pallet<T> {
|
|||||||
/// To be called immediately after an extrinsic has been applied.
|
/// To be called immediately after an extrinsic has been applied.
|
||||||
pub fn note_applied_extrinsic(r: &DispatchResultWithPostInfo, mut info: DispatchInfo) {
|
pub fn note_applied_extrinsic(r: &DispatchResultWithPostInfo, mut info: DispatchInfo) {
|
||||||
info.weight = extract_actual_weight(r, &info);
|
info.weight = extract_actual_weight(r, &info);
|
||||||
|
info.pays_fee = extract_actual_pays_fee(r, &info);
|
||||||
Self::deposit_event(match r {
|
Self::deposit_event(match r {
|
||||||
Ok(_) => Event::ExtrinsicSuccess { dispatch_info: info },
|
Ok(_) => Event::ExtrinsicSuccess { dispatch_info: info },
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
@@ -17,7 +17,9 @@
|
|||||||
|
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use frame_support::{
|
use frame_support::{
|
||||||
assert_noop, assert_ok, dispatch::PostDispatchInfo, weights::WithPostDispatchInfo,
|
assert_noop, assert_ok,
|
||||||
|
dispatch::PostDispatchInfo,
|
||||||
|
weights::{Pays, WithPostDispatchInfo},
|
||||||
};
|
};
|
||||||
use mock::{Origin, *};
|
use mock::{Origin, *};
|
||||||
use sp_core::H256;
|
use sp_core::H256;
|
||||||
@@ -216,7 +218,7 @@ fn deposit_event_should_work() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deposit_event_uses_actual_weight() {
|
fn deposit_event_uses_actual_weight_and_pays_fee() {
|
||||||
new_test_ext().execute_with(|| {
|
new_test_ext().execute_with(|| {
|
||||||
System::reset_events();
|
System::reset_events();
|
||||||
System::initialize(&1, &[0u8; 32].into(), &Default::default());
|
System::initialize(&1, &[0u8; 32].into(), &Default::default());
|
||||||
@@ -230,8 +232,34 @@ fn deposit_event_uses_actual_weight() {
|
|||||||
&Ok(Some(1200).into()),
|
&Ok(Some(1200).into()),
|
||||||
pre_info,
|
pre_info,
|
||||||
);
|
);
|
||||||
|
System::note_applied_extrinsic(&Ok((Some(2_500_000), Pays::Yes).into()), pre_info);
|
||||||
|
System::note_applied_extrinsic(&Ok(Pays::No.into()), pre_info);
|
||||||
|
System::note_applied_extrinsic(&Ok((Some(2_500_000), Pays::No).into()), pre_info);
|
||||||
|
System::note_applied_extrinsic(&Ok((Some(500), Pays::No).into()), pre_info);
|
||||||
System::note_applied_extrinsic(&Err(DispatchError::BadOrigin.with_weight(999)), pre_info);
|
System::note_applied_extrinsic(&Err(DispatchError::BadOrigin.with_weight(999)), pre_info);
|
||||||
|
|
||||||
|
System::note_applied_extrinsic(
|
||||||
|
&Err(DispatchErrorWithPostInfo {
|
||||||
|
post_info: PostDispatchInfo { actual_weight: None, pays_fee: Pays::Yes },
|
||||||
|
error: DispatchError::BadOrigin,
|
||||||
|
}),
|
||||||
|
pre_info,
|
||||||
|
);
|
||||||
|
System::note_applied_extrinsic(
|
||||||
|
&Err(DispatchErrorWithPostInfo {
|
||||||
|
post_info: PostDispatchInfo { actual_weight: Some(800), pays_fee: Pays::Yes },
|
||||||
|
error: DispatchError::BadOrigin,
|
||||||
|
}),
|
||||||
|
pre_info,
|
||||||
|
);
|
||||||
|
System::note_applied_extrinsic(
|
||||||
|
&Err(DispatchErrorWithPostInfo {
|
||||||
|
post_info: PostDispatchInfo { actual_weight: Some(800), pays_fee: Pays::No },
|
||||||
|
error: DispatchError::BadOrigin,
|
||||||
|
}),
|
||||||
|
pre_info,
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
System::events(),
|
System::events(),
|
||||||
vec![
|
vec![
|
||||||
@@ -261,6 +289,54 @@ fn deposit_event_uses_actual_weight() {
|
|||||||
},
|
},
|
||||||
EventRecord {
|
EventRecord {
|
||||||
phase: Phase::ApplyExtrinsic(3),
|
phase: Phase::ApplyExtrinsic(3),
|
||||||
|
event: SysEvent::ExtrinsicSuccess {
|
||||||
|
dispatch_info: DispatchInfo {
|
||||||
|
weight: 1000,
|
||||||
|
pays_fee: Pays::Yes,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
topics: vec![]
|
||||||
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(4),
|
||||||
|
event: SysEvent::ExtrinsicSuccess {
|
||||||
|
dispatch_info: DispatchInfo {
|
||||||
|
weight: 1000,
|
||||||
|
pays_fee: Pays::No,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
topics: vec![]
|
||||||
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(5),
|
||||||
|
event: SysEvent::ExtrinsicSuccess {
|
||||||
|
dispatch_info: DispatchInfo {
|
||||||
|
weight: 1000,
|
||||||
|
pays_fee: Pays::No,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
topics: vec![]
|
||||||
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(6),
|
||||||
|
event: SysEvent::ExtrinsicSuccess {
|
||||||
|
dispatch_info: DispatchInfo {
|
||||||
|
weight: 500,
|
||||||
|
pays_fee: Pays::No,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
topics: vec![]
|
||||||
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(7),
|
||||||
event: SysEvent::ExtrinsicFailed {
|
event: SysEvent::ExtrinsicFailed {
|
||||||
dispatch_error: DispatchError::BadOrigin.into(),
|
dispatch_error: DispatchError::BadOrigin.into(),
|
||||||
dispatch_info: DispatchInfo { weight: 999, ..Default::default() },
|
dispatch_info: DispatchInfo { weight: 999, ..Default::default() },
|
||||||
@@ -268,6 +344,45 @@ fn deposit_event_uses_actual_weight() {
|
|||||||
.into(),
|
.into(),
|
||||||
topics: vec![]
|
topics: vec![]
|
||||||
},
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(8),
|
||||||
|
event: SysEvent::ExtrinsicFailed {
|
||||||
|
dispatch_error: DispatchError::BadOrigin.into(),
|
||||||
|
dispatch_info: DispatchInfo {
|
||||||
|
weight: 1000,
|
||||||
|
pays_fee: Pays::Yes,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
topics: vec![]
|
||||||
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(9),
|
||||||
|
event: SysEvent::ExtrinsicFailed {
|
||||||
|
dispatch_error: DispatchError::BadOrigin.into(),
|
||||||
|
dispatch_info: DispatchInfo {
|
||||||
|
weight: 800,
|
||||||
|
pays_fee: Pays::Yes,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
topics: vec![]
|
||||||
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(10),
|
||||||
|
event: SysEvent::ExtrinsicFailed {
|
||||||
|
dispatch_error: DispatchError::BadOrigin.into(),
|
||||||
|
dispatch_info: DispatchInfo {
|
||||||
|
weight: 800,
|
||||||
|
pays_fee: Pays::No,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
topics: vec![]
|
||||||
|
},
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user