From a2df54dd6aa4a7519439050b28aa3fc5602ce46c Mon Sep 17 00:00:00 2001 From: akildemir Date: Thu, 15 Aug 2024 12:51:05 +0300 Subject: [PATCH] merge genesis complete block with genesis ended --- .../client/src/serai/genesis_liquidity.rs | 5 ++--- substrate/client/tests/emissions.rs | 22 +++++++++---------- substrate/client/tests/genesis_liquidity.rs | 5 +++-- substrate/emissions/pallet/src/lib.rs | 14 ++---------- substrate/genesis-liquidity/pallet/src/lib.rs | 8 +++---- 5 files changed, 22 insertions(+), 32 deletions(-) diff --git a/substrate/client/src/serai/genesis_liquidity.rs b/substrate/client/src/serai/genesis_liquidity.rs index 00a369c8c..187844bef 100644 --- a/substrate/client/src/serai/genesis_liquidity.rs +++ b/substrate/client/src/serai/genesis_liquidity.rs @@ -63,8 +63,7 @@ impl<'a> SeraiGenesisLiquidity<'a> { Ok(self.0.storage(PALLET, "Supply", coin).await?.unwrap_or(LiquidityAmount::zero())) } - pub async fn genesis_complete(&self) -> Result { - let result: Option<()> = self.0.storage(PALLET, "GenesisComplete", ()).await?; - Ok(result.is_some()) + pub async fn genesis_complete_block(&self) -> Result, SeraiError> { + self.0.storage(PALLET, "GenesisCompleteBlock", ()).await } } diff --git a/substrate/client/tests/emissions.rs b/substrate/client/tests/emissions.rs index 62ff043ea..f510d4862 100644 --- a/substrate/client/tests/emissions.rs +++ b/substrate/client/tests/emissions.rs @@ -54,18 +54,18 @@ async fn test_emissions(serai: Serai) { let (_, mut batch_ids) = set_up_genesis(&serai, &coins, &values).await; // wait until genesis is complete - while !serai - .as_of_latest_finalized_block() - .await - .unwrap() - .genesis_liquidity() - .genesis_complete() - .await - .unwrap() - { + let mut genesis_complete_block = None; + while genesis_complete_block.is_none() { tokio::time::sleep(Duration::from_secs(1)).await; + genesis_complete_block = serai + .as_of_latest_finalized_block() + .await + .unwrap() + .genesis_liquidity() + .genesis_complete_block() + .await + .unwrap(); } - let genesis_complete_block = serai.latest_finalized_block().await.unwrap().number(); for _ in 0 .. 3 { // get current stakes @@ -99,7 +99,7 @@ async fn test_emissions(serai: Serai) { // calculate how much reward in this session let reward_this_epoch = - if change_block_number < (genesis_complete_block + FAST_EPOCH_INITIAL_PERIOD) { + if change_block_number < (genesis_complete_block.unwrap() + FAST_EPOCH_INITIAL_PERIOD) { block_count * INITIAL_REWARD_PER_BLOCK } else { let blocks_until = SECURE_BY - change_block_number; diff --git a/substrate/client/tests/genesis_liquidity.rs b/substrate/client/tests/genesis_liquidity.rs index 867763e13..8b2b8d8f9 100644 --- a/substrate/client/tests/genesis_liquidity.rs +++ b/substrate/client/tests/genesis_liquidity.rs @@ -24,14 +24,15 @@ pub async fn test_genesis_liquidity(serai: Serai) { let (accounts, _) = set_up_genesis(&serai, &coins, &values).await; // wait until genesis is complete - while !serai + while serai .as_of_latest_finalized_block() .await .unwrap() .genesis_liquidity() - .genesis_complete() + .genesis_complete_block() .await .unwrap() + .is_none() { tokio::time::sleep(Duration::from_secs(1)).await; } diff --git a/substrate/emissions/pallet/src/lib.rs b/substrate/emissions/pallet/src/lib.rs index 66310d5cd..e280ea894 100644 --- a/substrate/emissions/pallet/src/lib.rs +++ b/substrate/emissions/pallet/src/lib.rs @@ -82,10 +82,6 @@ pub mod pallet { pub(crate) type EconomicSecurityReached = StorageMap<_, Identity, NetworkId, bool, ValueQuery>; - // TODO: Find a better place for this - #[pallet::storage] - pub(crate) type GenesisCompleteBlock = StorageValue<_, u64, OptionQuery>; - #[pallet::storage] pub(crate) type LastSwapVolume = StorageMap<_, Identity, Coin, u64, OptionQuery>; @@ -107,12 +103,6 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { fn on_initialize(n: BlockNumberFor) -> Weight { - if GenesisCompleteBlock::::get().is_none() && - GenesisLiquidity::::genesis_complete().is_some() - { - GenesisCompleteBlock::::set(Some(n.saturated_into::())); - } - // 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 @@ -121,7 +111,7 @@ pub mod pallet { // 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 = GenesisCompleteBlock::::get(); + let gcb = GenesisLiquidity::::genesis_complete_block(); let genesis_ended = gcb.is_some() && (n.saturated_into::() > gcb.unwrap()); // we accept we reached economic security once we can mint smallest amount of a network's coin @@ -339,7 +329,7 @@ pub mod pallet { #[cfg(not(feature = "fast-epoch"))] let initial_period_duration = 2 * MONTHS; - let genesis_complete_block = GenesisCompleteBlock::::get(); + let genesis_complete_block = GenesisLiquidity::::genesis_complete_block(); genesis_complete_block.is_some() && (n.saturated_into::() < (genesis_complete_block.unwrap() + initial_period_duration)) } diff --git a/substrate/genesis-liquidity/pallet/src/lib.rs b/substrate/genesis-liquidity/pallet/src/lib.rs index 0b463af58..54bec3de5 100644 --- a/substrate/genesis-liquidity/pallet/src/lib.rs +++ b/substrate/genesis-liquidity/pallet/src/lib.rs @@ -72,8 +72,8 @@ pub mod pallet { pub(crate) type Oracle = StorageMap<_, Identity, Coin, u64, OptionQuery>; #[pallet::storage] - #[pallet::getter(fn genesis_complete)] - pub(crate) type GenesisComplete = StorageValue<_, (), OptionQuery>; + #[pallet::getter(fn genesis_complete_block)] + pub(crate) type GenesisCompleteBlock = StorageValue<_, u64, OptionQuery>; #[pallet::hooks] impl Hooks> for Pallet { @@ -87,7 +87,7 @@ pub mod pallet { // Distribute the genesis sri to pools after a month if (n.saturated_into::() >= final_block) && Self::oraclization_is_done() && - GenesisComplete::::get().is_none() + GenesisCompleteBlock::::get().is_none() { // mint the SRI Coins::::mint( @@ -167,7 +167,7 @@ pub mod pallet { assert_eq!(Coins::::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), coin), Amount(0)); } - GenesisComplete::::set(Some(())); + GenesisCompleteBlock::::set(Some(n.saturated_into::())); } // we accept we reached economic security once we can mint smallest amount of a network's coin