Skip to content

Commit

Permalink
feat: add super trait for block number (#2334)
Browse files Browse the repository at this point in the history
And also related to a subxt PR
paritytech/subxt#1265
  • Loading branch information
yjhmelody authored Dec 14, 2023
1 parent 6dece52 commit 2cee874
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion polkadot/runtime/parachains/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ impl<T: Config> Pallet<T> {
}

let rotations_since_session_start: BlockNumberFor<T> =
(at - session_start_block) / config.group_rotation_frequency.into();
(at - session_start_block) / config.group_rotation_frequency;

let rotations_since_session_start =
<BlockNumberFor<T> as TryInto<u32>>::try_into(rotations_since_session_start)
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/consensus/beefy/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<B: Block> VoterOracle<B> {
// 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.
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/rpc-spec-v2/src/archive/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())])
}

Expand Down
22 changes: 2 additions & 20 deletions substrate/primitives/runtime/src/generic/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ use crate::{
codec::{Codec, Decode, Encode},
generic::Digest,
scale_info::TypeInfo,
traits::{
self, AtLeast32BitUnsigned, Hash as HashT, MaybeDisplay, MaybeFromStr,
MaybeSerializeDeserialize, Member,
},
traits::{self, AtLeast32BitUnsigned, BlockNumber, Hash as HashT, MaybeDisplay, Member},
};
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)]
Expand Down Expand Up @@ -79,20 +74,7 @@ where

impl<Number, Hash> traits::Header for Header<Number, Hash>
where
Number: Member
+ MaybeSerializeDeserialize
+ MaybeFromStr
+ Debug
+ Default
+ sp_std::hash::Hash
+ MaybeDisplay
+ AtLeast32BitUnsigned
+ FullCodec
+ Copy
+ MaxEncodedLen
+ Into<U256>
+ TryFrom<U256>
+ TypeInfo,
Number: BlockNumber,
Hash: HashT,
{
type Number = Number;
Expand Down
53 changes: 40 additions & 13 deletions substrate/primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1149,6 +1149,44 @@ pub trait IsMember<MemberId> {
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<U256>
+ TryFrom<U256>
+ Default
+ TypeInfo
+ MaxEncodedLen
+ FullCodec
{
}

impl<
T: Member
+ MaybeSerializeDeserialize
+ MaybeFromStr
+ Debug
+ sp_std::hash::Hash
+ Copy
+ MaybeDisplay
+ AtLeast32BitUnsigned
+ Into<U256>
+ TryFrom<U256>
+ 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`.
Expand All @@ -1158,18 +1196,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
Expand Down

0 comments on commit 2cee874

Please sign in to comment.