Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 41 additions & 22 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) -> Option<(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,10 +381,11 @@ impl<T: Config> Pallet<T> {
) -> DispatchResult {
let maybe_auction = AuctionInfo::<T>::get();
ensure!(maybe_auction.is_none(), Error::<T>::AuctionInProgress);
ensure!(
lease_period_index >= T::Leaser::lease_period_index(),
Error::<T>::LeasePeriodInPast
);
let now = frame_system::Pallet::<T>::block_number();
if let Some((current_lease_period, _)) = T::Leaser::lease_period_index(now) {
// 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.
let n = AuctionCounter::<T>::mutate(|n| {
Expand Down Expand Up @@ -567,7 +575,9 @@ impl<T: Config> Pallet<T> {
let period_count = LeasePeriodOf::<T>::from(range.len() as u32);

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
// present period). But if it does, there's not much we can do.
},
Expand Down Expand Up @@ -735,7 +745,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 +759,10 @@ 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();
let (current_lease_period, _) =
Self::lease_period_index(now).ok_or(LeaseError::NoLeasePeriod)?;
if period_begin < current_lease_period {
return Err(LeaseError::AlreadyEnded)
}
for period in period_begin..(period_begin + period_count) {
Expand Down Expand Up @@ -779,12 +792,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) -> Option<(Self::LeasePeriod, bool)> {
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(
Expand Down
Loading