Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d598a1e
Price adjustements should be based on price
May 10, 2024
ae90650
AllowedRenewal -> PotentialRenewal
May 20, 2024
2855832
WIP: New price based `AdaptPrice` trait.
May 20, 2024
627acf7
Pub
May 21, 2024
31ea3a9
Also adjust sellout_price on renew.
May 21, 2024
63f5856
Adapt price implementation.
May 21, 2024
3630ccc
Some tests + handle going down to zero gracefully.
May 21, 2024
b615fbe
Fix tests.
May 21, 2024
fd03dc1
More linear tests.
May 21, 2024
6fdc11b
Add prdoc.
May 22, 2024
cb5485d
Put base price in the middle of the range.
May 23, 2024
9299010
new leadin curve.
May 23, 2024
28f13f4
price -> base_price
May 23, 2024
4b5a66d
Fmt
May 23, 2024
fad4aab
base_price -> min_price
May 24, 2024
4979fbb
Merge branch 'master' into rk-broker-new-price-adapter
May 27, 2024
5869882
New example adapter name + more parameters (cores)
May 27, 2024
baeab0e
Fix kitchensink
May 27, 2024
aa4defc
Some logging + test.
May 27, 2024
d5992b4
Fix prdoc
May 27, 2024
03db451
regular_price -> end_price
May 27, 2024
d67dfc4
Fixes.
May 27, 2024
edeaf69
Merge remote-tracking branch 'origin/master' into rk-broker-new-price…
May 27, 2024
890e6ca
Fix benchmarks
May 27, 2024
66bce5a
Update prdoc/pr_4521.prdoc
eskimor May 29, 2024
fbe5241
min_price -> end_price
May 29, 2024
fdf764e
Better docs for target_price.
May 29, 2024
86ec07c
Improved prdoc
May 29, 2024
1ca75af
Merge remote-tracking branch 'origin/rk-broker-new-price-adapter' int…
May 29, 2024
2a940a5
Make change major again.
May 29, 2024
52e0b80
One more test.
May 29, 2024
812f66c
Add migration for name change `PotentialRenewals`.
May 29, 2024
ae20d3c
Merge remote-tracking branch 'origin/master' into rk-broker-new-price…
May 29, 2024
7e2992c
Fix prdoc?
May 29, 2024
654d1e4
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
May 29, 2024
4cc238a
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
May 29, 2024
d6857aa
Change bump to minor again to make CI happy.
May 29, 2024
c98a945
Merge remote-tracking branch 'origin/rk-broker-new-price-adapter' int…
May 29, 2024
d179b89
prdoc fix
May 29, 2024
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
Prev Previous commit
Next Next commit
price -> base_price
  • Loading branch information
eskimor committed May 23, 2024
commit 28f13f463cc2e1fb4953221c6dd178b82daed074
68 changes: 34 additions & 34 deletions substrate/frame/broker/src/adapt_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ pub struct SalePerformance<Balance> {
pub sellout_price: Option<Balance>,

/// The base price (lowest possible price) that was used in this sale.
pub price: Balance,
pub base_price: Balance,
}

/// Result of `AdaptPrice::adapt_price`.
#[derive(Copy, Clone)]
pub struct AdaptedPrices<Balance> {
/// New base price to use.
pub price: Balance,
pub base_price: Balance,
/// Price we optimize for.
pub renewal_price: Balance,
pub target_price: Balance,
}

impl<Balance: Copy> SalePerformance<Balance> {
/// Construct performance via data from a `SaleInfoRecord`.
pub fn from_sale<BlockNumber>(record: &SaleInfoRecord<Balance, BlockNumber>) -> Self {
Self { sellout_price: record.sellout_price, price: record.price }
Self { sellout_price: record.sellout_price, base_price: record.base_price }
}
}

Expand All @@ -67,8 +67,8 @@ impl<Balance: Copy> AdaptPrice<Balance> for () {
FixedU64::one()
}
fn adapt_price(performance: SalePerformance<Balance>) -> AdaptedPrices<Balance> {
let price = performance.sellout_price.unwrap_or(performance.price);
AdaptedPrices { price, renewal_price: price }
let price = performance.sellout_price.unwrap_or(performance.base_price);
AdaptedPrices { base_price: price, target_price: price }
}
}

Expand All @@ -88,8 +88,8 @@ impl<Balance: FixedPointOperand> AdaptPrice<Balance> for Linear<Balance> {
fn adapt_price(performance: SalePerformance<Balance>) -> AdaptedPrices<Balance> {
let Some(sellout_price) = performance.sellout_price else {
return AdaptedPrices {
price: performance.price,
renewal_price: FixedU64::from(10).saturating_mul_int(performance.price),
base_price: performance.base_price,
target_price: FixedU64::from(10).saturating_mul_int(performance.base_price),
}
};

Expand All @@ -101,7 +101,7 @@ impl<Balance: FixedPointOperand> AdaptPrice<Balance> for Linear<Balance> {
price
};

AdaptedPrices { price, renewal_price: sellout_price }
AdaptedPrices { base_price: price, target_price: sellout_price }
}
}

Expand All @@ -114,77 +114,77 @@ mod tests {
for sellout in 0..11 {
for price in 0..10 {
let sellout_price = if sellout == 11 { None } else { Some(sellout) };
Linear::adapt_price(SalePerformance { sellout_price, price });
Linear::adapt_price(SalePerformance { sellout_price, base_price: price });
}
}
}

#[test]
fn no_op_sale_is_good() {
let prices = Linear::adapt_price(SalePerformance { sellout_price: None, price: 1 });
assert_eq!(prices.renewal_price, 10);
assert_eq!(prices.price, 1);
let prices = Linear::adapt_price(SalePerformance { sellout_price: None, base_price: 1 });
assert_eq!(prices.target_price, 10);
assert_eq!(prices.base_price, 1);
}

#[test]
fn price_stays_stable_on_optimal_sale() {
// Check price stays stable if sold at the optimal price:
let mut performance = SalePerformance { sellout_price: Some(1000), price: 100 };
let mut performance = SalePerformance { sellout_price: Some(1000), base_price: 100 };
for _ in 0..10 {
let prices = Linear::adapt_price(performance);
performance.sellout_price = Some(1000);
performance.price = prices.price;
performance.base_price = prices.base_price;

assert!(prices.price <= 101);
assert!(prices.price >= 99);
assert!(prices.renewal_price <= 1001);
assert!(prices.renewal_price >= 999);
assert!(prices.base_price <= 101);
assert!(prices.base_price >= 99);
assert!(prices.target_price <= 1001);
assert!(prices.target_price >= 999);
}
}

#[test]
fn price_adjusts_correctly_upwards() {
let performance = SalePerformance { sellout_price: Some(10_000), price: 100 };
let performance = SalePerformance { sellout_price: Some(10_000), base_price: 100 };
let prices = Linear::adapt_price(performance);
assert_eq!(prices.renewal_price, 10_000);
assert_eq!(prices.price, 1000);
assert_eq!(prices.target_price, 10_000);
assert_eq!(prices.base_price, 1000);
}

#[test]
fn price_adjusts_correctly_downwards() {
let performance = SalePerformance { sellout_price: Some(100), price: 100 };
let performance = SalePerformance { sellout_price: Some(100), base_price: 100 };
let prices = Linear::adapt_price(performance);
assert_eq!(prices.renewal_price, 100);
assert_eq!(prices.price, 10);
assert_eq!(prices.target_price, 100);
assert_eq!(prices.base_price, 10);
}

#[test]
fn price_never_goes_to_zero_and_recovers() {
// Check price stays stable if sold at the optimal price:
let sellout_price = 1;
let mut performance = SalePerformance { sellout_price: Some(sellout_price), price: 1 };
let mut performance = SalePerformance { sellout_price: Some(sellout_price), base_price: 1 };
for _ in 0..11 {
let prices = Linear::adapt_price(performance);
performance.sellout_price = Some(sellout_price);
performance.price = prices.price;
performance.base_price = prices.base_price;

assert!(prices.price <= sellout_price);
assert!(prices.price > 0);
assert!(prices.base_price <= sellout_price);
assert!(prices.base_price > 0);
}
}

#[test]
fn renewal_price_is_correct_on_no_sale() {
let performance = SalePerformance { sellout_price: None, price: 100 };
let performance = SalePerformance { sellout_price: None, base_price: 100 };
let prices = Linear::adapt_price(performance);
assert_eq!(prices.renewal_price, 1000);
assert_eq!(prices.price, 100);
assert_eq!(prices.target_price, 1000);
assert_eq!(prices.base_price, 100);
}

#[test]
fn renewal_price_is_sell_out() {
let performance = SalePerformance { sellout_price: Some(1000), price: 100 };
let performance = SalePerformance { sellout_price: Some(1000), base_price: 100 };
let prices = Linear::adapt_price(performance);
assert_eq!(prices.renewal_price, 1000);
assert_eq!(prices.target_price, 1000);
}
}
6 changes: 3 additions & 3 deletions substrate/frame/broker/src/dispatchable_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub(crate) fn do_start_sales(price: BalanceOf<T>, extra_cores: CoreIndex) -> DispatchResult {
pub(crate) fn do_start_sales(base_price: BalanceOf<T>, extra_cores: CoreIndex) -> DispatchResult {
let config = Configuration::<T>::get().ok_or(Error::<T>::Uninitialized)?;

// Determine the core count
Expand All @@ -93,7 +93,7 @@ impl<T: Config> Pallet<T> {
let old_sale = SaleInfoRecord {
sale_start: now,
leadin_length: Zero::zero(),
price,
base_price,
sellout_price: None,
region_begin: commit_timeslice,
region_end: commit_timeslice.saturating_add(config.region_length),
Expand All @@ -102,7 +102,7 @@ impl<T: Config> Pallet<T> {
cores_offered: 0,
cores_sold: 0,
};
Self::deposit_event(Event::<T>::SalesStarted { price, core_count });
Self::deposit_event(Event::<T>::SalesStarted { price: base_price, core_count });
Self::rotate_sale(old_sale, &config, &status);
Status::<T>::put(&status);
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions substrate/frame/broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ pub mod pallet {
/// Begin the Bulk Coretime sales rotation.
///
/// - `origin`: Must be Root or pass `AdminOrigin`.
/// - `initial_price`: The price of Bulk Coretime in the first sale.
/// - `base_price`: The price after the leadin period of Bulk Coretime in the first sale.
/// - `extra_cores`: Number of extra cores that should be requested on top of the cores
/// required for `Reservations` and `Leases`.
///
Expand All @@ -571,11 +571,11 @@ pub mod pallet {
))]
pub fn start_sales(
origin: OriginFor<T>,
initial_price: BalanceOf<T>,
base_price: BalanceOf<T>,
extra_cores: CoreIndex,
) -> DispatchResultWithPostInfo {
T::AdminOrigin::ensure_origin_or_root(origin)?;
Self::do_start_sales(initial_price, extra_cores)?;
Self::do_start_sales(base_price, extra_cores)?;
Ok(Pays::No.into())
}

Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/broker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ fn purchase_requires_valid_status_and_sale_info() {
let mut dummy_sale = SaleInfoRecord {
sale_start: 0,
leadin_length: 0,
price: 200,
base_price: 200,
sellout_price: None,
region_begin: 0,
region_end: 3,
Expand Down Expand Up @@ -1146,7 +1146,7 @@ fn renewal_requires_valid_status_and_sale_info() {
let mut dummy_sale = SaleInfoRecord {
sale_start: 0,
leadin_length: 0,
price: 200,
base_price: 200,
sellout_price: None,
region_begin: 0,
region_end: 3,
Expand Down
10 changes: 5 additions & 5 deletions substrate/frame/broker/src/tick_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ impl<T: Config> Pallet<T> {
// last time for this one - make it renewable in the next sale.
let renewal_id = PotentialRenewalId { core: first_core, when: region_end };
let record = PotentialRenewalRecord {
price: new_prices.renewal_price,
price: new_prices.target_price,
completion: Complete(schedule),
};
PotentialRenewals::<T>::insert(renewal_id, &record);
Self::deposit_event(Event::Renewable {
core: first_core,
price: new_prices.renewal_price,
price: new_prices.target_price,
begin: region_end,
workload: record.completion.drain_complete().unwrap_or_default(),
});
Expand All @@ -222,7 +222,7 @@ impl<T: Config> Pallet<T> {
let ideal_cores_sold = (config.ideal_bulk_proportion * cores_offered as u32) as u16;
let sellout_price = if cores_offered > 0 {
// No core sold -> price was too high -> we have to adjust downwards.
Some(new_prices.price)
Some(new_prices.base_price)
} else {
None
};
Expand All @@ -231,7 +231,7 @@ impl<T: Config> Pallet<T> {
let new_sale = SaleInfoRecord {
sale_start,
leadin_length,
price: new_prices.price,
base_price: new_prices.base_price,
sellout_price,
region_begin,
region_end,
Expand All @@ -246,7 +246,7 @@ impl<T: Config> Pallet<T> {
sale_start,
leadin_length,
start_price: Self::sale_price(&new_sale, now),
regular_price: new_prices.price,
regular_price: new_prices.base_price,
region_begin,
region_end,
ideal_cores_sold,
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/broker/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub struct SaleInfoRecord<Balance, BlockNumber> {
/// The length in blocks of the Leadin Period (where the price is decreasing).
pub leadin_length: BlockNumber,
/// The price of Bulk Coretime after the Leadin Period.
pub price: Balance,
pub base_price: Balance,
/// The first timeslice of the Regions which are being sold in this sale.
pub region_begin: Timeslice,
/// The timeslice on which the Regions which are being sold in the sale terminate. (i.e. One
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/broker/src/utility_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<T: Config> Pallet<T> {
pub fn sale_price(sale: &SaleInfoRecordOf<T>, now: BlockNumberFor<T>) -> BalanceOf<T> {
let num = now.saturating_sub(sale.sale_start).min(sale.leadin_length).saturated_into();
let through = FixedU64::from_rational(num, sale.leadin_length.saturated_into());
T::PriceAdapter::leadin_factor_at(through).saturating_mul_int(sale.price)
T::PriceAdapter::leadin_factor_at(through).saturating_mul_int(sale.base_price)
}

pub(crate) fn charge(who: &T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
Expand Down