1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// This file is part of Gear.

// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]
#![doc(html_logo_url = "https://docs.gear.rs/logo.svg")]
#![doc(html_favicon_url = "https://gear-tech.io/favicons/favicon.ico")]

use common::{storage::*, DelegateFee, ExtractCall};
use frame_support::{
    dispatch::{DispatchInfo, GetDispatchInfo, PostDispatchInfo},
    pallet_prelude::*,
    traits::Contains,
};
use pallet_transaction_payment::{
    ChargeTransactionPayment, FeeDetails, Multiplier, MultiplierUpdate, OnChargeTransaction,
    RuntimeDispatchInfo,
};
use sp_runtime::{
    traits::{Bounded, Convert, DispatchInfoOf, Dispatchable, PostDispatchInfoOf, SignedExtension},
    transaction_validity::TransactionValidityError,
    FixedPointNumber, FixedPointOperand, Perquintill, SaturatedConversion,
};
use sp_std::borrow::Cow;

pub use pallet::*;

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type BalanceOf<T> =
    <<T as pallet_transaction_payment::Config>::OnChargeTransaction as OnChargeTransaction<T>>::Balance;
type CallOf<T> = <T as frame_system::Config>::RuntimeCall;
pub(crate) type QueueOf<T> = <<T as Config>::Messenger as Messenger>::Queue;
pub type TransactionPayment<T> = pallet_transaction_payment::Pallet<T>;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

/// A wrapper around the `pallet_transaction_payment::ChargeTransactionPayment`.
/// Adjusts `DispatchInfo` to reflect custom fee add-ons.
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
pub struct CustomChargeTransactionPayment<T: Config>(ChargeTransactionPayment<T>);

impl<T: Config> CustomChargeTransactionPayment<T>
where
    BalanceOf<T>: Send + Sync + FixedPointOperand,
    CallOf<T>: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
{
    pub fn from(tip: BalanceOf<T>) -> Self {
        Self(ChargeTransactionPayment::<T>::from(tip))
    }
}

impl<T: Config> sp_std::fmt::Debug for CustomChargeTransactionPayment<T> {
    #[cfg(feature = "std")]
    fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
        write!(f, "CustomChargeTransactionPayment({:?})", self.0)
    }
    #[cfg(not(feature = "std"))]
    fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
        Ok(())
    }
}

// Follow pallet-transaction-payment implementation
impl<T: Config> SignedExtension for CustomChargeTransactionPayment<T>
where
    T: TypeInfo,
    BalanceOf<T>: Send + Sync + From<u64> + FixedPointOperand,
    CallOf<T>: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
{
    const IDENTIFIER: &'static str = <ChargeTransactionPayment<T> as SignedExtension>::IDENTIFIER;
    type AccountId = <ChargeTransactionPayment<T> as SignedExtension>::AccountId;
    type Call = CallOf<T>;
    type AdditionalSigned = <ChargeTransactionPayment<T> as SignedExtension>::AdditionalSigned;
    type Pre = <ChargeTransactionPayment<T> as SignedExtension>::Pre;
    fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
        self.0.additional_signed()
    }

    fn validate(
        &self,
        who: &Self::AccountId,
        call: &Self::Call,
        info: &DispatchInfoOf<Self::Call>,
        len: usize,
    ) -> TransactionValidity {
        // Override DispatchInfo struct for call variants exempted from weight fee multiplication
        let info = Self::pre_dispatch_info(call, info);
        let payer = Self::fee_payer_account(call, who);
        self.0.validate(&payer, call, &info, len)
    }

    fn pre_dispatch(
        self,
        who: &Self::AccountId,
        call: &Self::Call,
        info: &DispatchInfoOf<Self::Call>,
        len: usize,
    ) -> Result<Self::Pre, TransactionValidityError> {
        // Override DispatchInfo struct for call variants exempted from weight fee multiplication
        let info = Self::pre_dispatch_info(call, info);
        // Replace payer if delegated
        let payer = Self::fee_payer_account(call, who);
        self.0.pre_dispatch(&payer, call, &info, len)
    }

    fn post_dispatch(
        maybe_pre: Option<Self::Pre>,
        info: &DispatchInfoOf<Self::Call>,
        post_info: &PostDispatchInfoOf<Self::Call>,
        len: usize,
        result: &sp_runtime::DispatchResult,
    ) -> Result<(), TransactionValidityError> {
        // There is no easy way to modify the original `DispatchInfo` struct similarly
        // it's done in `pre_dispatch()` because a call is not supplied.
        // However, we can just leave it as is and yet get the correct fee refund if any:
        //   - if `None` is returned as the actual weight (i.e. worst case) nothing is supposed
        //   to be refunded anyway and saturating subtraction guarantees we don't have overflow;
        //   - if `post_info` has `Some(actual_weight)`, the minimum of it and `info.weight` will
        //   be used to calculate the correct fee so it is just our responsibility to do
        //   weight normalization before returning it from the extrinsic.
        //
        // TODO: still think of a more robust way to deal with fee refunds
        <ChargeTransactionPayment<T> as SignedExtension>::post_dispatch(
            maybe_pre, info, post_info, len, result,
        )
    }
}

impl<T: Config> CustomChargeTransactionPayment<T>
where
    CallOf<T>: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
{
    fn pre_dispatch_info<'a>(
        call: &'a <T as frame_system::Config>::RuntimeCall,
        info: &'a DispatchInfoOf<<T as frame_system::Config>::RuntimeCall>,
    ) -> Cow<'a, DispatchInfoOf<<T as frame_system::Config>::RuntimeCall>> {
        // If the call is not subject to fee multiplication, divide weight by fee multiplier.
        // This action will effectively be cancelled out at the time the fee is calculated.
        //
        // TODO: consider reimplementing.
        // This procedure does introduce a rounding error 𝞮 =  w - ⎣w / m⎦⋅m
        // However, we argue that such error is negligible:
        // - the rounding error can never exceed `m` (multiplier). Order of `w` (weight) is
        // usually not less than 10^8 while the fee multiplier should not be greater than 10^3.
        // Therefore the rounding error shouln't exceed 0.001% in the worst case.
        // Note: this only applies to calls that do not affect message queue, that is are
        // relatively rare. Still, a better solution can be found.
        if !T::ExtraFeeCallFilter::contains(call) {
            let multiplier = TransactionPayment::<T>::next_fee_multiplier();
            if multiplier > Multiplier::saturating_from_integer(1) {
                let mut info: DispatchInfo = *info;
                info.weight = Weight::from_parts(
                    multiplier
                        .reciprocal() // take inverse
                        .unwrap_or_else(Multiplier::max_value)
                        .saturating_mul_int(info.weight.ref_time()),
                    0,
                );
                Cow::Owned(info)
            } else {
                Cow::Borrowed(info)
            }
        } else {
            Cow::Borrowed(info)
        }
    }

    fn fee_payer_account<'a>(
        call: &'a <T as frame_system::Config>::RuntimeCall,
        who: &'a <T as frame_system::Config>::AccountId,
    ) -> Cow<'a, <T as frame_system::Config>::AccountId> {
        // Check if the extrinsic being called allows to charge fee payment to another account.
        // The only such call at the moment is `GearVoucher::call`.
        if let Some(acc) = T::DelegateFee::delegate_fee(call, who) {
            Cow::Owned(acc)
        } else {
            Cow::Borrowed(who)
        }
    }
}

/// Custom fee multiplier which looks at the message queue size to increase weight fee
pub struct GearFeeMultiplier<T, S>(sp_std::marker::PhantomData<(T, S)>);

impl<T, S> Convert<Multiplier, Multiplier> for GearFeeMultiplier<T, S>
where
    T: Config,
    S: Get<u128>,
{
    fn convert(_previous: Multiplier) -> Multiplier {
        let len_step = S::get().max(1); // Avoiding division by 0.

        let queue_len: u128 = QueueOf::<T>::len().saturated_into();
        let multiplier = queue_len.saturating_div(len_step).saturating_add(1);
        Multiplier::saturating_from_integer(multiplier)
    }
}

impl<T, S> MultiplierUpdate for GearFeeMultiplier<T, S>
where
    T: Config,
    S: Get<u128>,
{
    fn max() -> Multiplier {
        Default::default()
    }
    fn min() -> Multiplier {
        Default::default()
    }
    fn target() -> Perquintill {
        Default::default()
    }
    fn variability() -> Multiplier {
        Default::default()
    }
}

impl<T: Config> Pallet<T> {
    /// Modification of the `pallet_transaction_payment::Pallet<T>::query_info()`
    /// that is aware of the transaction fee customization based on a specific call
    pub fn query_info<
        Extrinsic: sp_runtime::traits::Extrinsic + GetDispatchInfo + ExtractCall<CallOf<T>>,
    >(
        unchecked_extrinsic: Extrinsic,
        len: u32,
    ) -> RuntimeDispatchInfo<BalanceOf<T>>
    where
        CallOf<T>: Dispatchable<Info = DispatchInfo>,
        BalanceOf<T>: FixedPointOperand,
    {
        let DispatchInfo {
            weight,
            class,
            pays_fee,
        } = <Extrinsic as GetDispatchInfo>::get_dispatch_info(&unchecked_extrinsic);

        let partial_fee = if unchecked_extrinsic.is_signed().unwrap_or(false) {
            let call: CallOf<T> =
                <Extrinsic as ExtractCall<CallOf<T>>>::extract_call(&unchecked_extrinsic);
            // If call is exempted from weight multiplication pre-divide it with the fee multiplier
            let adjusted_weight = if !T::ExtraFeeCallFilter::contains(&call) {
                Weight::from_parts(
                    TransactionPayment::<T>::next_fee_multiplier()
                        .reciprocal()
                        .unwrap_or_else(Multiplier::max_value)
                        .saturating_mul_int(weight.ref_time()),
                    0,
                )
            } else {
                weight
            };
            TransactionPayment::<T>::compute_fee(
                len,
                &DispatchInfo {
                    weight: adjusted_weight,
                    class,
                    pays_fee,
                },
                0u32.into(),
            )
        } else {
            // Unsigned extrinsics have no partial fee.
            0u32.into()
        };

        RuntimeDispatchInfo {
            weight,
            class,
            partial_fee,
        }
    }

    /// Modification of the `pallet_transaction_payment::Pallet<T>::query_fee_details()`
    pub fn query_fee_details<
        Extrinsic: sp_runtime::traits::Extrinsic + GetDispatchInfo + ExtractCall<CallOf<T>>,
    >(
        unchecked_extrinsic: Extrinsic,
        len: u32,
    ) -> FeeDetails<BalanceOf<T>>
    where
        CallOf<T>: Dispatchable<Info = DispatchInfo>,
        BalanceOf<T>: FixedPointOperand,
    {
        let DispatchInfo {
            weight,
            class,
            pays_fee,
        } = <Extrinsic as GetDispatchInfo>::get_dispatch_info(&unchecked_extrinsic);

        let tip = 0u32.into();

        if unchecked_extrinsic.is_signed().unwrap_or(false) {
            let call: CallOf<T> =
                <Extrinsic as ExtractCall<CallOf<T>>>::extract_call(&unchecked_extrinsic);
            let adjusted_weight = if !T::ExtraFeeCallFilter::contains(&call) {
                Weight::from_parts(
                    TransactionPayment::<T>::next_fee_multiplier()
                        .reciprocal()
                        .unwrap_or_else(Multiplier::max_value)
                        .saturating_mul_int(weight.ref_time()),
                    0,
                )
            } else {
                weight
            };
            TransactionPayment::<T>::compute_fee_details(
                len,
                &DispatchInfo {
                    weight: adjusted_weight,
                    class,
                    pays_fee,
                },
                tip,
            )
        } else {
            // Unsigned extrinsics have no inclusion fee.
            FeeDetails {
                inclusion_fee: None,
                tip,
            }
        }
    }
}

#[frame_support::pallet]
pub mod pallet {
    use super::*;

    #[pallet::config]
    pub trait Config: frame_system::Config + pallet_transaction_payment::Config {
        /// Filter for calls subbject for extra fees
        type ExtraFeeCallFilter: Contains<CallOf<Self>>;

        /// Filter for a call that delegates fee payment to another account
        type DelegateFee: DelegateFee<CallOf<Self>, AccountIdOf<Self>>;

        /// Type representing message queue
        type Messenger: Messenger<Capacity = u32>;
    }

    #[pallet::pallet]
    #[pallet::without_storage_info]
    pub struct Pallet<T>(_);
}