Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Co-Authored-By: parity-processbot <>
  • Loading branch information
shawntabrizi committed Jun 24, 2021
1 parent 716e160 commit d4b3f90
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions runtime/common/src/auctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ pub mod pallet {
NotAuction,
/// Auction has already ended.
AuctionEnded,
/// The para is already leased out for part of this range.
AlreadyLeasedOut,
}

/// Number of auctions started so far.
Expand Down Expand Up @@ -418,6 +420,9 @@ impl<T: Config> Pallet<T> {
let now = frame_system::Pallet::<T>::block_number();
ensure!(now < late_end, Error::<T>::AuctionEnded);

// We also make sure that the bid is not for any existing leases the para already has.
ensure!(!T::Leaser::already_leased(para, first_slot, last_slot), Error::<T>::AlreadyLeasedOut);

// Our range.
let range = SlotRange::new_bounded(first_lease_period, first_slot, last_slot)?;
// Range as an array index.
Expand Down Expand Up @@ -755,12 +760,15 @@ mod tests {
}

fn already_leased(
_para_id: ParaId,
_first_period: Self::LeasePeriod,
_last_period: Self::LeasePeriod
para_id: ParaId,
first_period: Self::LeasePeriod,
last_period: Self::LeasePeriod
) -> bool {
// TODO
false
leases().into_iter().any(|((para, period), _data)| {
para == para_id &&
first_period <= period &&
period <= last_period
})
}
}

Expand Down Expand Up @@ -1334,6 +1342,42 @@ mod tests {
});
}

#[test]
fn handle_bid_checks_existing_lease_periods() {
new_test_ext().execute_with(|| {
run_to_block(1);
assert_ok!(Auctions::new_auction(Origin::signed(6), 5, 1));
assert_ok!(Auctions::bid(Origin::signed(1), 0.into(), 1, 2, 3, 1));
assert_eq!(Balances::reserved_balance(1), 1);
assert_eq!(Balances::free_balance(1), 9);
run_to_block(9);

assert_eq!(leases(), vec![
((0.into(), 2), LeaseData { leaser: 1, amount: 1 }),
((0.into(), 3), LeaseData { leaser: 1, amount: 1 }),
]);
assert_eq!(TestLeaser::deposit_held(0.into(), &1), 1);

// Para 1 just won an auction above and won some lease periods.
// No bids can work which overlap these periods.
assert_ok!(Auctions::new_auction(Origin::signed(6), 5, 1));
assert_noop!(
Auctions::bid(Origin::signed(1), 0.into(), 2, 1, 4, 1),
Error::<Test>::AlreadyLeasedOut,
);
assert_noop!(
Auctions::bid(Origin::signed(1), 0.into(), 2, 1, 2, 1),
Error::<Test>::AlreadyLeasedOut,
);
assert_noop!(
Auctions::bid(Origin::signed(1), 0.into(), 2, 3, 4, 1),
Error::<Test>::AlreadyLeasedOut,
);
// This is okay, not an overlapping bid.
assert_ok!(Auctions::bid(Origin::signed(1), 0.into(), 2, 1, 1, 1));
});
}

// Here we will test that taking only 10 samples during the ending period works as expected.
#[test]
fn less_winning_samples_work() {
Expand Down

0 comments on commit d4b3f90

Please sign in to comment.