Skip to content

make trigger_call free and operational #610

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

Merged
merged 1 commit into from
Sep 16, 2021
Merged
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
11 changes: 7 additions & 4 deletions authority/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -377,12 +377,15 @@ pub mod module {
})
}

#[pallet::weight(T::WeightInfo::trigger_call().saturating_add(*call_weight_bound))]
#[pallet::weight((
T::WeightInfo::trigger_call().saturating_add(*call_weight_bound),
DispatchClass::Operational,
))]
pub fn trigger_call(
origin: OriginFor<T>,
hash: T::Hash,
#[pallet::compact] call_weight_bound: Weight,
) -> DispatchResult {
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
SavedCalls::<T>::try_mutate_exists(hash, |maybe_call| {
let (call, maybe_caller) = maybe_call.take().ok_or(Error::<T>::CallNotAuthorized)?;
Expand All @@ -396,7 +399,7 @@ pub mod module {
let result = call.dispatch(OriginFor::<T>::root());
Self::deposit_event(Event::TriggeredCallBy(hash, who));
Self::deposit_event(Event::Dispatched(result.map(|_| ()).map_err(|e| e.error)));
Ok(())
Ok(Pays::No.into())
})
}
}
Expand Down
34 changes: 34 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,
traits::{schedule::DispatchTime, OriginTrait},
};
use frame_system::RawOrigin;
Expand Down Expand Up @@ -533,3 +534,36 @@ fn remove_authorized_call_works() {
assert_eq!(Authority::saved_calls(&hash), None);
});
}

#[test]
fn trigger_call_should_be_free_and_operational() {
ExtBuilder::default().build().execute_with(|| {
let call = Call::System(frame_system::Call::fill_block(Perbill::one()));
let hash = <Runtime as frame_system::Config>::Hashing::hash_of(&call);
let call_weight_bound = call.get_dispatch_info().weight;
let trigger_call = Call::Authority(authority::Call::trigger_call(hash, call_weight_bound));

assert_ok!(Authority::authorize_call(Origin::root(), Box::new(call), Some(1)));

// bad caller pays fee
assert_eq!(
trigger_call.clone().dispatch(Origin::signed(2)),
Err(DispatchErrorWithPostInfo {
post_info: PostDispatchInfo {
actual_weight: None,
pays_fee: Pays::Yes
},
error: Error::<Runtime>::TriggerCallNotPermitted.into()
})
);

// successfull call doesn't pay fee
assert_eq!(
trigger_call.clone().dispatch(Origin::signed(1)),
Ok(PostDispatchInfo {
actual_weight: None,
pays_fee: Pays::No
})
);
});
}