Westend<>Rococo Headers Relay (#875)

* 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 <tomasz@parity.io>
This commit is contained in:
Hernando Castano
2021-04-15 11:00:35 -04:00
committed by Bastian Köcher
parent a69026af44
commit bca83fd020
18 changed files with 388 additions and 110 deletions
@@ -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};
@@ -44,6 +44,8 @@ arg_enum! {
MillauToRialto,
RialtoToMillau,
WestendToMillau,
WestendToRococo,
RococoToWestend,
}
}
@@ -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
}
}
@@ -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()
@@ -42,6 +42,8 @@ arg_enum! {
MillauToRialto,
RialtoToMillau,
WestendToMillau,
WestendToRococo,
RococoToWestend,
}
}
@@ -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
}
}
@@ -21,6 +21,7 @@
//! 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},
@@ -28,7 +29,6 @@ use bp_header_chain::{
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;
@@ -20,7 +20,9 @@ pub mod millau_headers_to_rialto;
pub mod millau_messages_to_rialto;
pub mod rialto_headers_to_millau;
pub mod rialto_messages_to_millau;
pub mod rococo_headers_to_westend;
pub mod westend_headers_to_millau;
pub mod westend_headers_to_rococo;
use crate::cli::{
bridge,
@@ -32,9 +34,44 @@ use frame_support::weights::{GetDispatchInfo, Weight};
use pallet_bridge_dispatch::{CallOrigin, MessagePayload};
use relay_millau_client::Millau;
use relay_rialto_client::Rialto;
use relay_rococo_client::Rococo;
use relay_utils::metrics::{FloatJsonValueMetric, MetricsParams};
use relay_westend_client::Westend;
use sp_version::RuntimeVersion;
pub(crate) fn add_polkadot_kusama_price_metrics<T: finality_relay::FinalitySyncPipeline>(
params: MetricsParams,
) -> anyhow::Result<MetricsParams> {
Ok(
relay_utils::relay_metrics(Some(finality_relay::metrics_prefix::<T>()), params)
// Polkadot/Kusama prices are added as metrics here, because atm we don't have Polkadot <-> Kusama
// relays, but we want to test metrics/dashboards in advance
.standalone_metric(|registry, prefix| {
FloatJsonValueMetric::new(
registry,
prefix,
"https://api.coingecko.com/api/v3/simple/price?ids=Polkadot&vs_currencies=usd".into(),
"$.polkadot.usd".into(),
"polkadot_price".into(),
"Polkadot price in USD".into(),
)
})
.map_err(|e| anyhow::format_err!("{}", e))?
.standalone_metric(|registry, prefix| {
FloatJsonValueMetric::new(
registry,
prefix,
"https://api.coingecko.com/api/v3/simple/price?ids=Kusama&vs_currencies=usd".into(),
"$.kusama.usd".into(),
"kusama_price".into(),
"Kusama price in USD".into(),
)
})
.map_err(|e| anyhow::format_err!("{}", e))?
.into_params(),
)
}
impl CliEncodeCall for Millau {
fn max_extrinsic_size() -> u32 {
bp_millau::max_extrinsic_size()
@@ -197,6 +234,25 @@ impl CliChain for Westend {
}
}
impl CliChain for Rococo {
const RUNTIME_VERSION: RuntimeVersion = bp_rococo::VERSION;
type KeyPair = sp_core::sr25519::Pair;
type MessagePayload = ();
fn ss58_format() -> u16 {
42
}
fn max_extrinsic_weight() -> Weight {
0
}
fn encode_message(_message: encode_message::MessagePayload) -> Result<Self::MessagePayload, String> {
Err("Sending messages from Rococo is not yet supported.".into())
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -373,3 +429,89 @@ mod tests {
);
}
}
#[cfg(test)]
mod rococo_tests {
use bp_header_chain::justification::GrandpaJustification;
use codec::Encode;
#[test]
fn scale_compatibility_of_bridges_call() {
// given
let header = sp_runtime::generic::Header {
parent_hash: Default::default(),
number: Default::default(),
state_root: Default::default(),
extrinsics_root: Default::default(),
digest: sp_runtime::generic::Digest { logs: vec![] },
};
let justification = GrandpaJustification {
round: 0,
commit: finality_grandpa::Commit {
target_hash: Default::default(),
target_number: Default::default(),
precommits: vec![],
},
votes_ancestries: vec![],
};
let actual = bp_rococo::BridgeGrandpaWestendCall::submit_finality_proof(header.clone(), justification.clone());
let expected = millau_runtime::BridgeGrandpaRialtoCall::<millau_runtime::Runtime>::submit_finality_proof(
header,
justification,
);
// when
let actual_encoded = actual.encode();
let expected_encoded = expected.encode();
// then
assert_eq!(
actual_encoded, expected_encoded,
"Encoding difference. Raw: {:?} vs {:?}",
actual, expected
);
}
}
#[cfg(test)]
mod westend_tests {
use bp_header_chain::justification::GrandpaJustification;
use codec::Encode;
#[test]
fn scale_compatibility_of_bridges_call() {
// given
let header = sp_runtime::generic::Header {
parent_hash: Default::default(),
number: Default::default(),
state_root: Default::default(),
extrinsics_root: Default::default(),
digest: sp_runtime::generic::Digest { logs: vec![] },
};
let justification = GrandpaJustification {
round: 0,
commit: finality_grandpa::Commit {
target_hash: Default::default(),
target_number: Default::default(),
precommits: vec![],
},
votes_ancestries: vec![],
};
let actual = bp_westend::BridgeGrandpaRococoCall::submit_finality_proof(header.clone(), justification.clone());
let expected = millau_runtime::BridgeGrandpaRialtoCall::<millau_runtime::Runtime>::submit_finality_proof(
header,
justification,
);
// when
let actual_encoded = actual.encode();
let expected_encoded = expected.encode();
// then
assert_eq!(
actual_encoded, expected_encoded,
"Encoding difference. Raw: {:?} vs {:?}",
actual, expected
);
}
}
@@ -0,0 +1,60 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
//! Rococo-to-Westend headers sync entrypoint.
use crate::finality_pipeline::{SubstrateFinalitySyncPipeline, SubstrateFinalityToSubstrate};
use bp_header_chain::justification::GrandpaJustification;
use codec::Encode;
use relay_rococo_client::{Rococo, SyncHeader as RococoSyncHeader};
use relay_substrate_client::{Chain, TransactionSignScheme};
use relay_utils::metrics::MetricsParams;
use relay_westend_client::{SigningParams as WestendSigningParams, Westend};
use sp_core::{Bytes, Pair};
/// Rococo-to-Westend finality sync pipeline.
pub(crate) type RococoFinalityToWestend = SubstrateFinalityToSubstrate<Rococo, Westend, WestendSigningParams>;
impl SubstrateFinalitySyncPipeline for RococoFinalityToWestend {
const BEST_FINALIZED_SOURCE_HEADER_ID_AT_TARGET: &'static str = bp_rococo::BEST_FINALIZED_ROCOCO_HEADER_METHOD;
type TargetChain = Westend;
fn customize_metrics(params: MetricsParams) -> anyhow::Result<MetricsParams> {
crate::rialto_millau::add_polkadot_kusama_price_metrics::<Self>(params)
}
fn transactions_author(&self) -> bp_westend::AccountId {
(*self.target_sign.public().as_array_ref()).into()
}
fn make_submit_finality_proof_transaction(
&self,
transaction_nonce: <Westend as Chain>::Index,
header: RococoSyncHeader,
proof: GrandpaJustification<bp_rococo::Header>,
) -> Bytes {
let call = bp_westend::Call::BridgeGrandpaRococo(bp_westend::BridgeGrandpaRococoCall::submit_finality_proof(
header.into_inner(),
proof,
));
let genesis_hash = *self.target_client.genesis_hash();
let transaction = Westend::sign_transaction(genesis_hash, &self.target_sign, transaction_nonce, call);
Bytes(transaction.encode())
}
}
@@ -22,7 +22,7 @@ use bp_header_chain::justification::GrandpaJustification;
use codec::Encode;
use relay_millau_client::{Millau, SigningParams as MillauSigningParams};
use relay_substrate_client::{Chain, TransactionSignScheme};
use relay_utils::metrics::{FloatJsonValueMetric, MetricsParams};
use relay_utils::metrics::MetricsParams;
use relay_westend_client::{SyncHeader as WestendSyncHeader, Westend};
use sp_core::{Bytes, Pair};
@@ -35,34 +35,7 @@ impl SubstrateFinalitySyncPipeline for WestendFinalityToMillau {
type TargetChain = Millau;
fn customize_metrics(params: MetricsParams) -> anyhow::Result<MetricsParams> {
Ok(
relay_utils::relay_metrics(Some(finality_relay::metrics_prefix::<Self>()), params)
// Polkadot/Kusama prices are added as metrics here, because atm we don't have Polkadot <-> Kusama
// relays, but we want to test metrics/dashboards in advance
.standalone_metric(|registry, prefix| {
FloatJsonValueMetric::new(
registry,
prefix,
"https://api.coingecko.com/api/v3/simple/price?ids=Polkadot&vs_currencies=usd".into(),
"$.polkadot.usd".into(),
"polkadot_price".into(),
"Polkadot price in USD".into(),
)
})
.map_err(|e| anyhow::format_err!("{}", e))?
.standalone_metric(|registry, prefix| {
FloatJsonValueMetric::new(
registry,
prefix,
"https://api.coingecko.com/api/v3/simple/price?ids=Kusama&vs_currencies=usd".into(),
"$.kusama.usd".into(),
"kusama_price".into(),
"Kusama price in USD".into(),
)
})
.map_err(|e| anyhow::format_err!("{}", e))?
.into_params(),
)
crate::rialto_millau::add_polkadot_kusama_price_metrics::<Self>(params)
}
fn transactions_author(&self) -> bp_millau::AccountId {
@@ -0,0 +1,60 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
//! Westend-to-Rococo headers sync entrypoint.
use crate::finality_pipeline::{SubstrateFinalitySyncPipeline, SubstrateFinalityToSubstrate};
use bp_header_chain::justification::GrandpaJustification;
use codec::Encode;
use relay_rococo_client::{Rococo, SigningParams as RococoSigningParams};
use relay_substrate_client::{Chain, TransactionSignScheme};
use relay_utils::metrics::MetricsParams;
use relay_westend_client::{SyncHeader as WestendSyncHeader, Westend};
use sp_core::{Bytes, Pair};
/// Westend-to-Rococo finality sync pipeline.
pub(crate) type WestendFinalityToRococo = SubstrateFinalityToSubstrate<Westend, Rococo, RococoSigningParams>;
impl SubstrateFinalitySyncPipeline for WestendFinalityToRococo {
const BEST_FINALIZED_SOURCE_HEADER_ID_AT_TARGET: &'static str = bp_westend::BEST_FINALIZED_WESTEND_HEADER_METHOD;
type TargetChain = Rococo;
fn customize_metrics(params: MetricsParams) -> anyhow::Result<MetricsParams> {
crate::rialto_millau::add_polkadot_kusama_price_metrics::<Self>(params)
}
fn transactions_author(&self) -> bp_rococo::AccountId {
(*self.target_sign.public().as_array_ref()).into()
}
fn make_submit_finality_proof_transaction(
&self,
transaction_nonce: <Rococo as Chain>::Index,
header: WestendSyncHeader,
proof: GrandpaJustification<bp_westend::Header>,
) -> Bytes {
let call = bp_rococo::Call::BridgeGrandpaWestend(bp_rococo::BridgeGrandpaWestendCall::submit_finality_proof(
header.into_inner(),
proof,
));
let genesis_hash = *self.target_client.genesis_hash();
let transaction = Rococo::sign_transaction(genesis_hash, &self.target_sign, transaction_nonce, call);
Bytes(transaction.encode())
}
}