Skip to content

Commit

Permalink
PR review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Aug 5, 2024
1 parent c87cdaa commit 853bc6e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 86 deletions.
14 changes: 9 additions & 5 deletions pallets/dapp-staking-v3/src/benchmarking/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,20 @@ pub(super) fn init_tier_settings<T: Config>() {
};

let total_issuance = SEED as u128 * UNIT;
let tier_thresholds_with_issuance = ThresholdsWithIssuance {
thresholds: tier_params.tier_thresholds.clone(),
total_issuance,
};
let tier_thresholds = tier_params
.tier_thresholds
.iter()
.map(|t| t.threshold(total_issuance))
.collect::<Vec<Balance>>()
.try_into()
.expect("Invalid number of tier thresholds provided.");

// Init tier config, based on the initial params
let init_tier_config =
TiersConfiguration::<T::NumberOfTiers, T::TierSlots, T::BaseNativeCurrencyPrice> {
slots_per_tier: BoundedVec::try_from(vec![10, 20, 30, 40]).unwrap(),
reward_portion: tier_params.reward_portion.clone(),
tier_thresholds: BoundedVec::from(tier_thresholds_with_issuance),
tier_thresholds,
_phantom: Default::default(),
};

Expand Down
14 changes: 9 additions & 5 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,14 @@ pub mod pallet {
"Invalid tier parameters values provided."
);

let tier_thresholds_with_issuance = ThresholdsWithIssuance {
thresholds: tier_params.tier_thresholds.clone(),
total_issuance: T::Currency::total_issuance(),
};
let total_issuance = T::Currency::total_issuance();
let tier_thresholds = tier_params
.tier_thresholds
.iter()
.map(|t| t.threshold(total_issuance))
.collect::<Vec<Balance>>()
.try_into()
.expect("Invalid number of tier thresholds provided.");

let tier_config =
TiersConfiguration::<T::NumberOfTiers, T::TierSlots, T::BaseNativeCurrencyPrice> {
Expand All @@ -566,7 +570,7 @@ pub mod pallet {
)
.expect("Invalid number of slots per tier entries provided."),
reward_portion: tier_params.reward_portion.clone(),
tier_thresholds: BoundedVec::from(tier_thresholds_with_issuance),
tier_thresholds,
_phantom: Default::default(),
};
assert!(
Expand Down
81 changes: 54 additions & 27 deletions pallets/dapp-staking-v3/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,49 +66,76 @@ mod v8 {
{
fn on_runtime_upgrade() -> Weight {
// 1. Update static tier parameters with new thresholds from the runtime configurable param TierThresholds
let _ = StaticTierParams::<T>::translate::<TierParametersV7<T::NumberOfTiers>, _>(
let result = StaticTierParams::<T>::translate::<TierParametersV7<T::NumberOfTiers>, _>(
|maybe_old_params| match maybe_old_params {
Some(old_params) => {
let tier_thresholds: BoundedVec<TierThreshold, T::NumberOfTiers> =
BoundedVec::try_from(TierThresholds::get().to_vec()).unwrap();

Some(TierParameters {
slot_distribution: old_params.slot_distribution,
reward_portion: old_params.reward_portion,
tier_thresholds,
})
let tier_thresholds: Result<
BoundedVec<TierThreshold, T::NumberOfTiers>,
_,
> = BoundedVec::try_from(TierThresholds::get().to_vec());

match tier_thresholds {
Ok(tier_thresholds) => Some(TierParameters {
slot_distribution: old_params.slot_distribution,
reward_portion: old_params.reward_portion,
tier_thresholds,
}),
Err(err) => {
log::error!(
"Failed to convert TierThresholds parameters: {:?}",
err
);
None
}
}
}
_ => None,
},
);

if result.is_err() {
log::error!("Failed to translate StaticTierParams from previous V7 type to current V8 type. Check TierParametersV7 decoding.");
return T::DbWeight::get().reads_writes(1, 0);
}

// 2. Translate tier thresholds from V7 TierThresholds to Balance
let _ = TierConfig::<T>::translate::<
let result = TierConfig::<T>::translate::<
TiersConfigurationV7<T::NumberOfTiers, T::TierSlots, T::BaseNativeCurrencyPrice>,
_,
>(|maybe_old_config| match maybe_old_config {
Some(old_config) => {
let new_tier_thresholds = old_config
.tier_thresholds
.iter()
.map(|t| match t {
v7::TierThreshold::DynamicTvlAmount { amount, .. } => *amount,
v7::TierThreshold::FixedTvlAmount { amount } => *amount,
})
.collect::<Vec<Balance>>()
.try_into()
.expect("Invalid number of tier thresholds provided.");

Some(TiersConfiguration {
slots_per_tier: old_config.slots_per_tier,
reward_portion: old_config.reward_portion,
tier_thresholds: new_tier_thresholds,
_phantom: Default::default(),
})
let new_tier_thresholds: Result<BoundedVec<Balance, T::NumberOfTiers>, _> =
old_config
.tier_thresholds
.iter()
.map(|t| match t {
v7::TierThreshold::DynamicTvlAmount { amount, .. } => *amount,
v7::TierThreshold::FixedTvlAmount { amount } => *amount,
})
.collect::<Vec<Balance>>()
.try_into();

match new_tier_thresholds {
Ok(new_tier_thresholds) => Some(TiersConfiguration {
slots_per_tier: old_config.slots_per_tier,
reward_portion: old_config.reward_portion,
tier_thresholds: new_tier_thresholds,
_phantom: Default::default(),
}),
Err(err) => {
log::error!("Failed to convert tier thresholds to balances: {:?}", err);
None
}
}
}
_ => None,
});

if result.is_err() {
log::error!("Failed to translate TierConfig from previous V7 type to current V8 type. Check TiersConfigurationV7 decoding.");
return T::DbWeight::get().reads_writes(2, 1);
}

T::DbWeight::get().reads_writes(2, 2)
}

Expand Down
14 changes: 9 additions & 5 deletions pallets/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,14 @@ impl ExtBuilder {
};

let total_issuance = <Test as Config>::Currency::total_issuance();
let tier_thresholds_with_issuance = ThresholdsWithIssuance {
thresholds: tier_params.tier_thresholds.clone(),
total_issuance,
};
let tier_thresholds = tier_params
.tier_thresholds
.iter()
.map(|t| t.threshold(total_issuance))
.collect::<Vec<Balance>>()
.try_into()
.expect("Invalid number of tier thresholds provided.");

// Init tier config, based on the initial params. Needs to be adjusted to the init price.
let init_tier_config = TiersConfiguration::<
<Test as Config>::NumberOfTiers,
Expand All @@ -350,7 +354,7 @@ impl ExtBuilder {
> {
slots_per_tier: BoundedVec::try_from(vec![2, 5, 13, 20]).unwrap(),
reward_portion: tier_params.reward_portion.clone(),
tier_thresholds: BoundedVec::from(tier_thresholds_with_issuance),
tier_thresholds,
_phantom: Default::default(),
}
.calculate_new(
Expand Down
33 changes: 18 additions & 15 deletions pallets/dapp-staking-v3/src/test/tests_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2933,14 +2933,18 @@ fn tier_configuration_basic_tests() {
pub const BaseNativeCurrencyPrice: FixedU128 = FixedU128::from_rational(5, 100);
}
let total_issuance: Balance = 9_000_000_000;
let tier_thresholds_with_issuance = ThresholdsWithIssuance {
thresholds: params.tier_thresholds.clone(),
total_issuance,
};
let tier_thresholds = params
.tier_thresholds
.iter()
.map(|t| t.threshold(total_issuance))
.collect::<Vec<Balance>>()
.try_into()
.expect("Invalid number of tier thresholds provided.");

let init_config = TiersConfiguration::<TiersNum, StandardTierSlots, BaseNativeCurrencyPrice> {
slots_per_tier: BoundedVec::try_from(vec![10, 20, 30, 40]).unwrap(),
reward_portion: params.reward_portion.clone(),
tier_thresholds: BoundedVec::from(tier_thresholds_with_issuance),
tier_thresholds,
_phantom: Default::default(),
};
assert!(init_config.is_valid(), "Init config must be valid!");
Expand Down Expand Up @@ -3098,7 +3102,7 @@ fn dapp_tier_rewards_with_rank() {
}

#[test]
fn tier_thresholds_from_conversion_test() {
fn tier_thresholds_conversion_test() {
get_u32_type!(TiersNum, 2);
let total_issuance: Balance = 1_000_000;

Expand All @@ -3113,14 +3117,13 @@ fn tier_thresholds_from_conversion_test() {
])
.unwrap();

let tier_thresholds_with_issuance = ThresholdsWithIssuance {
thresholds,
total_issuance,
};

let thresholds_values: BoundedVec<Balance, TiersNum> =
BoundedVec::from(tier_thresholds_with_issuance);
let tier_thresholds: BoundedVec<Balance, TiersNum> = thresholds
.iter()
.map(|t| t.threshold(total_issuance))
.collect::<Vec<Balance>>()
.try_into()
.expect("Invalid number of tier thresholds provided.");

assert_eq!(thresholds_values[0], 100_000); // 10% of total issuance
assert_eq!(thresholds_values[1], 50_000); // 5% of total issuance
assert_eq!(tier_thresholds[0], 100_000); // 10% of total issuance
assert_eq!(tier_thresholds[1], 50_000); // 5% of total issuance
}
41 changes: 12 additions & 29 deletions pallets/dapp-staking-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,18 @@ pub enum TierThreshold {
},
}

impl TierThreshold {
/// Return threshold amount for the tier.
pub fn threshold(&self, total_issuance: Balance) -> Balance {
match self {
Self::DynamicPercentage { percentage, .. } => *percentage * total_issuance,
Self::FixedPercentage {
required_percentage,
} => *required_percentage * total_issuance,
}
}
}

/// Top level description of tier slot parameters used to calculate tier configuration.
#[derive(
Encode,
Expand Down Expand Up @@ -1839,32 +1851,3 @@ impl CleanupMarker {
|| self.dapp_tiers_index != self.oldest_valid_era
}
}

// Wrap the total issuance value with tier percentages.
pub struct ThresholdsWithIssuance<NT: Get<u32>> {
pub thresholds: BoundedVec<TierThreshold, NT>,
pub total_issuance: Balance,
}

impl<NT: Get<u32>> From<ThresholdsWithIssuance<NT>> for BoundedVec<Balance, NT> {
fn from(wrapper: ThresholdsWithIssuance<NT>) -> Self {
wrapper
.thresholds
.iter()
.map(|t| match t {
TierThreshold::DynamicPercentage { percentage, .. } => {
*percentage * wrapper.total_issuance
}
TierThreshold::FixedPercentage {
required_percentage,
} => *required_percentage * wrapper.total_issuance,
})
.collect::<Vec<Balance>>()
.try_into()
.expect("Invalid number of tier thresholds provided.")
}
}

// Implremt from TierParams to BoundedVec<Balance, NT>??

// test it

0 comments on commit 853bc6e

Please sign in to comment.