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

introduce new field in struct and update genesis file used explicitly in DEV #1248

Merged
merged 12 commits into from
May 24, 2024
1 change: 1 addition & 0 deletions bin/collator/src/local/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ fn testnet_genesis(
TierThreshold::FixedTvlAmount { amount: 10 * AST },
],
slots_per_tier: vec![10, 20, 30, 40],
disable_safeguard: true,
..Default::default()
},
inflation: InflationConfig {
Expand Down
1 change: 1 addition & 0 deletions bin/collator/src/parachain/chain_spec/astar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ fn make_genesis(
},
],
slots_per_tier: vec![10, 20, 30, 40],
disable_safeguard: true,
..Default::default()
},
inflation: InflationConfig {
Expand Down
1 change: 1 addition & 0 deletions bin/collator/src/parachain/chain_spec/shibuya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ fn make_genesis(
TierThreshold::FixedTvlAmount { amount: 10 * SBY },
],
slots_per_tier: vec![10, 20, 30, 40],
disable_safeguard: true,
..Default::default()
},
inflation: InflationConfig {
Expand Down
1 change: 1 addition & 0 deletions bin/collator/src/parachain/chain_spec/shiden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ fn make_genesis(
TierThreshold::FixedTvlAmount { amount: 5000 * SDN },
],
slots_per_tier: vec![10, 20, 30, 40],
disable_safeguard: true,
..Default::default()
},
inflation: InflationConfig {
Expand Down
6 changes: 6 additions & 0 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ pub mod pallet {
#[pallet::type_value]
pub fn DefaultSafeguard<T: Config>() -> bool {
// In production, safeguard is enabled by default.
// Safeguard can be disabled per chain via Genesis Config.
true
}

Expand All @@ -478,6 +479,7 @@ pub mod pallet {
pub tier_thresholds: Vec<TierThreshold>,
pub slots_per_tier: Vec<u16>,
pub _config: PhantomData<T>,
pub disable_safeguard: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the default value of Safeguard changes, does this approach still work?
E.g. Safeguard is set to false by default, and user sets disable_safeguard to false. What's the result?

Perhaps it's better to set the storage value directly.

Nitpick comment - PhantomData should be at the bottom of the fields declaration.

}

#[pallet::genesis_build]
Expand Down Expand Up @@ -540,6 +542,10 @@ pub mod pallet {
ActiveProtocolState::<T>::put(protocol_state);
StaticTierParams::<T>::put(tier_params);
TierConfig::<T>::put(tier_config.clone());

if self.disable_safeguard {
Safeguard::<T>::put(false);
}
}
}

Expand Down
48 changes: 46 additions & 2 deletions pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use crate::test::{mock::*, testing_utils::*};
use crate::{
pallet::Config, ActiveProtocolState, ContractStake, DAppId, EraRewards, Error, Event,
ForcingType, IntegratedDApps, Ledger, NextDAppId, PeriodNumber, Safeguard, StakerInfo,
Subperiod, TierConfig,
ForcingType, GenesisConfig, IntegratedDApps, Ledger, NextDAppId, PeriodNumber, Permill,
Safeguard, StakerInfo, Subperiod, TierConfig, TierThreshold,
};

use frame_support::{
Expand Down Expand Up @@ -2962,3 +2962,47 @@ fn safeguard_on_by_default() {
assert!(Safeguard::<Test>::get());
});
}

#[test]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok to test all scenarios if you wish, of course, but the setup code bloats 99% of the added test code. Maybe optimize that, and reuse storage for creating multiple test externalities.

fn safeguard_can_be_disabled_by_genesis_config() {
use sp_runtime::BuildStorage;
let storage = GenesisConfig::<Test> {
reward_portion: vec![
Permill::from_percent(40),
Permill::from_percent(30),
Permill::from_percent(20),
Permill::from_percent(10),
],
slot_distribution: vec![
Permill::from_percent(10),
Permill::from_percent(20),
Permill::from_percent(30),
Permill::from_percent(40),
],
tier_thresholds: vec![
TierThreshold::DynamicTvlAmount {
amount: 30000,
minimum_amount: 20000,
},
TierThreshold::DynamicTvlAmount {
amount: 7500,
minimum_amount: 5000,
},
TierThreshold::DynamicTvlAmount {
amount: 20000,
minimum_amount: 15000,
},
TierThreshold::FixedTvlAmount { amount: 5000 },
],
slots_per_tier: vec![10, 20, 30, 40],
disable_safeguard: true,
..Default::default()
}
.build_storage()
.unwrap();

let mut ext = sp_io::TestExternalities::from(storage);
ext.execute_with(|| {
assert!(!Safeguard::<Test>::get());
});
}