1#![cfg_attr(not(feature = "std"), no_std)]
19
20#[cfg(not(feature = "std"))]
21extern crate alloc;
22
23pub mod fees;
24
25pub use pallet::*;
26
27#[frame_support::pallet]
28mod pallet {
29 #[cfg(not(feature = "std"))]
30 use alloc::vec::Vec;
31 use frame_support::pallet_prelude::*;
32 use frame_support::storage::generator::StorageValue as _;
33 use frame_system::pallet_prelude::*;
34 use parity_scale_codec::{Codec, MaxEncodedLen};
35 use scale_info::TypeInfo;
36 use sp_block_fees::{InherentError, InherentType, INHERENT_IDENTIFIER};
37 use sp_domains::{BlockFees, ChainId};
38 use sp_runtime::traits::{AtLeast32BitUnsigned, MaybeSerializeDeserialize, Saturating};
39 use sp_runtime::{FixedPointOperand, SaturatedConversion};
40 use sp_std::fmt::Debug;
41 use sp_std::result;
42
43 #[pallet::config]
44 pub trait Config: frame_system::Config {
45 type Balance: Parameter
47 + Member
48 + AtLeast32BitUnsigned
49 + Codec
50 + Default
51 + Copy
52 + MaybeSerializeDeserialize
53 + Debug
54 + MaxEncodedLen
55 + TypeInfo
56 + FixedPointOperand;
57
58 type DomainChainByteFee: Get<Self::Balance>;
60 }
61
62 #[pallet::storage]
67 #[pallet::getter(fn collected_block_fees)]
68 pub(super) type CollectedBlockFees<T: Config> =
69 StorageValue<_, BlockFees<T::Balance>, ValueQuery>;
70
71 #[pallet::storage]
76 #[pallet::getter(fn consensus_chain_byte_fee)]
77 pub(super) type ConsensusChainByteFee<T: Config> = StorageValue<_, T::Balance, ValueQuery>;
78
79 #[pallet::storage]
83 pub(super) type NextConsensusChainByteFee<T: Config> = StorageValue<_, T::Balance, ValueQuery>;
84
85 #[pallet::pallet]
87 #[pallet::without_storage_info]
88 pub struct Pallet<T>(_);
89
90 #[pallet::hooks]
91 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
92 fn on_initialize(_now: BlockNumberFor<T>) -> Weight {
93 CollectedBlockFees::<T>::set(BlockFees::<T::Balance>::default());
97 T::DbWeight::get().writes(1)
98 }
99
100 fn on_finalize(_now: BlockNumberFor<T>) {
101 let transaction_byte_fee = NextConsensusChainByteFee::<T>::take();
102 ConsensusChainByteFee::<T>::put(transaction_byte_fee);
103 }
104 }
105
106 #[pallet::call]
107 impl<T: Config> Pallet<T> {
108 #[pallet::call_index(0)]
109 #[pallet::weight((
110 Weight::from_all(10_000),
112 DispatchClass::Mandatory
113 ))]
114 pub fn set_next_consensus_chain_byte_fee(
115 origin: OriginFor<T>,
116 #[pallet::compact] transaction_byte_fee: T::Balance,
117 ) -> DispatchResult {
118 ensure_none(origin)?;
119 NextConsensusChainByteFee::<T>::put(transaction_byte_fee);
120 Ok(())
121 }
122 }
123
124 #[pallet::inherent]
125 impl<T: Config> ProvideInherent for Pallet<T> {
126 type Call = Call<T>;
127 type Error = InherentError;
128 const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
129
130 fn create_inherent(data: &InherentData) -> Option<Self::Call> {
131 let inherent_data = data
132 .get_data::<InherentType>(&INHERENT_IDENTIFIER)
133 .expect("Domain block fees inherent data not correctly encoded")
134 .expect("Domain block fees inherent data must be provided");
135
136 let transaction_byte_fee = inherent_data.saturated_into::<T::Balance>();
137
138 Some(Call::set_next_consensus_chain_byte_fee {
139 transaction_byte_fee,
140 })
141 }
142
143 fn check_inherent(
144 call: &Self::Call,
145 data: &InherentData,
146 ) -> result::Result<(), Self::Error> {
147 let inherent_data = data
148 .get_data::<InherentType>(&INHERENT_IDENTIFIER)
149 .expect("Domain block fees inherent data not correctly encoded")
150 .expect("Domain block fees inherent data must be provided");
151
152 let provided_transaction_byte_fee = inherent_data.saturated_into::<T::Balance>();
153
154 if let Call::set_next_consensus_chain_byte_fee {
155 transaction_byte_fee,
156 } = call
157 {
158 if transaction_byte_fee != &provided_transaction_byte_fee {
159 return Err(InherentError::IncorrectConsensusChainByteFee);
160 }
161 }
162
163 Ok(())
164 }
165
166 fn is_inherent(call: &Self::Call) -> bool {
167 matches!(call, Call::set_next_consensus_chain_byte_fee { .. })
168 }
169 }
170
171 impl<T: Config> Pallet<T> {
172 pub fn note_domain_execution_fee(rewards: T::Balance) {
175 CollectedBlockFees::<T>::mutate(|block_fees| {
176 block_fees.domain_execution_fee =
177 block_fees.domain_execution_fee.saturating_add(rewards);
178 });
179 }
180
181 pub fn note_consensus_storage_fee(storage_fee: T::Balance) {
183 CollectedBlockFees::<T>::mutate(|block_fees| {
184 block_fees.consensus_storage_fee =
185 block_fees.consensus_storage_fee.saturating_add(storage_fee);
186 });
187 }
188
189 pub fn note_burned_balance(burned_balance: T::Balance) {
191 CollectedBlockFees::<T>::mutate(|block_fees| {
192 block_fees.burned_balance =
193 block_fees.burned_balance.saturating_add(burned_balance);
194 });
195 }
196
197 pub fn note_chain_rewards(chain_id: ChainId, balance: T::Balance) {
199 CollectedBlockFees::<T>::mutate(|block_fees| {
200 let total_balance = match block_fees.chain_rewards.get(&chain_id) {
201 None => balance,
202 Some(prev_balance) => prev_balance.saturating_add(balance),
203 };
204 block_fees.chain_rewards.insert(chain_id, total_balance)
205 });
206 }
207
208 pub fn final_domain_transaction_byte_fee() -> T::Balance {
214 ConsensusChainByteFee::<T>::get().saturating_add(T::DomainChainByteFee::get())
215 }
216
217 pub fn block_fees_storage_key() -> Vec<u8> {
218 CollectedBlockFees::<T>::storage_value_final_key().to_vec()
219 }
220 }
221}