Adds syntax for marking calls feeless (#1926)

Fixes https://github.com/paritytech/polkadot-sdk/issues/1725

This PR adds the following changes:
1. An attribute `pallet::feeless_if` that can be optionally attached to
a call like so:
```rust
#[pallet::feeless_if(|_origin: &OriginFor<T>, something: &u32| -> bool {
	*something == 0
})]
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
     ....
}
```
The closure passed accepts references to arguments as specified in the
call fn. It returns a boolean that denotes the conditions required for
this call to be "feeless".

2. A signed extension `SkipCheckIfFeeless<T: SignedExtension>` that
wraps a transaction payment processor such as
`pallet_transaction_payment::ChargeTransactionPayment`. It checks for
all calls annotated with `pallet::feeless_if` to see if the conditions
are met. If so, the wrapped signed extension is not called, essentially
making the call feeless.

In order to use this, you can simply replace your existing signed
extension that manages transaction payment like so:
```diff
- pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
+ pallet_skip_feeless_payment::SkipCheckIfFeeless<
+	Runtime,
+	pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
+ >,
```

### Todo
- [x] Tests
- [x] Docs
- [x] Prdoc

---------

Co-authored-by: Nikhil Gupta <>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Francisco Aguirre <franciscoaguirreperez@gmail.com>
Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
This commit is contained in:
gupnik
2023-11-13 19:14:41 +05:30
committed by GitHub
parent ebcf0a0f1c
commit 60c77a2e9a
33 changed files with 874 additions and 54 deletions
@@ -0,0 +1,145 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//! # Skip Feeless Payment Pallet
//!
//! This pallet allows runtimes that include it to skip payment of transaction fees for
//! dispatchables marked by [`#[pallet::feeless_if]`](`macro@
//! frame_support::pallet_prelude::feeless_if`).
//!
//! ## Overview
//!
//! It does this by wrapping an existing [`SignedExtension`] implementation (e.g.
//! [`pallet-transaction-payment`]) and checking if the dispatchable is feeless before applying the
//! wrapped extension. If the dispatchable is indeed feeless, the extension is skipped and a custom
//! event is emitted instead. Otherwise, the extension is applied as usual.
//!
//!
//! ## Integration
//!
//! This pallet wraps an existing transaction payment pallet. This means you should both pallets
//! in your `construct_runtime` macro and include this pallet's
//! [`SignedExtension`] ([`SkipCheckIfFeeless`]) that would accept the existing one as an argument.
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use frame_support::{
dispatch::{CheckIfFeeless, DispatchResult},
traits::{IsType, OriginTrait},
};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{DispatchInfoOf, PostDispatchInfoOf, SignedExtension},
transaction_validity::TransactionValidityError,
};
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// A transaction fee was skipped.
FeeSkipped { who: T::AccountId },
}
}
/// A [`SignedExtension`] that skips the wrapped extension if the dispatchable is feeless.
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct SkipCheckIfFeeless<T: Config, S: SignedExtension>(pub S, sp_std::marker::PhantomData<T>);
impl<T: Config, S: SignedExtension> sp_std::fmt::Debug for SkipCheckIfFeeless<T, S> {
#[cfg(feature = "std")]
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
write!(f, "SkipCheckIfFeeless<{:?}>", self.0.encode())
}
#[cfg(not(feature = "std"))]
fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
Ok(())
}
}
impl<T: Config + Send + Sync, S: SignedExtension> SkipCheckIfFeeless<T, S> {
/// utility constructor. Used only in client/factory code.
pub fn from(s: S) -> Self {
Self(s, sp_std::marker::PhantomData)
}
}
impl<T: Config + Send + Sync, S: SignedExtension<AccountId = T::AccountId>> SignedExtension
for SkipCheckIfFeeless<T, S>
where
S::Call: CheckIfFeeless<Origin = frame_system::pallet_prelude::OriginFor<T>>,
{
type AccountId = T::AccountId;
type Call = S::Call;
type AdditionalSigned = S::AdditionalSigned;
type Pre = (Self::AccountId, Option<<S as SignedExtension>::Pre>);
const IDENTIFIER: &'static str = "SkipCheckIfFeeless";
fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
self.0.additional_signed()
}
fn pre_dispatch(
self,
who: &Self::AccountId,
call: &Self::Call,
info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
if call.is_feeless(&<T as frame_system::Config>::RuntimeOrigin::signed(who.clone())) {
Ok((who.clone(), None))
} else {
Ok((who.clone(), Some(self.0.pre_dispatch(who, call, info, len)?)))
}
}
fn post_dispatch(
pre: Option<Self::Pre>,
info: &DispatchInfoOf<Self::Call>,
post_info: &PostDispatchInfoOf<Self::Call>,
len: usize,
result: &DispatchResult,
) -> Result<(), TransactionValidityError> {
if let Some(pre) = pre {
if let Some(pre) = pre.1 {
S::post_dispatch(Some(pre), info, post_info, len, result)?;
} else {
Pallet::<T>::deposit_event(Event::<T>::FeeSkipped { who: pre.0 });
}
}
Ok(())
}
}