From 83c7288d71fb8540148b829b8b4cee82b3b2c31d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Pestana?= Date: Thu, 14 Nov 2024 11:14:30 +0100 Subject: [PATCH] nits and removes sp-std dependency from staking primitives --- Cargo.lock | 1 - substrate/primitives/staking/Cargo.toml | 2 -- substrate/primitives/staking/src/lib.rs | 26 +++++++++++++------------ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 771cffeebe5a..8408adf93675 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27272,7 +27272,6 @@ dependencies = [ "serde", "sp-core 28.0.0", "sp-runtime 31.0.1", - "sp-std 14.0.0", ] [[package]] diff --git a/substrate/primitives/staking/Cargo.toml b/substrate/primitives/staking/Cargo.toml index 80220cd1f6fc..35e7e4f60413 100644 --- a/substrate/primitives/staking/Cargo.toml +++ b/substrate/primitives/staking/Cargo.toml @@ -23,7 +23,6 @@ impl-trait-for-tuples = { workspace = true } sp-core = { workspace = true } sp-runtime = { workspace = true } -sp-std = { workspace = true } [features] default = ["std"] @@ -33,6 +32,5 @@ std = [ "serde/std", "sp-core/std", "sp-runtime/std", - "sp-std/std", ] runtime-benchmarks = ["sp-runtime/runtime-benchmarks"] diff --git a/substrate/primitives/staking/src/lib.rs b/substrate/primitives/staking/src/lib.rs index 9e9fde3a6db0..b53d75f512a5 100644 --- a/substrate/primitives/staking/src/lib.rs +++ b/substrate/primitives/staking/src/lib.rs @@ -25,7 +25,7 @@ extern crate alloc; use crate::currency_to_vote::CurrencyToVote; use alloc::{collections::btree_map::BTreeMap, vec, vec::Vec}; use codec::{Decode, Encode, FullCodec, HasCompact, MaxEncodedLen}; -use core::ops::Sub; +use core::ops::{Add, AddAssign, Sub, SubAssign}; use scale_info::TypeInfo; use sp_runtime::{ traits::{AtLeast32BitUnsigned, Zero}, @@ -357,8 +357,6 @@ pub struct IndividualExposure { /// A snapshot of the stake backing a single validator in the system. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -#[codec(mel_bound(T: Config))] -#[scale_info(skip_type_params(T))] pub struct Exposure { /// The total balance backing this validator. #[codec(compact)] @@ -385,6 +383,9 @@ impl< /// /// `n_others` individual exposures are consumed from self and returned as part of the new /// exposure. + /// + /// Since this method splits `others` of a single exposure, `total.own` will be the same for + /// both `self` and the returned exposure. pub fn split_others(&mut self, n_others: u32) -> Self { let head_others: Vec<_> = self.others.drain(..(n_others as usize).min(self.others.len())).collect(); @@ -455,8 +456,8 @@ impl Default for ExposurePage { } /// Returns an exposure page from a set of individual exposures. -impl - From>> for ExposurePage +impl From>> + for ExposurePage { fn from(exposures: Vec>) -> Self { exposures.into_iter().fold(ExposurePage::default(), |mut page, e| { @@ -503,8 +504,8 @@ impl PagedExposureMetadata where Balance: HasCompact + codec::MaxEncodedLen - + sp_std::ops::Add - + sp_std::ops::Sub + + Add + + Sub + sp_runtime::Saturating + PartialEq + Copy @@ -733,7 +734,8 @@ mod tests { }; let mut exposure_0 = exposure.clone(); - // split others with with 0 `n_others` is a noop and returns an exposure with own only. + // split others with with 0 `n_others` is a noop and returns an empty exposure (with `own` + // only). let split_exposure = exposure_0.split_others(0); assert_eq!(exposure_0, exposure); assert_eq!(split_exposure, Exposure { total: 20, own: 20, others: vec![] }); @@ -761,12 +763,12 @@ mod tests { assert_eq!(split_exposure.total, 20 + 3 * 20); assert_eq!(split_exposure.others.len(), 3); - let mut exposure_100 = exposure.clone(); - // split others with with more `n_others` than the number of others in the exposure returns + let mut exposure_max = exposure.clone(); + // split others with with more `n_others` than the number of others in the exposure // consumes all the individual exposures of the original Exposure and returns them in the // new exposure. - let split_exposure = exposure_100.split_others(100); + let split_exposure = exposure_max.split_others(u32::MAX); assert_eq!(split_exposure, exposure); - assert_eq!(exposure_100, Exposure { total: 20, own: 20, others: vec![] }); + assert_eq!(exposure_max, Exposure { total: 20, own: 20, others: vec![] }); } }