From dc99f15469463ff80355a598eb6bf5ffbaaf9471 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Thu, 16 Sep 2021 09:53:36 +0200 Subject: [PATCH 1/4] make authorize_call free and operational --- authority/src/lib.rs | 8 ++++++-- authority/src/tests.rs | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/authority/src/lib.rs b/authority/src/lib.rs index 2502691fd..d5cc217d8 100644 --- a/authority/src/lib.rs +++ b/authority/src/lib.rs @@ -26,7 +26,7 @@ use frame_support::{ schedule::{DispatchTime, Named as ScheduleNamed, Priority}, EnsureOrigin, Get, IsType, OriginTrait, }, - weights::GetDispatchInfo, + weights::{DispatchClass, GetDispatchInfo, Pays}, }; use frame_system::{pallet_prelude::*, EnsureOneOf, EnsureRoot, EnsureSigned}; use sp_runtime::{ @@ -342,7 +342,11 @@ pub mod module { Ok(()) } - #[pallet::weight(T::WeightInfo::authorize_call())] + #[pallet::weight(( + T::WeightInfo::authorize_call(), + DispatchClass::Operational, + Pays::No, + ))] pub fn authorize_call( origin: OriginFor, call: Box>, diff --git a/authority/src/tests.rs b/authority/src/tests.rs index 530089ac0..df7424745 100644 --- a/authority/src/tests.rs +++ b/authority/src/tests.rs @@ -5,6 +5,7 @@ use super::*; use frame_support::{ assert_noop, assert_ok, + dispatch::DispatchInfo, traits::{schedule::DispatchTime, OriginTrait}, }; use frame_system::RawOrigin; @@ -405,6 +406,22 @@ fn call_size_limit() { ); } +#[test] +fn authorize_call_should_be_free_and_operational() { + ExtBuilder::default().build().execute_with(|| { + let call = Call::System(frame_system::Call::fill_block(Perbill::one())); + let dispatch_info = Call::Authority(authority::Call::authorize_call(Box::new(call), None)).get_dispatch_info(); + assert_eq!( + dispatch_info, + DispatchInfo { + weight: ::WeightInfo::authorize_call(), + class: DispatchClass::Operational, + pays_fee: Pays::No, + } + ); + }); +} + #[test] fn authorize_call_works() { ExtBuilder::default().build().execute_with(|| { From f426c1c2872d917e859139738a31ec0e4f701544 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Thu, 16 Sep 2021 10:19:07 +0200 Subject: [PATCH 2/4] update test --- authority/src/tests.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/authority/src/tests.rs b/authority/src/tests.rs index df7424745..96f4f2f0e 100644 --- a/authority/src/tests.rs +++ b/authority/src/tests.rs @@ -5,7 +5,7 @@ use super::*; use frame_support::{ assert_noop, assert_ok, - dispatch::DispatchInfo, + dispatch::{DispatchErrorWithPostInfo, DispatchInfo}, traits::{schedule::DispatchTime, OriginTrait}, }; use frame_system::RawOrigin; @@ -410,7 +410,8 @@ fn call_size_limit() { fn authorize_call_should_be_free_and_operational() { ExtBuilder::default().build().execute_with(|| { let call = Call::System(frame_system::Call::fill_block(Perbill::one())); - let dispatch_info = Call::Authority(authority::Call::authorize_call(Box::new(call), None)).get_dispatch_info(); + let authorize_call = Call::Authority(authority::Call::authorize_call(Box::new(call), None)); + let dispatch_info = authorize_call.get_dispatch_info(); assert_eq!( dispatch_info, DispatchInfo { @@ -419,6 +420,18 @@ fn authorize_call_should_be_free_and_operational() { pays_fee: Pays::No, } ); + // failed call should pay fee + let result = authorize_call.dispatch(Origin::signed(1)); + assert_eq!( + result, + Err(DispatchErrorWithPostInfo { + post_info: PostDispatchInfo { + actual_weight: None, + pays_fee: Pays::Yes + }, + error: DispatchError::BadOrigin + }) + ); }); } From fd510cf1de503be657569d6796049226dd121a08 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Thu, 16 Sep 2021 10:20:47 +0200 Subject: [PATCH 3/4] update test --- authority/src/tests.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/authority/src/tests.rs b/authority/src/tests.rs index 96f4f2f0e..79a9ad607 100644 --- a/authority/src/tests.rs +++ b/authority/src/tests.rs @@ -411,15 +411,16 @@ fn authorize_call_should_be_free_and_operational() { ExtBuilder::default().build().execute_with(|| { let call = Call::System(frame_system::Call::fill_block(Perbill::one())); let authorize_call = Call::Authority(authority::Call::authorize_call(Box::new(call), None)); - let dispatch_info = authorize_call.get_dispatch_info(); + assert_eq!( - dispatch_info, + authorize_call.get_dispatch_info(), DispatchInfo { weight: ::WeightInfo::authorize_call(), class: DispatchClass::Operational, pays_fee: Pays::No, } ); + // failed call should pay fee let result = authorize_call.dispatch(Origin::signed(1)); assert_eq!( From 4d2f0f0be277c3703a5b43984e2962359ad1cfcd Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Thu, 16 Sep 2021 10:31:13 +0200 Subject: [PATCH 4/4] update to post_info --- authority/src/lib.rs | 12 ++++-------- authority/src/tests.rs | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/authority/src/lib.rs b/authority/src/lib.rs index d5cc217d8..dd44d2cdb 100644 --- a/authority/src/lib.rs +++ b/authority/src/lib.rs @@ -20,7 +20,7 @@ #![allow(clippy::unused_unit)] use frame_support::{ - dispatch::PostDispatchInfo, + dispatch::{DispatchResultWithPostInfo, PostDispatchInfo}, pallet_prelude::*, traits::{ schedule::{DispatchTime, Named as ScheduleNamed, Priority}, @@ -342,21 +342,17 @@ pub mod module { Ok(()) } - #[pallet::weight(( - T::WeightInfo::authorize_call(), - DispatchClass::Operational, - Pays::No, - ))] + #[pallet::weight((T::WeightInfo::authorize_call(), DispatchClass::Operational))] pub fn authorize_call( origin: OriginFor, call: Box>, caller: Option, - ) -> DispatchResult { + ) -> DispatchResultWithPostInfo { ensure_root(origin)?; let hash = T::Hashing::hash_of(&call); SavedCalls::::insert(hash, (call, caller.clone())); Self::deposit_event(Event::AuthorizedCall(hash, caller)); - Ok(()) + Ok(Pays::No.into()) } #[pallet::weight(T::WeightInfo::remove_authorized_call())] diff --git a/authority/src/tests.rs b/authority/src/tests.rs index 79a9ad607..29f44829c 100644 --- a/authority/src/tests.rs +++ b/authority/src/tests.rs @@ -413,18 +413,26 @@ fn authorize_call_should_be_free_and_operational() { let authorize_call = Call::Authority(authority::Call::authorize_call(Box::new(call), None)); assert_eq!( - authorize_call.get_dispatch_info(), + authorize_call.clone().get_dispatch_info(), DispatchInfo { weight: ::WeightInfo::authorize_call(), class: DispatchClass::Operational, - pays_fee: Pays::No, + pays_fee: Pays::Yes, } ); - // failed call should pay fee - let result = authorize_call.dispatch(Origin::signed(1)); + // successfull call doesn't pay fee assert_eq!( - result, + authorize_call.clone().dispatch(Origin::root()), + Ok(PostDispatchInfo { + actual_weight: None, + pays_fee: Pays::No + }) + ); + + // bad origin pays fee + assert_eq!( + authorize_call.dispatch(Origin::signed(1)), Err(DispatchErrorWithPostInfo { post_info: PostDispatchInfo { actual_weight: None,