#![feature(associated_type_defaults)]
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
pub use crate::pallet::*;
use frame_support::{pallet_prelude::Weight, traits::Get};
pub use t3rn_primitives::{
account_manager::AccountManager, claimable::ClaimableArtifacts, clock::Clock,
common::RoundInfo, executors::Executors, gateway::GatewayABIConfig, ChainId, EscrowTrait,
GatewayGenesisConfig, GatewayType, GatewayVendor,
};
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
mod weights;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use t3rn_primitives::clock::OnHookQueues;
const FIVE: u64 = 5;
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
#[pallet::constant]
type RoundDuration: Get<BlockNumberFor<Self>>;
type OnInitializeQueues: OnHookQueues<Self>;
type OnFinalizeQueues: OnHookQueues<Self>;
}
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::storage]
#[pallet::getter(fn current_round)]
pub type CurrentRound<T: Config> = StorageValue<_, RoundInfo<BlockNumberFor<T>>, ValueQuery>;
impl<T: Config> Pallet<T> {
pub fn check_bump_round(n: frame_system::pallet_prelude::BlockNumberFor<T>) -> Weight {
let past_round = <CurrentRound<T>>::get();
let term = T::RoundDuration::get();
let new_round = RoundInfo {
index: past_round.index.saturating_add(1),
head: n,
term,
};
log::debug!(
"check_bump_round: past_round: {:?}, new_round: {:?}",
past_round,
new_round
);
if past_round.should_update(n) {
<CurrentRound<T>>::put(new_round);
Self::deposit_event(Event::NewRound {
index: new_round.index,
head: new_round.head,
term: new_round.term,
});
T::DbWeight::get().reads_writes(2, 1)
} else {
T::DbWeight::get().reads(2)
}
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(n: frame_system::pallet_prelude::BlockNumberFor<T>) {
let max_on_finalize_weight = T::BlockWeights::get().max_block.saturating_div(FIVE);
log::debug!(
"Clock::on_finalize process hooks with max_on_finalize_weight: {:?}",
max_on_finalize_weight
);
T::OnFinalizeQueues::process(n, max_on_finalize_weight);
}
fn on_initialize(n: frame_system::pallet_prelude::BlockNumberFor<T>) -> Weight {
let max_on_initialize_weight = T::BlockWeights::get().max_block.saturating_div(FIVE);
log::debug!(
"Clock::on_initialize process hooks with max_on_initialize_weight: {:?} and block number: {:?}",
max_on_initialize_weight,
n
);
T::OnInitializeQueues::process(n, max_on_initialize_weight)
}
fn offchain_worker(_n: frame_system::pallet_prelude::BlockNumberFor<T>) {
}
}
#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
NewRound {
index: u32,
head: frame_system::pallet_prelude::BlockNumberFor<T>,
term: frame_system::pallet_prelude::BlockNumberFor<T>,
},
}
#[pallet::error]
pub enum Error<T> {}
#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
#[serde(skip)]
pub _marker: PhantomData<T>,
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {}
}
impl<T: Config> Clock<T> for Pallet<T> {
fn current_round() -> RoundInfo<BlockNumberFor<T>> {
Self::current_round()
}
fn round_duration() -> frame_system::pallet_prelude::BlockNumberFor<T> {
T::RoundDuration::get()
}
}
}