Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Uses custom bound for AsPrimitive
Browse files Browse the repository at this point in the history
  • Loading branch information
gupnik committed Jul 11, 2023
1 parent 9596411 commit 838e528
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 30 deletions.
5 changes: 4 additions & 1 deletion bridges/bin/runtime-common/src/refund_relayer_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ use pallet_transaction_payment::{Config as TransactionPaymentConfig, OnChargeTra
use pallet_utility::{Call as UtilityCall, Config as UtilityConfig, Pallet as UtilityPallet};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT, DispatchInfoOf, Get, PostDispatchInfoOf, SignedExtension, Zero},
traits::{
Block as BlockT, DispatchInfoOf, Get, Header as HeaderT, PostDispatchInfoOf,
SignedExtension, Zero,
},
transaction_validity::{
TransactionPriority, TransactionValidity, TransactionValidityError, ValidTransactionBuilder,
},
Expand Down
11 changes: 4 additions & 7 deletions bridges/modules/parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,15 @@ pub mod pallet {
pub trait BoundedBridgeGrandpaConfig<I: 'static>:
pallet_bridge_grandpa::Config<I, BridgedChain = Self::BridgedRelayChain>
{
type BridgedRelayChain: Chain<
Hash = RelayBlockHash,
Hasher = RelayBlockHasher,
>;
type BridgedRelayChain: Chain<Hash = RelayBlockHash, Hasher = RelayBlockHasher>;
}

impl<T, I: 'static> BoundedBridgeGrandpaConfig<I> for T
where
T: pallet_bridge_grandpa::Config<I>,
T::BridgedChain:
Chain<Hash = RelayBlockHash, Hasher = RelayBlockHasher>,
<<T::BridgedChain as Chain>::Block as sp_runtime::traits::Block>::Header: sp_runtime::traits::Header<Number = RelayBlockNumber>
T::BridgedChain: Chain<Hash = RelayBlockHash, Hasher = RelayBlockHasher>,
<<T::BridgedChain as Chain>::Block as sp_runtime::traits::Block>::Header:
sp_runtime::traits::Header<Number = RelayBlockNumber>,
{
type BridgedRelayChain = T::BridgedChain;
}
Expand Down
28 changes: 21 additions & 7 deletions bridges/primitives/runtime/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{weights::Weight, Parameter};
use num_traits::{Bounded, CheckedSub, SaturatingAdd, Zero};
use num_traits::{AsPrimitive, Bounded, CheckedSub, SaturatingAdd, Zero};
use sp_runtime::{
traits::{
AtLeast32Bit, AtLeast32BitUnsigned, Block as BlockT, Hash as HashT, Header as HeaderT,
Expand Down Expand Up @@ -90,7 +90,10 @@ impl<ChainCall: Encode> Encode for EncodedOrDecodedCall<ChainCall> {
}

/// Minimal Substrate-based chain representation that may be used from no_std environment.
pub trait Chain: Send + Sync + 'static {
pub trait Chain: Send + Sync + 'static
where
<<Self::Block as BlockT>::Header as HeaderT>::Number: AsPrimitive<usize>,
{
/// A type that fulfills the abstract idea of what a Substrate hash is.
// Constraits come from the associated Hash type of `sp_runtime::traits::Header`
// See here for more info:
Expand Down Expand Up @@ -118,8 +121,7 @@ pub trait Chain: Send + Sync + 'static {
/// A type that fulfills the abstract idea of what a Substrate block is.
// See here for more info:
// https://crates.parity.io/sp_runtime/traits/trait.Block.html
type Block: Parameter + BlockT<Hash = Self::Hash> + MaybeSerialize
where Block::Header::Number: AsPrimitive<usize>;
type Block: Parameter + BlockT<Hash = Self::Hash> + MaybeSerialize;

/// The user account identifier type for the runtime.
type AccountId: Parameter
Expand Down Expand Up @@ -168,14 +170,18 @@ pub trait Chain: Send + Sync + 'static {
}

/// A trait that provides the type of the underlying chain.
pub trait UnderlyingChainProvider {
pub trait UnderlyingChainProvider
where
<<<Self::Chain as Chain>::Block as BlockT>::Header as HeaderT>::Number: AsPrimitive<usize>,
{
/// Underlying chain type.
type Chain: Chain;
}

impl<T> Chain for T
where
T: Send + Sync + 'static + UnderlyingChainProvider,
<<<T::Chain as Chain>::Block as BlockT>::Header as HeaderT>::Number: AsPrimitive<usize>,
{
type Hash = <T::Chain as Chain>::Hash;
type Hasher = <T::Chain as Chain>::Hasher;
Expand All @@ -195,7 +201,10 @@ where
}

/// Minimal parachain representation that may be used from no_std environment.
pub trait Parachain: Chain {
pub trait Parachain: Chain
where
<<<Self as Chain>::Block as BlockT>::Header as HeaderT>::Number: AsPrimitive<usize>,
{
/// Parachain identifier.
const PARACHAIN_ID: u32;
}
Expand All @@ -204,13 +213,18 @@ impl<T> Parachain for T
where
T: Chain + UnderlyingChainProvider,
<T as UnderlyingChainProvider>::Chain: Parachain,
<<<T as Chain>::Block as BlockT>::Header as HeaderT>::Number: AsPrimitive<usize>,
<<<T::Chain as Chain>::Block as BlockT>::Header as HeaderT>::Number: AsPrimitive<usize>,
{
const PARACHAIN_ID: u32 = <<T as UnderlyingChainProvider>::Chain as Parachain>::PARACHAIN_ID;
}

/// Adapter for `Get<u32>` to access `PARACHAIN_ID` from `trait Parachain`
pub struct ParachainIdOf<Para>(sp_std::marker::PhantomData<Para>);
impl<Para: Parachain> frame_support::traits::Get<u32> for ParachainIdOf<Para> {
impl<Para: Parachain> frame_support::traits::Get<u32> for ParachainIdOf<Para>
where
<<<Para as Chain>::Block as BlockT>::Header as HeaderT>::Number: AsPrimitive<usize>,
{
fn get() -> u32 {
Para::PARACHAIN_ID
}
Expand Down
3 changes: 1 addition & 2 deletions pallets/parachain-system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ use frame_support::{
traits::{OnFinalize, OnInitialize},
weights::Weight,
};
use frame_system::RawOrigin;
use frame_system::pallet_prelude::BlockNumberFor;
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use hex_literal::hex;
use relay_chain::HrmpChannelId;
use sp_core::{blake2_256, H256};
Expand Down
4 changes: 2 additions & 2 deletions parachains/runtimes/assets/asset-hub-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ use pallet_nfts::PalletFeatures;
pub use parachains_common as common;
use parachains_common::{
impls::{AssetsToBlockAuthor, DealWithFees},
AccountId, AssetIdForTrustBackedAssets, AuraId, Balance, BlockNumber, Hash, Header,
Index, Signature, AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS, MAXIMUM_BLOCK_WEIGHT,
AccountId, AssetIdForTrustBackedAssets, AuraId, Balance, BlockNumber, Hash, Header, Index,
Signature, AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS, MAXIMUM_BLOCK_WEIGHT,
NORMAL_DISPATCH_RATIO, SLOT_DURATION,
};
use xcm_config::{
Expand Down
6 changes: 3 additions & 3 deletions parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ use pallet_nfts::PalletFeatures;
pub use parachains_common as common;
use parachains_common::{
impls::{AssetsToBlockAuthor, DealWithFees},
AccountId, AssetHubPolkadotAuraId as AuraId, AssetIdForTrustBackedAssets, Balance,
BlockNumber, Hash, Header, Index, Signature, AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS,
MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO, SLOT_DURATION,
AccountId, AssetHubPolkadotAuraId as AuraId, AssetIdForTrustBackedAssets, Balance, BlockNumber,
Hash, Header, Index, Signature, AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS, MAXIMUM_BLOCK_WEIGHT,
NORMAL_DISPATCH_RATIO, SLOT_DURATION,
};
use xcm_config::{
DotLocation, FellowshipLocation, ForeignAssetsConvertedConcreteId, GovernanceLocation,
Expand Down
6 changes: 3 additions & 3 deletions parachains/runtimes/assets/asset-hub-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ use pallet_asset_conversion_tx_payment::AssetConversionAdapter;
use pallet_nfts::PalletFeatures;
pub use parachains_common as common;
use parachains_common::{
impls::DealWithFees, AccountId, AssetIdForTrustBackedAssets, AuraId, Balance,
BlockNumber, Hash, Header, Index, Signature, AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS,
MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO, SLOT_DURATION,
impls::DealWithFees, AccountId, AssetIdForTrustBackedAssets, AuraId, Balance, BlockNumber,
Hash, Header, Index, Signature, AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS, MAXIMUM_BLOCK_WEIGHT,
NORMAL_DISPATCH_RATIO, SLOT_DURATION,
};
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
Expand Down
3 changes: 1 addition & 2 deletions parachains/runtimes/bridge-hubs/test-utils/src/test_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,7 @@ pub fn complex_relay_extrinsic_works<Runtime, XcmConfig, HrmpChannelOpener, GPI,
.execute_with(|| {
let zero: BlockNumberFor<Runtime> = 0u32.into();
let genesis_hash = frame_system::Pallet::<Runtime>::block_hash(zero);
let mut header: HeaderFor<Runtime> =
bp_test_utils::test_header(1u32.into());
let mut header: HeaderFor<Runtime> = bp_test_utils::test_header(1u32.into());
header.set_parent_hash(genesis_hash);
executive_init_block(&header);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ use frame_system::{
};
pub use parachains_common as common;
use parachains_common::{
impls::DealWithFees, AccountId, AuraId, Balance, BlockNumber, Hash, Header, Index,
Signature, AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS, MAXIMUM_BLOCK_WEIGHT, MINUTES,
NORMAL_DISPATCH_RATIO, SLOT_DURATION,
impls::DealWithFees, AccountId, AuraId, Balance, BlockNumber, Hash, Header, Index, Signature,
AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS, MAXIMUM_BLOCK_WEIGHT, MINUTES, NORMAL_DISPATCH_RATIO,
SLOT_DURATION,
};
use xcm_config::{GovernanceLocation, XcmConfig, XcmOriginToTransactDispatchOrigin};

Expand Down

0 comments on commit 838e528

Please sign in to comment.