From d4b3f90d5a740c0efbc0bbe7bd5fc1cd9b330c33 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 24 Jun 2021 15:08:40 +0100 Subject: [PATCH] add unit test Co-Authored-By: parity-processbot <> --- runtime/common/src/auctions.rs | 54 ++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/runtime/common/src/auctions.rs b/runtime/common/src/auctions.rs index 1a40b0e22a3d..fc304ea91507 100644 --- a/runtime/common/src/auctions.rs +++ b/runtime/common/src/auctions.rs @@ -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. @@ -418,6 +420,9 @@ impl Pallet { let now = frame_system::Pallet::::block_number(); ensure!(now < late_end, Error::::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::::AlreadyLeasedOut); + // Our range. let range = SlotRange::new_bounded(first_lease_period, first_slot, last_slot)?; // Range as an array index. @@ -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 + }) } } @@ -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::::AlreadyLeasedOut, + ); + assert_noop!( + Auctions::bid(Origin::signed(1), 0.into(), 2, 1, 2, 1), + Error::::AlreadyLeasedOut, + ); + assert_noop!( + Auctions::bid(Origin::signed(1), 0.into(), 2, 3, 4, 1), + Error::::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() {