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

Allow an Offset to Lease Periods #3980

Merged
merged 18 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions runtime/common/src/auctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ use primitives::v1::Id as ParaId;
use sp_runtime::traits::{CheckedSub, One, Saturating, Zero};
use sp_std::{mem::swap, prelude::*};

type CurrencyOf<T> = <<T as Config>::Leaser as Leaser>::Currency;
type BalanceOf<T> = <<<T as Config>::Leaser as Leaser>::Currency as Currency<
type CurrencyOf<T> =
<<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,
>>::Balance;

Expand Down Expand Up @@ -65,7 +66,9 @@ impl WeightInfo for TestWeightInfo {
/// An auction index. We count auctions in this type.
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.
type WinningData<T> = [Option<(<T as frame_system::Config>::AccountId, ParaId, BalanceOf<T>)>;
SlotRange::SLOT_RANGE_COUNT];
Expand All @@ -91,7 +94,11 @@ pub mod pallet {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

/// 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.
type Registrar: Registrar<AccountId = Self::AccountId>;
Expand Down Expand Up @@ -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 BlockNumber = T::BlockNumber;
type LeasePeriod = T::BlockNumber;
type Currency = CurrencyOf<T>;

Expand All @@ -313,7 +319,7 @@ impl<T: Config> Auctioneer for Pallet<T> {
}

// 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() {
Some((_, early_end)) => early_end,
None => return AuctionStatus::NotStarted,
Expand Down Expand Up @@ -346,12 +352,13 @@ impl<T: Config> Auctioneer for Pallet<T> {
Self::handle_bid(bidder, para, AuctionCounter::<T>::get(), first_slot, last_slot, amount)
}

fn lease_period_index() -> Self::LeasePeriod {
T::Leaser::lease_period_index()
fn lease_period_index(b: T::BlockNumber) -> (Self::LeasePeriod, bool) {
T::Leaser::lease_period_index(b)
}

fn lease_period() -> Self::LeasePeriod {
T::Leaser::lease_period()
#[cfg(any(feature = "runtime-benchmarks", test))]
fn lease_period_length() -> (T::BlockNumber, T::BlockNumber) {
T::Leaser::lease_period_length()
}

fn has_won_an_auction(para: ParaId, bidder: &T::AccountId) -> bool {
Expand All @@ -374,8 +381,9 @@ impl<T: Config> Pallet<T> {
) -> DispatchResult {
let maybe_auction = AuctionInfo::<T>::get();
ensure!(maybe_auction.is_none(), Error::<T>::AuctionInProgress);
let now = frame_system::Pallet::<T>::block_number();
ensure!(
lease_period_index >= T::Leaser::lease_period_index(),
lease_period_index >= T::Leaser::lease_period_index(now).0,
Error::<T>::LeasePeriodInPast
);

Expand Down Expand Up @@ -735,7 +743,7 @@ mod tests {
}

pub struct TestLeaser;
impl Leaser for TestLeaser {
impl Leaser<BlockNumber> for TestLeaser {
type AccountId = u64;
type LeasePeriod = BlockNumber;
type Currency = Balances;
Expand All @@ -749,7 +757,8 @@ mod tests {
) -> Result<(), LeaseError> {
LEASES.with(|l| {
let mut leases = l.borrow_mut();
if period_begin < Self::lease_period_index() {
let now = System::block_number();
if period_begin < Self::lease_period_index(now).0 {
return Err(LeaseError::AlreadyEnded)
}
for period in period_begin..(period_begin + period_count) {
Expand Down Expand Up @@ -779,12 +788,18 @@ mod tests {
.unwrap_or_default()
}

fn lease_period() -> Self::LeasePeriod {
10
fn lease_period_length() -> (BlockNumber, BlockNumber) {
(10, 0)
}

fn lease_period_index() -> Self::LeasePeriod {
(System::block_number() / Self::lease_period()).into()
fn lease_period_index(b: BlockNumber) -> (Self::LeasePeriod, bool) {
let (lease_period_length, offset) = Self::lease_period_length();
let b = b.saturating_sub(offset);
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved

let lease_period = b / lease_period_length;
let first_block = (b % lease_period_length).is_zero();

(lease_period, first_block)
}

fn already_leased(
Expand Down
96 changes: 67 additions & 29 deletions runtime/common/src/crowdloan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ use sp_runtime::{
};
use sp_std::vec::Vec;

type CurrencyOf<T> = <<T as Config>::Auctioneer as Auctioneer>::Currency;
type LeasePeriodOf<T> = <<T as Config>::Auctioneer as Auctioneer>::LeasePeriod;
type CurrencyOf<T> =
<<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;

#[allow(dead_code)]
Expand Down Expand Up @@ -203,8 +206,8 @@ pub mod pallet {

/// The type representing the auctioning system.
type Auctioneer: Auctioneer<
Self::BlockNumber,
AccountId = Self::AccountId,
BlockNumber = Self::BlockNumber,
LeasePeriod = Self::BlockNumber,
>;

Expand Down Expand Up @@ -365,18 +368,29 @@ pub mod pallet {
verifier: Option<MultiSigner>,
) -> DispatchResult {
let depositor = ensure_signed(origin)?;
let now = frame_system::Pallet::<T>::block_number();

ensure!(first_period <= last_period, Error::<T>::LastPeriodBeforeFirstPeriod);
let last_period_limit = first_period
.checked_add(&((SlotRange::LEASE_PERIODS_PER_SLOT as u32) - 1).into())
.ok_or(Error::<T>::FirstPeriodTooFarInFuture)?;
ensure!(last_period <= last_period_limit, Error::<T>::LastPeriodTooFarInFuture);
ensure!(end > <frame_system::Pallet<T>>::block_number(), Error::<T>::CannotEndInPast);
let last_possible_win_date = (first_period.saturating_add(One::one()))
.saturating_mul(T::Auctioneer::lease_period());
ensure!(end <= last_possible_win_date, Error::<T>::EndTooFarInFuture);
ensure!(end > now, Error::<T>::CannotEndInPast);

// Here we check the lease period on the ending block is at most the first block of the
// period after `first_period`. If it would be larger, there is no way we could win an
// active auction, thus it would make no sense to have a crowdloan this long.
let (lease_period_at_end, is_first_block) = T::Auctioneer::lease_period_index(end);
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.
ensure!(
first_period >= T::Auctioneer::lease_period_index(),
first_period >= T::Auctioneer::lease_period_index(now).0,
Error::<T>::FirstPeriodInPast
);

Expand Down Expand Up @@ -439,7 +453,8 @@ pub mod pallet {
ensure!(now < fund.end, Error::<T>::ContributionPeriodOver);

// 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).0;
ensure!(current_lease_period <= fund.first_period, Error::<T>::ContributionPeriodOver);

// Make sure crowdloan has not already won.
Expand Down Expand Up @@ -751,7 +766,7 @@ impl<T: Config> Pallet<T> {
// `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
// 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).0;
ensure!(
now >= fund.end || current_lease_period > fund.first_period,
Error::<T>::FundNotEnded
Expand Down Expand Up @@ -931,14 +946,14 @@ mod tests {
}

pub struct TestAuctioneer;
impl Auctioneer for TestAuctioneer {
impl Auctioneer<u64> for TestAuctioneer {
type AccountId = u64;
type BlockNumber = BlockNumber;
type LeasePeriod = u64;
type Currency = Balances;

fn new_auction(duration: u64, lease_period_index: u64) -> DispatchResult {
assert!(lease_period_index >= Self::lease_period_index());
let now = System::block_number();
assert!(lease_period_index >= Self::lease_period_index(now).0);

let ending = System::block_number().saturating_add(duration);
AUCTION.with(|p| *p.borrow_mut() = Some((lease_period_index, ending)));
Expand Down Expand Up @@ -991,12 +1006,17 @@ mod tests {
Ok(())
}

fn lease_period_index() -> u64 {
System::block_number() / Self::lease_period()
fn lease_period_index(b: BlockNumber) -> (u64, bool) {
let (lease_period_length, offset) = Self::lease_period_length();
let b = b.saturating_sub(offset);

let lease_period = b / lease_period_length;
let first_block = (b % lease_period_length).is_zero();
(lease_period, first_block)
}

fn lease_period() -> u64 {
20
fn lease_period_length() -> (u64, u64) {
(20, 0)
}

fn has_won_an_auction(para: ParaId, bidder: &u64) -> bool {
Expand Down Expand Up @@ -1367,7 +1387,8 @@ mod tests {
let para_3 = new_para();
assert_ok!(Crowdloan::create(Origin::signed(1), para_3, 1000, 1, 4, 40, None));
run_to_block(40);
assert_eq!(TestAuctioneer::lease_period_index(), 2);
let now = System::block_number();
assert_eq!(TestAuctioneer::lease_period_index(now).0, 2);
assert_noop!(
Crowdloan::contribute(Origin::signed(1), para_3, 49, None),
Error::<Test>::ContributionPeriodOver
Expand Down Expand Up @@ -1842,7 +1863,8 @@ mod benchmarking {

fn create_fund<T: Config>(id: u32, end: T::BlockNumber) -> ParaId {
let cap = BalanceOf::<T>::max_value();
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).0;
let first_period = lease_period_index;
let last_period =
lease_period_index + ((SlotRange::LEASE_PERIODS_PER_SLOT as u32) - 1).into();
Expand Down Expand Up @@ -1894,7 +1916,8 @@ mod benchmarking {
let cap = BalanceOf::<T>::max_value();
let first_period = 0u32.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 head_data = T::Registrar::worst_head_data();
Expand All @@ -1913,7 +1936,9 @@ mod benchmarking {

// Contribute has two arms: PreEnding and Ending, but both are equal complexity.
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 contribution = T::MinContribution::get();
CurrencyOf::<T>::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
Expand All @@ -1931,7 +1956,9 @@ mod benchmarking {
}

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 contributor = account("contributor", 0, 0);
contribute_fund::<T>(&contributor, fund_index);
Expand All @@ -1945,7 +1972,9 @@ mod benchmarking {
#[skip_meta]
refund {
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.
for i in 0 .. k {
Expand All @@ -1960,7 +1989,9 @@ mod benchmarking {
}

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();
frame_system::Pallet::<T>::set_block_number(T::BlockNumber::max_value());
}: _(RawOrigin::Signed(caller.clone()), fund_index)
Expand All @@ -1973,7 +2004,8 @@ mod benchmarking {
let cap = BalanceOf::<T>::max_value();
let first_period = 0u32.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 head_data = T::Registrar::worst_head_data();
Expand All @@ -1997,7 +2029,9 @@ mod benchmarking {
}

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();
contribute_fund::<T>(&caller, fund_index);
let worst_memo = vec![42; T::MaxMemoLength::get().into()];
Expand All @@ -2011,7 +2045,9 @@ mod benchmarking {
}

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();
contribute_fund::<T>(&caller, fund_index);
NewRaise::<T>::kill();
Expand All @@ -2028,7 +2064,8 @@ mod benchmarking {
on_initialize {
// We test the complexity over different number of new raise
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());

Expand All @@ -2043,7 +2080,8 @@ mod benchmarking {
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).0;
let duration = end_block
.checked_sub(&frame_system::Pallet::<T>::block_number())
.ok_or("duration of auction less than zero")?;
Expand Down
Loading