#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(not(feature = "std"))]
extern crate alloc;
pub mod fees;
pub use pallet::*;
#[frame_support::pallet]
mod pallet {
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Codec, MaxEncodedLen};
use frame_support::pallet_prelude::*;
use frame_support::storage::generator::StorageValue as _;
use frame_system::pallet_prelude::*;
use scale_info::TypeInfo;
use sp_block_fees::{InherentError, InherentType, INHERENT_IDENTIFIER};
use sp_domains::{BlockFees, ChainId};
use sp_runtime::traits::{AtLeast32BitUnsigned, MaybeSerializeDeserialize, Saturating};
use sp_runtime::{FixedPointOperand, SaturatedConversion};
use sp_std::fmt::Debug;
use sp_std::result;
#[pallet::config]
pub trait Config: frame_system::Config {
type Balance: Parameter
+ Member
+ AtLeast32BitUnsigned
+ Codec
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Debug
+ MaxEncodedLen
+ TypeInfo
+ FixedPointOperand;
type DomainChainByteFee: Get<Self::Balance>;
}
#[pallet::storage]
#[pallet::getter(fn collected_block_fees)]
pub(super) type CollectedBlockFees<T: Config> =
StorageValue<_, BlockFees<T::Balance>, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn consensus_chain_byte_fee)]
pub(super) type ConsensusChainByteFee<T: Config> = StorageValue<_, T::Balance, ValueQuery>;
#[pallet::storage]
pub(super) type NextConsensusChainByteFee<T: Config> = StorageValue<_, T::Balance, ValueQuery>;
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_now: BlockNumberFor<T>) -> Weight {
CollectedBlockFees::<T>::set(BlockFees::<T::Balance>::default());
T::DbWeight::get().writes(1)
}
fn on_finalize(_now: BlockNumberFor<T>) {
let transaction_byte_fee = NextConsensusChainByteFee::<T>::take();
ConsensusChainByteFee::<T>::put(transaction_byte_fee);
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight((
Weight::from_all(10_000),
DispatchClass::Mandatory
))]
pub fn set_next_consensus_chain_byte_fee(
origin: OriginFor<T>,
#[pallet::compact] transaction_byte_fee: T::Balance,
) -> DispatchResult {
ensure_none(origin)?;
NextConsensusChainByteFee::<T>::put(transaction_byte_fee);
Ok(())
}
}
#[pallet::inherent]
impl<T: Config> ProvideInherent for Pallet<T> {
type Call = Call<T>;
type Error = InherentError;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
let inherent_data = data
.get_data::<InherentType>(&INHERENT_IDENTIFIER)
.expect("Domain block fees inherent data not correctly encoded")
.expect("Domain block fees inherent data must be provided");
let transaction_byte_fee = inherent_data.saturated_into::<T::Balance>();
Some(Call::set_next_consensus_chain_byte_fee {
transaction_byte_fee,
})
}
fn check_inherent(
call: &Self::Call,
data: &InherentData,
) -> result::Result<(), Self::Error> {
let inherent_data = data
.get_data::<InherentType>(&INHERENT_IDENTIFIER)
.expect("Domain block fees inherent data not correctly encoded")
.expect("Domain block fees inherent data must be provided");
let provided_transaction_byte_fee = inherent_data.saturated_into::<T::Balance>();
if let Call::set_next_consensus_chain_byte_fee {
transaction_byte_fee,
} = call
{
if transaction_byte_fee != &provided_transaction_byte_fee {
return Err(InherentError::IncorrectConsensusChainByteFee);
}
}
Ok(())
}
fn is_inherent(call: &Self::Call) -> bool {
matches!(call, Call::set_next_consensus_chain_byte_fee { .. })
}
}
impl<T: Config> Pallet<T> {
pub fn note_domain_execution_fee(rewards: T::Balance) {
CollectedBlockFees::<T>::mutate(|block_fees| {
block_fees.domain_execution_fee =
block_fees.domain_execution_fee.saturating_add(rewards);
});
}
pub fn note_consensus_storage_fee(storage_fee: T::Balance) {
CollectedBlockFees::<T>::mutate(|block_fees| {
block_fees.consensus_storage_fee =
block_fees.consensus_storage_fee.saturating_add(storage_fee);
});
}
pub fn note_burned_balance(burned_balance: T::Balance) {
CollectedBlockFees::<T>::mutate(|block_fees| {
block_fees.burned_balance =
block_fees.burned_balance.saturating_add(burned_balance);
});
}
pub fn note_chain_rewards(chain_id: ChainId, balance: T::Balance) {
CollectedBlockFees::<T>::mutate(|block_fees| {
let total_balance = match block_fees.chain_rewards.get(&chain_id) {
None => balance,
Some(prev_balance) => prev_balance.saturating_add(balance),
};
block_fees.chain_rewards.insert(chain_id, total_balance)
});
}
pub fn final_domain_transaction_byte_fee() -> T::Balance {
ConsensusChainByteFee::<T>::get().saturating_add(T::DomainChainByteFee::get())
}
pub fn block_fees_storage_key() -> Vec<u8> {
CollectedBlockFees::<T>::storage_value_final_key().to_vec()
}
}
}