From c66650d259be95871de10b411ec5ea18c49cbb4f Mon Sep 17 00:00:00 2001 From: akildemir Date: Mon, 30 Sep 2024 15:51:25 +0300 Subject: [PATCH] misc fixes --- coordinator/src/cosign_evaluator.rs | 10 ++++------ substrate/abi/src/economic_security.rs | 4 ++-- substrate/abi/src/genesis_liquidity.rs | 1 - .../client/src/serai/liquidity_tokens.rs | 19 ++++++++++++------- substrate/dex/pallet/src/lib.rs | 13 ++++++------- substrate/dex/pallet/src/tests.rs | 5 ++++- substrate/economic-security/pallet/src/lib.rs | 3 ++- .../primitives/src/shorthand.rs | 4 ++-- substrate/primitives/src/networks.rs | 1 - substrate/runtime/src/lib.rs | 12 +++++------- substrate/validator-sets/pallet/src/lib.rs | 7 +++++++ 11 files changed, 44 insertions(+), 35 deletions(-) diff --git a/coordinator/src/cosign_evaluator.rs b/coordinator/src/cosign_evaluator.rs index 51288da55..a1c2f5f6a 100644 --- a/coordinator/src/cosign_evaluator.rs +++ b/coordinator/src/cosign_evaluator.rs @@ -12,7 +12,7 @@ use tokio::{ use borsh::BorshSerialize; use sp_application_crypto::RuntimePublic; use serai_client::{ - primitives::{ExternalNetworkId, NetworkId, Signature, EXTERNAL_NETWORKS, NETWORKS}, + primitives::{ExternalNetworkId, Signature, EXTERNAL_NETWORKS}, validator_sets::primitives::{ExternalValidatorSet, Session}, Serai, SeraiError, TemporalSerai, }; @@ -204,11 +204,9 @@ impl CosignEvaluator { let mut total_stake = 0; let mut total_on_distinct_chain = 0; - for network in NETWORKS { - if network == NetworkId::Serai { - continue; - } - + // TODO: `network` isn't being used in the following loop. is this a bug? + // why are we going through the networks here? + for _network in EXTERNAL_NETWORKS { // Get the current set for this network let set_with_keys = { let mut res; diff --git a/substrate/abi/src/economic_security.rs b/substrate/abi/src/economic_security.rs index fc548def9..2899e0908 100644 --- a/substrate/abi/src/economic_security.rs +++ b/substrate/abi/src/economic_security.rs @@ -1,8 +1,8 @@ -use serai_primitives::NetworkId; +use serai_primitives::ExternalNetworkId; #[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)] #[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Event { - EconomicSecurityReached { network: NetworkId }, + EconomicSecurityReached { network: ExternalNetworkId }, } diff --git a/substrate/abi/src/genesis_liquidity.rs b/substrate/abi/src/genesis_liquidity.rs index 408a958cb..7660b3ab6 100644 --- a/substrate/abi/src/genesis_liquidity.rs +++ b/substrate/abi/src/genesis_liquidity.rs @@ -17,5 +17,4 @@ pub enum Event { GenesisLiquidityAdded { by: SeraiAddress, balance: ExternalBalance }, GenesisLiquidityRemoved { by: SeraiAddress, balance: ExternalBalance }, GenesisLiquidityAddedToPool { coin: ExternalBalance, sri: Amount }, - EconomicSecurityReached { network: NetworkId }, } diff --git a/substrate/client/src/serai/liquidity_tokens.rs b/substrate/client/src/serai/liquidity_tokens.rs index 3e9052b2c..c7ec93cf0 100644 --- a/substrate/client/src/serai/liquidity_tokens.rs +++ b/substrate/client/src/serai/liquidity_tokens.rs @@ -1,6 +1,6 @@ use scale::Encode; -use serai_abi::primitives::{SeraiAddress, Amount, Coin, Balance}; +use serai_abi::primitives::{Amount, ExternalBalance, ExternalCoin, SeraiAddress}; use crate::{TemporalSerai, SeraiError}; @@ -9,13 +9,13 @@ const PALLET: &str = "LiquidityTokens"; #[derive(Clone, Copy)] pub struct SeraiLiquidityTokens<'a>(pub(crate) &'a TemporalSerai<'a>); impl<'a> SeraiLiquidityTokens<'a> { - pub async fn token_supply(&self, coin: Coin) -> Result { + pub async fn token_supply(&self, coin: ExternalCoin) -> Result { Ok(self.0.storage(PALLET, "Supply", coin).await?.unwrap_or(Amount(0))) } pub async fn token_balance( &self, - coin: Coin, + coin: ExternalCoin, address: SeraiAddress, ) -> Result { Ok( @@ -31,11 +31,16 @@ impl<'a> SeraiLiquidityTokens<'a> { ) } - pub fn transfer(to: SeraiAddress, balance: Balance) -> serai_abi::Call { - serai_abi::Call::LiquidityTokens(serai_abi::liquidity_tokens::Call::transfer { to, balance }) + pub fn transfer(to: SeraiAddress, balance: ExternalBalance) -> serai_abi::Call { + serai_abi::Call::LiquidityTokens(serai_abi::liquidity_tokens::Call::transfer { + to, + balance: balance.into(), + }) } - pub fn burn(balance: Balance) -> serai_abi::Call { - serai_abi::Call::LiquidityTokens(serai_abi::liquidity_tokens::Call::burn { balance }) + pub fn burn(balance: ExternalBalance) -> serai_abi::Call { + serai_abi::Call::LiquidityTokens(serai_abi::liquidity_tokens::Call::burn { + balance: balance.into(), + }) } } diff --git a/substrate/dex/pallet/src/lib.rs b/substrate/dex/pallet/src/lib.rs index 10dcb11e0..2a69fd24b 100644 --- a/substrate/dex/pallet/src/lib.rs +++ b/substrate/dex/pallet/src/lib.rs @@ -78,7 +78,7 @@ mod tests; #[cfg(test)] mod mock; -use frame_support::ensure; +use frame_support::{ensure, pallet_prelude::*, BoundedBTreeSet}; use frame_system::{ pallet_prelude::{BlockNumberFor, OriginFor}, ensure_signed, @@ -86,9 +86,12 @@ use frame_system::{ pub use pallet::*; -use sp_runtime::{traits::TrailingZeroInput, DispatchError}; +use sp_runtime::{ + traits::{TrailingZeroInput, IntegerSquareRoot}, + DispatchError, +}; -use serai_primitives::{Coin, ExternalCoin, SubstrateAmount}; +use serai_primitives::*; use sp_std::prelude::*; pub use types::*; @@ -103,15 +106,11 @@ pub use weights::WeightInfo; #[frame_support::pallet] pub mod pallet { use super::*; - use frame_support::{pallet_prelude::*, BoundedBTreeSet}; use sp_core::sr25519::Public; - use sp_runtime::traits::IntegerSquareRoot; use coins_pallet::{Pallet as CoinsPallet, Config as CoinsConfig}; - use serai_primitives::{NetworkId, *}; - /// Pool ID. /// /// The pool's `AccountId` is derived from this type. Any changes to the type may necessitate a diff --git a/substrate/dex/pallet/src/tests.rs b/substrate/dex/pallet/src/tests.rs index 8bc7d684b..8770b5174 100644 --- a/substrate/dex/pallet/src/tests.rs +++ b/substrate/dex/pallet/src/tests.rs @@ -18,7 +18,10 @@ // It has been forked into a crate distributed under the AGPL 3.0. // Please check the current distribution for up-to-date copyright and licensing information. -use crate::{mock::*, *}; +use crate::{ + mock::{*, MEDIAN_PRICE_WINDOW_LENGTH}, + *, +}; use frame_support::{assert_noop, assert_ok}; pub use coins_pallet as coins; diff --git a/substrate/economic-security/pallet/src/lib.rs b/substrate/economic-security/pallet/src/lib.rs index 2abe84a40..045297f46 100644 --- a/substrate/economic-security/pallet/src/lib.rs +++ b/substrate/economic-security/pallet/src/lib.rs @@ -41,7 +41,8 @@ pub mod pallet { // we accept we reached economic security once we can mint smallest amount of a network's coin for coin in EXTERNAL_COINS { let existing = EconomicSecurityBlock::::get(coin.network()); - // TODO: we don't need this if is_allowed returns false when there is no coin value + // TODO: we don't need to check for oracle value if is_allowed returns false when there is + // no coin value if existing.is_none() && Dex::::security_oracle_value(coin).is_some() && ::AllowMint::is_allowed(&ExternalBalance { coin, amount: Amount(1) }) diff --git a/substrate/in-instructions/primitives/src/shorthand.rs b/substrate/in-instructions/primitives/src/shorthand.rs index 8b1103c3b..6f29f6f3d 100644 --- a/substrate/in-instructions/primitives/src/shorthand.rs +++ b/substrate/in-instructions/primitives/src/shorthand.rs @@ -9,7 +9,7 @@ use serde::{Serialize, Deserialize}; use scale::{Encode, Decode, MaxEncodedLen}; use scale_info::TypeInfo; -use serai_primitives::{Coin, Amount, SeraiAddress, ExternalAddress}; +use serai_primitives::{Amount, ExternalAddress, ExternalCoin, SeraiAddress}; use coins_primitives::OutInstruction; @@ -25,7 +25,7 @@ pub enum Shorthand { Raw(RefundableInInstruction), Swap { origin: Option, - coin: Coin, + coin: ExternalCoin, minimum: Amount, out: OutInstruction, }, diff --git a/substrate/primitives/src/networks.rs b/substrate/primitives/src/networks.rs index 75875467c..412a5a745 100644 --- a/substrate/primitives/src/networks.rs +++ b/substrate/primitives/src/networks.rs @@ -215,7 +215,6 @@ impl Coin { pub fn decimals(&self) -> u32 { match self { - // Ether and DAI have 18 decimals, yet we only track 8 in order to fit them within u64s Coin::Serai => 8, Coin::External(c) => c.decimals(), } diff --git a/substrate/runtime/src/lib.rs b/substrate/runtime/src/lib.rs index 93aba991d..151242712 100644 --- a/substrate/runtime/src/lib.rs +++ b/substrate/runtime/src/lib.rs @@ -53,8 +53,9 @@ use sp_runtime::{ #[allow(unused_imports)] use primitives::{ - NetworkId, PublicKey, AccountLookup, SubstrateAmount, Coin, NETWORKS, MEDIAN_PRICE_WINDOW_LENGTH, - HOURS, DAYS, MINUTES, TARGET_BLOCK_TIME, BLOCK_SIZE, FAST_EPOCH_DURATION, + NetworkId, PublicKey, AccountLookup, SubstrateAmount, Coin, EXTERNAL_NETWORKS, + MEDIAN_PRICE_WINDOW_LENGTH, HOURS, DAYS, MINUTES, TARGET_BLOCK_TIME, BLOCK_SIZE, + FAST_EPOCH_DURATION, }; use support::{ @@ -570,10 +571,7 @@ sp_api::impl_runtime_apis! { .map(|(id, _)| id.into_inner().0) .collect::>(); let mut all = serai_validators; - for network in NETWORKS { - if network == NetworkId::Serai { - continue; - } + for network in EXTERNAL_NETWORKS { // Returning the latest-decided, not latest and active, means the active set // may fail to peer find if there isn't sufficient overlap. If a large amount reboot, // forcing some validators to successfully peer find in order for the threshold to become @@ -581,7 +579,7 @@ sp_api::impl_runtime_apis! { // // This is assumed not to matter in real life, yet an interesting note. let participants = - ValidatorSets::participants_for_latest_decided_set(network) + ValidatorSets::participants_for_latest_decided_set(NetworkId::from(network)) .map_or(vec![], BoundedVec::into_inner); for (participant, _) in participants { all.insert(participant.0); diff --git a/substrate/validator-sets/pallet/src/lib.rs b/substrate/validator-sets/pallet/src/lib.rs index 59390f3c4..f343b80b7 100644 --- a/substrate/validator-sets/pallet/src/lib.rs +++ b/substrate/validator-sets/pallet/src/lib.rs @@ -171,6 +171,7 @@ pub mod pallet { /// /// This will still include participants which were removed from the DKG. pub fn in_set(network: NetworkId, account: Public) -> bool { + // TODO: should InSet only work for `ExternalNetworkId`? if InSet::::contains_key(network, account) { return true; } @@ -743,6 +744,9 @@ pub mod pallet { // Serai doesn't set keys and network slashes are handled by BABE/GRANDPA if let NetworkId::External(n) = set.network { // If the prior prior set didn't report, emit they're retired now + // TODO: we will emit the events 1 session late if there was no call to report_slashes. + // Also report_slashes calls must be made after the set publishes its first batch for this + // flow to work as expected. if PendingSlashReport::::get(n).is_some() { Self::deposit_event(Event::SetRetired { set: ValidatorSet { network: set.network, session: Session(set.session.0 - 1) }, @@ -754,6 +758,9 @@ pub mod pallet { let keys = Keys::::take(ExternalValidatorSet { network: n, session: set.session }).unwrap(); PendingSlashReport::::set(n, Some(keys.0)); + } else { + // emit the event for serai network + Self::deposit_event(Event::SetRetired { set }); } // We're retiring this set because the set after it accepted the handover