Skip to content

Commit

Permalink
Westend<>Rococo Headers Relay (paritytech#875)
Browse files Browse the repository at this point in the history
* Add modules for Rococo<>Westend header sync

* Use mock Westend and Rococo finaltiy tx calls

* Add Westend<>Rococo variants to `init_bridge`

* Add Westend<>Rococo variants to `relay_headers`

* Simplify the Rococo and Westend signing params

* Add `submit_finality_proof` mock Call variant

* Add note to more closely match `initialize` Call variant

* Accidentally committed `cargo-expand`ed code 🤦

* Add `initialize` Call variant to Rococo mock

* Fix call enums.

* Add explainatory comment.

* clippy.

* Add issue number.

* De-duplicate metrics customisation.

* Add comments to Rococo/Westend runtimes.

* Add scale-encoding compatibility test.

* Fix tests.

* Clippy.

Co-authored-by: Tomasz Drwięga <[email protected]>
  • Loading branch information
2 people authored and serban300 committed Apr 10, 2024
1 parent 2824c41 commit 8fb2137
Show file tree
Hide file tree
Showing 18 changed files with 388 additions and 110 deletions.
2 changes: 2 additions & 0 deletions bridges/modules/dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use sp_std::{fmt::Debug, marker::PhantomData, prelude::*};
/// Spec version type.
pub type SpecVersion = u32;

// TODO [#895] move to primitives
/// Origin of a Call when it is dispatched on the target chain.
///
/// The source chain can (and should) verify that the message can be dispatched on the target chain
Expand Down Expand Up @@ -89,6 +90,7 @@ pub enum CallOrigin<SourceChainAccountId, TargetChainAccountPublic, TargetChainS
SourceAccount(SourceChainAccountId),
}

// TODO [#895] move to primitives
/// Message payload type used by dispatch module.
#[derive(RuntimeDebug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct MessagePayload<SourceChainAccountId, TargetChainAccountPublic, TargetChainSignature, Call> {
Expand Down
21 changes: 1 addition & 20 deletions bridges/modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@
use crate::weights::WeightInfo;

use bp_header_chain::justification::GrandpaJustification;
use bp_header_chain::InitializationData;
use bp_runtime::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf};
use codec::{Decode, Encode};
use finality_grandpa::voter_set::VoterSet;
use frame_support::ensure;
use frame_system::{ensure_signed, RawOrigin};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_finality_grandpa::{ConsensusLog, GRANDPA_ENGINE_ID};
use sp_runtime::traits::{BadOrigin, Header as HeaderT, Zero};
use sp_runtime::RuntimeDebug;

#[cfg(test)]
mod mock;
Expand Down Expand Up @@ -511,22 +508,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}
}

/// Data required for initializing the bridge pallet.
///
/// The bridge needs to know where to start its sync from, and this provides that initial context.
#[derive(Default, Encode, Decode, RuntimeDebug, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct InitializationData<H: HeaderT> {
/// The header from which we should start syncing.
pub header: H,
/// The initial authorities of the pallet.
pub authority_list: sp_finality_grandpa::AuthorityList,
/// The ID of the initial authority set.
pub set_id: sp_finality_grandpa::SetId,
/// Should the pallet block transaction immediately after initialization.
pub is_halted: bool,
}

pub(crate) fn find_scheduled_change<H: HeaderT>(header: &H) -> Option<sp_finality_grandpa::ScheduledChange<H::Number>> {
use sp_runtime::generic::OpaqueDigestItemId;

Expand Down
2 changes: 2 additions & 0 deletions bridges/primitives/chain-rococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
parity-scale-codec = { version = "2.0.0", default-features = false, features = ["derive"] }

# Bridge Dependencies
bp-header-chain = { path = "../header-chain", default-features = false }
bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }
Expand All @@ -23,6 +24,7 @@ sp-version = { git = "https://github.com/paritytech/substrate", branch = "master
[features]
default = ["std"]
std = [
"bp-header-chain/std",
"bp-messages/std",
"bp-polkadot-core/std",
"bp-runtime/std",
Expand Down
27 changes: 26 additions & 1 deletion bridges/primitives/chain-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![allow(clippy::unnecessary_mut_passed)]

use bp_messages::{LaneId, MessageNonce, UnrewardedRelayersState, Weight};
use bp_runtime::Chain;
use sp_std::prelude::*;
use sp_version::RuntimeVersion;

Expand All @@ -41,9 +42,33 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
transaction_version: 6,
};

/// Rococo Runtime `Call` enum.
///
/// The enum represents a subset of possible `Call`s we can send to Rococo chain.
/// Ideally this code would be auto-generated from Metadata, because we want to
/// avoid depending directly on the ENTIRE runtime just to get the encoding of `Dispatchable`s.
///
/// All entries here (like pretty much in the entire file) must be kept in sync with Rococo
/// `construct_runtime`, so that we maintain SCALE-compatibility.
///
/// See: https://github.com/paritytech/polkadot/blob/master/runtime/rococo/src/lib.rs
#[derive(parity_scale_codec::Encode, parity_scale_codec::Decode, Debug, PartialEq, Eq, Clone)]
pub enum Call {
MockPallet,
/// Westend bridge pallet.
#[codec(index = 40)]
BridgeGrandpaWestend(BridgeGrandpaWestendCall),
}

#[derive(parity_scale_codec::Encode, parity_scale_codec::Decode, Debug, PartialEq, Eq, Clone)]
#[allow(non_camel_case_types)]
pub enum BridgeGrandpaWestendCall {
#[codec(index = 0)]
submit_finality_proof(
<PolkadotLike as Chain>::Header,
bp_header_chain::justification::GrandpaJustification<<PolkadotLike as Chain>::Header>,
),
#[codec(index = 1)]
initialize(bp_header_chain::InitializationData<<PolkadotLike as Chain>::Header>),
}

impl sp_runtime::traits::Dispatchable for Call {
Expand Down
2 changes: 2 additions & 0 deletions bridges/primitives/chain-westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
parity-scale-codec = { version = "2.0.0", default-features = false, features = ["derive"] }

# Bridge Dependencies
bp-header-chain = { path = "../header-chain", default-features = false }
bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }
Expand All @@ -23,6 +24,7 @@ sp-version = { git = "https://github.com/paritytech/substrate", branch = "master
[features]
default = ["std"]
std = [
"bp-header-chain/std",
"bp-messages/std",
"bp-polkadot-core/std",
"bp-runtime/std",
Expand Down
27 changes: 26 additions & 1 deletion bridges/primitives/chain-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![allow(clippy::unnecessary_mut_passed)]

use bp_messages::{LaneId, MessageNonce, UnrewardedRelayersState, Weight};
use bp_runtime::Chain;
use sp_std::prelude::*;
use sp_version::RuntimeVersion;

Expand All @@ -42,9 +43,33 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
transaction_version: 5,
};

/// Westend Runtime `Call` enum.
///
/// The enum represents a subset of possible `Call`s we can send to Westend chain.
/// Ideally this code would be auto-generated from Metadata, because we want to
/// avoid depending directly on the ENTIRE runtime just to get the encoding of `Dispatchable`s.
///
/// All entries here (like pretty much in the entire file) must be kept in sync with Westend
/// `construct_runtime`, so that we maintain SCALE-compatibility.
///
/// See: https://github.com/paritytech/polkadot/blob/master/runtime/westend/src/lib.rs
#[derive(parity_scale_codec::Encode, parity_scale_codec::Decode, Debug, PartialEq, Eq, Clone)]
pub enum Call {
MockModule,
/// Rococo bridge pallet.
#[codec(index = 40)]
BridgeGrandpaRococo(BridgeGrandpaRococoCall),
}

#[derive(parity_scale_codec::Encode, parity_scale_codec::Decode, Debug, PartialEq, Eq, Clone)]
#[allow(non_camel_case_types)]
pub enum BridgeGrandpaRococoCall {
#[codec(index = 0)]
submit_finality_proof(
<PolkadotLike as Chain>::Header,
bp_header_chain::justification::GrandpaJustification<<PolkadotLike as Chain>::Header>,
),
#[codec(index = 1)]
initialize(bp_header_chain::InitializationData<<PolkadotLike as Chain>::Header>),
}

impl sp_runtime::traits::Dispatchable for Call {
Expand Down
16 changes: 16 additions & 0 deletions bridges/primitives/header-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ impl AuthoritySet {
}
}

/// Data required for initializing the bridge pallet.
///
/// The bridge needs to know where to start its sync from, and this provides that initial context.
#[derive(Default, Encode, Decode, RuntimeDebug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct InitializationData<H: HeaderT> {
/// The header from which we should start syncing.
pub header: H,
/// The initial authorities of the pallet.
pub authority_list: AuthorityList,
/// The ID of the initial authority set.
pub set_id: SetId,
/// Should the pallet block transaction immediately after initialization.
pub is_halted: bool,
}

/// base trait for verifying transaction inclusion proofs.
pub trait InclusionProofVerifier {
/// Transaction type.
Expand Down
3 changes: 2 additions & 1 deletion bridges/relays/bin-substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bp-messages = { path = "../../primitives/messages" }
bp-millau = { path = "../../primitives/chain-millau" }
bp-polkadot = { path = "../../primitives/chain-polkadot" }
bp-rialto = { path = "../../primitives/chain-rialto" }
bp-rococo = { path = "../../primitives/chain-rococo" }
bp-runtime = { path = "../../primitives/runtime" }
bp-westend = { path = "../../primitives/chain-westend" }
bridge-runtime-common = { path = "../../bin/runtime-common" }
Expand All @@ -35,12 +36,12 @@ headers-relay = { path = "../headers" }
messages-relay = { path = "../messages" }
millau-runtime = { path = "../../bin/millau/runtime" }
pallet-bridge-dispatch = { path = "../../modules/dispatch" }
pallet-bridge-grandpa = { path = "../../modules/grandpa" }
pallet-bridge-messages = { path = "../../modules/messages" }
relay-kusama-client = { path = "../client-kusama" }
relay-millau-client = { path = "../client-millau" }
relay-polkadot-client = { path = "../client-polkadot" }
relay-rialto-client = { path = "../client-rialto" }
relay-rococo-client = { path = "../client-rococo" }
relay-substrate-client = { path = "../client-substrate" }
relay-utils = { path = "../utils" }
relay-westend-client = { path = "../client-westend" }
Expand Down
28 changes: 27 additions & 1 deletion bridges/relays/bin-substrate/src/cli/init_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use crate::cli::{SourceConnectionParams, TargetConnectionParams, TargetSigningParams};
use bp_header_chain::InitializationData;
use bp_runtime::Chain as ChainBase;
use codec::Encode;
use pallet_bridge_grandpa::InitializationData;
use relay_substrate_client::{Chain, TransactionSignScheme};
use sp_core::{Bytes, Pair};
use structopt::{clap::arg_enum, StructOpt};
Expand All @@ -44,6 +44,8 @@ arg_enum! {
MillauToRialto,
RialtoToMillau,
WestendToMillau,
WestendToRococo,
RococoToWestend,
}
}

Expand Down Expand Up @@ -98,6 +100,30 @@ macro_rules! select_bridge {
.into()
}

$generic
}
InitBridgeName::WestendToRococo => {
type Source = relay_westend_client::Westend;
type Target = relay_rococo_client::Rococo;

fn encode_init_bridge(
init_data: InitializationData<<Source as ChainBase>::Header>,
) -> <Target as Chain>::Call {
bp_rococo::Call::BridgeGrandpaWestend(bp_rococo::BridgeGrandpaWestendCall::initialize(init_data))
}

$generic
}
InitBridgeName::RococoToWestend => {
type Source = relay_rococo_client::Rococo;
type Target = relay_westend_client::Westend;

fn encode_init_bridge(
init_data: InitializationData<<Source as ChainBase>::Header>,
) -> <Target as Chain>::Call {
bp_westend::Call::BridgeGrandpaRococo(bp_westend::BridgeGrandpaRococoCall::initialize(init_data))
}

$generic
}
}
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/bin-substrate/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ macro_rules! declare_chain_options {
/// Parse signing params into chain-specific KeyPair.
pub fn to_keypair<Chain: CliChain>(&self) -> anyhow::Result<Chain::KeyPair> {
use sp_core::crypto::Pair;

Chain::KeyPair::from_string(
&self.[<$chain_prefix _signer>],
self.[<$chain_prefix _signer_password>].as_deref()
Expand Down
16 changes: 16 additions & 0 deletions bridges/relays/bin-substrate/src/cli/relay_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ arg_enum! {
MillauToRialto,
RialtoToMillau,
WestendToMillau,
WestendToRococo,
RococoToWestend,
}
}

Expand All @@ -67,6 +69,20 @@ macro_rules! select_bridge {
type Target = relay_millau_client::Millau;
type Finality = crate::rialto_millau::westend_headers_to_millau::WestendFinalityToMillau;

$generic
}
RelayHeadersBridge::WestendToRococo => {
type Source = relay_westend_client::Westend;
type Target = relay_rococo_client::Rococo;
type Finality = crate::rialto_millau::westend_headers_to_rococo::WestendFinalityToRococo;

$generic
}
RelayHeadersBridge::RococoToWestend => {
type Source = relay_rococo_client::Rococo;
type Target = relay_westend_client::Westend;
type Finality = crate::rialto_millau::rococo_headers_to_westend::RococoFinalityToWestend;

$generic
}
}
Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/bin-substrate/src/headers_initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
//! and authorities set from source to target chain. The headers sync starts
//! with this header.

use bp_header_chain::InitializationData;
use bp_header_chain::{
find_grandpa_authorities_scheduled_change,
justification::{verify_justification, GrandpaJustification},
};
use codec::Decode;
use finality_grandpa::voter_set::VoterSet;
use num_traits::{One, Zero};
use pallet_bridge_grandpa::InitializationData;
use relay_substrate_client::{Chain, Client};
use sp_core::Bytes;
use sp_finality_grandpa::AuthorityList as GrandpaAuthoritiesSet;
Expand Down
Loading

0 comments on commit 8fb2137

Please sign in to comment.