Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broker new price adapter #4521

Merged
merged 39 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ impl<T: frame_system::Config> pallet_broker::WeightInfo for WeightInfo<T> {
/// Proof: `Broker::Status` (`max_values`: Some(1), `max_size`: Some(18), added: 513, mode: `MaxEncodedLen`)
/// Storage: `Broker::SaleInfo` (r:1 w:1)
/// Proof: `Broker::SaleInfo` (`max_values`: Some(1), `max_size`: Some(57), added: 552, mode: `MaxEncodedLen`)
/// Storage: `Broker::AllowedRenewals` (r:1 w:2)
/// Proof: `Broker::AllowedRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
/// Storage: `Broker::PotentialRenewals` (r:1 w:2)
/// Proof: `Broker::PotentialRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:0)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `Broker::Workplan` (r:0 w:1)
Expand Down Expand Up @@ -337,8 +337,8 @@ impl<T: frame_system::Config> pallet_broker::WeightInfo for WeightInfo<T> {
}
/// Storage: `Broker::Status` (r:1 w:0)
/// Proof: `Broker::Status` (`max_values`: Some(1), `max_size`: Some(18), added: 513, mode: `MaxEncodedLen`)
/// Storage: `Broker::AllowedRenewals` (r:1 w:1)
/// Proof: `Broker::AllowedRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
/// Storage: `Broker::PotentialRenewals` (r:1 w:1)
/// Proof: `Broker::PotentialRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
fn drop_renewal() -> Weight {
// Proof Size summary in bytes:
// Measured: `957`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ impl<T: frame_system::Config> pallet_broker::WeightInfo for WeightInfo<T> {
/// Proof: `Broker::Status` (`max_values`: Some(1), `max_size`: Some(18), added: 513, mode: `MaxEncodedLen`)
/// Storage: `Broker::SaleInfo` (r:1 w:1)
/// Proof: `Broker::SaleInfo` (`max_values`: Some(1), `max_size`: Some(57), added: 552, mode: `MaxEncodedLen`)
/// Storage: `Broker::AllowedRenewals` (r:1 w:2)
/// Proof: `Broker::AllowedRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
/// Storage: `Broker::PotentialRenewals` (r:1 w:2)
/// Proof: `Broker::PotentialRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:0)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `Broker::Workplan` (r:0 w:1)
Expand Down Expand Up @@ -335,8 +335,8 @@ impl<T: frame_system::Config> pallet_broker::WeightInfo for WeightInfo<T> {
}
/// Storage: `Broker::Status` (r:1 w:0)
/// Proof: `Broker::Status` (`max_values`: Some(1), `max_size`: Some(18), added: 513, mode: `MaxEncodedLen`)
/// Storage: `Broker::AllowedRenewals` (r:1 w:1)
/// Proof: `Broker::AllowedRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
/// Storage: `Broker::PotentialRenewals` (r:1 w:1)
/// Proof: `Broker::PotentialRenewals` (`max_values`: None, `max_size`: Some(1233), added: 3708, mode: `MaxEncodedLen`)
fn drop_renewal() -> Weight {
// Proof Size summary in bytes:
// Measured: `556`
Expand Down
23 changes: 23 additions & 0 deletions prdoc/pr_4521.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
title: Broker pallet: AdaptPrice price controller is now price controlled.

doc:
- audience: Runtime Dev
description: |
We discovered a flaw of the current price controller interface in the
broker pallet. This is fixed by changing the interface to no longer
operate on the number of cores sold, but rather on the price that was
achieved during the sale. More information here:
eskimor marked this conversation as resolved.
Show resolved Hide resolved

https://github.com/paritytech/polkadot-sdk/issues/4360

- audience: Runtime User
description: |
The price controller of the Coretime chain will be adjusted with this
eskimor marked this conversation as resolved.
Show resolved Hide resolved
release. This will very likely be used in the fellowship production
runtime to have a much larger leadin. This fixes a price manipulation
issue we discovered with the Kusama launch.

crates:
- name: pallet-broker
bump: major

198 changes: 140 additions & 58 deletions substrate/frame/broker/src/adapt_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,97 @@

#![deny(missing_docs)]

use crate::CoreIndex;
use crate::SaleInfoRecord;
use sp_arithmetic::{traits::One, FixedU64};
use sp_runtime::Saturating;
use sp_runtime::{FixedPointNumber, FixedPointOperand, Saturating};

/// Performance of a past sale.
#[derive(Copy, Clone)]
pub struct SalePerformance<Balance> {
eskimor marked this conversation as resolved.
Show resolved Hide resolved
/// The price at which the last core was sold.
///
/// Will be `None` if no cores have been offered.
pub sellout_price: Option<Balance>,

/// The base price (lowest possible price) that was used in this sale.
pub price: Balance,
eskimor marked this conversation as resolved.
Show resolved Hide resolved
}

/// Result of `AdaptPrice::adapt_price`.
#[derive(Copy, Clone)]
pub struct AdaptedPrices<Balance> {
/// New base price to use.
pub price: Balance,
/// Price to use for renewals of leases.
pub renewal_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 }
}
}

/// Type for determining how to set price.
pub trait AdaptPrice {
pub trait AdaptPrice<Balance> {
/// Return the factor by which the regular price must be multiplied during the leadin period.
///
/// - `when`: The amount through the leadin period; between zero and one.
fn leadin_factor_at(when: FixedU64) -> FixedU64;
/// Return the correction factor by which the regular price must be multiplied based on market
/// performance.

/// Return adapted prices for next sale.
///
/// - `sold`: The number of cores sold.
/// - `target`: The target number of cores to be sold (must be larger than zero).
/// - `limit`: The maximum number of cores to be sold.
fn adapt_price(sold: CoreIndex, target: CoreIndex, limit: CoreIndex) -> FixedU64;
/// Based on this sale's performance.
fn adapt_price(performance: SalePerformance<Balance>) -> AdaptedPrices<Balance>;
}

impl AdaptPrice for () {
impl<Balance: Copy> AdaptPrice<Balance> for () {
fn leadin_factor_at(_: FixedU64) -> FixedU64 {
FixedU64::one()
}
fn adapt_price(_: CoreIndex, _: CoreIndex, _: CoreIndex) -> FixedU64 {
FixedU64::one()
fn adapt_price(performance: SalePerformance<Balance>) -> AdaptedPrices<Balance> {
let price = performance.sellout_price.unwrap_or(performance.price);
AdaptedPrices { price, renewal_price: price }
eskimor marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Simple implementation of `AdaptPrice` giving a monotonic leadin and a linear price change based
/// on cores sold.
pub struct Linear;
eskimor marked this conversation as resolved.
Show resolved Hide resolved
impl AdaptPrice for Linear {
pub struct Linear<Balance>(std::marker::PhantomData<Balance>);

impl<Balance: FixedPointOperand> AdaptPrice<Balance> for Linear<Balance> {
fn leadin_factor_at(when: FixedU64) -> FixedU64 {
FixedU64::from(2).saturating_sub(when)
FixedU64::from(101).saturating_sub(when.saturating_mul(100.into()))
eskimor marked this conversation as resolved.
Show resolved Hide resolved
}
fn adapt_price(sold: CoreIndex, target: CoreIndex, limit: CoreIndex) -> FixedU64 {
if sold <= target {
// Range of [0.5, 1.0].
FixedU64::from_rational(1, 2).saturating_add(FixedU64::from_rational(
sold.into(),
target.saturating_mul(2).into(),
))

fn adapt_price(performance: SalePerformance<Balance>) -> AdaptedPrices<Balance> {
let leadin_max = Self::leadin_factor_at(0.into());
let leadin_min = Self::leadin_factor_at(1.into());
let spread = leadin_max.saturating_sub(leadin_min);

let Some(sellout_price) = performance.sellout_price else {
eskimor marked this conversation as resolved.
Show resolved Hide resolved
return AdaptedPrices {
price: performance.price,
renewal_price: spread
.saturating_add(2.into())
.div(2.into())
.saturating_mul_int(performance.price),
}
};

eskimor marked this conversation as resolved.
Show resolved Hide resolved
let price = FixedU64::from(2)
.div(spread.saturating_add(2.into()))
.saturating_mul_int(sellout_price);

let price = if price == Balance::zero() {
// We could not recover from a price equal 0 ever.
sellout_price
} else {
// Range of (1.0, 2].

// Unchecked math: In this branch we know that sold > target. The limit must be >= sold
// by construction, and thus target must be < limit.
FixedU64::one().saturating_add(FixedU64::from_rational(
(sold - target).into(),
(limit - target).into(),
))
}
price
};

AdaptedPrices { price, renewal_price: sellout_price }
}
}

Expand All @@ -78,37 +117,80 @@ mod tests {

#[test]
fn linear_no_panic() {
for limit in 0..10 {
for target in 1..10 {
for sold in 0..=limit {
let price = Linear::adapt_price(sold, target, limit);

if sold > target {
assert!(price > FixedU64::one());
} else {
assert!(price <= FixedU64::one());
}
}
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 });
}
}
}

#[test]
fn linear_bound_check() {
// Using constraints from pallet implementation i.e. `limit >= sold`.
// Check extremes
let limit = 10;
let target = 5;

// Maximally sold: `sold == limit`
assert_eq!(Linear::adapt_price(limit, target, limit), FixedU64::from_float(2.0));
// Ideally sold: `sold == target`
assert_eq!(Linear::adapt_price(target, target, limit), FixedU64::one());
// Minimally sold: `sold == 0`
assert_eq!(Linear::adapt_price(0, target, limit), FixedU64::from_float(0.5));
// Optimistic target: `target == limit`
assert_eq!(Linear::adapt_price(limit, limit, limit), FixedU64::one());
// Pessimistic target: `target == 0`
assert_eq!(Linear::adapt_price(limit, 0, limit), FixedU64::from_float(2.0));
fn no_op_sale_is_good() {
let prices = Linear::adapt_price(SalePerformance { sellout_price: None, price: 1 });
assert_eq!(prices.renewal_price, 51);
assert_eq!(prices.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(5100), price: 100 };
for _ in 0..10 {
let prices = Linear::adapt_price(performance);
performance.sellout_price = Some(5100);
performance.price = prices.price;

assert!(prices.price <= 101);
assert!(prices.price >= 99);
assert!(prices.renewal_price <= 5101);
assert!(prices.renewal_price >= 5099);
}
}

#[test]
fn price_adjusts_correctly_upwards() {
let performance = SalePerformance { sellout_price: Some(10_100), price: 100 };
let prices = Linear::adapt_price(performance);
assert_eq!(prices.renewal_price, 10_100);
assert_eq!(prices.price, 2 * 10_100 / 102);
}

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

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

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

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

#[test]
fn renewal_price_is_sell_out() {
let performance = SalePerformance { sellout_price: Some(1000), price: 100 };
let prices = Linear::adapt_price(performance);
assert_eq!(prices.renewal_price, 1000);
}
}
14 changes: 7 additions & 7 deletions substrate/frame/broker/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ mod benches {
#[extrinsic_call]
_(RawOrigin::Signed(caller), region.core);

let id = AllowedRenewalId { core: region.core, when: region.begin + region_len * 2 };
assert!(AllowedRenewals::<T>::get(id).is_some());
let id = PotentialRenewalId { core: region.core, when: region.begin + region_len * 2 };
assert!(PotentialRenewals::<T>::get(id).is_some());

Ok(())
}
Expand Down Expand Up @@ -670,20 +670,20 @@ mod benches {
(T::TimeslicePeriod::get() * (region_len * 3).into()).try_into().ok().unwrap(),
);

let id = AllowedRenewalId { core, when };
let record = AllowedRenewalRecord {
let id = PotentialRenewalId { core, when };
let record = PotentialRenewalRecord {
price: 1u32.into(),
completion: CompletionStatus::Complete(new_schedule()),
};
AllowedRenewals::<T>::insert(id, record);
PotentialRenewals::<T>::insert(id, record);

let caller: T::AccountId = whitelisted_caller();

#[extrinsic_call]
_(RawOrigin::Signed(caller), core, when);

assert!(AllowedRenewals::<T>::get(id).is_none());
assert_last_event::<T>(Event::AllowedRenewalDropped { core, when }.into());
assert!(PotentialRenewals::<T>::get(id).is_none());
assert_last_event::<T>(Event::PotentialRenewalDropped { core, when }.into());

Ok(())
}
Expand Down
Loading
Loading