Skip to content

Commit

Permalink
Merge branch 'main' into trigger-release-1.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
joepetrowski authored Jun 5, 2024
2 parents 500e291 + 2539cb5 commit 6c4a58e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 105 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- Staking runtime api to check if reward is pending for an era ([polkadot-fellows/runtimes#318](https://github.com/polkadot-fellows/runtimes/pull/318))
- Allow any parachain to have bidirectional channel with any system parachains ([polkadot-fellows/runtimes#329](https://github.com/polkadot-fellows/runtimes/pull/329))
- Update price controller of broker pallet to use higher leadin, without adjusting the minimum price too much ([polkadot-fellows/runtimes#334](https://github.com/polkadot-fellows/runtimes/pull/334)
- Enable support for new hardware signers like the generic ledger app ([polkadot-fellows/runtimes#337](https://github.com/polkadot-fellows/runtimes/pull/337))

### Changed
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pallet-bridge-grandpa = { version = "0.8.0", default-features = false }
pallet-bridge-messages = { version = "0.8.0", default-features = false }
pallet-bridge-parachains = { version = "0.8.0", default-features = false }
pallet-bridge-relayers = { version = "0.8.0", default-features = false }
pallet-broker = { version = "0.7.1", default-features = false }
pallet-broker = { version = "0.7.2", default-features = false }
pallet-child-bounties = { version = "28.0.0", default-features = false }
pallet-collator-selection = { version = "10.0.2", default-features = false }
pallet-collective = { version = "29.0.0", default-features = false }
Expand Down
48 changes: 3 additions & 45 deletions system-parachains/coretime/coretime-kusama/src/coretime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ use frame_support::{
OnUnbalanced,
},
};
use pallet_broker::{
AdaptPrice, CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf,
};
use sp_runtime::{
traits::{AccountIdConversion, One, Saturating},
FixedU64,
};
use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf};
use sp_runtime::traits::AccountIdConversion;
use xcm::latest::prelude::*;

/// A type containing the encoding of the coretime pallet in the Relay chain runtime. Used to
Expand Down Expand Up @@ -200,40 +195,6 @@ impl CoretimeInterface for CoretimeAllocator {
}
}

/// Implements the [`AdaptPrice`] trait to control the price changes of bulk coretime. This tweaks
/// the [`pallet_broker::Linear`] implementation which hard-corrects to 0 if cores are offered but
/// not sold for just one sale. The monotonic lead-in is increased in magnitude to enable faster
/// price finding. The change in base price between sales has a lower limit of 0.5 to allow downward
/// pressure to be applied, while keeping a conservative upper limit of 1.2 (movements capped at 20%
/// if cores sell out) to avoid runaway prices in the early sales. The intention is that this will
/// be coupled with a low number of cores per sale and a 100% ideal bulk ratio for the first sales.
pub struct PriceAdapter;
impl AdaptPrice for PriceAdapter {
fn leadin_factor_at(when: FixedU64) -> FixedU64 {
// Start at 5x the base price and decrease to the base price at the end of leadin.
FixedU64::from(5).saturating_sub(FixedU64::from(4) * when)
}

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(),
))
} else {
// Range of (1.0, 1.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).saturating_mul(5).into(),
))
}
}
}

parameter_types! {
pub const BrokerPalletId: PalletId = PalletId(*b"py/broke");
}
Expand All @@ -253,8 +214,5 @@ impl pallet_broker::Config for Runtime {
type WeightInfo = weights::pallet_broker::WeightInfo<Runtime>;
type PalletId = BrokerPalletId;
type AdminOrigin = EnsureRoot<AccountId>;
#[cfg(feature = "runtime-benchmarks")]
type PriceAdapter = pallet_broker::Linear;
#[cfg(not(feature = "runtime-benchmarks"))]
type PriceAdapter = PriceAdapter;
type PriceAdapter = pallet_broker::CenterTargetPrice<Balance>;
}
62 changes: 5 additions & 57 deletions system-parachains/coretime/coretime-kusama/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.

use crate::{
coretime::{BrokerPalletId, CoretimeBurnAccount, PriceAdapter},
coretime::{BrokerPalletId, CoretimeBurnAccount},
*,
};
use frame_support::{
Expand All @@ -25,61 +25,9 @@ use frame_support::{
OnInitialize,
},
};
use pallet_broker::{AdaptPrice, ConfigRecordOf, SaleInfo};
use pallet_broker::{ConfigRecordOf, SaleInfo};
use parachains_runtimes_test_utils::ExtBuilder;
use sp_runtime::{
traits::{AccountIdConversion, One},
FixedU64,
};

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

if sold > target {
assert!(price > FixedU64::one());
} else {
assert!(price <= FixedU64::one());
}
}
}
}
}

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

// Maximally sold: `sold == limit`
assert_eq!(PriceAdapter::adapt_price(limit, target, limit), FixedU64::from_float(1.2));
// Ideally sold: `sold == target`
assert_eq!(PriceAdapter::adapt_price(target, target, limit), FixedU64::one());
// Minimally sold: `sold == 0`
assert_eq!(PriceAdapter::adapt_price(0, target, limit), FixedU64::from_float(0.5));
// Optimistic target: `target == limit`
assert_eq!(PriceAdapter::adapt_price(limit, limit, limit), FixedU64::one());
// Pessimistic target: `target == 0`
assert_eq!(PriceAdapter::adapt_price(limit, 0, limit), FixedU64::from_float(1.2));
}

#[test]
fn leadin_price_bound_check() {
// Using assumptions from pallet implementation i.e. `when` in range [0, 1].
// Linear, therefore we need three points to fully test it

// Start of leadin: 5x
assert_eq!(PriceAdapter::leadin_factor_at(FixedU64::from(0)), FixedU64::from(5));
// Midway through leadin: 3x
assert_eq!(PriceAdapter::leadin_factor_at(FixedU64::from_float(0.5)), FixedU64::from(3));
// End of leadin: 1x
assert_eq!(PriceAdapter::leadin_factor_at(FixedU64::one()), FixedU64::one());
}
use sp_runtime::traits::AccountIdConversion;

fn advance_to(b: BlockNumber) {
while System::block_number() < b {
Expand Down Expand Up @@ -124,14 +72,14 @@ fn bulk_revenue_is_burnt() {
let broker_account = BrokerPalletId::get().into_account_truncating();
let coretime_burn_account = CoretimeBurnAccount::get();
let treasury_account = xcm_config::RelayTreasuryPalletAccount::get();
assert_ok!(Balances::mint_into(&AccountId::from(ALICE), 10 * ed));
assert_ok!(Balances::mint_into(&AccountId::from(ALICE), 200 * ed));
let alice_balance_before = Balances::balance(&AccountId::from(ALICE));
let treasury_balance_before = Balances::balance(&treasury_account);
let broker_balance_before = Balances::balance(&broker_account);
let burn_balance_before = Balances::balance(&coretime_burn_account);

// Purchase coretime.
assert_ok!(Broker::purchase(RuntimeOrigin::signed(AccountId::from(ALICE)), 5 * ed));
assert_ok!(Broker::purchase(RuntimeOrigin::signed(AccountId::from(ALICE)), 100 * ed));

// Alice decreases.
assert!(Balances::balance(&AccountId::from(ALICE)) < alice_balance_before);
Expand Down

0 comments on commit 6c4a58e

Please sign in to comment.