Skip to content

Commit

Permalink
introduce new field in struct and update genesis file used explicitly…
Browse files Browse the repository at this point in the history
… in DEV (#1248)

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

* add test

* format

* update inline comment

* undo formatting added via intelliJ

* move inline comment

* reword inline comment

* address review comments and update tests

* update data type in genesis config for dev

* reuse storage within tests to avoid duplicated code

* cargo fmt

* add missing field to mock
  • Loading branch information
yuriechan authored May 24, 2024
1 parent 088f2c9 commit 7a788a3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 2 deletions.
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]
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

0 comments on commit 7a788a3

Please sign in to comment.