Skip to content

Commit

Permalink
Remove schedule notify extrinsic (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
v9n authored Jul 6, 2023
1 parent 1cb7c36 commit 450d44c
Show file tree
Hide file tree
Showing 6 changed files with 539 additions and 247 deletions.
27 changes: 0 additions & 27 deletions pallets/automation-time/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,33 +162,6 @@ fn increment_provided_id(mut provided_id: Vec<u8>) -> Vec<u8> {
}

benchmarks! {
schedule_notify_task_empty {
let caller: T::AccountId = account("caller", 0, SEED);
let time: u64 = 7200;
let time_moment: u32 = time.try_into().unwrap();
Timestamp::<T>::set_timestamp(time_moment.into());
let transfer_amount = T::Currency::minimum_balance().saturating_mul(ED_MULTIPLIER.into());
T::Currency::deposit_creating(&caller, transfer_amount.clone().saturating_mul(DEPOSIT_MULTIPLIER.into()));
}: schedule_notify_task(RawOrigin::Signed(caller), vec![10], vec![time], vec![4, 5])

schedule_notify_task_full {
let v in 1 .. T::MaxExecutionTimes::get();

let caller: T::AccountId = account("caller", 0, SEED);
let time: u64 = 7200;

let mut times: Vec<u64> = vec![];
for i in 0..v {
let hour: u64 = (3600 * (i + 1)).try_into().unwrap();
times.push(hour);
}

let mut provided_id = schedule_notify_tasks::<T>(caller.clone(), times.clone(), T::MaxTasksPerSlot::get() - 1);
provided_id = increment_provided_id(provided_id);
let transfer_amount = T::Currency::minimum_balance().saturating_mul(ED_MULTIPLIER.into());
T::Currency::deposit_creating(&caller, transfer_amount.clone().saturating_mul(DEPOSIT_MULTIPLIER.into()));
}: schedule_notify_task(RawOrigin::Signed(caller), provided_id, times, vec![4, 5])

schedule_xcmp_task_full {
let v in 1..T::MaxExecutionTimes::get();

Expand Down
1 change: 1 addition & 0 deletions pallets/automation-time/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ where

// Manually check for ExistenceRequirement since MultiCurrency doesn't currently support it
let free_balance = T::MultiCurrency::free_balance(self.currency_id, &self.owner);

free_balance
.checked_sub(&fee)
.ok_or(DispatchError::Token(BelowMinimum))?
Expand Down
50 changes: 6 additions & 44 deletions pallets/automation-time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,63 +349,21 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: T::BlockNumber) -> Weight {
fn on_initialize(_block: T::BlockNumber) -> Weight {
if Self::is_shutdown() == true {
return T::DbWeight::get().reads(1u64)
}

let max_weight: Weight = Weight::from_ref_time(
T::MaxWeightPercentage::get().mul_floor(T::MaxBlockWeight::get()),
);

Self::trigger_tasks(max_weight)
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Schedule a task to fire an event with a custom message.
///
/// Before the task can be scheduled the task must past validation checks.
/// * The transaction is signed
/// * The provided_id's length > 0
/// * The message's length > 0
/// * The times are valid
///
/// # Parameters
/// * `provided_id`: An id provided by the user. This id must be unique for the user.
/// * `execution_times`: The list of unix standard times in seconds for when the task should run.
/// * `message`: The message you want the event to have.
///
/// # Errors
/// * `InvalidTime`: Time must end in a whole hour.
/// * `PastTime`: Time must be in the future.
/// * `EmptyMessage`: The message cannot be empty.
/// * `DuplicateTask`: There can be no duplicate tasks.
/// * `TimeTooFarOut`: Execution time or frequency are past the max time horizon.
/// * `TimeSlotFull`: Time slot is full. No more tasks can be scheduled for this time.
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::schedule_notify_task_full(execution_times.len().try_into().unwrap()))]
pub fn schedule_notify_task(
origin: OriginFor<T>,
provided_id: Vec<u8>,
execution_times: Vec<UnixTime>,
message: Vec<u8>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
if message.len() == 0 {
Err(Error::<T>::EmptyMessage)?
}

let schedule = Schedule::new_fixed_schedule::<T>(execution_times)?;
Self::validate_and_schedule_task(
Action::Notify { message },
who,
provided_id,
schedule,
)?;
Ok(().into())
}

/// Schedule a task to transfer native token balance from sender to recipient.
///
/// Before the task can be scheduled the task must past validation checks.
Expand Down Expand Up @@ -724,6 +682,7 @@ pub mod pallet {
// It might take multiple blocks to fully catch up, so we limit update to a max weight.
let max_update_weight: Weight =
Weight::from_ref_time(T::UpdateQueueRatio::get().mul_floor(weight_left.ref_time()));

let update_weight = Self::update_task_queue(max_update_weight);

weight_left = weight_left.saturating_sub(update_weight);
Expand All @@ -732,6 +691,7 @@ pub mod pallet {
let run_task_weight = <T as Config>::WeightInfo::run_tasks_many_found(1)
.saturating_add(T::DbWeight::get().reads(1u64))
.saturating_add(T::DbWeight::get().writes(1u64));

if weight_left.ref_time() < run_task_weight.ref_time() {
return weight_left
}
Expand Down Expand Up @@ -793,6 +753,7 @@ pub mod pallet {
last_missed_slot,
missed_queue_allotted_weight,
);

LastTimeSlot::<T>::put((updated_last_time_slot, updated_last_missed_slot));
total_weight = total_weight
.saturating_add(missed_queue_update_weight)
Expand Down Expand Up @@ -1052,6 +1013,7 @@ pub mod pallet {
/// Fire the notify event with the custom message.
pub fn run_notify_task(message: Vec<u8>) -> (Weight, Option<DispatchError>) {
Self::deposit_event(Event::Notify { message });

(<T as Config>::WeightInfo::run_notify_task(), None)
}

Expand Down
46 changes: 39 additions & 7 deletions pallets/automation-time/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl<Test: frame_system::Config> pallet_automation_time::WeightInfo for MockWeig
Weight::from_ref_time(20_000)
}
fn shift_missed_tasks() -> Weight {
Weight::from_ref_time(20_000)
Weight::from_ref_time(900_000)
}
fn migration_upgrade_weight_struct(_: u32) -> Weight {
Weight::zero()
Expand Down Expand Up @@ -389,19 +389,51 @@ pub fn new_test_ext(state_block_time: u64) -> sp_io::TestExternalities {
ext
}

// A function to support test scheduleing a Fixed schedule
// We don't focus on making sure the execution run properly. We just focus on
// making sure a task is scheduled into the queue
pub fn schedule_task(
owner: [u8; 32],
provided_id: Vec<u8>,
scheduled_times: Vec<u64>,
message: Vec<u8>,
) -> sp_core::H256 {
get_funds(AccountId32::new(owner));
let task_hash_input = TaskHashInput::new(AccountId32::new(owner), provided_id.clone());
assert_ok!(AutomationTime::schedule_notify_task(
RuntimeOrigin::signed(AccountId32::new(owner)),
let account_id = AccountId32::new(owner);
let task_hash_input = TaskHashInput::new(account_id.clone(), provided_id.clone());
let call: RuntimeCall = frame_system::Call::remark_with_event { remark: message }.into();

assert_ok!(fund_account_dynamic_dispatch(&account_id, scheduled_times.len(), call.encode()));

assert_ok!(AutomationTime::schedule_dynamic_dispatch_task(
RuntimeOrigin::signed(account_id.clone()),
provided_id,
ScheduleParam::Fixed { execution_times: scheduled_times },
Box::new(call),
));
BlakeTwo256::hash_of(&task_hash_input)
}

// A function to support test scheduling a Recurring schedule
// We don't focus on making sure the execution run properly. We just focus on
// making sure a task is scheduled into the queue
pub fn schedule_recurring_task(
owner: [u8; 32],
provided_id: Vec<u8>,
next_execution_time: UnixTime,
frequency: Seconds,
message: Vec<u8>,
) -> sp_core::H256 {
let account_id = AccountId32::new(owner);
let task_hash_input = TaskHashInput::new(account_id.clone(), provided_id.clone());
let call: RuntimeCall = frame_system::Call::remark_with_event { remark: message }.into();

assert_ok!(fund_account_dynamic_dispatch(&account_id, 1, call.encode()));

assert_ok!(AutomationTime::schedule_dynamic_dispatch_task(
RuntimeOrigin::signed(account_id.clone()),
provided_id,
scheduled_times,
message,
ScheduleParam::Recurring { next_execution_time, frequency },
Box::new(call),
));
BlakeTwo256::hash_of(&task_hash_input)
}
Expand Down
Loading

0 comments on commit 450d44c

Please sign in to comment.