mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
move economic security into it's own pallet (#596)
* move economic security into it's own pallet * fix deny * Update Cargo.toml, .github for the new crates * Remove unused import --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
@@ -33,6 +33,8 @@ validator-sets-pallet = { package = "serai-validator-sets-pallet", path = "../..
|
||||
dex-pallet = { package = "serai-dex-pallet", path = "../../dex/pallet", default-features = false }
|
||||
genesis-liquidity-pallet = { package = "serai-genesis-liquidity-pallet", path = "../../genesis-liquidity/pallet", default-features = false }
|
||||
|
||||
economic-security-pallet = { package = "serai-economic-security-pallet", path = "../../economic-security/pallet", default-features = false }
|
||||
|
||||
serai-primitives = { path = "../../primitives", default-features = false }
|
||||
validator-sets-primitives = { package = "serai-validator-sets-primitives", path = "../../validator-sets/primitives", default-features = false }
|
||||
emissions-primitives = { package = "serai-emissions-primitives", path = "../primitives", default-features = false }
|
||||
@@ -52,6 +54,8 @@ std = [
|
||||
"validator-sets-pallet/std",
|
||||
"dex-pallet/std",
|
||||
"genesis-liquidity-pallet/std",
|
||||
|
||||
"economic-security-pallet/std",
|
||||
|
||||
"serai-primitives/std",
|
||||
"emissions-primitives/std",
|
||||
|
||||
@@ -10,12 +10,14 @@ pub mod pallet {
|
||||
use sp_std::{vec, vec::Vec, ops::Mul, collections::btree_map::BTreeMap};
|
||||
use sp_runtime;
|
||||
|
||||
use coins_pallet::{Config as CoinsConfig, Pallet as Coins, AllowMint};
|
||||
use coins_pallet::{Config as CoinsConfig, Pallet as Coins};
|
||||
use dex_pallet::{Config as DexConfig, Pallet as Dex};
|
||||
|
||||
use validator_sets_pallet::{Pallet as ValidatorSets, Config as ValidatorSetsConfig};
|
||||
use genesis_liquidity_pallet::{Pallet as GenesisLiquidity, Config as GenesisLiquidityConfig};
|
||||
|
||||
use economic_security_pallet::{Config as EconomicSecurityConfig, Pallet as EconomicSecurity};
|
||||
|
||||
use serai_primitives::*;
|
||||
use validator_sets_primitives::{MAX_KEY_SHARES_PER_SET, Session};
|
||||
pub use emissions_primitives as primitives;
|
||||
@@ -28,6 +30,7 @@ pub mod pallet {
|
||||
+ CoinsConfig
|
||||
+ DexConfig
|
||||
+ GenesisLiquidityConfig
|
||||
+ EconomicSecurityConfig
|
||||
{
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
@@ -76,12 +79,6 @@ pub mod pallet {
|
||||
#[pallet::getter(fn session)]
|
||||
pub type CurrentSession<T: Config> = StorageMap<_, Identity, NetworkId, u32, ValueQuery>;
|
||||
|
||||
// TODO: Find a better place for this
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn economic_security_reached)]
|
||||
pub(crate) type EconomicSecurityReached<T: Config> =
|
||||
StorageMap<_, Identity, NetworkId, bool, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub(crate) type LastSwapVolume<T: Config> = StorageMap<_, Identity, Coin, u64, OptionQuery>;
|
||||
|
||||
@@ -95,7 +92,6 @@ pub mod pallet {
|
||||
}
|
||||
Participants::<T>::set(id, Some(participants.try_into().unwrap()));
|
||||
CurrentSession::<T>::set(id, 0);
|
||||
EconomicSecurityReached::<T>::set(id, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,25 +99,7 @@ pub mod pallet {
|
||||
#[pallet::hooks]
|
||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
||||
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
|
||||
// we wait 1 extra block after genesis ended to see the changes. We only need this extra
|
||||
// block in dev&test networks where we start the chain with accounts that already has some
|
||||
// staked SRI. So when we check for ec-security pre-genesis we look like we are economically
|
||||
// secure. The reason for this although we only check for it once the genesis is complete(so
|
||||
// if the genesis complete we shouldn't be economically secure because the funds are not
|
||||
// enough) is because ValidatorSets pallet runs before the genesis pallet in runtime.
|
||||
// So ValidatorSets pallet sees the old state until next block.
|
||||
// TODO: revisit this when mainnet genesis validator stake code is done.
|
||||
let gcb = GenesisLiquidity::<T>::genesis_complete_block();
|
||||
let genesis_ended = gcb.is_some() && (n.saturated_into::<u64>() > gcb.unwrap());
|
||||
|
||||
// we accept we reached economic security once we can mint smallest amount of a network's coin
|
||||
for coin in COINS {
|
||||
let check = genesis_ended && !Self::economic_security_reached(coin.network());
|
||||
if check && <T as CoinsConfig>::AllowMint::is_allowed(&Balance { coin, amount: Amount(1) })
|
||||
{
|
||||
EconomicSecurityReached::<T>::set(coin.network(), true);
|
||||
}
|
||||
}
|
||||
let genesis_ended = GenesisLiquidity::<T>::genesis_complete_block().is_some();
|
||||
|
||||
// check if we got a new session
|
||||
let mut session_changed = false;
|
||||
@@ -341,7 +319,7 @@ pub mod pallet {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !Self::economic_security_reached(n) {
|
||||
if EconomicSecurity::<T>::economic_security_block(n).is_none() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -383,7 +361,7 @@ pub mod pallet {
|
||||
balance: Balance,
|
||||
) -> DispatchResult {
|
||||
// check the network didn't reach the economic security yet
|
||||
if Self::economic_security_reached(network) {
|
||||
if EconomicSecurity::<T>::economic_security_block(network).is_some() {
|
||||
Err(Error::<T>::NetworkHasEconomicSecurity)?;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user