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

Commit

Permalink
add integration test and fix
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 d4b3f90 commit be4014d
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
151 changes: 151 additions & 0 deletions runtime/common/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,154 @@ fn gap_bids_work() {
assert_eq!(Balances::reserved_balance(&20), 0);
});
}

// This test verifies that if a parachain already has won some lease periods, that it cannot bid for
// any of those same lease periods again.
#[test]
fn cant_bid_on_existing_lease_periods() {
new_test_ext().execute_with(|| {
assert!(System::block_number().is_one()); // So events are emitted
Balances::make_free_balance_be(&1, 1_000_000_000);
// First register 2 parathreads
assert_ok!(Registrar::reserve(Origin::signed(1)));
assert_ok!(Registrar::register(
Origin::signed(1),
ParaId::from(2000),
test_genesis_head(10),
test_validation_code(10),
));

// Start a new auction in the future
let starting_block = System::block_number();
let duration = 99u32;
let lease_period_index_start = 4u32;
assert_ok!(Auctions::new_auction(Origin::root(), duration, lease_period_index_start));

// 2 sessions later they are parathreads
run_to_session(2);

// Open a crowdloan for Para 1 for slots 0-3
assert_ok!(Crowdloan::create(
Origin::signed(1),
ParaId::from(2000),
1_000_000, // Cap
lease_period_index_start + 0, // First Slot
lease_period_index_start + 1, // Last Slot
400, // Long block end
None,
));
let crowdloan_account = Crowdloan::fund_account_id(ParaId::from(2000));

// Bunch of contributions
let mut total = 0;
for i in 10 .. 20 {
Balances::make_free_balance_be(&i, 1_000_000_000);
assert_ok!(Crowdloan::contribute(Origin::signed(i), ParaId::from(2000), 900 - i, None));
total += 900 - i;
}
assert!(total > 0);
assert_eq!(Balances::free_balance(&crowdloan_account), total);

// Finish the auction.
run_to_block(starting_block + 110);

// Appropriate Paras should have won slots
assert_eq!(
slots::Leases::<Test>::get(ParaId::from(2000)),
// -- 1 --- 2 --- 3 --- 4 --- 5 ------------- 6 ------------------------ 7 -------------
vec![None, None, None, Some((crowdloan_account, 8855)), Some((crowdloan_account, 8855))],
);

// Let's start another auction for the same range
let starting_block = System::block_number();
let duration = 99u32;
let lease_period_index_start = 4u32;
assert_ok!(Auctions::new_auction(Origin::root(), duration, lease_period_index_start));

// Poke the crowdloan into `NewRaise`
assert_ok!(Crowdloan::poke(Origin::signed(1), ParaId::from(2000)));
assert_eq!(Crowdloan::new_raise(), vec![ParaId::from(2000)]);

// Beginning of ending block.
run_to_block(starting_block + 100);

// Bids cannot be made which intersect
assert_noop!(
Auctions::bid(
Origin::signed(crowdloan_account),
ParaId::from(2000),
2,
lease_period_index_start + 0,
lease_period_index_start + 1,
100,
), AuctionsError::<Test>::AlreadyLeasedOut,
);

assert_noop!(
Auctions::bid(
Origin::signed(crowdloan_account),
ParaId::from(2000),
2,
lease_period_index_start + 1,
lease_period_index_start + 2,
100,
), AuctionsError::<Test>::AlreadyLeasedOut,
);

assert_noop!(
Auctions::bid(
Origin::signed(crowdloan_account),
ParaId::from(2000),
2,
lease_period_index_start - 1,
lease_period_index_start + 0,
100,
), AuctionsError::<Test>::AlreadyLeasedOut,
);

assert_noop!(
Auctions::bid(
Origin::signed(crowdloan_account),
ParaId::from(2000),
2,
lease_period_index_start + 0,
lease_period_index_start + 0,
100,
), AuctionsError::<Test>::AlreadyLeasedOut,
);

assert_noop!(
Auctions::bid(
Origin::signed(crowdloan_account),
ParaId::from(2000),
2,
lease_period_index_start + 1,
lease_period_index_start + 1,
100,
), AuctionsError::<Test>::AlreadyLeasedOut,
);

assert_noop!(
Auctions::bid(
Origin::signed(crowdloan_account),
ParaId::from(2000),
2,
lease_period_index_start - 1,
lease_period_index_start + 5,
100,
), AuctionsError::<Test>::AlreadyLeasedOut,
);

// Will work when not overlapping
assert_ok!(
Auctions::bid(
Origin::signed(crowdloan_account),
ParaId::from(2000),
2,
lease_period_index_start + 2,
lease_period_index_start + 3,
100,
)
);
});
}
2 changes: 1 addition & 1 deletion runtime/common/src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ impl<T: Config> Leaser for Module<T> {

// Get the leases, and check each item in the vec which is part of the range we ar checking.
let leases = Leases::<T>::get(para_id);
for slot in offset .. offset + period_count {
for slot in offset ..= offset + period_count {
if let Some(maybe_lease) = leases.get(slot) {
// If there exists any lease period, we exit early and return true.
if maybe_lease.is_some() {
Expand Down

0 comments on commit be4014d

Please sign in to comment.