Skip to content

Commit

Permalink
enable_auto_renew_renews
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed May 12, 2024
1 parent 9c1c7e3 commit c7ccf5a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 5 additions & 1 deletion substrate/frame/broker/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ pub fn new_config() -> ConfigRecordOf<Test> {
}
}

pub fn endow(who: u64, amount: u64) {
assert_ok!(<<Test as Config>::Currency as Mutate<_>>::mint_into(&who, amount));
}

pub struct TestExt(ConfigRecordOf<Test>);
#[allow(dead_code)]
impl TestExt {
Expand Down Expand Up @@ -306,7 +310,7 @@ impl TestExt {
}

pub fn endow(self, who: u64, amount: u64) -> Self {
assert_ok!(<<Test as Config>::Currency as Mutate<_>>::mint_into(&who, amount));
endow(who, amount);
self
}

Expand Down
50 changes: 48 additions & 2 deletions substrate/frame/broker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,8 +1411,8 @@ fn renewal_works_leases_ended_before_start_sales() {

#[test]
fn enable_auto_renew_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
TestExt::new().endow(1, 1000).limit_cores_offered(Some(10)).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 3));
advance_to(2);
let region_id = Broker::do_purchase(1, u64::max_value()).unwrap();
let record = Regions::<Test>::get(region_id).unwrap();
Expand Down Expand Up @@ -1444,6 +1444,52 @@ fn enable_auto_renew_works() {
System::assert_has_event(
Event::<Test>::AutoRenewalEnabled { core: region_id.core, task: 1001 }.into(),
);

// Enabling auto-renewal for more cores to ensure they are sorted based on core index.
let region_2 = Broker::do_purchase(1, u64::max_value()).unwrap();
let region_3 = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region_2, Some(1), 1002, Final));
assert_ok!(Broker::do_assign(region_3, Some(1), 1003, Final));
assert_ok!(Broker::do_enable_auto_renew(1003, region_3.core, None));
assert_ok!(Broker::do_enable_auto_renew(1002, region_2.core, None));

assert_eq!(AutoRenewals::<Test>::get().to_vec(), vec![(0, 1001), (1, 1002), (2, 1003)]);
});
}

#[test]
fn enable_auto_renew_renews() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region_id = Broker::do_purchase(1, u64::max_value()).unwrap();

assert_ok!(Broker::do_assign(region_id, Some(1), 1001, Final));
// advance to next bulk sale:
advance_to(6);

// Since we didn't renew for the next bulk period, enabling auto-renewal will renew.

// Will fail because we didn't fund the sovereign account:
assert_noop!(
Broker::do_enable_auto_renew(1001, region_id.core, None),
TokenError::FundsUnavailable
);

// Will succeed after funding the sovereign account:
endow(1001, 1000);

assert_ok!(Broker::do_enable_auto_renew(1001, region_id.core, None));
assert_eq!(AutoRenewals::<Test>::get().to_vec(), vec![(0, 1001)]);
assert!(AllowedRenewals::<Test>::get(AllowedRenewalId {
core: region_id.core,
when: 10 // region end after renewal
})
.is_some());

System::assert_has_event(
Event::<Test>::AutoRenewalEnabled { core: region_id.core, task: 1001 }.into(),
);
});
}

Expand Down

0 comments on commit c7ccf5a

Please sign in to comment.