Unverified Commit 2a3fca3c authored by Shawn Tabrizi's avatar Shawn Tabrizi Committed by GitHub
Browse files

Allow an Offset to Lease Periods (#3980)



* add slot offset for slots

* trying things out

* fix test

* improve api to return the first block of a new lease period

* add an integration test with offset

* de-duplicate test

* hide lease period_period_length from public api

* fix benchmarks

* Update runtime/common/src/slots.rs

* support the exact same range of crowdloans

* fix docs

* fix docs again

* introduce offset to runtimes

* fix and check edge case w/ offset and lease period first block

* remove newline

* turn into an option

* fix benchmarks

Co-authored-by: asynchronous rob's avatarRobert Habermeier <rphmeier@gmail.com>
parent 1f50b492
Pipeline #160667 failed with stages
in 32 minutes and 29 seconds
...@@ -34,8 +34,9 @@ use primitives::v1::Id as ParaId; ...@@ -34,8 +34,9 @@ use primitives::v1::Id as ParaId;
use sp_runtime::traits::{CheckedSub, One, Saturating, Zero}; use sp_runtime::traits::{CheckedSub, One, Saturating, Zero};
use sp_std::{mem::swap, prelude::*}; use sp_std::{mem::swap, prelude::*};
type CurrencyOf<T> = <<T as Config>::Leaser as Leaser>::Currency; type CurrencyOf<T> =
type BalanceOf<T> = <<<T as Config>::Leaser as Leaser>::Currency as Currency< <<T as Config>::Leaser as Leaser<<T as frame_system::Config>::BlockNumber>>::Currency;
type BalanceOf<T> = <<<T as Config>::Leaser as Leaser<<T as frame_system::Config>::BlockNumber>>::Currency as Currency<
<T as frame_system::Config>::AccountId, <T as frame_system::Config>::AccountId,
>>::Balance; >>::Balance;
...@@ -65,7 +66,9 @@ impl WeightInfo for TestWeightInfo { ...@@ -65,7 +66,9 @@ impl WeightInfo for TestWeightInfo {
/// An auction index. We count auctions in this type. /// An auction index. We count auctions in this type.
pub type AuctionIndex = u32; pub type AuctionIndex = u32;
type LeasePeriodOf<T> = <<T as Config>::Leaser as Leaser>::LeasePeriod; type LeasePeriodOf<T> =
<<T as Config>::Leaser as Leaser<<T as frame_system::Config>::BlockNumber>>::LeasePeriod;
// Winning data type. This encodes the top bidders of each range together with their bid. // Winning data type. This encodes the top bidders of each range together with their bid.
type WinningData<T> = [Option<(<T as frame_system::Config>::AccountId, ParaId, BalanceOf<T>)>; type WinningData<T> = [Option<(<T as frame_system::Config>::AccountId, ParaId, BalanceOf<T>)>;
SlotRange::SLOT_RANGE_COUNT]; SlotRange::SLOT_RANGE_COUNT];
...@@ -91,7 +94,11 @@ pub mod pallet { ...@@ -91,7 +94,11 @@ pub mod pallet {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>; type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// The type representing the leasing system. /// The type representing the leasing system.
type Leaser: Leaser<AccountId = Self::AccountId, LeasePeriod = Self::BlockNumber>; type Leaser: Leaser<
Self::BlockNumber,
AccountId = Self::AccountId,
LeasePeriod = Self::BlockNumber,
>;
/// The parachain registrar type. /// The parachain registrar type.
type Registrar: Registrar<AccountId = Self::AccountId>; type Registrar: Registrar<AccountId = Self::AccountId>;
...@@ -299,9 +306,8 @@ pub mod pallet { ...@@ -299,9 +306,8 @@ pub mod pallet {
} }
} }
impl<T: Config> Auctioneer for Pallet<T> { impl<T: Config> Auctioneer<T::BlockNumber> for Pallet<T> {
type AccountId = T::AccountId; type AccountId = T::AccountId;
type BlockNumber = T::BlockNumber;
type LeasePeriod = T::BlockNumber; type LeasePeriod = T::BlockNumber;
type Currency = CurrencyOf<T>; type Currency = CurrencyOf<T>;
...@@ -313,7 +319,7 @@ impl<T: Config> Auctioneer for Pallet<T> { ...@@ -313,7 +319,7 @@ impl<T: Config> Auctioneer for Pallet<T> {
} }
// Returns the status of the auction given the current block number. // Returns the status of the auction given the current block number.
fn auction_status(now: Self::BlockNumber) -> AuctionStatus<Self::BlockNumber> { fn auction_status(now: T::BlockNumber) -> AuctionStatus<T::BlockNumber> {
let early_end = match AuctionInfo::<T>::get() { let early_end = match AuctionInfo::<T>::get() {
Some((_, early_end)) => early_end, Some((_, early_end)) => early_end,
None => return AuctionStatus::NotStarted, None => return AuctionStatus::NotStarted,
...@@ -346,12 +352,13 @@ impl<T: Config> Auctioneer for Pallet<T> { ...@@ -346,12 +352,13 @@ impl<T: Config> Auctioneer for Pallet<T> {
Self::handle_bid(bidder, para, AuctionCounter::<T>::get(), first_slot, last_slot, amount) Self::handle_bid(bidder, para, AuctionCounter::<T>::get(), first_slot, last_slot, amount)
} }
fn lease_period_index() -> Self::LeasePeriod { fn lease_period_index(b: T::BlockNumber) -> Option<(Self::LeasePeriod, bool)> {
T::Leaser::lease_period_index() T::Leaser::lease_period_index(b)
} }
fn lease_period() -> Self::LeasePeriod { #[cfg(any(feature = "runtime-benchmarks", test))]
T::Leaser::lease_period() fn lease_period_length() -> (T::BlockNumber, T::BlockNumber) {
T::Leaser::lease_period_length()
} }
fn has_won_an_auction(para: ParaId, bidder: &T::AccountId) -> bool { fn has_won_an_auction(para: ParaId, bidder: &T::AccountId) -> bool {
...@@ -374,10 +381,11 @@ impl<T: Config> Pallet<T> { ...@@ -374,10 +381,11 @@ impl<T: Config> Pallet<T> {
) -> DispatchResult { ) -> DispatchResult {
let maybe_auction = AuctionInfo::<T>::get(); let maybe_auction = AuctionInfo::<T>::get();
ensure!(maybe_auction.is_none(), Error::<T>::AuctionInProgress); ensure!(maybe_auction.is_none(), Error::<T>::AuctionInProgress);
ensure!( let now = frame_system::Pallet::<T>::block_number();
lease_period_index >= T::Leaser::lease_period_index(), if let Some((current_lease_period, _)) = T::Leaser::lease_period_index(now) {
Error::<T>::LeasePeriodInPast // If there is no active lease period, then we don't need to make this check.
); ensure!(lease_period_index >= current_lease_period, Error::<T>::LeasePeriodInPast);
}
// Bump the counter. // Bump the counter.
let n = AuctionCounter::<T>::mutate(|n| { let n = AuctionCounter::<T>::mutate(|n| {
...@@ -567,7 +575,9 @@ impl<T: Config> Pallet<T> { ...@@ -567,7 +575,9 @@ impl<T: Config> Pallet<T> {
let period_count = LeasePeriodOf::<T>::from(range.len() as u32); let period_count = LeasePeriodOf::<T>::from(range.len() as u32);
match T::Leaser::lease_out(para, &leaser, amount, period_begin, period_count) { match T::Leaser::lease_out(para, &leaser, amount, period_begin, period_count) {
Err(LeaseError::ReserveFailed) | Err(LeaseError::AlreadyEnded) => { Err(LeaseError::ReserveFailed) |
Err(LeaseError::AlreadyEnded) |
Err(LeaseError::NoLeasePeriod) => {
// Should never happen since we just unreserved this amount (and our offset is from the // Should never happen since we just unreserved this amount (and our offset is from the
// present period). But if it does, there's not much we can do. // present period). But if it does, there's not much we can do.
}, },
...@@ -735,7 +745,7 @@ mod tests { ...@@ -735,7 +745,7 @@ mod tests {
} }
pub struct TestLeaser; pub struct TestLeaser;
impl Leaser for TestLeaser { impl Leaser<BlockNumber> for TestLeaser {
type AccountId = u64; type AccountId = u64;
type LeasePeriod = BlockNumber; type LeasePeriod = BlockNumber;
type Currency = Balances; type Currency = Balances;
...@@ -749,7 +759,10 @@ mod tests { ...@@ -749,7 +759,10 @@ mod tests {
) -> Result<(), LeaseError> { ) -> Result<(), LeaseError> {
LEASES.with(|l| { LEASES.with(|l| {
let mut leases = l.borrow_mut(); let mut leases = l.borrow_mut();
if period_begin < Self::lease_period_index() { let now = System::block_number();
let (current_lease_period, _) =
Self::lease_period_index(now).ok_or(LeaseError::NoLeasePeriod)?;
if period_begin < current_lease_period {
return Err(LeaseError::AlreadyEnded) return Err(LeaseError::AlreadyEnded)
} }
for period in period_begin..(period_begin + period_count) { for period in period_begin..(period_begin + period_count) {
...@@ -779,12 +792,18 @@ mod tests { ...@@ -779,12 +792,18 @@ mod tests {
.unwrap_or_default() .unwrap_or_default()
} }
fn lease_period() -> Self::LeasePeriod { fn lease_period_length() -> (BlockNumber, BlockNumber) {
10 (10, 0)
} }
fn lease_period_index() -> Self::LeasePeriod { fn lease_period_index(b: BlockNumber) -> Option<(Self::LeasePeriod, bool)> {
(System::block_number() / Self::lease_period()).into() let (lease_period_length, offset) = Self::lease_period_length();
let b = b.checked_sub(offset)?;
let lease_period = b / lease_period_length;
let first_block = (b % lease_period_length).is_zero();
Some((lease_period, first_block))
} }
fn already_leased( fn already_leased(
......
...@@ -72,8 +72,11 @@ use sp_runtime::{ ...@@ -72,8 +72,11 @@ use sp_runtime::{
}; };
use sp_std::vec::Vec; use sp_std::vec::Vec;
type CurrencyOf<T> = <<T as Config>::Auctioneer as Auctioneer>::Currency; type CurrencyOf<T> =
type LeasePeriodOf<T> = <<T as Config>::Auctioneer as Auctioneer>::LeasePeriod; <<T as Config>::Auctioneer as Auctioneer<<T as frame_system::Config>::BlockNumber>>::Currency;
type LeasePeriodOf<T> = <<T as Config>::Auctioneer as Auctioneer<
<T as frame_system::Config>::BlockNumber,
>>::LeasePeriod;
type BalanceOf<T> = <CurrencyOf<T> as Currency<<T as frame_system::Config>::AccountId>>::Balance; type BalanceOf<T> = <CurrencyOf<T> as Currency<<T as frame_system::Config>::AccountId>>::Balance;
#[allow(dead_code)] #[allow(dead_code)]
...@@ -203,8 +206,8 @@ pub mod pallet { ...@@ -203,8 +206,8 @@ pub mod pallet {
/// The type representing the auctioning system. /// The type representing the auctioning system.
type Auctioneer: Auctioneer< type Auctioneer: Auctioneer<
Self::BlockNumber,
AccountId = Self::AccountId, AccountId = Self::AccountId,
BlockNumber = Self::BlockNumber,
LeasePeriod = Self::BlockNumber, LeasePeriod = Self::BlockNumber,
>; >;
...@@ -313,6 +316,8 @@ pub mod pallet { ...@@ -313,6 +316,8 @@ pub mod pallet {
AlreadyInNewRaise, AlreadyInNewRaise,
/// No contributions allowed during the VRF delay /// No contributions allowed during the VRF delay
VrfDelayInProgress, VrfDelayInProgress,
/// A lease period has not started yet, due to an offset in the starting block.
NoLeasePeriod,
} }
#[pallet::hooks] #[pallet::hooks]
...@@ -365,20 +370,31 @@ pub mod pallet { ...@@ -365,20 +370,31 @@ pub mod pallet {
verifier: Option<MultiSigner>, verifier: Option<MultiSigner>,
) -> DispatchResult { ) -> DispatchResult {
let depositor = ensure_signed(origin)?; let depositor = ensure_signed(origin)?;
let now = frame_system::Pallet::<T>::block_number();
ensure!(first_period <= last_period, Error::<T>::LastPeriodBeforeFirstPeriod); ensure!(first_period <= last_period, Error::<T>::LastPeriodBeforeFirstPeriod);
let last_period_limit = first_period let last_period_limit = first_period
.checked_add(&((SlotRange::LEASE_PERIODS_PER_SLOT as u32) - 1).into()) .checked_add(&((SlotRange::LEASE_PERIODS_PER_SLOT as u32) - 1).into())
.ok_or(Error::<T>::FirstPeriodTooFarInFuture)?; .ok_or(Error::<T>::FirstPeriodTooFarInFuture)?;
ensure!(last_period <= last_period_limit, Error::<T>::LastPeriodTooFarInFuture); ensure!(last_period <= last_period_limit, Error::<T>::LastPeriodTooFarInFuture);
ensure!(end > <frame_system::Pallet<T>>::block_number(), Error::<T>::CannotEndInPast); ensure!(end > now, Error::<T>::CannotEndInPast);
let last_possible_win_date = (first_period.saturating_add(One::one()))
.saturating_mul(T::Auctioneer::lease_period()); // Here we check the lease period on the ending block is at most the first block of the
ensure!(end <= last_possible_win_date, Error::<T>::EndTooFarInFuture); // period after `first_period`. If it would be larger, there is no way we could win an
ensure!( // active auction, thus it would make no sense to have a crowdloan this long.
first_period >= T::Auctioneer::lease_period_index(), let (lease_period_at_end, is_first_block) =
Error::<T>::FirstPeriodInPast T::Auctioneer::lease_period_index(end).ok_or(Error::<T>::NoLeasePeriod)?;
); let adjusted_lease_period_at_end = if is_first_block {
lease_period_at_end.saturating_sub(One::one())
} else {
lease_period_at_end
};
ensure!(adjusted_lease_period_at_end <= first_period, Error::<T>::EndTooFarInFuture);
// Can't start a crowdloan for a lease period that already passed.
if let Some((current_lease_period, _)) = T::Auctioneer::lease_period_index(now) {
ensure!(first_period >= current_lease_period, Error::<T>::FirstPeriodInPast);
}
// There should not be an existing fund. // There should not be an existing fund.
ensure!(!Funds::<T>::contains_key(index), Error::<T>::FundNotEnded); ensure!(!Funds::<T>::contains_key(index), Error::<T>::FundNotEnded);
...@@ -439,7 +455,9 @@ pub mod pallet { ...@@ -439,7 +455,9 @@ pub mod pallet {
ensure!(now < fund.end, Error::<T>::ContributionPeriodOver); ensure!(now < fund.end, Error::<T>::ContributionPeriodOver);
// Make sure crowdloan is in a valid lease period // Make sure crowdloan is in a valid lease period
let current_lease_period = T::Auctioneer::lease_period_index(); let now = frame_system::Pallet::<T>::block_number();
let (current_lease_period, _) =
T::Auctioneer::lease_period_index(now).ok_or(Error::<T>::NoLeasePeriod)?;
ensure!(current_lease_period <= fund.first_period, Error::<T>::ContributionPeriodOver); ensure!(current_lease_period <= fund.first_period, Error::<T>::ContributionPeriodOver);
// Make sure crowdloan has not already won. // Make sure crowdloan has not already won.
...@@ -751,7 +769,8 @@ impl<T: Config> Pallet<T> { ...@@ -751,7 +769,8 @@ impl<T: Config> Pallet<T> {
// `fund.end` can represent the end of a failed crowdloan or the beginning of retirement // `fund.end` can represent the end of a failed crowdloan or the beginning of retirement
// If the current lease period is past the first period they are trying to bid for, then // If the current lease period is past the first period they are trying to bid for, then
// it is already too late to win the bid. // it is already too late to win the bid.
let current_lease_period = T::Auctioneer::lease_period_index(); let (current_lease_period, _) =
T::Auctioneer::lease_period_index(now).ok_or(Error::<T>::NoLeasePeriod)?;
ensure!( ensure!(
now >= fund.end || current_lease_period > fund.first_period, now >= fund.end || current_lease_period > fund.first_period,
Error::<T>::FundNotEnded Error::<T>::FundNotEnded
...@@ -931,14 +950,16 @@ mod tests { ...@@ -931,14 +950,16 @@ mod tests {
} }
pub struct TestAuctioneer; pub struct TestAuctioneer;
impl Auctioneer for TestAuctioneer { impl Auctioneer<u64> for TestAuctioneer {
type AccountId = u64; type AccountId = u64;
type BlockNumber = BlockNumber;
type LeasePeriod = u64; type LeasePeriod = u64;
type Currency = Balances; type Currency = Balances;
fn new_auction(duration: u64, lease_period_index: u64) -> DispatchResult { fn new_auction(duration: u64, lease_period_index: u64) -> DispatchResult {
assert!(lease_period_index >= Self::lease_period_index()); let now = System::block_number();
let (current_lease_period, _) =
Self::lease_period_index(now).ok_or("no lease period yet")?;
assert!(lease_period_index >= current_lease_period);
let ending = System::block_number().saturating_add(duration); let ending = System::block_number().saturating_add(duration);
AUCTION.with(|p| *p.borrow_mut() = Some((lease_period_index, ending))); AUCTION.with(|p| *p.borrow_mut() = Some((lease_period_index, ending)));
...@@ -991,12 +1012,17 @@ mod tests { ...@@ -991,12 +1012,17 @@ mod tests {
Ok(()) Ok(())
} }
fn lease_period_index() -> u64 { fn lease_period_index(b: BlockNumber) -> Option<(u64, bool)> {
System::block_number() / Self::lease_period() let (lease_period_length, offset) = Self::lease_period_length();
let b = b.checked_sub(offset)?;
let lease_period = b / lease_period_length;
let first_block = (b % lease_period_length).is_zero();
Some((lease_period, first_block))
} }
fn lease_period() -> u64 { fn lease_period_length() -> (u64, u64) {
20 (20, 0)
} }
fn has_won_an_auction(para: ParaId, bidder: &u64) -> bool { fn has_won_an_auction(para: ParaId, bidder: &u64) -> bool {
...@@ -1367,7 +1393,8 @@ mod tests { ...@@ -1367,7 +1393,8 @@ mod tests {
let para_3 = new_para(); let para_3 = new_para();
assert_ok!(Crowdloan::create(Origin::signed(1), para_3, 1000, 1, 4, 40, None)); assert_ok!(Crowdloan::create(Origin::signed(1), para_3, 1000, 1, 4, 40, None));
run_to_block(40); run_to_block(40);
assert_eq!(TestAuctioneer::lease_period_index(), 2); let now = System::block_number();
assert_eq!(TestAuctioneer::lease_period_index(now).unwrap().0, 2);
assert_noop!( assert_noop!(
Crowdloan::contribute(Origin::signed(1), para_3, 49, None), Crowdloan::contribute(Origin::signed(1), para_3, 49, None),
Error::<Test>::ContributionPeriodOver Error::<Test>::ContributionPeriodOver
...@@ -1842,7 +1869,11 @@ mod benchmarking { ...@@ -1842,7 +1869,11 @@ mod benchmarking {
fn create_fund<T: Config>(id: u32, end: T::BlockNumber) -> ParaId { fn create_fund<T: Config>(id: u32, end: T::BlockNumber) -> ParaId {
let cap = BalanceOf::<T>::max_value(); let cap = BalanceOf::<T>::max_value();
let lease_period_index = T::Auctioneer::lease_period_index(); let (_, offset) = T::Auctioneer::lease_period_length();
// Set to the very beginning of lease period index 0.
frame_system::Pallet::<T>::set_block_number(offset);
let now = frame_system::Pallet::<T>::block_number();
let (lease_period_index, _) = T::Auctioneer::lease_period_index(now).unwrap_or_default();
let first_period = lease_period_index; let first_period = lease_period_index;
let last_period = let last_period =
lease_period_index + ((SlotRange::LEASE_PERIODS_PER_SLOT as u32) - 1).into(); lease_period_index + ((SlotRange::LEASE_PERIODS_PER_SLOT as u32) - 1).into();
...@@ -1894,7 +1925,8 @@ mod benchmarking { ...@@ -1894,7 +1925,8 @@ mod benchmarking {
let cap = BalanceOf::<T>::max_value(); let cap = BalanceOf::<T>::max_value();
let first_period = 0u32.into(); let first_period = 0u32.into();
let last_period = 3u32.into(); let last_period = 3u32.into();
let end = T::Auctioneer::lease_period(); let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let caller: T::AccountId = whitelisted_caller(); let caller: T::AccountId = whitelisted_caller();
let head_data = T::Registrar::worst_head_data(); let head_data = T::Registrar::worst_head_data();
...@@ -1913,7 +1945,9 @@ mod benchmarking { ...@@ -1913,7 +1945,9 @@ mod benchmarking {
// Contribute has two arms: PreEnding and Ending, but both are equal complexity. // Contribute has two arms: PreEnding and Ending, but both are equal complexity.
contribute { contribute {
let fund_index = create_fund::<T>(1, 100u32.into()); let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1, end);
let caller: T::AccountId = whitelisted_caller(); let caller: T::AccountId = whitelisted_caller();
let contribution = T::MinContribution::get(); let contribution = T::MinContribution::get();
CurrencyOf::<T>::make_free_balance_be(&caller, BalanceOf::<T>::max_value()); CurrencyOf::<T>::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
...@@ -1931,7 +1965,9 @@ mod benchmarking { ...@@ -1931,7 +1965,9 @@ mod benchmarking {
} }
withdraw { withdraw {
let fund_index = create_fund::<T>(1337, 100u32.into()); let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1337, end);
let caller: T::AccountId = whitelisted_caller(); let caller: T::AccountId = whitelisted_caller();
let contributor = account("contributor", 0, 0); let contributor = account("contributor", 0, 0);
contribute_fund::<T>(&contributor, fund_index); contribute_fund::<T>(&contributor, fund_index);
...@@ -1945,7 +1981,9 @@ mod benchmarking { ...@@ -1945,7 +1981,9 @@ mod benchmarking {
#[skip_meta] #[skip_meta]
refund { refund {
let k in 0 .. T::RemoveKeysLimit::get(); let k in 0 .. T::RemoveKeysLimit::get();
let fund_index = create_fund::<T>(1337, 100u32.into()); let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1337, end);
// Dissolve will remove at most `RemoveKeysLimit` at once. // Dissolve will remove at most `RemoveKeysLimit` at once.
for i in 0 .. k { for i in 0 .. k {
...@@ -1960,7 +1998,9 @@ mod benchmarking { ...@@ -1960,7 +1998,9 @@ mod benchmarking {
} }
dissolve { dissolve {
let fund_index = create_fund::<T>(1337, 100u32.into()); let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1337, end);
let caller: T::AccountId = whitelisted_caller(); let caller: T::AccountId = whitelisted_caller();
frame_system::Pallet::<T>::set_block_number(T::BlockNumber::max_value()); frame_system::Pallet::<T>::set_block_number(T::BlockNumber::max_value());
}: _(RawOrigin::Signed(caller.clone()), fund_index) }: _(RawOrigin::Signed(caller.clone()), fund_index)
...@@ -1973,7 +2013,8 @@ mod benchmarking { ...@@ -1973,7 +2013,8 @@ mod benchmarking {
let cap = BalanceOf::<T>::max_value(); let cap = BalanceOf::<T>::max_value();
let first_period = 0u32.into(); let first_period = 0u32.into();
let last_period = 3u32.into(); let last_period = 3u32.into();
let end = T::Auctioneer::lease_period(); let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let caller: T::AccountId = whitelisted_caller(); let caller: T::AccountId = whitelisted_caller();
let head_data = T::Registrar::worst_head_data(); let head_data = T::Registrar::worst_head_data();
...@@ -1997,7 +2038,9 @@ mod benchmarking { ...@@ -1997,7 +2038,9 @@ mod benchmarking {
} }
add_memo { add_memo {
let fund_index = create_fund::<T>(1, 100u32.into()); let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1, end);
let caller: T::AccountId = whitelisted_caller(); let caller: T::AccountId = whitelisted_caller();
contribute_fund::<T>(&caller, fund_index); contribute_fund::<T>(&caller, fund_index);
let worst_memo = vec![42; T::MaxMemoLength::get().into()]; let worst_memo = vec![42; T::MaxMemoLength::get().into()];
...@@ -2011,7 +2054,9 @@ mod benchmarking { ...@@ -2011,7 +2054,9 @@ mod benchmarking {
} }
poke { poke {
let fund_index = create_fund::<T>(1, 100u32.into()); let (lpl, offset) = T::Auctioneer::lease_period_length();
let end = lpl + offset;
let fund_index = create_fund::<T>(1, end);
let caller: T::AccountId = whitelisted_caller(); let caller: T::AccountId = whitelisted_caller();
contribute_fund::<T>(&caller, fund_index); contribute_fund::<T>(&caller, fund_index);
NewRaise::<T>::kill(); NewRaise::<T>::kill();
...@@ -2028,7 +2073,8 @@ mod benchmarking { ...@@ -2028,7 +2073,8 @@ mod benchmarking {
on_initialize { on_initialize {
// We test the complexity over different number of new raise // We test the complexity over different number of new raise
let n in 2 .. 100; let n in 2 .. 100;
let end_block: T::BlockNumber = 100u32.into(); let (lpl, offset) = T::Auctioneer::lease_period_length();
let end_block = lpl + offset - 1u32.into();
let pubkey = crypto::create_ed25519_pubkey(b"//verifier".to_vec()); let pubkey = crypto::create_ed25519_pubkey(b"//verifier".to_vec());
...@@ -2043,7 +2089,8 @@ mod benchmarking { ...@@ -2043,7 +2089,8 @@ mod benchmarking {
Crowdloan::<T>::contribute(RawOrigin::Signed(contributor).into(), fund_index, contribution, Some(sig))?; Crowdloan::<T>::contribute(RawOrigin::Signed(contributor).into(), fund_index, contribution, Some(sig))?;
} }
let lease_period_index = T::Auctioneer::lease_period_index(); let now = frame_system::Pallet::<T>::block_number();
let (lease_period_index, _) = T::Auctioneer::lease_period_index(now).unwrap_or_default();
let duration = end_block let duration = end_block
.checked_sub(&frame_system::Pallet::<T>::block_number()) .checked_sub(&frame_system::Pallet::<T>::block_number())
.ok_or("duration of auction less than zero")?; .ok_or("duration of auction less than zero")?;
...@@ -2062,7 +2109,7 @@ mod benchmarking { ...@@ -2062,7 +2109,7 @@ mod benchmarking {
impl_benchmark_test_suite!( impl_benchmark_test_suite!(
Crowdloan, Crowdloan,
crate::integration_tests::new_test_ext(), crate::integration_tests::new_test_ext_with_offset(10),
crate::integration_tests::Test, crate::integration_tests::Test,
); );
} }
...@@ -203,6 +203,7 @@ impl auctions::Config for Test { ...@@ -203,6 +203,7 @@ impl auctions::Config for Test {
parameter_types! { parameter_types! {
pub const LeasePeriod: BlockNumber = 100; pub const LeasePeriod: BlockNumber = 100;
pub static LeaseOffset: BlockNumber = 0;
} }
impl slots::Config for Test { impl slots::Config for Test {
...@@ -210,6 +211,7 @@ impl slots::Config for Test { ...@@ -210,6 +211,7 @@ impl slots::Config for Test {
type Currency = Balances; type Currency = Balances;
type Registrar = Registrar; type Registrar = Registrar;
type LeasePeriod = LeasePeriod; type LeasePeriod = LeasePeriod;
type LeaseOffset = LeaseOffset;
type WeightInfo = crate::slots::TestWeightInfo; type WeightInfo = crate::slots::TestWeightInfo;