Skip to content

Commit

Permalink
rename None to Index in idle-scheduler module
Browse files Browse the repository at this point in the history
  • Loading branch information
zjb0807 committed Apr 12, 2024
1 parent 1bead11 commit 7aa13f3
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 35 deletions.
1 change: 1 addition & 0 deletions modules/evm/src/bench/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ parameter_types! {
impl module_idle_scheduler::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Index = Nonce;
type Task = ScheduledTasks;
type MinimumWeightRemainInBlock = MinimumWeightRemainInBlock;
type RelayChainBlockNumberProvider = MockBlockNumberProvider;
Expand Down
18 changes: 9 additions & 9 deletions modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub mod module {
type Task: DispatchableTask + FullCodec + Debug + Clone + PartialEq + TypeInfo + From<EvmTask<Self>>;

/// Idle scheduler for the evm task.
type IdleScheduler: IdleScheduler<Self::Task>;
type IdleScheduler: IdleScheduler<Nonce, Self::Task>;

/// Weight information for the extrinsics in this module.
type WeightInfo: WeightInfo;
Expand All @@ -259,13 +259,13 @@ pub mod module {
}

#[derive(Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, TypeInfo)]
pub struct AccountInfo<Index> {
pub nonce: Index,
pub struct AccountInfo<Nonce> {
pub nonce: Nonce,
pub contract_info: Option<ContractInfo>,
}

impl<Index> AccountInfo<Index> {
pub fn new(nonce: Index, contract_info: Option<ContractInfo>) -> Self {
impl<Nonce> AccountInfo<Nonce> {
pub fn new(nonce: Nonce, contract_info: Option<ContractInfo>) -> Self {
Self { nonce, contract_info }
}
}
Expand All @@ -278,9 +278,9 @@ pub mod module {

#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Serialize, Deserialize)]
/// Account definition used for genesis block construction.
pub struct GenesisAccount<Balance, Index> {
pub struct GenesisAccount<Balance, Nonce> {
/// Account nonce.
pub nonce: Index,
pub nonce: Nonce,
/// Account balance.
pub balance: Balance,
/// Full account storage.
Expand Down Expand Up @@ -432,8 +432,8 @@ pub mod module {
let out = runtime.machine().return_value();
<Pallet<T>>::create_contract(source, *address, true, out);

for (index, value) in &account.storage {
AccountStorages::<T>::insert(address, index, value);
for (nonce, value) in &account.storage {
AccountStorages::<T>::insert(address, nonce, value);
}
}
});
Expand Down
1 change: 1 addition & 0 deletions modules/evm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ parameter_types! {
impl module_idle_scheduler::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Index = Nonce;
type Task = ScheduledTasks;
type MinimumWeightRemainInBlock = MinimumWeightRemainInBlock;
type RelayChainBlockNumberProvider = MockBlockNumberProvider;
Expand Down
35 changes: 19 additions & 16 deletions modules/idle-scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub use module_support::{DispatchableTask, IdleScheduler};
use parity_scale_codec::FullCodec;
use primitives::{task::TaskResult, BlockNumber, Nonce};
use primitives::{task::TaskResult, BlockNumber};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{BlockNumberProvider, One},
traits::{BlockNumberProvider, CheckedAdd, One},
ArithmeticError,
};
use sp_std::{cmp::PartialEq, fmt::Debug, prelude::*};
Expand All @@ -53,6 +53,9 @@ pub mod module {
/// Weight information for the extrinsics in this module.
type WeightInfo: WeightInfo;

/// The index of tasks.
type Index: Parameter + Default + One + CheckedAdd + Copy + TypeInfo;

/// Dispatchable tasks.
type Task: DispatchableTask + FullCodec + Debug + Clone + PartialEq + TypeInfo;

Expand All @@ -74,22 +77,22 @@ pub mod module {
#[pallet::generate_deposit(pub fn deposit_event)]
pub enum Event<T: Config> {
/// A task has been dispatched on_idle.
TaskDispatched { task_id: Nonce, result: DispatchResult },
TaskDispatched { task_id: T::Index, result: DispatchResult },
/// A task is added.
TaskAdded { task_id: Nonce, task: T::Task },
TaskAdded { task_id: T::Index, task: T::Task },
}

/// The schedule tasks waiting to dispatch. After task is dispatched, it's removed.
///
/// Tasks: map Nonce => Task
/// Tasks: map T::Index => Task
#[pallet::storage]
#[pallet::getter(fn tasks)]
pub type Tasks<T: Config> = StorageMap<_, Twox64Concat, Nonce, T::Task, OptionQuery>;
pub type Tasks<T: Config> = StorageMap<_, Twox64Concat, T::Index, T::Task, OptionQuery>;

/// The task id used to index tasks.
#[pallet::storage]
#[pallet::getter(fn next_task_id)]
pub type NextTaskId<T: Config> = StorageValue<_, Nonce, ValueQuery>;
pub type NextTaskId<T: Config> = StorageValue<_, T::Index, ValueQuery>;

/// A temporary variable used to check if should skip dispatch schedule task or not.
#[pallet::storage]
Expand Down Expand Up @@ -151,18 +154,18 @@ pub mod module {

impl<T: Config> Pallet<T> {
/// Add the task to the queue to be dispatched later.
fn do_schedule_task(task: T::Task) -> Result<Nonce, DispatchError> {
fn do_schedule_task(task: T::Task) -> Result<T::Index, DispatchError> {
let id = Self::get_next_task_id()?;
Tasks::<T>::insert(id, &task);
Self::deposit_event(Event::<T>::TaskAdded { task_id: id, task });
Ok(id)
}

/// Retrieves the next task ID from storage, and increment it by one.
fn get_next_task_id() -> Result<Nonce, DispatchError> {
NextTaskId::<T>::mutate(|current| -> Result<Nonce, DispatchError> {
fn get_next_task_id() -> Result<T::Index, DispatchError> {
NextTaskId::<T>::mutate(|current| -> Result<T::Index, DispatchError> {
let id = *current;
*current = current.checked_add(One::one()).ok_or(ArithmeticError::Overflow)?;
*current = current.checked_add(&One::one()).ok_or(ArithmeticError::Overflow)?;
Ok(id)
})
}
Expand All @@ -175,7 +178,7 @@ impl<T: Config> Pallet<T> {
return total_weight;
}

let mut completed_tasks: Vec<(Nonce, TaskResult)> = vec![];
let mut completed_tasks: Vec<(T::Index, TaskResult)> = vec![];

for (id, task) in Tasks::<T>::iter() {
let result = task.dispatch(weight_remaining);
Expand All @@ -197,7 +200,7 @@ impl<T: Config> Pallet<T> {
}

/// Removes completed tasks and deposits events.
pub fn remove_completed_tasks(completed_tasks: Vec<(Nonce, TaskResult)>) {
pub fn remove_completed_tasks(completed_tasks: Vec<(T::Index, TaskResult)>) {
// Deposit event and remove completed tasks.
for (id, result) in completed_tasks {
Self::deposit_event(Event::<T>::TaskDispatched {
Expand All @@ -209,16 +212,16 @@ impl<T: Config> Pallet<T> {
}
}

impl<T: Config> IdleScheduler<T::Task> for Pallet<T> {
fn schedule(task: T::Task) -> Result<Nonce, DispatchError> {
impl<T: Config> IdleScheduler<T::Index, T::Task> for Pallet<T> {
fn schedule(task: T::Task) -> Result<T::Index, DispatchError> {
Self::do_schedule_task(task)
}

/// If the task can be executed under given weight limit, dispatch it.
/// Otherwise the scheduler will keep the task and run it later.
/// NOTE: Only used for synchronous execution case, because `T::WeightInfo::clear_tasks()` is
/// not considered.
fn dispatch(id: Nonce, weight_limit: Weight) -> Weight {
fn dispatch(id: T::Index, weight_limit: Weight) -> Weight {
if let Some(task) = Tasks::<T>::get(id) {
let result = task.dispatch(weight_limit);
let used_weight = result.used_weight;
Expand Down
4 changes: 2 additions & 2 deletions modules/idle-scheduler/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use crate as module_idle_scheduler;
use frame_support::weights::Weight;
use frame_support::{construct_runtime, derive_impl, parameter_types, traits::ConstU32};
use module_support::DispatchableTask;
use primitives::{define_combined_task, task::TaskResult};
pub use sp_runtime::offchain::storage::StorageValueRef;
use primitives::{define_combined_task, task::TaskResult, Nonce};
use sp_runtime::BuildStorage;

use super::*;
Expand Down Expand Up @@ -62,6 +61,7 @@ parameter_types! {
impl module_idle_scheduler::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Index = Nonce;
type Task = ScheduledTasks;
type MinimumWeightRemainInBlock = MinimumWeightRemainInBlock;
type RelayChainBlockNumberProvider = MockBlockNumberProvider;
Expand Down
14 changes: 7 additions & 7 deletions modules/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#![allow(clippy::type_complexity)]

use frame_support::pallet_prelude::{DispatchClass, Pays, Weight};
use primitives::{task::TaskResult, Balance, CurrencyId, Multiplier, Nonce, ReserveIdentifier};
use primitives::{task::TaskResult, Balance, CurrencyId, Multiplier, ReserveIdentifier};
use sp_runtime::{
traits::CheckedDiv, transaction_validity::TransactionValidityError, DispatchError, DispatchResult, FixedU128,
};
Expand Down Expand Up @@ -139,9 +139,9 @@ pub trait DispatchableTask {
}

/// Idle scheduler trait
pub trait IdleScheduler<Task> {
fn schedule(task: Task) -> Result<Nonce, DispatchError>;
fn dispatch(id: Nonce, weight: Weight) -> Weight;
pub trait IdleScheduler<Index, Task> {
fn schedule(task: Task) -> Result<Index, DispatchError>;
fn dispatch(id: Index, weight: Weight) -> Weight;
}

#[cfg(feature = "std")]
Expand All @@ -152,11 +152,11 @@ impl DispatchableTask for () {
}

#[cfg(feature = "std")]
impl<Task> IdleScheduler<Task> for () {
fn schedule(_task: Task) -> Result<Nonce, DispatchError> {
impl<Index, Task> IdleScheduler<Index, Task> for () {
fn schedule(_task: Task) -> Result<Index, DispatchError> {
unimplemented!()
}
fn dispatch(_id: Nonce, _weight: Weight) -> Weight {
fn dispatch(_id: Index, _weight: Weight) -> Weight {
unimplemented!()
}
}
Expand Down
1 change: 1 addition & 0 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,7 @@ parameter_types!(
impl module_idle_scheduler::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Index = Nonce;
type Task = ScheduledTasks;
type MinimumWeightRemainInBlock = MinimumWeightRemainInBlock;
type RelayChainBlockNumberProvider = RelaychainDataProvider<Runtime>;
Expand Down
3 changes: 2 additions & 1 deletion runtime/common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use module_support::{mocks::MockAddressMapping, DispatchableTask};
use orml_traits::parameter_type_with_key;
use parity_scale_codec::{Decode, Encode};
use primitives::{
define_combined_task, evm::convert_decimals_to_evm, task::TaskResult, Amount, BlockNumber, CurrencyId,
define_combined_task, evm::convert_decimals_to_evm, task::TaskResult, Amount, BlockNumber, CurrencyId, Nonce,
ReserveIdentifier, TokenSymbol,
};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -136,6 +136,7 @@ parameter_types! {
impl module_idle_scheduler::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Index = Nonce;
type Task = ScheduledTasks;
type MinimumWeightRemainInBlock = MinimumWeightRemainInBlock;
type RelayChainBlockNumberProvider = MockBlockNumberProvider;
Expand Down
1 change: 1 addition & 0 deletions runtime/common/src/precompile/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ parameter_types! {
impl module_idle_scheduler::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Index = Nonce;
type Task = ScheduledTasks;
type MinimumWeightRemainInBlock = MinimumWeightRemainInBlock;
type RelayChainBlockNumberProvider = MockBlockNumberProvider;
Expand Down
1 change: 1 addition & 0 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,7 @@ parameter_types!(
impl module_idle_scheduler::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Index = Nonce;
type Task = ScheduledTasks;
type MinimumWeightRemainInBlock = MinimumWeightRemainInBlock;
type RelayChainBlockNumberProvider = RelaychainDataProvider<Runtime>;
Expand Down
1 change: 1 addition & 0 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,7 @@ parameter_types!(
impl module_idle_scheduler::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = weights::module_idle_scheduler::WeightInfo<Runtime>;
type Index = Nonce;
type Task = ScheduledTasks;
type MinimumWeightRemainInBlock = MinimumWeightRemainInBlock;
type RelayChainBlockNumberProvider = RelaychainDataProvider<Runtime>;
Expand Down

0 comments on commit 7aa13f3

Please sign in to comment.