diff --git a/modules/evm/src/bench/mock.rs b/modules/evm/src/bench/mock.rs index 85f1744cdf..48ffe44054 100644 --- a/modules/evm/src/bench/mock.rs +++ b/modules/evm/src/bench/mock.rs @@ -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; diff --git a/modules/evm/src/lib.rs b/modules/evm/src/lib.rs index 50eaadfbe7..71fd023eca 100644 --- a/modules/evm/src/lib.rs +++ b/modules/evm/src/lib.rs @@ -245,7 +245,7 @@ pub mod module { type Task: DispatchableTask + FullCodec + Debug + Clone + PartialEq + TypeInfo + From>; /// Idle scheduler for the evm task. - type IdleScheduler: IdleScheduler; + type IdleScheduler: IdleScheduler; /// Weight information for the extrinsics in this module. type WeightInfo: WeightInfo; @@ -259,13 +259,13 @@ pub mod module { } #[derive(Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, TypeInfo)] - pub struct AccountInfo { - pub nonce: Index, + pub struct AccountInfo { + pub nonce: Nonce, pub contract_info: Option, } - impl AccountInfo { - pub fn new(nonce: Index, contract_info: Option) -> Self { + impl AccountInfo { + pub fn new(nonce: Nonce, contract_info: Option) -> Self { Self { nonce, contract_info } } } @@ -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 { + pub struct GenesisAccount { /// Account nonce. - pub nonce: Index, + pub nonce: Nonce, /// Account balance. pub balance: Balance, /// Full account storage. @@ -432,8 +432,8 @@ pub mod module { let out = runtime.machine().return_value(); >::create_contract(source, *address, true, out); - for (index, value) in &account.storage { - AccountStorages::::insert(address, index, value); + for (nonce, value) in &account.storage { + AccountStorages::::insert(address, nonce, value); } } }); diff --git a/modules/evm/src/mock.rs b/modules/evm/src/mock.rs index 9c1653de49..1a27dd39ae 100644 --- a/modules/evm/src/mock.rs +++ b/modules/evm/src/mock.rs @@ -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; diff --git a/modules/idle-scheduler/src/lib.rs b/modules/idle-scheduler/src/lib.rs index 5c4b87641f..d373de69b9 100644 --- a/modules/idle-scheduler/src/lib.rs +++ b/modules/idle-scheduler/src/lib.rs @@ -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::*}; @@ -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; @@ -74,22 +77,22 @@ pub mod module { #[pallet::generate_deposit(pub fn deposit_event)] pub enum Event { /// 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 = StorageMap<_, Twox64Concat, Nonce, T::Task, OptionQuery>; + pub type Tasks = 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 = StorageValue<_, Nonce, ValueQuery>; + pub type NextTaskId = StorageValue<_, T::Index, ValueQuery>; /// A temporary variable used to check if should skip dispatch schedule task or not. #[pallet::storage] @@ -151,7 +154,7 @@ pub mod module { impl Pallet { /// Add the task to the queue to be dispatched later. - fn do_schedule_task(task: T::Task) -> Result { + fn do_schedule_task(task: T::Task) -> Result { let id = Self::get_next_task_id()?; Tasks::::insert(id, &task); Self::deposit_event(Event::::TaskAdded { task_id: id, task }); @@ -159,10 +162,10 @@ impl Pallet { } /// Retrieves the next task ID from storage, and increment it by one. - fn get_next_task_id() -> Result { - NextTaskId::::mutate(|current| -> Result { + fn get_next_task_id() -> Result { + NextTaskId::::mutate(|current| -> Result { 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) }) } @@ -175,7 +178,7 @@ impl Pallet { 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::::iter() { let result = task.dispatch(weight_remaining); @@ -197,7 +200,7 @@ impl Pallet { } /// 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::::TaskDispatched { @@ -209,8 +212,8 @@ impl Pallet { } } -impl IdleScheduler for Pallet { - fn schedule(task: T::Task) -> Result { +impl IdleScheduler for Pallet { + fn schedule(task: T::Task) -> Result { Self::do_schedule_task(task) } @@ -218,7 +221,7 @@ impl IdleScheduler for Pallet { /// 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::::get(id) { let result = task.dispatch(weight_limit); let used_weight = result.used_weight; diff --git a/modules/idle-scheduler/src/mock.rs b/modules/idle-scheduler/src/mock.rs index f3aa1e4e45..71ab1ae6ad 100644 --- a/modules/idle-scheduler/src/mock.rs +++ b/modules/idle-scheduler/src/mock.rs @@ -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::*; @@ -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; diff --git a/modules/support/src/lib.rs b/modules/support/src/lib.rs index d2ca4f104a..7828a4f476 100644 --- a/modules/support/src/lib.rs +++ b/modules/support/src/lib.rs @@ -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, }; @@ -139,9 +139,9 @@ pub trait DispatchableTask { } /// Idle scheduler trait -pub trait IdleScheduler { - fn schedule(task: Task) -> Result; - fn dispatch(id: Nonce, weight: Weight) -> Weight; +pub trait IdleScheduler { + fn schedule(task: Task) -> Result; + fn dispatch(id: Index, weight: Weight) -> Weight; } #[cfg(feature = "std")] @@ -152,11 +152,11 @@ impl DispatchableTask for () { } #[cfg(feature = "std")] -impl IdleScheduler for () { - fn schedule(_task: Task) -> Result { +impl IdleScheduler for () { + fn schedule(_task: Task) -> Result { unimplemented!() } - fn dispatch(_id: Nonce, _weight: Weight) -> Weight { + fn dispatch(_id: Index, _weight: Weight) -> Weight { unimplemented!() } } diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index c5ce678594..dab6332264 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -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; diff --git a/runtime/common/src/mock.rs b/runtime/common/src/mock.rs index ec4f58a480..f750a913b4 100644 --- a/runtime/common/src/mock.rs +++ b/runtime/common/src/mock.rs @@ -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; @@ -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; diff --git a/runtime/common/src/precompile/mock.rs b/runtime/common/src/precompile/mock.rs index d925889724..e82baf67c4 100644 --- a/runtime/common/src/precompile/mock.rs +++ b/runtime/common/src/precompile/mock.rs @@ -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; diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index b36513aa51..3cc5ad92f7 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -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; diff --git a/runtime/mandala/src/lib.rs b/runtime/mandala/src/lib.rs index adf81885f7..83373d1d7e 100644 --- a/runtime/mandala/src/lib.rs +++ b/runtime/mandala/src/lib.rs @@ -1799,6 +1799,7 @@ parameter_types!( impl module_idle_scheduler::Config for Runtime { type RuntimeEvent = RuntimeEvent; type WeightInfo = weights::module_idle_scheduler::WeightInfo; + type Index = Nonce; type Task = ScheduledTasks; type MinimumWeightRemainInBlock = MinimumWeightRemainInBlock; type RelayChainBlockNumberProvider = RelaychainDataProvider;