Skip to content

Commit d6502bc

Browse files
fix take() limit on read
1 parent ab44472 commit d6502bc

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

payments/src/lib.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -215,30 +215,33 @@ pub mod pallet {
215215
/// This function will look for any pending scheduled tasks that can
216216
/// be executed and will process them.
217217
fn on_idle(now: T::BlockNumber, mut remaining_weight: Weight) -> Weight {
218-
const MAX_TASKS_TO_PROCESS: usize = 10;
218+
const MAX_TASKS_TO_PROCESS: usize = 5;
219219
// reduce the weight used to read the task list
220220
remaining_weight = remaining_weight.saturating_sub(T::WeightInfo::remove_task());
221+
let cancel_weight = T::WeightInfo::cancel();
222+
223+
// calculate count of tasks that can be processed with remaining weight
224+
let possible_task_count: usize = remaining_weight
225+
.saturating_div(cancel_weight)
226+
.try_into()
227+
.unwrap_or(MAX_TASKS_TO_PROCESS);
221228

222229
ScheduledTasks::<T>::mutate(|tasks| {
223230
let mut task_list: Vec<_> = tasks
224231
.clone()
225232
.into_iter()
233+
.take(possible_task_count)
226234
// leave out tasks in the future
227235
.filter(|(_, ScheduledTask { when, task })| when <= &now && matches!(task, Task::Cancel))
228236
.collect();
229237

230-
// limit the max tasks to reduce computation
231-
task_list.truncate(MAX_TASKS_TO_PROCESS);
232-
233238
// order by oldest task to process
234239
task_list.sort_by(|(_, t), (_, x)| x.when.cmp(&t.when));
235240

236-
let cancel_weight = T::WeightInfo::cancel();
237-
238241
while !task_list.is_empty() && remaining_weight >= cancel_weight {
239242
if let Some((account_pair, _)) = task_list.pop() {
240243
remaining_weight = remaining_weight.saturating_sub(cancel_weight);
241-
// remove the task form the tasks
244+
// remove the task form the tasks storage
242245
tasks.remove(&account_pair);
243246

244247
// process the cancel payment

0 commit comments

Comments
 (0)