pallet_feeless/types.rs
1// GNU General Public License (GPL)
2// Version 3, 29 June 2007
3// http://www.gnu.org/licenses/gpl-3.0.html
4//
5// Copyright 2024 Benjamin Gallois
6//
7// Licensed under the GNU General Public License, Version 3 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License at
10//
11// http://www.gnu.org/licenses/gpl-3.0.html
12//
13// Unless required by applicable law or agreed to in writing, software
14// distributed under the License is distributed on an "AS IS" BASIS,
15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16// See the License for the specific language governing permissions and
17// limitations under the License.
18//
19// You may not distribute modified versions of the software without providing
20// the source code, and any derivative works must be licensed under the GPL
21// License as well. This ensures that the software remains free and open
22// for all users.
23//
24// You should have received a copy of the GPL along with this program.
25// If not, see <http://www.gnu.org/licenses/>.
26use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
27use frame_system::pallet_prelude::BlockNumberFor;
28use scale_info::TypeInfo;
29use sp_runtime::RuntimeDebug;
30
31#[derive(
32 Encode,
33 Decode,
34 DecodeWithMemTracking,
35 Clone,
36 PartialEq,
37 Eq,
38 Default,
39 RuntimeDebug,
40 MaxEncodedLen,
41 TypeInfo,
42)]
43pub enum Status {
44 #[default]
45 Limited,
46 Unlimited,
47}
48
49/// Tracks transaction rates for an account over blocks.
50#[derive(
51 Encode,
52 Decode,
53 DecodeWithMemTracking,
54 Clone,
55 PartialEq,
56 Eq,
57 Default,
58 RuntimeDebug,
59 MaxEncodedLen,
60 TypeInfo,
61)]
62pub struct Rate<BlockNumber> {
63 /// Block number of the last transaction.
64 pub last_block: BlockNumber,
65 /// Number of transactions since the last block.
66 pub tx_since_last: u32,
67 /// Size of transactions since the last block.
68 pub size_since_last: u32,
69 pub status: Status,
70}
71
72/// Custom account data structure with rate limiting.
73#[derive(
74 Encode,
75 Decode,
76 DecodeWithMemTracking,
77 Clone,
78 PartialEq,
79 Eq,
80 Default,
81 RuntimeDebug,
82 MaxEncodedLen,
83 TypeInfo,
84)]
85pub struct AccountData<Balance, BlockNumber> {
86 /// Balance data from the `pallet_balances` module.
87 pub balance: pallet_balances::AccountData<Balance>,
88 /// Rate limiter data.
89 pub rate: Rate<BlockNumber>,
90}
91
92/// Rate-limiting behavior.
93pub trait RateLimiter<T: frame_system::Config> {
94 /// Checks if a transaction is allowed for the current block.
95 fn is_allowed(&self, b: BlockNumberFor<T>, size: u32) -> bool;
96 /// Updates the rate limiter after a transaction.
97 fn update_rate(&mut self, b: BlockNumberFor<T>, size: u32);
98}