Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add super trait for block number #2334

Merged
merged 15 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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