From 388597fd93f8a6dc07d8518800aa1b9a7a54377a Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 6 Jun 2022 15:18:23 +0100 Subject: [PATCH 1/6] Update curve.rs --- primitives/runtime/src/curve.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/primitives/runtime/src/curve.rs b/primitives/runtime/src/curve.rs index c6bfa66017870..81683a790bdc8 100644 --- a/primitives/runtime/src/curve.rs +++ b/primitives/runtime/src/curve.rs @@ -25,9 +25,9 @@ use core::ops::Sub; /// Piecewise Linear function in [0, 1] -> [0, 1]. #[derive(PartialEq, Eq, sp_core::RuntimeDebug, scale_info::TypeInfo)] -pub struct PiecewiseLinear<'a> { +pub struct PiecewiseLinear { /// Array of points. Must be in order from the lowest abscissas to the highest. - pub points: &'a [(Perbill, Perbill)], + pub points: &'static [(Perbill, Perbill)], /// The maximum value that can be returned. pub maximum: Perbill, } @@ -36,7 +36,7 @@ fn abs_sub + Clone>(a: N, b: N) -> N where { a.clone().max(b.clone()) - a.min(b) } -impl<'a> PiecewiseLinear<'a> { +impl PiecewiseLinear { /// Compute `f(n/d)*d` with `n <= d`. This is useful to avoid loss of precision. pub fn calculate_for_fraction_times_denominator(&self, n: N, d: N) -> N where From 83bd7245e1bb98fbef1576d63bdf8baccc47cd76 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 6 Jun 2022 15:23:07 +0100 Subject: [PATCH 2/6] Update bounded_vec.rs --- frame/support/src/storage/bounded_vec.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/frame/support/src/storage/bounded_vec.rs b/frame/support/src/storage/bounded_vec.rs index f1f4330ab2960..dd01c9b219ce0 100644 --- a/frame/support/src/storage/bounded_vec.rs +++ b/frame/support/src/storage/bounded_vec.rs @@ -109,16 +109,13 @@ where /// Similar to a `BoundedVec`, but not owned and cannot be decoded. #[derive(Encode, scale_info::TypeInfo)] #[scale_info(skip_type_params(S))] -pub struct BoundedSlice<'a, T, S>(&'a [T], PhantomData); +pub struct BoundedSlice(&'static [T], PhantomData); // `BoundedSlice`s encode to something which will always decode into a `BoundedVec`, // `WeakBoundedVec`, or a `Vec`. -impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} -impl<'a, T: Encode + Decode, S: Get> EncodeLike> - for BoundedSlice<'a, T, S> -{ -} -impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} +impl> EncodeLike> for BoundedSlice {} +impl> EncodeLike> for BoundedSlice {} +impl> EncodeLike> for BoundedSlice {} impl> PartialOrd for BoundedVec { fn partial_cmp(&self, other: &Self) -> Option { @@ -132,9 +129,9 @@ impl> Ord for BoundedVec { } } -impl<'a, T, S: Get> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> { +impl> TryFrom<&'static [T]> for BoundedSlice { type Error = (); - fn try_from(t: &'a [T]) -> Result { + fn try_from(t: &'static [T]) -> Result { if t.len() < S::get() as usize { Ok(BoundedSlice(t, PhantomData)) } else { @@ -143,8 +140,8 @@ impl<'a, T, S: Get> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> { } } -impl<'a, T, S> From> for &'a [T] { - fn from(t: BoundedSlice<'a, T, S>) -> Self { +impl From> for &'static [T] { + fn from(t: BoundedSlice) -> Self { t.0 } } From e9909d2d8625104935c0c75f44e561991780d112 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 6 Jun 2022 15:26:31 +0100 Subject: [PATCH 3/6] fix piecewiselinear --- bin/node/runtime/src/lib.rs | 4 ++-- frame/babe/src/mock.rs | 4 ++-- frame/grandpa/src/mock.rs | 4 ++-- frame/nomination-pools/benchmarking/src/mock.rs | 4 ++-- frame/offences/benchmarking/src/mock.rs | 4 ++-- frame/session/benchmarking/src/mock.rs | 4 ++-- frame/staking/reward-curve/src/lib.rs | 2 +- frame/staking/reward-curve/tests/test.rs | 4 ++-- frame/staking/src/inflation.rs | 4 ++-- frame/staking/src/lib.rs | 2 +- frame/staking/src/mock.rs | 4 ++-- 11 files changed, 20 insertions(+), 20 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index b421a4f05ed1a..4693438862266 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -510,7 +510,7 @@ impl pallet_session::historical::Config for Runtime { } pallet_staking_reward_curve::build! { - const REWARD_CURVE: PiecewiseLinear<'static> = curve!( + const REWARD_CURVE: PiecewiseLinear = curve!( min_inflation: 0_025_000, max_inflation: 0_100_000, ideal_stake: 0_500_000, @@ -524,7 +524,7 @@ parameter_types! { pub const SessionsPerEra: sp_staking::SessionIndex = 6; pub const BondingDuration: sp_staking::EraIndex = 24 * 28; pub const SlashDeferDuration: sp_staking::EraIndex = 24 * 7; // 1/4 the bonding duration. - pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; + pub const RewardCurve: &'static PiecewiseLinear = &REWARD_CURVE; pub const MaxNominatorRewardedPerValidator: u32 = 256; pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17); pub OffchainRepeat: BlockNumber = 5; diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index 5677eb7e28e49..6bacda9565111 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -154,7 +154,7 @@ impl pallet_balances::Config for Test { } pallet_staking_reward_curve::build! { - const REWARD_CURVE: PiecewiseLinear<'static> = curve!( + const REWARD_CURVE: PiecewiseLinear = curve!( min_inflation: 0_025_000u64, max_inflation: 0_100_000, ideal_stake: 0_500_000, @@ -168,7 +168,7 @@ parameter_types! { pub const SessionsPerEra: SessionIndex = 3; pub const BondingDuration: EraIndex = 3; pub const SlashDeferDuration: EraIndex = 0; - pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; + pub const RewardCurve: &'static PiecewiseLinear = &REWARD_CURVE; pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(16); } diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index 5e6c955c441c5..d94516a65ff93 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -159,7 +159,7 @@ impl pallet_timestamp::Config for Test { } pallet_staking_reward_curve::build! { - const REWARD_CURVE: PiecewiseLinear<'static> = curve!( + const REWARD_CURVE: PiecewiseLinear = curve!( min_inflation: 0_025_000u64, max_inflation: 0_100_000, ideal_stake: 0_500_000, @@ -172,7 +172,7 @@ pallet_staking_reward_curve::build! { parameter_types! { pub const SessionsPerEra: SessionIndex = 3; pub const BondingDuration: EraIndex = 3; - pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; + pub const RewardCurve: &'static PiecewiseLinear = &REWARD_CURVE; pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17); } diff --git a/frame/nomination-pools/benchmarking/src/mock.rs b/frame/nomination-pools/benchmarking/src/mock.rs index eb884869f6d32..5f9788de40db1 100644 --- a/frame/nomination-pools/benchmarking/src/mock.rs +++ b/frame/nomination-pools/benchmarking/src/mock.rs @@ -74,7 +74,7 @@ impl pallet_balances::Config for Runtime { } pallet_staking_reward_curve::build! { - const I_NPOS: sp_runtime::curve::PiecewiseLinear<'static> = curve!( + const I_NPOS: sp_runtime::curve::PiecewiseLinear = curve!( min_inflation: 0_025_000, max_inflation: 0_100_000, ideal_stake: 0_500_000, @@ -84,7 +84,7 @@ pallet_staking_reward_curve::build! { ); } parameter_types! { - pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear<'static> = &I_NPOS; + pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear = &I_NPOS; } impl pallet_staking::Config for Runtime { type MaxNominations = ConstU32<16>; diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index d51a81b1212c0..1fca7fd7faa6e 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -133,7 +133,7 @@ impl pallet_session::Config for Test { } pallet_staking_reward_curve::build! { - const I_NPOS: sp_runtime::curve::PiecewiseLinear<'static> = curve!( + const I_NPOS: sp_runtime::curve::PiecewiseLinear = curve!( min_inflation: 0_025_000, max_inflation: 0_100_000, ideal_stake: 0_500_000, @@ -143,7 +143,7 @@ pallet_staking_reward_curve::build! { ); } parameter_types! { - pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear<'static> = &I_NPOS; + pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear = &I_NPOS; } pub type Extrinsic = sp_runtime::testing::TestXt; diff --git a/frame/session/benchmarking/src/mock.rs b/frame/session/benchmarking/src/mock.rs index c777f2c56de3a..dd45285eb6082 100644 --- a/frame/session/benchmarking/src/mock.rs +++ b/frame/session/benchmarking/src/mock.rs @@ -131,7 +131,7 @@ impl pallet_session::Config for Test { type WeightInfo = (); } pallet_staking_reward_curve::build! { - const I_NPOS: sp_runtime::curve::PiecewiseLinear<'static> = curve!( + const I_NPOS: sp_runtime::curve::PiecewiseLinear = curve!( min_inflation: 0_025_000, max_inflation: 0_100_000, ideal_stake: 0_500_000, @@ -141,7 +141,7 @@ pallet_staking_reward_curve::build! { ); } parameter_types! { - pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear<'static> = &I_NPOS; + pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear = &I_NPOS; } pub type Extrinsic = sp_runtime::testing::TestXt; diff --git a/frame/staking/reward-curve/src/lib.rs b/frame/staking/reward-curve/src/lib.rs index e66f6fde37599..4872d7c3b829c 100644 --- a/frame/staking/reward-curve/src/lib.rs +++ b/frame/staking/reward-curve/src/lib.rs @@ -60,7 +60,7 @@ use syn::parse::{Parse, ParseStream}; /// use sp_runtime::curve::PiecewiseLinear; /// /// pallet_staking_reward_curve::build! { -/// const I_NPOS: PiecewiseLinear<'static> = curve!( +/// const I_NPOS: PiecewiseLinear = curve!( /// min_inflation: 0_025_000, /// max_inflation: 0_100_000, /// ideal_stake: 0_500_000, diff --git a/frame/staking/reward-curve/tests/test.rs b/frame/staking/reward-curve/tests/test.rs index aa19b1782453d..c53b23f6c945a 100644 --- a/frame/staking/reward-curve/tests/test.rs +++ b/frame/staking/reward-curve/tests/test.rs @@ -20,7 +20,7 @@ mod test_small_falloff { pallet_staking_reward_curve::build! { - const REWARD_CURVE: sp_runtime::curve::PiecewiseLinear<'static> = curve!( + const REWARD_CURVE: sp_runtime::curve::PiecewiseLinear = curve!( min_inflation: 0_020_000, max_inflation: 0_200_000, ideal_stake: 0_600_000, @@ -33,7 +33,7 @@ mod test_small_falloff { mod test_big_falloff { pallet_staking_reward_curve::build! { - const REWARD_CURVE: sp_runtime::curve::PiecewiseLinear<'static> = curve!( + const REWARD_CURVE: sp_runtime::curve::PiecewiseLinear = curve!( min_inflation: 0_100_000, max_inflation: 0_400_000, ideal_stake: 0_400_000, diff --git a/frame/staking/src/inflation.rs b/frame/staking/src/inflation.rs index c7519683c75d1..e2eab1f727aa8 100644 --- a/frame/staking/src/inflation.rs +++ b/frame/staking/src/inflation.rs @@ -30,7 +30,7 @@ use sp_runtime::{curve::PiecewiseLinear, traits::AtLeast32BitUnsigned, Perbill}; /// /// `era_duration` is expressed in millisecond. pub fn compute_total_payout( - yearly_inflation: &PiecewiseLinear<'static>, + yearly_inflation: &PiecewiseLinear, npos_token_staked: N, total_tokens: N, era_duration: u64, @@ -54,7 +54,7 @@ mod test { use sp_runtime::curve::PiecewiseLinear; pallet_staking_reward_curve::build! { - const I_NPOS: PiecewiseLinear<'static> = curve!( + const I_NPOS: PiecewiseLinear = curve!( min_inflation: 0_025_000, max_inflation: 0_100_000, ideal_stake: 0_500_000, diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 331095774b741..fb50ec597d8ba 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -778,7 +778,7 @@ impl EraPayout for () { /// Adaptor to turn a `PiecewiseLinear` curve definition into an `EraPayout` impl, used for /// backwards compatibility. pub struct ConvertCurve(sp_std::marker::PhantomData); -impl>> +impl> EraPayout for ConvertCurve { fn era_payout( diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index bd2d8cdc32ce9..01c963f730968 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -201,7 +201,7 @@ impl pallet_timestamp::Config for Test { } pallet_staking_reward_curve::build! { - const I_NPOS: PiecewiseLinear<'static> = curve!( + const I_NPOS: PiecewiseLinear = curve!( min_inflation: 0_025_000, max_inflation: 0_100_000, ideal_stake: 0_500_000, @@ -212,7 +212,7 @@ pallet_staking_reward_curve::build! { } parameter_types! { pub const BondingDuration: EraIndex = 3; - pub const RewardCurve: &'static PiecewiseLinear<'static> = &I_NPOS; + pub const RewardCurve: &'static PiecewiseLinear = &I_NPOS; pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(75); } From 274a334b17e8c6ef48115ce6455a300496ea1507 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 6 Jun 2022 15:28:13 +0100 Subject: [PATCH 4/6] fmt --- frame/staking/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index fb50ec597d8ba..64c53174f2e6b 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -778,8 +778,8 @@ impl EraPayout for () { /// Adaptor to turn a `PiecewiseLinear` curve definition into an `EraPayout` impl, used for /// backwards compatibility. pub struct ConvertCurve(sp_std::marker::PhantomData); -impl> - EraPayout for ConvertCurve +impl> EraPayout + for ConvertCurve { fn era_payout( total_staked: Balance, From 081861839a2a6a4bdbc2e9c2af0ca80f7bb990bd Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 6 Jun 2022 15:52:38 +0100 Subject: [PATCH 5/6] one more --- frame/staking/reward-curve/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/staking/reward-curve/src/lib.rs b/frame/staking/reward-curve/src/lib.rs index 4872d7c3b829c..22aa55851cd74 100644 --- a/frame/staking/reward-curve/src/lib.rs +++ b/frame/staking/reward-curve/src/lib.rs @@ -371,7 +371,7 @@ fn generate_piecewise_linear(points: Vec<(u32, u32)>) -> TokenStream2 { } quote!( - _sp_runtime::curve::PiecewiseLinear::<'static> { + _sp_runtime::curve::PiecewiseLinear { points: & [ #points_tokens ], maximum: _sp_runtime::Perbill::from_parts(#max), } From 869f86088517e31a922d5e9a978d73deb6bf0335 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 6 Jun 2022 17:26:09 +0100 Subject: [PATCH 6/6] undo bounded vec stuff --- frame/support/src/storage/bounded_vec.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/frame/support/src/storage/bounded_vec.rs b/frame/support/src/storage/bounded_vec.rs index 295a3f75cf487..232f9f97fc6f8 100644 --- a/frame/support/src/storage/bounded_vec.rs +++ b/frame/support/src/storage/bounded_vec.rs @@ -109,13 +109,16 @@ where /// Similar to a `BoundedVec`, but not owned and cannot be decoded. #[derive(Encode, scale_info::TypeInfo)] #[scale_info(skip_type_params(S))] -pub struct BoundedSlice(&'static [T], PhantomData); +pub struct BoundedSlice<'a, T, S>(&'a [T], PhantomData); // `BoundedSlice`s encode to something which will always decode into a `BoundedVec`, // `WeakBoundedVec`, or a `Vec`. -impl> EncodeLike> for BoundedSlice {} -impl> EncodeLike> for BoundedSlice {} -impl> EncodeLike> for BoundedSlice {} +impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} +impl<'a, T: Encode + Decode, S: Get> EncodeLike> + for BoundedSlice<'a, T, S> +{ +} +impl<'a, T: Encode + Decode, S: Get> EncodeLike> for BoundedSlice<'a, T, S> {} impl> PartialOrd for BoundedVec { fn partial_cmp(&self, other: &Self) -> Option { @@ -129,9 +132,9 @@ impl> Ord for BoundedVec { } } -impl> TryFrom<&'static [T]> for BoundedSlice { +impl<'a, T, S: Get> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> { type Error = (); - fn try_from(t: &'static [T]) -> Result { + fn try_from(t: &'a [T]) -> Result { if t.len() <= S::get() as usize { Ok(BoundedSlice(t, PhantomData)) } else { @@ -140,8 +143,8 @@ impl> TryFrom<&'static [T]> for BoundedSlice { } } -impl From> for &'static [T] { - fn from(t: BoundedSlice) -> Self { +impl<'a, T, S> From> for &'a [T] { + fn from(t: BoundedSlice<'a, T, S>) -> Self { t.0 } }