Skip to content

Commit

Permalink
Remove SOURCE_PARACHAIN_PARA_ID (paritytech#1716)
Browse files Browse the repository at this point in the history
* Remove SOURCE_PARACHAIN_PARA_ID

Remove SubstrateParachainsPipeline::SOURCE_PARACHAIN_PARA_ID

* Avoid duplicate ChainBase and ParachainBase implementations

* Replace ChainShadow with UnderlyingChainProvider
  • Loading branch information
serban300 authored Dec 14, 2022
1 parent d1cb5d1 commit af9abbe
Show file tree
Hide file tree
Showing 27 changed files with 154 additions and 272 deletions.
9 changes: 1 addition & 8 deletions bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use bp_messages::{
InboundLaneData, LaneId, Message, MessageKey, MessageNonce, MessagePayload, OutboundLaneData,
};
use bp_runtime::{messages::MessageDispatchResult, Chain, ChainId, Size, StorageProofChecker};
pub use bp_runtime::{UnderlyingChainOf, UnderlyingChainProvider};
use codec::{Decode, DecodeLimit, Encode};
use frame_support::{traits::Get, weights::Weight, RuntimeDebug};
use hash_db::Hasher;
Expand Down Expand Up @@ -54,12 +55,6 @@ pub trait MessageBridge {
type BridgedHeaderChain: HeaderChain<UnderlyingChainOf<Self::BridgedChain>>;
}

/// A trait that provides the type of the underlying chain.
pub trait UnderlyingChainProvider {
/// Underlying chain type.
type Chain: Chain;
}

/// This chain that has `pallet-bridge-messages` module.
pub trait ThisChainWithMessages: UnderlyingChainProvider {
/// Call origin on the chain.
Expand Down Expand Up @@ -87,8 +82,6 @@ pub trait BridgedChainWithMessages: UnderlyingChainProvider {
pub type ThisChain<B> = <B as MessageBridge>::ThisChain;
/// Bridged chain in context of message bridge.
pub type BridgedChain<B> = <B as MessageBridge>::BridgedChain;
/// Underlying chain type.
pub type UnderlyingChainOf<C> = <C as UnderlyingChainProvider>::Chain;
/// Hash used on the chain.
pub type HashOf<C> = bp_runtime::HashOf<<C as UnderlyingChainProvider>::Chain>;
/// Hasher used on the chain.
Expand Down
39 changes: 39 additions & 0 deletions primitives/runtime/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,51 @@ pub trait Chain: Send + Sync + 'static {
fn max_extrinsic_weight() -> Weight;
}

/// A trait that provides the type of the underlying chain.
pub trait UnderlyingChainProvider {
/// Underlying chain type.
type Chain: Chain;
}

impl<T> Chain for T
where
T: Send + Sync + 'static + UnderlyingChainProvider,
{
type BlockNumber = <T::Chain as Chain>::BlockNumber;
type Hash = <T::Chain as Chain>::Hash;
type Hasher = <T::Chain as Chain>::Hasher;
type Header = <T::Chain as Chain>::Header;
type AccountId = <T::Chain as Chain>::AccountId;
type Balance = <T::Chain as Chain>::Balance;
type Index = <T::Chain as Chain>::Index;
type Signature = <T::Chain as Chain>::Signature;

fn max_extrinsic_size() -> u32 {
<T::Chain as Chain>::max_extrinsic_size()
}

fn max_extrinsic_weight() -> Weight {
<T::Chain as Chain>::max_extrinsic_weight()
}
}

/// Minimal parachain representation that may be used from no_std environment.
pub trait Parachain: Chain {
/// Parachain identifier.
const PARACHAIN_ID: u32;
}

impl<T> Parachain for T
where
T: Chain + UnderlyingChainProvider,
<T as UnderlyingChainProvider>::Chain: Parachain,
{
const PARACHAIN_ID: u32 = <<T as UnderlyingChainProvider>::Chain as Parachain>::PARACHAIN_ID;
}

/// Underlying chain type.
pub type UnderlyingChainOf<C> = <C as UnderlyingChainProvider>::Chain;

/// Block number used by the chain.
pub type BlockNumberOf<C> = <C as Chain>::BlockNumber;

Expand Down
3 changes: 2 additions & 1 deletion primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ use sp_std::{convert::TryFrom, fmt::Debug, vec, vec::Vec};

pub use chain::{
AccountIdOf, AccountPublicOf, BalanceOf, BlockNumberOf, Chain, EncodedOrDecodedCall, HashOf,
HasherOf, HeaderOf, IndexOf, Parachain, SignatureOf, TransactionEraOf,
HasherOf, HeaderOf, IndexOf, Parachain, SignatureOf, TransactionEraOf, UnderlyingChainOf,
UnderlyingChainProvider,
};
pub use frame_support::storage::storage_prefix as storage_value_final_key;
use num_traits::{CheckedSub, One};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ impl SubstrateParachainsPipeline for RialtoParachainsToMillau {
type TargetChain = Millau;

type SubmitParachainHeadsCallBuilder = RialtoParachainsToMillauSubmitParachainHeadsCallBuilder;

const SOURCE_PARACHAIN_PARA_ID: u32 = bp_rialto_parachain::RIALTO_PARACHAIN_ID;
}

/// `submit_parachain_heads` call builder for Rialto-to-Millau parachains sync pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ impl SubstrateParachainsPipeline for BridgeHubRococoToBridgeHubWococo {
type TargetChain = relay_bridge_hub_wococo_client::BridgeHubWococo;

type SubmitParachainHeadsCallBuilder = BridgeHubRococoToBridgeHubWococoCallBuilder;

const SOURCE_PARACHAIN_PARA_ID: u32 = bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID;
}

pub struct BridgeHubRococoToBridgeHubWococoCallBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ impl SubstrateParachainsPipeline for WestendParachainsToMillau {
type TargetChain = Millau;

type SubmitParachainHeadsCallBuilder = WestendParachainsToMillauSubmitParachainHeadsCallBuilder;

const SOURCE_PARACHAIN_PARA_ID: u32 = bp_westend::WESTMINT_PARACHAIN_ID;
}

/// `submit_parachain_heads` call builder for Rialto-to-Millau parachains sync pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ impl SubstrateParachainsPipeline for BridgeHubWococoToBridgeHubRococo {
type TargetChain = relay_bridge_hub_rococo_client::BridgeHubRococo;

type SubmitParachainHeadsCallBuilder = BridgeHubWococoToBridgeHubRococoCallBuilder;

const SOURCE_PARACHAIN_PARA_ID: u32 = bp_bridge_hub_wococo::BRIDGE_HUB_WOCOCO_PARACHAIN_ID;
}

pub struct BridgeHubWococoToBridgeHubRococoCallBuilder;
Expand Down
9 changes: 7 additions & 2 deletions relays/bin-substrate/src/cli/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use crate::cli::CliChain;
use pallet_bridge_parachains::{RelayBlockHash, RelayBlockHasher, RelayBlockNumber};
use parachains_relay::ParachainsPipeline;
use relay_substrate_client::{AccountKeyPairOf, Chain, ChainWithTransactions, RelayChain};
use relay_substrate_client::{
AccountKeyPairOf, Chain, ChainWithTransactions, Parachain, RelayChain,
};
use strum::{EnumString, EnumVariantNames};
use substrate_relay_helper::{
finality::SubstrateFinalitySyncPipeline, messages_lane::SubstrateMessageLane,
Expand Down Expand Up @@ -76,7 +78,10 @@ pub trait RelayToRelayHeadersCliBridge: CliBridgeBase {

/// Bridge representation that can be used from the CLI for relaying headers
/// from a parachain to a relay chain.
pub trait ParachainToRelayHeadersCliBridge: CliBridgeBase {
pub trait ParachainToRelayHeadersCliBridge: CliBridgeBase
where
Self::Source: Parachain,
{
// The `CliBridgeBase` type represents the parachain in this situation.
// We need to add an extra type for the relay chain.
type SourceRelay: Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash, Hasher = RelayBlockHasher>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use bp_messages::LaneId;
use bp_runtime::{BalanceOf, BlockNumberOf};
use relay_substrate_client::{
AccountIdOf, AccountKeyPairOf, Chain, ChainWithBalances, ChainWithTransactions, Client,
Parachain,
};
use relay_utils::metrics::MetricsParams;
use sp_core::Pair;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use crate::cli::{
use bp_polkadot_core::parachains::ParaHash;
use bp_runtime::BlockNumberOf;
use pallet_bridge_parachains::{RelayBlockHash, RelayBlockHasher, RelayBlockNumber};
use relay_substrate_client::{AccountIdOf, AccountKeyPairOf, Chain, ChainWithTransactions, Client};
use relay_substrate_client::{
AccountIdOf, AccountKeyPairOf, Chain, ChainWithTransactions, Client, Parachain,
};
use sp_core::Pair;
use substrate_relay_helper::{
finality::SubstrateFinalitySyncPipeline,
Expand All @@ -42,7 +44,10 @@ use substrate_relay_helper::{
pub struct ParachainToParachainBridge<
L2R: MessagesCliBridge + ParachainToRelayHeadersCliBridge,
R2L: MessagesCliBridge + ParachainToRelayHeadersCliBridge,
> {
> where
<L2R as CliBridgeBase>::Source: Parachain,
<R2L as CliBridgeBase>::Source: Parachain,
{
/// Parameters that are shared by all bridge types.
pub common:
Full2WayBridgeCommonParams<<R2L as CliBridgeBase>::Target, <L2R as CliBridgeBase>::Target>,
Expand Down Expand Up @@ -118,9 +123,9 @@ macro_rules! declare_parachain_to_parachain_bridge_schema {

impl [<$left_parachain $right_parachain HeadersAndMessages>] {
async fn into_bridge<
Left: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Left>>,
Left: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Left>> + Parachain,
LeftRelay: CliChain,
Right: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Right>>,
Right: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Right>> + Parachain,
RightRelay: CliChain,
L2R: CliBridgeBase<Source = Left, Target = Right>
+ MessagesCliBridge
Expand Down Expand Up @@ -172,10 +177,14 @@ macro_rules! declare_parachain_to_parachain_bridge_schema {

#[async_trait]
impl<
Left: Chain<Hash = ParaHash> + ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Left>>,
Left: Chain<Hash = ParaHash>
+ ChainWithTransactions
+ CliChain<KeyPair = AccountKeyPairOf<Left>>
+ Parachain,
Right: Chain<Hash = ParaHash>
+ ChainWithTransactions
+ CliChain<KeyPair = AccountKeyPairOf<Right>>,
+ CliChain<KeyPair = AccountKeyPairOf<Right>>
+ Parachain,
LeftRelay: Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash, Hasher = RelayBlockHasher>
+ CliChain,
RightRelay: Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash, Hasher = RelayBlockHasher>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ use crate::cli::{
use bp_polkadot_core::parachains::ParaHash;
use bp_runtime::BlockNumberOf;
use pallet_bridge_parachains::{RelayBlockHash, RelayBlockHasher, RelayBlockNumber};
use relay_substrate_client::{AccountIdOf, AccountKeyPairOf, Chain, ChainWithTransactions, Client};
use relay_substrate_client::{
AccountIdOf, AccountKeyPairOf, Chain, ChainWithTransactions, Client, Parachain,
};
use sp_core::Pair;
use substrate_relay_helper::{
finality::SubstrateFinalitySyncPipeline,
Expand All @@ -45,7 +47,9 @@ use substrate_relay_helper::{
pub struct RelayToParachainBridge<
L2R: MessagesCliBridge + RelayToRelayHeadersCliBridge,
R2L: MessagesCliBridge + ParachainToRelayHeadersCliBridge,
> {
> where
<R2L as CliBridgeBase>::Source: Parachain,
{
/// Parameters that are shared by all bridge types.
pub common:
Full2WayBridgeCommonParams<<R2L as CliBridgeBase>::Target, <L2R as CliBridgeBase>::Target>,
Expand Down Expand Up @@ -112,7 +116,7 @@ macro_rules! declare_relay_to_parachain_bridge_schema {
impl [<$left_chain $right_parachain HeadersAndMessages>] {
async fn into_bridge<
Left: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Left>>,
Right: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Right>>,
Right: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Right>> + Parachain,
RightRelay: CliChain,
L2R: CliBridgeBase<Source = Left, Target = Right> + MessagesCliBridge + RelayToRelayHeadersCliBridge,
R2L: CliBridgeBase<Source = Right, Target = Left>
Expand Down Expand Up @@ -165,7 +169,8 @@ impl<
Left: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Left>>,
Right: Chain<Hash = ParaHash>
+ ChainWithTransactions
+ CliChain<KeyPair = AccountKeyPairOf<Right>>,
+ CliChain<KeyPair = AccountKeyPairOf<Right>>
+ Parachain,
RightRelay: Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash, Hasher = RelayBlockHasher>
+ CliChain,
L2R: CliBridgeBase<Source = Left, Target = Right>
Expand Down
10 changes: 8 additions & 2 deletions relays/bin-substrate/src/cli/relay_parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use bp_polkadot_core::parachains::ParaId;
use parachains_relay::parachains_loop::{
AvailableHeader, ParachainSyncParams, SourceClient, TargetClient,
};
use relay_substrate_client::{Parachain, ParachainBase};
use relay_utils::metrics::{GlobalMetrics, StandaloneMetric};
use std::sync::Arc;
use structopt::StructOpt;
Expand All @@ -38,7 +39,11 @@ use substrate_relay_helper::{
TransactionParams,
};

use crate::cli::{bridge::ParachainToRelayHeadersCliBridge, chain_schema::*, PrometheusParams};
use crate::cli::{
bridge::{CliBridgeBase, ParachainToRelayHeadersCliBridge},
chain_schema::*,
PrometheusParams,
};

/// Start parachain heads relayer process.
#[derive(StructOpt)]
Expand Down Expand Up @@ -74,6 +79,7 @@ where
SourceClient<ParachainsPipelineAdapter<Self::ParachainFinality>>,
ParachainsTarget<Self::ParachainFinality>:
TargetClient<ParachainsPipelineAdapter<Self::ParachainFinality>>,
<Self as CliBridgeBase>::Source: Parachain,
{
async fn relay_headers(data: RelayParachains) -> anyhow::Result<()> {
let source_client = data.source.into_client::<Self::SourceRelay>().await?;
Expand All @@ -100,7 +106,7 @@ where
target_client,
ParachainSyncParams {
parachains: vec![
ParaId(<Self::ParachainFinality as SubstrateParachainsPipeline>::SOURCE_PARACHAIN_PARA_ID)
ParaId(<Self::ParachainFinality as SubstrateParachainsPipeline>::SourceParachain::PARACHAIN_ID)
],
stall_timeout: std::time::Duration::from_secs(60),
strategy: parachains_relay::parachains_loop::ParachainSyncStrategy::Any,
Expand Down
26 changes: 5 additions & 21 deletions relays/client-bridge-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

//! Types used to connect to the BridgeHub-Rococo-Substrate parachain.

use bp_messages::{MessageNonce, Weight};
use bp_messages::MessageNonce;
use codec::Encode;
use relay_substrate_client::{
Chain, ChainBase, ChainWithBalances, ChainWithMessages, ChainWithTransactions,
Error as SubstrateError, SignParam, UnsignedTransaction,
Chain, ChainWithBalances, ChainWithMessages, ChainWithTransactions, Error as SubstrateError,
SignParam, UnderlyingChainProvider, UnsignedTransaction,
};
use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
Expand All @@ -34,24 +34,8 @@ pub use runtime_wrapper as runtime;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BridgeHubRococo;

impl ChainBase for BridgeHubRococo {
type BlockNumber = bp_bridge_hub_rococo::BlockNumber;
type Hash = bp_bridge_hub_rococo::Hash;
type Hasher = bp_bridge_hub_rococo::Hashing;
type Header = bp_bridge_hub_rococo::Header;

type AccountId = bp_bridge_hub_rococo::AccountId;
type Balance = bp_bridge_hub_rococo::Balance;
type Index = bp_bridge_hub_rococo::Nonce;
type Signature = bp_bridge_hub_rococo::Signature;

fn max_extrinsic_size() -> u32 {
bp_bridge_hub_rococo::BridgeHubRococo::max_extrinsic_size()
}

fn max_extrinsic_weight() -> Weight {
bp_bridge_hub_rococo::BridgeHubRococo::max_extrinsic_weight()
}
impl UnderlyingChainProvider for BridgeHubRococo {
type Chain = bp_bridge_hub_rococo::BridgeHubRococo;
}

impl Chain for BridgeHubRococo {
Expand Down
26 changes: 5 additions & 21 deletions relays/client-bridge-hub-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

//! Types used to connect to the BridgeHub-Wococo-Substrate parachain.

use bp_messages::{MessageNonce, Weight};
use bp_messages::MessageNonce;
use codec::Encode;
use relay_substrate_client::{
Chain, ChainBase, ChainWithBalances, ChainWithMessages, ChainWithTransactions,
Error as SubstrateError, SignParam, UnsignedTransaction,
Chain, ChainWithBalances, ChainWithMessages, ChainWithTransactions, Error as SubstrateError,
SignParam, UnderlyingChainProvider, UnsignedTransaction,
};
use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
Expand All @@ -34,24 +34,8 @@ pub use runtime_wrapper as runtime;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BridgeHubWococo;

impl ChainBase for BridgeHubWococo {
type BlockNumber = bp_bridge_hub_wococo::BlockNumber;
type Hash = bp_bridge_hub_wococo::Hash;
type Hasher = bp_bridge_hub_wococo::Hashing;
type Header = bp_bridge_hub_wococo::Header;

type AccountId = bp_bridge_hub_wococo::AccountId;
type Balance = bp_bridge_hub_wococo::Balance;
type Index = bp_bridge_hub_wococo::Nonce;
type Signature = bp_bridge_hub_wococo::Signature;

fn max_extrinsic_size() -> u32 {
bp_bridge_hub_wococo::BridgeHubWococo::max_extrinsic_size()
}

fn max_extrinsic_weight() -> Weight {
bp_bridge_hub_wococo::BridgeHubWococo::max_extrinsic_weight()
}
impl UnderlyingChainProvider for BridgeHubWococo {
type Chain = bp_bridge_hub_wococo::BridgeHubWococo;
}

impl Chain for BridgeHubWococo {
Expand Down
Loading

0 comments on commit af9abbe

Please sign in to comment.