-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] xtokens integrate delay-tasks to delay transfer_assets #981
Draft
wangjj9219
wants to merge
8
commits into
master
Choose a base branch
from
xtokens-delay-execute
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0d1ca5c
add delay-tasks
wangjj9219 cb41ec5
update
wangjj9219 5d8c7ff
xtokens impl DelayedTask
wangjj9219 8523a1b
rate-limit return delay error
wangjj9219 ffd4ebb
refactor delay-tasks
wangjj9219 9d70e37
impl DelayTaskHooks for Task to process
wangjj9219 be4e526
xtokens consume rate-limiter by CurrencyId
wangjj9219 face78f
add tests for XTokensTaskHooks
wangjj9219 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
[package] | ||
name = "orml-delay-tasks" | ||
description = "Scheduler delay task and execute." | ||
repository = "https://github.com/open-web3-stack/open-runtime-module-library/tree/master/auction" | ||
license = "Apache-2.0" | ||
version = "0.7.0" | ||
authors = ["Acala Developers"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
log = { workspace = true } | ||
parity-scale-codec = { workspace = true } | ||
scale-info = { workspace = true } | ||
serde = { workspace = true, optional = true } | ||
|
||
frame-support = { workspace = true } | ||
frame-system = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
sp-std = { workspace = true } | ||
|
||
xcm = { workspace = true } | ||
|
||
orml-traits = { path = "../traits", version = "0.7.0", default-features = false } | ||
orml-xtokens = { path = "../xtokens", version = "0.7.0", default-features = false } | ||
|
||
[dev-dependencies] | ||
sp-core = { workspace = true, features = ["std"] } | ||
sp-io = { workspace = true, features = ["std"] } | ||
pallet-preimage = { workspace = true, features = ["std"] } | ||
pallet-scheduler = { workspace = true, features = ["std"] } | ||
|
||
[features] | ||
default = [ "std" ] | ||
std = [ | ||
"frame-support/std", | ||
"frame-system/std", | ||
"orml-traits/std", | ||
"orml-xtokens/std", | ||
"parity-scale-codec/std", | ||
"scale-info/std", | ||
"serde", | ||
"sp-runtime/std", | ||
"sp-std/std", | ||
"xcm/std", | ||
] | ||
try-runtime = [ | ||
"frame-support/try-runtime", | ||
"frame-system/try-runtime", | ||
"sp-runtime/try-runtime", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,289 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use frame_support::{ | ||
pallet_prelude::*, | ||
traits::{ | ||
schedule::{v1::Named as ScheduleNamed, DispatchTime}, | ||
OriginTrait, | ||
}, | ||
weights::Weight, | ||
}; | ||
use frame_system::pallet_prelude::*; | ||
use orml_traits::{ | ||
task::{DelayTaskHooks, DelayTasksManager, DispatchableTask, TaskResult}, | ||
MultiCurrency, NamedMultiReservableCurrency, | ||
}; | ||
use parity_scale_codec::FullCodec; | ||
use scale_info::TypeInfo; | ||
use sp_runtime::{ | ||
traits::{CheckedAdd, Convert, Zero}, | ||
ArithmeticError, | ||
}; | ||
use sp_std::fmt::Debug; | ||
use sp_std::marker::PhantomData; | ||
use xcm::v4::prelude::*; | ||
|
||
pub use module::*; | ||
|
||
mod mock; | ||
mod tests; | ||
|
||
pub const DELAY_TASK_ID: [u8; 8] = *b"orml/dts"; | ||
|
||
/// A delayed origin. Can only be dispatched via `dispatch_as` with a delay. | ||
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)] | ||
pub struct DelayedExecuteOrigin; | ||
|
||
pub struct EnsureDelayed; | ||
impl<O: Into<Result<DelayedExecuteOrigin, O>> + From<DelayedExecuteOrigin>> EnsureOrigin<O> for EnsureDelayed { | ||
type Success = (); | ||
fn try_origin(o: O) -> Result<Self::Success, O> { | ||
o.into().and_then(|_| Ok(())) | ||
} | ||
|
||
#[cfg(feature = "runtime-benchmarks")] | ||
fn try_successful_origin() -> Result<O, ()> { | ||
Ok(O::from(DelayedExecuteOrigin)) | ||
} | ||
} | ||
|
||
#[frame_support::pallet] | ||
pub mod module { | ||
use super::*; | ||
|
||
type Nonce = u64; | ||
|
||
/// Origin for the delay tasks module. | ||
#[pallet::origin] | ||
pub type Origin = DelayedExecuteOrigin; | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config { | ||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; | ||
|
||
type RuntimeCall: Parameter + From<Call<Self>>; | ||
|
||
/// The outer origin type. | ||
type RuntimeOrigin: From<DelayedExecuteOrigin> | ||
+ From<<Self as frame_system::Config>::RuntimeOrigin> | ||
+ OriginTrait<PalletsOrigin = Self::PalletsOrigin>; | ||
|
||
/// The caller origin, overarching type of all pallets origins. | ||
type PalletsOrigin: Parameter + Into<<Self as frame_system::Config>::RuntimeOrigin>; | ||
|
||
type DelayOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>; | ||
|
||
type GovernanceOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>; | ||
|
||
type Task: DispatchableTask + FullCodec + Debug + Clone + PartialEq + TypeInfo; | ||
|
||
/// The Scheduler. | ||
type Scheduler: ScheduleNamed<BlockNumberFor<Self>, <Self as Config>::RuntimeCall, Self::PalletsOrigin>; | ||
|
||
type DelayTaskHooks: DelayTaskHooks<Self::Task>; | ||
|
||
/// Convert `Location` to `CurrencyId`. | ||
type CurrencyIdConvert: Convert< | ||
Location, | ||
Option<<Self::Currency as MultiCurrency<Self::AccountId>>::CurrencyId>, | ||
>; | ||
|
||
type Currency: NamedMultiReservableCurrency<Self::AccountId>; | ||
|
||
type ReserveId: Get<<Self::Currency as NamedMultiReservableCurrency<Self::AccountId>>::ReserveIdentifier>; | ||
} | ||
|
||
#[pallet::error] | ||
pub enum Error<T> { | ||
InvalidDelayBlock, | ||
InvalidId, | ||
FailedToSchedule, | ||
} | ||
|
||
#[pallet::event] | ||
#[pallet::generate_deposit(pub(crate) fn deposit_event)] | ||
pub enum Event<T: Config> { | ||
DelayedTaskAdded { | ||
id: Nonce, | ||
task: T::Task, | ||
execute_block: BlockNumberFor<T>, | ||
}, | ||
DelayedTaskExecuted { | ||
id: Nonce, | ||
result: DispatchResult, | ||
}, | ||
DelayedTaskReDelayed { | ||
id: Nonce, | ||
execute_block: BlockNumberFor<T>, | ||
}, | ||
DelayedTaskCanceled { | ||
id: Nonce, | ||
}, | ||
} | ||
|
||
#[pallet::pallet] | ||
#[pallet::without_storage_info] | ||
pub struct Pallet<T>(_); | ||
|
||
#[pallet::hooks] | ||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {} | ||
|
||
#[pallet::storage] | ||
#[pallet::getter(fn next_delayed_task_id)] | ||
pub type NextDelayedTaskId<T: Config> = StorageValue<_, Nonce, ValueQuery>; | ||
|
||
#[pallet::storage] | ||
#[pallet::getter(fn delayed_tasks)] | ||
pub type DelayedTasks<T: Config> = StorageMap<_, Twox64Concat, Nonce, (T::Task, BlockNumberFor<T>), OptionQuery>; | ||
|
||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
#[pallet::call_index(0)] | ||
#[pallet::weight(Weight::zero())] | ||
pub fn delayed_execute(origin: OriginFor<T>, id: Nonce) -> DispatchResult { | ||
T::DelayOrigin::ensure_origin(origin)?; | ||
|
||
let execute_result = Self::do_delayed_execute(id)?; | ||
|
||
Self::deposit_event(Event::<T>::DelayedTaskExecuted { | ||
id, | ||
result: execute_result.result, | ||
}); | ||
Ok(()) | ||
} | ||
|
||
#[pallet::call_index(1)] | ||
#[pallet::weight(Weight::zero())] | ||
pub fn reschedule_delay_task( | ||
origin: OriginFor<T>, | ||
id: Nonce, | ||
when: DispatchTime<BlockNumberFor<T>>, | ||
) -> DispatchResult { | ||
T::GovernanceOrigin::ensure_origin(origin)?; | ||
|
||
DelayedTasks::<T>::try_mutate_exists(id, |maybe_task| -> DispatchResult { | ||
let (_, execute_block) = maybe_task.as_mut().ok_or(Error::<T>::InvalidId)?; | ||
|
||
let now = frame_system::Pallet::<T>::block_number(); | ||
let new_execute_block = match when { | ||
DispatchTime::At(x) => x, | ||
DispatchTime::After(x) => x.checked_add(&now).ok_or(ArithmeticError::Overflow)?, | ||
}; | ||
ensure!(new_execute_block > now, Error::<T>::InvalidDelayBlock); | ||
|
||
*execute_block = new_execute_block; | ||
|
||
T::Scheduler::reschedule_named((&DELAY_TASK_ID, id).encode(), DispatchTime::At(new_execute_block)) | ||
.map_err(|_| Error::<T>::FailedToSchedule)?; | ||
|
||
Self::deposit_event(Event::<T>::DelayedTaskReDelayed { | ||
id, | ||
execute_block: new_execute_block, | ||
}); | ||
Ok(()) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[pallet::call_index(2)] | ||
#[pallet::weight(Weight::zero())] | ||
pub fn cancel_delayed_task(origin: OriginFor<T>, id: Nonce) -> DispatchResult { | ||
T::GovernanceOrigin::ensure_origin(origin)?; | ||
|
||
let (task, _) = DelayedTasks::<T>::take(id).ok_or(Error::<T>::InvalidId)?; | ||
|
||
// pre cancel | ||
T::DelayTaskHooks::on_cancel(&task)?; | ||
|
||
T::Scheduler::cancel_named((&DELAY_TASK_ID, id).encode()).map_err(|_| Error::<T>::FailedToSchedule)?; | ||
|
||
Self::deposit_event(Event::<T>::DelayedTaskCanceled { id }); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: Config> Pallet<T> { | ||
pub(crate) fn do_delayed_execute(id: Nonce) -> sp_std::result::Result<TaskResult, DispatchError> { | ||
let (delayed_task, _) = DelayedTasks::<T>::take(id).ok_or(Error::<T>::InvalidId)?; | ||
|
||
// pre delayed dispatch | ||
T::DelayTaskHooks::pre_delayed_execute(&delayed_task)?; | ||
|
||
Ok(delayed_task.dispatch(Weight::zero())) | ||
} | ||
|
||
/// Retrieves the next delayed task ID from storage, and increment it by | ||
/// one. | ||
fn get_next_delayed_task_id() -> Result<Nonce, DispatchError> { | ||
NextDelayedTaskId::<T>::mutate(|current| -> Result<Nonce, DispatchError> { | ||
let id = *current; | ||
|
||
*current = current.checked_add(1).ok_or(ArithmeticError::Overflow)?; | ||
Ok(id) | ||
}) | ||
} | ||
} | ||
|
||
impl<T: Config> DelayTasksManager<T::Task, BlockNumberFor<T>> for Pallet<T> { | ||
fn add_delay_task(task: T::Task, delay_blocks: BlockNumberFor<T>) -> DispatchResult { | ||
ensure!(!delay_blocks.is_zero(), Error::<T>::InvalidDelayBlock); | ||
let execute_block = frame_system::Pallet::<T>::block_number() | ||
.checked_add(&delay_blocks) | ||
.ok_or(ArithmeticError::Overflow)?; | ||
|
||
// pre schedule delay task | ||
T::DelayTaskHooks::pre_delay(&task)?; | ||
|
||
let id = Self::get_next_delayed_task_id()?; | ||
let delayed_origin: <T as Config>::RuntimeOrigin = From::from(DelayedExecuteOrigin); | ||
let pallets_origin = delayed_origin.caller().clone(); | ||
|
||
T::Scheduler::schedule_named( | ||
(&DELAY_TASK_ID, id).encode(), | ||
DispatchTime::At(execute_block), | ||
None, | ||
Zero::zero(), | ||
pallets_origin, | ||
<T as Config>::RuntimeCall::from(Call::<T>::delayed_execute { id }), | ||
) | ||
.map_err(|_| Error::<T>::FailedToSchedule)?; | ||
|
||
DelayedTasks::<T>::insert(id, (&task, execute_block)); | ||
|
||
Self::deposit_event(Event::<T>::DelayedTaskAdded { | ||
id, | ||
task, | ||
execute_block, | ||
}); | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub struct DelayedXtokensTaskHooks<T>(PhantomData<T>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should avoid xcm/xtokens code in this pallet |
||
impl<T: Config + orml_xtokens::Config> DelayTaskHooks<orml_xtokens::XtokensTask<T>> for DelayedXtokensTaskHooks<T> { | ||
fn pre_delay(task: &orml_xtokens::XtokensTask<T>) -> DispatchResult { | ||
match task { | ||
orml_xtokens::XtokensTask::<T>::TransferAssets { who, assets, fee, .. } => {} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn pre_delayed_execute(task: &orml_xtokens::XtokensTask<T>) -> DispatchResult { | ||
match task { | ||
orml_xtokens::XtokensTask::<T>::TransferAssets { who, assets, fee, .. } => {} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn on_cancel(task: &orml_xtokens::XtokensTask<T>) -> DispatchResult { | ||
match task { | ||
orml_xtokens::XtokensTask::<T>::TransferAssets { who, assets, fee, .. } => {} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should try to reuse the one from orml-authority. we can move it to orml-utils