Skip to content

make authorize_call free and operational #609

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions authority/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
#![allow(clippy::unused_unit)]

use frame_support::{
dispatch::PostDispatchInfo,
dispatch::{DispatchResultWithPostInfo, PostDispatchInfo},
pallet_prelude::*,
traits::{
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::{
Expand Down Expand Up @@ -342,17 +342,17 @@ pub mod module {
Ok(())
}

#[pallet::weight(T::WeightInfo::authorize_call())]
#[pallet::weight((T::WeightInfo::authorize_call(), DispatchClass::Operational))]
pub fn authorize_call(
origin: OriginFor<T>,
call: Box<CallOf<T>>,
caller: Option<T::AccountId>,
) -> DispatchResult {
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
let hash = T::Hashing::hash_of(&call);
SavedCalls::<T>::insert(hash, (call, caller.clone()));
Self::deposit_event(Event::AuthorizedCall(hash, caller));
Ok(())
Ok(Pays::No.into())
}

#[pallet::weight(T::WeightInfo::remove_authorized_call())]
Expand Down
39 changes: 39 additions & 0 deletions authority/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use super::*;
use frame_support::{
assert_noop, assert_ok,
dispatch::{DispatchErrorWithPostInfo, DispatchInfo},
traits::{schedule::DispatchTime, OriginTrait},
};
use frame_system::RawOrigin;
Expand Down Expand Up @@ -405,6 +406,44 @@ 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 authorize_call = Call::Authority(authority::Call::authorize_call(Box::new(call), None));

assert_eq!(
authorize_call.clone().get_dispatch_info(),
DispatchInfo {
weight: <Runtime as authority::Config>::WeightInfo::authorize_call(),
class: DispatchClass::Operational,
pays_fee: Pays::Yes,
}
);

// successfull call doesn't pay fee
assert_eq!(
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,
pays_fee: Pays::Yes
},
error: DispatchError::BadOrigin
})
);
});
}

#[test]
fn authorize_call_works() {
ExtBuilder::default().build().execute_with(|| {
Expand Down