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],
safeguard: Some(false),
..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],
safeguard: Some(false),
..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],
safeguard: Some(false),
..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],
safeguard: Some(false),
..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 @@ -477,6 +478,7 @@ pub mod pallet {
pub slot_distribution: Vec<Permill>,
pub tier_thresholds: Vec<TierThreshold>,
pub slots_per_tier: Vec<u16>,
pub safeguard: Option<bool>,
pub _config: PhantomData<T>,
}

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.safeguard.is_some() {
Safeguard::<T>::put(self.safeguard.unwrap());
}
}
}

Expand Down
64 changes: 62 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,63 @@ 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_configurable_by_genesis_config() {
use sp_runtime::BuildStorage;
let mut genesis_config = 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],
..Default::default()
};

// Test case 1: Safeguard enabled via Genesis Config
genesis_config.safeguard = Some(true);
let storage = genesis_config.build_storage().unwrap();
let mut ext = sp_io::TestExternalities::from(storage);
ext.execute_with(|| {
assert!(Safeguard::<Test>::get());
});

// Test case 2: Safeguard disabled via Genesis Config
genesis_config.safeguard = Some(false);
let storage = genesis_config.build_storage().unwrap();
let mut ext = sp_io::TestExternalities::from(storage);
ext.execute_with(|| {
assert!(!Safeguard::<Test>::get());
});

// Test case 3: Safeguard not set via Genesis Config
genesis_config.safeguard = None;
let storage = genesis_config.build_storage().unwrap();
let mut ext = sp_io::TestExternalities::from(storage);
ext.execute_with(|| {
assert!(Safeguard::<Test>::get());
});
}
1 change: 1 addition & 0 deletions precompiles/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ impl ExternalityBuilder {
TierThreshold::FixedTvlAmount { amount: 10 },
],
slots_per_tier: vec![10, 20, 30, 40],
safeguard: None,
_config: PhantomData,
},
&mut storage,
Expand Down
Loading