[pallet_broker] Remove leases that have already expired in rotate_sale (#3213)

Leases can be force set, but since `Leases` is a `StorageValue`, if a
lease misses its sale rotation in which it should expire, it can never
be cleared.

This can happen if a lease is added with an `until` timeslice that lies
in a region whose sale has already started or has passed, even if the
timeslice itself hasn't passed.

This solves that issue in a minimal way, with all expired leases being
cleaned up in each sale rotation, not just the ones that are expiring in
the coming region.

TODO:
- [x] Write test
This commit is contained in:
Dónal Murray
2024-02-08 12:23:36 +00:00
committed by GitHub
parent 2556e33fb4
commit 2ea6bcf195
2 changed files with 27 additions and 2 deletions
+23
View File
@@ -863,6 +863,29 @@ fn cannot_set_expired_lease() {
});
}
#[test]
fn short_leases_are_cleaned() {
TestExt::new().region_length(3).execute_with(|| {
assert_ok!(Broker::do_start_sales(200, 1));
advance_to(2);
// New leases are allowed to expire within this region given expiry > `current_timeslice`.
assert_noop!(
Broker::do_set_lease(1000, Broker::current_timeslice()),
Error::<Test>::AlreadyExpired
);
assert_eq!(Leases::<Test>::get().len(), 0);
assert_ok!(Broker::do_set_lease(1000, Broker::current_timeslice().saturating_add(1)));
assert_eq!(Leases::<Test>::get().len(), 1);
// But are cleaned up in the next rotate_sale.
let config = Configuration::<Test>::get().unwrap();
let timeslice_period: u64 = <Test as Config>::TimeslicePeriod::get();
advance_to(timeslice_period.saturating_mul(config.region_length.into()));
assert_eq!(Leases::<Test>::get().len(), 0);
});
}
#[test]
fn leases_are_limited() {
TestExt::new().execute_with(|| {