Newer
Older
#[test]
fn gap_bids_work() {
new_test_ext().execute_with(|| {
assert!(System::block_number().is_one()); // So events are emitted
// Start a new auction in the future
let duration = 99u32;
let lease_period_index_start = 4u32;
assert_ok!(Auctions::new_auction(Origin::root(), duration, lease_period_index_start));
Shawn Tabrizi
committed
Balances::make_free_balance_be(&1, 1_000_000_000);
Balances::make_free_balance_be(&2, 1_000_000_000);
// Now register 2 paras
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),
));
assert_ok!(Registrar::reserve(Origin::signed(2)));
assert_ok!(Registrar::register(
Origin::signed(2),
ParaId::from(2001),
test_genesis_head(10),
test_validation_code(10),
));
// Onboarded on Session 2
run_to_session(2);
// Make bids
Shawn Tabrizi
committed
Balances::make_free_balance_be(&10, 1_000_000_000);
Balances::make_free_balance_be(&20, 1_000_000_000);
// Slot 1 for 100 from 10
assert_ok!(Auctions::bid(
Origin::signed(10),
ParaId::from(2000),
lease_period_index_start + 0, // First Slot
lease_period_index_start + 0, // Last slot
));
// Slot 4 for 400 from 10
assert_ok!(Auctions::bid(
Origin::signed(10),
ParaId::from(2000),
lease_period_index_start + 3, // First Slot
lease_period_index_start + 3, // Last slot
));
// A bid for another para is counted separately.
assert_ok!(Auctions::bid(
Origin::signed(10),
ParaId::from(2001),
lease_period_index_start + 1, // First Slot
lease_period_index_start + 1, // Last slot
));
assert_eq!(Balances::reserved_balance(&10), 400 + 555);
// Slot 2 for 800 from 20, overtaking 10's bid
assert_ok!(Auctions::bid(
Origin::signed(20),
ParaId::from(2000),
lease_period_index_start + 1, // First Slot
lease_period_index_start + 1, // Last slot
));
// Slot 3 for 200 from 20
assert_ok!(Auctions::bid(
Origin::signed(20),
ParaId::from(2000),
lease_period_index_start + 2, // First Slot
lease_period_index_start + 2, // Last slot
));
// Finish the auction
run_to_block(110);
// Should have won the lease periods
assert_eq!(
slots::Leases::<Test>::get(ParaId::from(2000)),
// -- 1 --- 2 --- 3 ---------- 4 -------------- 5 -------------- 6 -------------- 7 -------
vec![
None,
None,
None,
Some((10, 100)),
Some((20, 800)),
Some((20, 200)),
Some((10, 400))
],
);
// Appropriate amount is reserved (largest of the values)
assert_eq!(Balances::reserved_balance(&10), 400);
// Appropriate amount is reserved (largest of the values)
assert_eq!(Balances::reserved_balance(&20), 800);
// Progress through the leases and note the correct amount of balance is reserved.
run_to_block(400);
assert_eq!(
slots::Leases::<Test>::get(ParaId::from(2000)),
// --------- 4 -------------- 5 -------------- 6 -------------- 7 -------
vec![Some((10, 100)), Some((20, 800)), Some((20, 200)), Some((10, 400))],
);
// Nothing changed.
assert_eq!(Balances::reserved_balance(&10), 400);
assert_eq!(Balances::reserved_balance(&20), 800);
// Lease period 4 is done, but nothing is unreserved since user 1 has a debt on lease 7
run_to_block(500);
assert_eq!(
slots::Leases::<Test>::get(ParaId::from(2000)),
// --------- 5 -------------- 6 -------------- 7 -------
vec![Some((20, 800)), Some((20, 200)), Some((10, 400))],
);
// Nothing changed.
assert_eq!(Balances::reserved_balance(&10), 400);
assert_eq!(Balances::reserved_balance(&20), 800);
// Lease period 5 is done, and 20 will unreserve down to 200.
run_to_block(600);
assert_eq!(
slots::Leases::<Test>::get(ParaId::from(2000)),
// --------- 6 -------------- 7 -------
vec![Some((20, 200)), Some((10, 400))],
);
assert_eq!(Balances::reserved_balance(&10), 400);
assert_eq!(Balances::reserved_balance(&20), 200);
// Lease period 6 is done, and 20 will unreserve everything.
run_to_block(700);
assert_eq!(
slots::Leases::<Test>::get(ParaId::from(2000)),
// --------- 7 -------
vec![Some((10, 400))],
);
assert_eq!(Balances::reserved_balance(&10), 400);
assert_eq!(Balances::reserved_balance(&20), 0);
// All leases are done. Everything is unreserved.
run_to_block(800);
assert_eq!(slots::Leases::<Test>::get(ParaId::from(2000)), vec![]);
assert_eq!(Balances::reserved_balance(&10), 0);
assert_eq!(Balances::reserved_balance(&20), 0);
});
}
Shawn Tabrizi
committed
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
// 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 a parathread
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),
Shawn Tabrizi
committed
lease_period_index_start + 0, // First Slot
lease_period_index_start + 1, // Last Slot
Shawn Tabrizi
committed
None,
));
let crowdloan_account = Crowdloan::fund_account_id(ParaId::from(2000));
// Bunch of contributions
let mut total = 0;
Shawn Tabrizi
committed
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 -------------
vec![
None,
None,
None,
Some((crowdloan_account, 8855)),
Some((crowdloan_account, 8855))
],
Shawn Tabrizi
committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
);
// 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,
Shawn Tabrizi
committed
);
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,
Shawn Tabrizi
committed
);
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,
Shawn Tabrizi
committed
);
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,
Shawn Tabrizi
committed
);
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,
Shawn Tabrizi
committed
);
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,
Shawn Tabrizi
committed
);
// 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,
));