From e65378011a27ae0a3f778bdc4644423566be0f23 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Wed, 15 Nov 2023 16:15:09 +0800 Subject: [PATCH 1/7] feat: add super trait for block number --- .../primitives/runtime/src/generic/header.rs | 20 +------- substrate/primitives/runtime/src/traits.rs | 50 ++++++++++++++----- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/substrate/primitives/runtime/src/generic/header.rs b/substrate/primitives/runtime/src/generic/header.rs index 82ab9a61f96d..430e0dd8b978 100644 --- a/substrate/primitives/runtime/src/generic/header.rs +++ b/substrate/primitives/runtime/src/generic/header.rs @@ -22,15 +22,12 @@ use crate::{ generic::Digest, scale_info::TypeInfo, traits::{ - self, AtLeast32BitUnsigned, Hash as HashT, MaybeDisplay, MaybeFromStr, - MaybeSerializeDeserialize, Member, + self, AtLeast32BitUnsigned, Hash as HashT, MaybeDisplay, Member,BlockNumber }, }; -use codec::{FullCodec, MaxEncodedLen}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use sp_core::U256; -use sp_std::fmt::Debug; /// Abstraction over a block header for a substrate chain. #[derive(Encode, Decode, PartialEq, Eq, Clone, sp_core::RuntimeDebug, TypeInfo)] @@ -79,20 +76,7 @@ where impl traits::Header for Header where - Number: Member - + MaybeSerializeDeserialize - + MaybeFromStr - + Debug - + Default - + sp_std::hash::Hash - + MaybeDisplay - + AtLeast32BitUnsigned - + FullCodec - + Copy - + MaxEncodedLen - + Into - + TryFrom - + TypeInfo, + Number: BlockNumber, Hash: HashT, { type Number = Number; diff --git a/substrate/primitives/runtime/src/traits.rs b/substrate/primitives/runtime/src/traits.rs index ec79f43cabdc..d485ab8d1616 100644 --- a/substrate/primitives/runtime/src/traits.rs +++ b/substrate/primitives/runtime/src/traits.rs @@ -38,7 +38,7 @@ pub use sp_arithmetic::traits::{ EnsureOp, EnsureOpAssign, EnsureSub, EnsureSubAssign, IntegerSquareRoot, One, SaturatedConversion, Saturating, UniqueSaturatedFrom, UniqueSaturatedInto, Zero, }; -use sp_core::{self, storage::StateVersion, Hasher, RuntimeDebug, TypeId}; +use sp_core::{self, storage::StateVersion, Hasher, RuntimeDebug, TypeId, U256}; #[doc(hidden)] pub use sp_core::{ parameter_types, ConstBool, ConstI128, ConstI16, ConstI32, ConstI64, ConstI8, ConstU128, @@ -1149,6 +1149,41 @@ pub trait IsMember { fn is_member(member_id: &MemberId) -> bool; } +/// Super trait with all the attributes for a block number. +pub trait BlockNumber: Member ++ MaybeSerializeDeserialize ++ MaybeFromStr ++ Debug ++ sp_std::hash::Hash ++ Copy ++ MaybeDisplay ++ AtLeast32BitUnsigned ++ Into ++ Into ++ TryFrom ++ TryFrom ++ Default ++ TypeInfo ++ MaxEncodedLen ++ FullCodec {} + +impl ++ Into ++ TryFrom ++ TryFrom ++ Default ++ TypeInfo ++ MaxEncodedLen ++ FullCodec> BlockNumber for T {} + /// Something which fulfills the abstract idea of a Substrate header. It has types for a `Number`, /// a `Hash` and a `Hashing`. It provides access to an `extrinsics_root`, `state_root` and /// `parent_hash`, as well as a `digest` and a block `number`. @@ -1158,18 +1193,7 @@ pub trait Header: Clone + Send + Sync + Codec + Eq + MaybeSerialize + Debug + TypeInfo + 'static { /// Header number. - type Number: Member - + MaybeSerializeDeserialize - + MaybeFromStr - + Debug - + sp_std::hash::Hash - + Copy - + MaybeDisplay - + AtLeast32BitUnsigned - + Default - + TypeInfo - + MaxEncodedLen - + FullCodec; + type Number: BlockNumber; /// Header hash type type Hash: HashOutput; /// Hashing algorithm From ecd6ef5131c6544f90903bd20d0ffbbf2a387b43 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Wed, 15 Nov 2023 16:18:22 +0800 Subject: [PATCH 2/7] fmt --- .../primitives/runtime/src/generic/header.rs | 6 +- substrate/primitives/runtime/src/traits.rs | 73 ++++++++++--------- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/substrate/primitives/runtime/src/generic/header.rs b/substrate/primitives/runtime/src/generic/header.rs index 430e0dd8b978..0eeef363a06d 100644 --- a/substrate/primitives/runtime/src/generic/header.rs +++ b/substrate/primitives/runtime/src/generic/header.rs @@ -21,9 +21,7 @@ use crate::{ codec::{Codec, Decode, Encode}, generic::Digest, scale_info::TypeInfo, - traits::{ - self, AtLeast32BitUnsigned, Hash as HashT, MaybeDisplay, Member,BlockNumber - }, + traits::{self, AtLeast32BitUnsigned, BlockNumber, Hash as HashT, MaybeDisplay, Member}, }; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -76,7 +74,7 @@ where impl traits::Header for Header where - Number: BlockNumber, + Number: BlockNumber, Hash: HashT, { type Number = Number; diff --git a/substrate/primitives/runtime/src/traits.rs b/substrate/primitives/runtime/src/traits.rs index d485ab8d1616..5f1024a370d4 100644 --- a/substrate/primitives/runtime/src/traits.rs +++ b/substrate/primitives/runtime/src/traits.rs @@ -1150,39 +1150,46 @@ pub trait IsMember { } /// Super trait with all the attributes for a block number. -pub trait BlockNumber: Member -+ MaybeSerializeDeserialize -+ MaybeFromStr -+ Debug -+ sp_std::hash::Hash -+ Copy -+ MaybeDisplay -+ AtLeast32BitUnsigned -+ Into -+ Into -+ TryFrom -+ TryFrom -+ Default -+ TypeInfo -+ MaxEncodedLen -+ FullCodec {} - -impl -+ Into -+ TryFrom -+ TryFrom -+ Default -+ TypeInfo -+ MaxEncodedLen -+ FullCodec> BlockNumber for T {} +pub trait BlockNumber: + Member + + MaybeSerializeDeserialize + + MaybeFromStr + + Debug + + sp_std::hash::Hash + + Copy + + MaybeDisplay + + AtLeast32BitUnsigned + + Into + + Into + + TryFrom + + TryFrom + + Default + + TypeInfo + + MaxEncodedLen + + FullCodec +{ +} + +impl< + T: Member + + MaybeSerializeDeserialize + + MaybeFromStr + + Debug + + sp_std::hash::Hash + + Copy + + MaybeDisplay + + AtLeast32BitUnsigned + + Into + + Into + + TryFrom + + TryFrom + + Default + + TypeInfo + + MaxEncodedLen + + FullCodec, + > BlockNumber for T +{ +} /// Something which fulfills the abstract idea of a Substrate header. It has types for a `Number`, /// a `Hash` and a `Hashing`. It provides access to an `extrinsics_root`, `state_root` and From 080cb88c4941a136fbaa3959e9b9203acebcc0a3 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Thu, 16 Nov 2023 10:31:09 +0800 Subject: [PATCH 3/7] remove `Into` --- substrate/primitives/runtime/src/traits.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/substrate/primitives/runtime/src/traits.rs b/substrate/primitives/runtime/src/traits.rs index 5f1024a370d4..bc22a7fc97f4 100644 --- a/substrate/primitives/runtime/src/traits.rs +++ b/substrate/primitives/runtime/src/traits.rs @@ -1160,7 +1160,6 @@ pub trait BlockNumber: + MaybeDisplay + AtLeast32BitUnsigned + Into - + Into + TryFrom + TryFrom + Default @@ -1180,7 +1179,6 @@ impl< + MaybeDisplay + AtLeast32BitUnsigned + Into - + Into + TryFrom + TryFrom + Default From 5e02c1df684422d1bfbf7bbfbdd86e28c21859c0 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Thu, 16 Nov 2023 10:43:56 +0800 Subject: [PATCH 4/7] fix `archive_unstable_hash_by_height` --- substrate/client/rpc-spec-v2/src/archive/archive.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/client/rpc-spec-v2/src/archive/archive.rs b/substrate/client/rpc-spec-v2/src/archive/archive.rs index bded842d8fd0..79ff5f90cbf9 100644 --- a/substrate/client/rpc-spec-v2/src/archive/archive.rs +++ b/substrate/client/rpc-spec-v2/src/archive/archive.rs @@ -124,7 +124,7 @@ where let finalized_num = self.client.info().finalized_number; if finalized_num >= height { - let Ok(Some(hash)) = self.client.block_hash(height.into()) else { return Ok(vec![]) }; + let Ok(Some(hash)) = self.client.block_hash(height) else { return Ok(vec![]) }; return Ok(vec![hex_string(&hash.as_ref())]) } From c149e3899731941f5318680becd828f84fec98a5 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Thu, 16 Nov 2023 11:10:25 +0800 Subject: [PATCH 5/7] fix `accepted_interval` --- substrate/client/consensus/beefy/src/worker.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/client/consensus/beefy/src/worker.rs b/substrate/client/consensus/beefy/src/worker.rs index 309d8c5135be..cabd7b4b58cb 100644 --- a/substrate/client/consensus/beefy/src/worker.rs +++ b/substrate/client/consensus/beefy/src/worker.rs @@ -212,7 +212,7 @@ impl VoterOracle { // Accept any vote for a GRANDPA finalized block in a better round. Ok(( rounds.session_start().max(self.best_beefy_block), - (*self.best_grandpa_block_header.number()).into(), + (*self.best_grandpa_block_header.number()), )) } else { // Current session has mandatory not done. From 99b3de124635533cca1ebb198d677c1cc500c120 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Thu, 16 Nov 2023 11:55:41 +0800 Subject: [PATCH 6/7] fix `group_assigned_to_core` --- polkadot/runtime/parachains/src/scheduler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polkadot/runtime/parachains/src/scheduler.rs b/polkadot/runtime/parachains/src/scheduler.rs index b81b68b5745e..dea84c69f52d 100644 --- a/polkadot/runtime/parachains/src/scheduler.rs +++ b/polkadot/runtime/parachains/src/scheduler.rs @@ -446,7 +446,7 @@ impl Pallet { } let rotations_since_session_start: BlockNumberFor = - (at - session_start_block) / config.group_rotation_frequency.into(); + (at - session_start_block) / config.group_rotation_frequency; let rotations_since_session_start = as TryInto>::try_into(rotations_since_session_start) From 19281e4a9c12bdc3c3d7e722adcbeb0ae6b2e79f Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 28 Nov 2023 10:34:13 +0800 Subject: [PATCH 7/7] remove `TryFrom` --- substrate/primitives/runtime/src/traits.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/substrate/primitives/runtime/src/traits.rs b/substrate/primitives/runtime/src/traits.rs index bc22a7fc97f4..ecb751e9bfbc 100644 --- a/substrate/primitives/runtime/src/traits.rs +++ b/substrate/primitives/runtime/src/traits.rs @@ -1161,7 +1161,6 @@ pub trait BlockNumber: + AtLeast32BitUnsigned + Into + TryFrom - + TryFrom + Default + TypeInfo + MaxEncodedLen @@ -1180,7 +1179,6 @@ impl< + AtLeast32BitUnsigned + Into + TryFrom - + TryFrom + Default + TypeInfo + MaxEncodedLen