Skip to content

Commit 0148924

Browse files
authored
Fix payments on-idle (#868)
* fix payments on-idle * update comment
1 parent 4f5a3f3 commit 0148924

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

payments/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,17 @@ pub mod pallet {
214214
/// Hook that execute when there is leftover space in a block
215215
/// This function will look for any pending scheduled tasks that can
216216
/// be executed and will process them.
217-
fn on_idle(now: T::BlockNumber, mut remaining_weight: Weight) -> Weight {
217+
fn on_idle(now: T::BlockNumber, remaining_weight: Weight) -> Weight {
218218
const MAX_TASKS_TO_PROCESS: usize = 5;
219-
// reduce the weight used to read the task list
220-
remaining_weight = remaining_weight.saturating_sub(T::WeightInfo::remove_task());
219+
// used to read the task list
220+
let mut used_weight = T::WeightInfo::remove_task();
221221
let cancel_weight = T::WeightInfo::cancel();
222222

223223
// calculate count of tasks that can be processed with remaining weight
224224
let possible_task_count: usize = remaining_weight
225-
.ref_time()
225+
.saturating_sub(used_weight)
226226
.saturating_div(cancel_weight.ref_time())
227+
.ref_time()
227228
.try_into()
228229
.unwrap_or(MAX_TASKS_TO_PROCESS);
229230

@@ -239,9 +240,9 @@ pub mod pallet {
239240
// order by oldest task to process
240241
task_list.sort_by(|(_, t), (_, x)| x.when.cmp(&t.when));
241242

242-
while !task_list.is_empty() && remaining_weight.all_gte(cancel_weight) {
243+
while !task_list.is_empty() && used_weight.all_lte(remaining_weight) {
243244
if let Some((account_pair, _)) = task_list.pop() {
244-
remaining_weight = remaining_weight.saturating_sub(cancel_weight);
245+
used_weight = used_weight.saturating_add(cancel_weight);
245246
// remove the task form the tasks storage
246247
tasks.remove(&account_pair);
247248

@@ -268,7 +269,7 @@ pub mod pallet {
268269
}
269270
}
270271
});
271-
remaining_weight
272+
used_weight
272273
}
273274
}
274275

payments/src/tests.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::{
22
mock::*,
33
types::{PaymentDetail, PaymentState},
4+
weights::WeightInfo,
45
Payment as PaymentStore, PaymentHandler, ScheduledTask, ScheduledTasks, Task,
56
};
6-
use frame_support::{assert_noop, assert_ok, storage::with_transaction};
7+
use frame_support::{assert_noop, assert_ok, storage::with_transaction, traits::OnIdle, weights::Weight};
78
use orml_traits::MultiCurrency;
89
use sp_runtime::{Percent, TransactionOutcome};
910

@@ -1398,3 +1399,52 @@ fn test_automatic_refund_works_for_multiple_payments() {
13981399
assert_eq!(Tokens::free_balance(CURRENCY_ID, &PAYMENT_RECIPENT_TWO), 0);
13991400
});
14001401
}
1402+
1403+
#[test]
1404+
fn on_idle_works() {
1405+
new_test_ext().execute_with(|| {
1406+
assert_eq!(
1407+
Payment::on_idle(System::block_number(), Weight::MAX),
1408+
<()>::remove_task()
1409+
);
1410+
1411+
let payment_amount = 20;
1412+
let expected_cancel_block = CANCEL_BLOCK_BUFFER + 1;
1413+
1414+
assert_ok!(Payment::pay(
1415+
RuntimeOrigin::signed(PAYMENT_CREATOR),
1416+
PAYMENT_RECIPENT,
1417+
CURRENCY_ID,
1418+
payment_amount,
1419+
None
1420+
));
1421+
1422+
// creator requests a refund
1423+
assert_ok!(Payment::request_refund(
1424+
RuntimeOrigin::signed(PAYMENT_CREATOR),
1425+
PAYMENT_RECIPENT
1426+
));
1427+
// ensure the request is added to the refund queue
1428+
let scheduled_tasks_list = ScheduledTasks::<Test>::get();
1429+
assert_eq!(scheduled_tasks_list.len(), 1);
1430+
assert_eq!(
1431+
scheduled_tasks_list.get(&(PAYMENT_CREATOR, PAYMENT_RECIPENT)).unwrap(),
1432+
&ScheduledTask {
1433+
task: Task::Cancel,
1434+
when: expected_cancel_block
1435+
}
1436+
);
1437+
1438+
assert_eq!(run_n_blocks(CANCEL_BLOCK_BUFFER - 1), 600);
1439+
assert_eq!(
1440+
Payment::on_idle(System::block_number(), Weight::MAX),
1441+
<()>::remove_task()
1442+
);
1443+
1444+
assert_eq!(run_n_blocks(1), 601);
1445+
assert_eq!(
1446+
Payment::on_idle(System::block_number(), Weight::MAX),
1447+
<()>::remove_task() + <()>::cancel()
1448+
);
1449+
});
1450+
}

0 commit comments

Comments
 (0)