Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Sep 30, 2024
1 parent 2379b5c commit c66650d
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 35 deletions.
10 changes: 4 additions & 6 deletions coordinator/src/cosign_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -204,11 +204,9 @@ impl<D: Db> CosignEvaluator<D> {

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;
Expand Down
4 changes: 2 additions & 2 deletions substrate/abi/src/economic_security.rs
Original file line number Diff line number Diff line change
@@ -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 },
}
1 change: 0 additions & 1 deletion substrate/abi/src/genesis_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
19 changes: 12 additions & 7 deletions substrate/client/src/serai/liquidity_tokens.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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<Amount, SeraiError> {
pub async fn token_supply(&self, coin: ExternalCoin) -> Result<Amount, SeraiError> {
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<Amount, SeraiError> {
Ok(
Expand All @@ -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(),
})
}
}
13 changes: 6 additions & 7 deletions substrate/dex/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,20 @@ 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,
};

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::*;
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion substrate/dex/pallet/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion substrate/economic-security/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>::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::<T>::security_oracle_value(coin).is_some() &&
<T as CoinsConfig>::AllowMint::is_allowed(&ExternalBalance { coin, amount: Amount(1) })
Expand Down
4 changes: 2 additions & 2 deletions substrate/in-instructions/primitives/src/shorthand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,7 +25,7 @@ pub enum Shorthand {
Raw(RefundableInInstruction),
Swap {
origin: Option<ExternalAddress>,
coin: Coin,
coin: ExternalCoin,
minimum: Amount,
out: OutInstruction,
},
Expand Down
1 change: 0 additions & 1 deletion substrate/primitives/src/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
12 changes: 5 additions & 7 deletions substrate/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -570,18 +571,15 @@ sp_api::impl_runtime_apis! {
.map(|(id, _)| id.into_inner().0)
.collect::<hashbrown::HashSet<_>>();
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
// online again, this may cause a liveness failure.
//
// 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);
Expand Down
7 changes: 7 additions & 0 deletions substrate/validator-sets/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>::contains_key(network, account) {
return true;
}
Expand Down Expand Up @@ -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::<T>::get(n).is_some() {
Self::deposit_event(Event::SetRetired {
set: ValidatorSet { network: set.network, session: Session(set.session.0 - 1) },
Expand All @@ -754,6 +758,9 @@ pub mod pallet {
let keys =
Keys::<T>::take(ExternalValidatorSet { network: n, session: set.session }).unwrap();
PendingSlashReport::<T>::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
Expand Down

0 comments on commit c66650d

Please sign in to comment.