From 4dbc7cbf4fdb9b76ce3854f0259f44796f73e5de Mon Sep 17 00:00:00 2001 From: Serban Iorga Date: Fri, 11 Aug 2023 12:35:54 +0300 Subject: [PATCH] Add basic equivocation detection pipeline schema (#2338) (#2341) * Move finality Engine to finality_base folder * Define SubstrateFinalityPipeline Extract basic parts of SubstrateFinalitySyncPipeline into SubstrateFinalityPipeline * Add equivocation detection pipeline * Fix comment --- .../kusama_headers_to_bridge_hub_polkadot.rs | 9 +- .../polkadot_headers_to_bridge_hub_kusama.rs | 9 +- .../rialto_millau/millau_headers_to_rialto.rs | 11 ++- .../rialto_millau/rialto_headers_to_millau.rs | 11 ++- .../millau_headers_to_rialto_parachain.rs | 10 ++- .../rococo_headers_to_bridge_hub_wococo.rs | 9 +- .../wococo_headers_to_bridge_hub_rococo.rs | 9 +- .../westend_headers_to_millau.rs | 11 ++- .../bin-substrate/src/cli/init_bridge.rs | 2 +- bridges/relays/client-kusama/Cargo.toml | 1 + bridges/relays/client-kusama/src/lib.rs | 3 + bridges/relays/client-millau/Cargo.toml | 1 + bridges/relays/client-millau/src/lib.rs | 3 + bridges/relays/client-polkadot/Cargo.toml | 1 + bridges/relays/client-polkadot/src/lib.rs | 3 + bridges/relays/client-rialto/Cargo.toml | 1 + bridges/relays/client-rialto/src/lib.rs | 3 + bridges/relays/client-rococo/Cargo.toml | 1 + bridges/relays/client-rococo/src/lib.rs | 3 + bridges/relays/client-substrate/src/chain.rs | 3 + bridges/relays/client-westend/Cargo.toml | 1 + bridges/relays/client-westend/src/lib.rs | 3 + bridges/relays/client-wococo/Cargo.toml | 1 + bridges/relays/client-wococo/src/lib.rs | 3 + bridges/relays/lib-substrate-relay/Cargo.toml | 1 + .../src/equivocation/mod.rs | 83 +++++++++++++++++++ .../src/finality/initialize.rs | 2 +- .../lib-substrate-relay/src/finality/mod.rs | 22 ++--- .../src/finality/source.rs | 13 +-- .../src/finality/target.rs | 3 +- .../src/{finality => finality_base}/engine.rs | 6 ++ .../src/finality_base/mod.rs | 35 ++++++++ bridges/relays/lib-substrate-relay/src/lib.rs | 2 + .../src/on_demand/headers.rs | 2 +- 34 files changed, 234 insertions(+), 47 deletions(-) create mode 100644 bridges/relays/lib-substrate-relay/src/equivocation/mod.rs rename bridges/relays/lib-substrate-relay/src/{finality => finality_base}/engine.rs (97%) create mode 100644 bridges/relays/lib-substrate-relay/src/finality_base/mod.rs diff --git a/bridges/relays/bin-substrate/src/bridges/kusama_polkadot/kusama_headers_to_bridge_hub_polkadot.rs b/bridges/relays/bin-substrate/src/bridges/kusama_polkadot/kusama_headers_to_bridge_hub_polkadot.rs index 1cfaf922692f6..271aa5c6777fc 100644 --- a/bridges/relays/bin-substrate/src/bridges/kusama_polkadot/kusama_headers_to_bridge_hub_polkadot.rs +++ b/bridges/relays/bin-substrate/src/bridges/kusama_polkadot/kusama_headers_to_bridge_hub_polkadot.rs @@ -21,7 +21,8 @@ use crate::cli::bridge::{CliBridgeBase, RelayToRelayHeadersCliBridge}; use async_trait::async_trait; use relay_substrate_client::{AccountKeyPairOf, Client}; use substrate_relay_helper::{ - finality::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline}, + finality::SubstrateFinalitySyncPipeline, + finality_base::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalityPipeline}, TransactionParams, }; @@ -37,11 +38,15 @@ substrate_relay_helper::generate_submit_finality_proof_call_builder!( ); #[async_trait] -impl SubstrateFinalitySyncPipeline for KusamaFinalityToBridgeHubPolkadot { +impl SubstrateFinalityPipeline for KusamaFinalityToBridgeHubPolkadot { type SourceChain = relay_kusama_client::Kusama; type TargetChain = relay_bridge_hub_polkadot_client::BridgeHubPolkadot; type FinalityEngine = GrandpaFinalityEngine; +} + +#[async_trait] +impl SubstrateFinalitySyncPipeline for KusamaFinalityToBridgeHubPolkadot { type SubmitFinalityProofCallBuilder = KusamaFinalityToBridgeHubPolkadotCallBuilder; async fn start_relay_guards( diff --git a/bridges/relays/bin-substrate/src/bridges/kusama_polkadot/polkadot_headers_to_bridge_hub_kusama.rs b/bridges/relays/bin-substrate/src/bridges/kusama_polkadot/polkadot_headers_to_bridge_hub_kusama.rs index 6827c24768cb8..44d7ee94e3d21 100644 --- a/bridges/relays/bin-substrate/src/bridges/kusama_polkadot/polkadot_headers_to_bridge_hub_kusama.rs +++ b/bridges/relays/bin-substrate/src/bridges/kusama_polkadot/polkadot_headers_to_bridge_hub_kusama.rs @@ -21,7 +21,8 @@ use crate::cli::bridge::{CliBridgeBase, RelayToRelayHeadersCliBridge}; use async_trait::async_trait; use relay_substrate_client::{AccountKeyPairOf, Client}; use substrate_relay_helper::{ - finality::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline}, + finality::SubstrateFinalitySyncPipeline, + finality_base::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalityPipeline}, TransactionParams, }; @@ -37,11 +38,15 @@ substrate_relay_helper::generate_submit_finality_proof_call_builder!( ); #[async_trait] -impl SubstrateFinalitySyncPipeline for PolkadotFinalityToBridgeHubKusama { +impl SubstrateFinalityPipeline for PolkadotFinalityToBridgeHubKusama { type SourceChain = relay_polkadot_client::Polkadot; type TargetChain = relay_bridge_hub_kusama_client::BridgeHubKusama; type FinalityEngine = GrandpaFinalityEngine; +} + +#[async_trait] +impl SubstrateFinalitySyncPipeline for PolkadotFinalityToBridgeHubKusama { type SubmitFinalityProofCallBuilder = PolkadotFinalityToBridgeHubKusamaCallBuilder; async fn start_relay_guards( diff --git a/bridges/relays/bin-substrate/src/bridges/rialto_millau/millau_headers_to_rialto.rs b/bridges/relays/bin-substrate/src/bridges/rialto_millau/millau_headers_to_rialto.rs index f805b29c6a943..490ded1f67cb9 100644 --- a/bridges/relays/bin-substrate/src/bridges/rialto_millau/millau_headers_to_rialto.rs +++ b/bridges/relays/bin-substrate/src/bridges/rialto_millau/millau_headers_to_rialto.rs @@ -17,20 +17,23 @@ //! Millau-to-Rialto headers sync entrypoint. use crate::cli::bridge::{CliBridgeBase, MessagesCliBridge, RelayToRelayHeadersCliBridge}; -use substrate_relay_helper::finality::{ - engine::Grandpa as GrandpaFinalityEngine, DirectSubmitGrandpaFinalityProofCallBuilder, - SubstrateFinalitySyncPipeline, +use substrate_relay_helper::{ + finality::{DirectSubmitGrandpaFinalityProofCallBuilder, SubstrateFinalitySyncPipeline}, + finality_base::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalityPipeline}, }; /// Description of Millau -> Rialto finalized headers bridge. #[derive(Clone, Debug)] pub struct MillauFinalityToRialto; -impl SubstrateFinalitySyncPipeline for MillauFinalityToRialto { +impl SubstrateFinalityPipeline for MillauFinalityToRialto { type SourceChain = relay_millau_client::Millau; type TargetChain = relay_rialto_client::Rialto; type FinalityEngine = GrandpaFinalityEngine; +} + +impl SubstrateFinalitySyncPipeline for MillauFinalityToRialto { type SubmitFinalityProofCallBuilder = DirectSubmitGrandpaFinalityProofCallBuilder< Self, rialto_runtime::Runtime, diff --git a/bridges/relays/bin-substrate/src/bridges/rialto_millau/rialto_headers_to_millau.rs b/bridges/relays/bin-substrate/src/bridges/rialto_millau/rialto_headers_to_millau.rs index 7c979f5279567..c6ac05011f301 100644 --- a/bridges/relays/bin-substrate/src/bridges/rialto_millau/rialto_headers_to_millau.rs +++ b/bridges/relays/bin-substrate/src/bridges/rialto_millau/rialto_headers_to_millau.rs @@ -17,20 +17,23 @@ //! Rialto-to-Millau headers sync entrypoint. use crate::cli::bridge::{CliBridgeBase, MessagesCliBridge, RelayToRelayHeadersCliBridge}; -use substrate_relay_helper::finality::{ - engine::Grandpa as GrandpaFinalityEngine, DirectSubmitGrandpaFinalityProofCallBuilder, - SubstrateFinalitySyncPipeline, +use substrate_relay_helper::{ + finality::{DirectSubmitGrandpaFinalityProofCallBuilder, SubstrateFinalitySyncPipeline}, + finality_base::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalityPipeline}, }; /// Description of Millau -> Rialto finalized headers bridge. #[derive(Clone, Debug)] pub struct RialtoFinalityToMillau; -impl SubstrateFinalitySyncPipeline for RialtoFinalityToMillau { +impl SubstrateFinalityPipeline for RialtoFinalityToMillau { type SourceChain = relay_rialto_client::Rialto; type TargetChain = relay_millau_client::Millau; type FinalityEngine = GrandpaFinalityEngine; +} + +impl SubstrateFinalitySyncPipeline for RialtoFinalityToMillau { type SubmitFinalityProofCallBuilder = DirectSubmitGrandpaFinalityProofCallBuilder< Self, millau_runtime::Runtime, diff --git a/bridges/relays/bin-substrate/src/bridges/rialto_parachain_millau/millau_headers_to_rialto_parachain.rs b/bridges/relays/bin-substrate/src/bridges/rialto_parachain_millau/millau_headers_to_rialto_parachain.rs index d1c090c0797e0..79d3bde22fa75 100644 --- a/bridges/relays/bin-substrate/src/bridges/rialto_parachain_millau/millau_headers_to_rialto_parachain.rs +++ b/bridges/relays/bin-substrate/src/bridges/rialto_parachain_millau/millau_headers_to_rialto_parachain.rs @@ -35,8 +35,9 @@ //! Millau-to-RialtoParachain headers sync entrypoint. use crate::cli::bridge::{CliBridgeBase, MessagesCliBridge, RelayToRelayHeadersCliBridge}; -use substrate_relay_helper::finality::{ - engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline, +use substrate_relay_helper::{ + finality::SubstrateFinalitySyncPipeline, + finality_base::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalityPipeline}, }; substrate_relay_helper::generate_submit_finality_proof_call_builder!( @@ -50,11 +51,14 @@ substrate_relay_helper::generate_submit_finality_proof_call_builder!( #[derive(Clone, Debug)] pub struct MillauFinalityToRialtoParachain; -impl SubstrateFinalitySyncPipeline for MillauFinalityToRialtoParachain { +impl SubstrateFinalityPipeline for MillauFinalityToRialtoParachain { type SourceChain = relay_millau_client::Millau; type TargetChain = relay_rialto_parachain_client::RialtoParachain; type FinalityEngine = GrandpaFinalityEngine; +} + +impl SubstrateFinalitySyncPipeline for MillauFinalityToRialtoParachain { type SubmitFinalityProofCallBuilder = MillauFinalityToRialtoParachainCallBuilder; } diff --git a/bridges/relays/bin-substrate/src/bridges/rococo_wococo/rococo_headers_to_bridge_hub_wococo.rs b/bridges/relays/bin-substrate/src/bridges/rococo_wococo/rococo_headers_to_bridge_hub_wococo.rs index b7d17d931f843..906340633ab4c 100644 --- a/bridges/relays/bin-substrate/src/bridges/rococo_wococo/rococo_headers_to_bridge_hub_wococo.rs +++ b/bridges/relays/bin-substrate/src/bridges/rococo_wococo/rococo_headers_to_bridge_hub_wococo.rs @@ -21,7 +21,8 @@ use crate::cli::bridge::{CliBridgeBase, RelayToRelayHeadersCliBridge}; use async_trait::async_trait; use relay_substrate_client::{AccountKeyPairOf, Client}; use substrate_relay_helper::{ - finality::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline}, + finality::SubstrateFinalitySyncPipeline, + finality_base::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalityPipeline}, TransactionParams, }; @@ -37,11 +38,15 @@ substrate_relay_helper::generate_submit_finality_proof_call_builder!( ); #[async_trait] -impl SubstrateFinalitySyncPipeline for RococoFinalityToBridgeHubWococo { +impl SubstrateFinalityPipeline for RococoFinalityToBridgeHubWococo { type SourceChain = relay_rococo_client::Rococo; type TargetChain = relay_bridge_hub_wococo_client::BridgeHubWococo; type FinalityEngine = GrandpaFinalityEngine; +} + +#[async_trait] +impl SubstrateFinalitySyncPipeline for RococoFinalityToBridgeHubWococo { type SubmitFinalityProofCallBuilder = RococoFinalityToBridgeHubWococoCallBuilder; async fn start_relay_guards( diff --git a/bridges/relays/bin-substrate/src/bridges/rococo_wococo/wococo_headers_to_bridge_hub_rococo.rs b/bridges/relays/bin-substrate/src/bridges/rococo_wococo/wococo_headers_to_bridge_hub_rococo.rs index 206c83b815314..418225d9bacab 100644 --- a/bridges/relays/bin-substrate/src/bridges/rococo_wococo/wococo_headers_to_bridge_hub_rococo.rs +++ b/bridges/relays/bin-substrate/src/bridges/rococo_wococo/wococo_headers_to_bridge_hub_rococo.rs @@ -21,7 +21,8 @@ use crate::cli::bridge::{CliBridgeBase, RelayToRelayHeadersCliBridge}; use async_trait::async_trait; use relay_substrate_client::{AccountKeyPairOf, Client}; use substrate_relay_helper::{ - finality::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline}, + finality::SubstrateFinalitySyncPipeline, + finality_base::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalityPipeline}, TransactionParams, }; @@ -37,11 +38,15 @@ substrate_relay_helper::generate_submit_finality_proof_call_builder!( ); #[async_trait] -impl SubstrateFinalitySyncPipeline for WococoFinalityToBridgeHubRococo { +impl SubstrateFinalityPipeline for WococoFinalityToBridgeHubRococo { type SourceChain = relay_wococo_client::Wococo; type TargetChain = relay_bridge_hub_rococo_client::BridgeHubRococo; type FinalityEngine = GrandpaFinalityEngine; +} + +#[async_trait] +impl SubstrateFinalitySyncPipeline for WococoFinalityToBridgeHubRococo { type SubmitFinalityProofCallBuilder = WococoFinalityToBridgeHubRococoCallBuilder; async fn start_relay_guards( diff --git a/bridges/relays/bin-substrate/src/bridges/westend_millau/westend_headers_to_millau.rs b/bridges/relays/bin-substrate/src/bridges/westend_millau/westend_headers_to_millau.rs index 2a253756c2b98..eadbc3057045a 100644 --- a/bridges/relays/bin-substrate/src/bridges/westend_millau/westend_headers_to_millau.rs +++ b/bridges/relays/bin-substrate/src/bridges/westend_millau/westend_headers_to_millau.rs @@ -17,20 +17,23 @@ //! Westend-to-Millau headers sync entrypoint. use crate::cli::bridge::{CliBridgeBase, RelayToRelayHeadersCliBridge}; -use substrate_relay_helper::finality::{ - engine::Grandpa as GrandpaFinalityEngine, DirectSubmitGrandpaFinalityProofCallBuilder, - SubstrateFinalitySyncPipeline, +use substrate_relay_helper::{ + finality::{DirectSubmitGrandpaFinalityProofCallBuilder, SubstrateFinalitySyncPipeline}, + finality_base::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalityPipeline}, }; /// Description of Westend -> Millau finalized headers bridge. #[derive(Clone, Debug)] pub struct WestendFinalityToMillau; -impl SubstrateFinalitySyncPipeline for WestendFinalityToMillau { +impl SubstrateFinalityPipeline for WestendFinalityToMillau { type SourceChain = relay_westend_client::Westend; type TargetChain = relay_millau_client::Millau; type FinalityEngine = GrandpaFinalityEngine; +} + +impl SubstrateFinalitySyncPipeline for WestendFinalityToMillau { type SubmitFinalityProofCallBuilder = DirectSubmitGrandpaFinalityProofCallBuilder< Self, millau_runtime::Runtime, diff --git a/bridges/relays/bin-substrate/src/cli/init_bridge.rs b/bridges/relays/bin-substrate/src/cli/init_bridge.rs index f297a8db6b44a..110d5e94f8b39 100644 --- a/bridges/relays/bin-substrate/src/cli/init_bridge.rs +++ b/bridges/relays/bin-substrate/src/cli/init_bridge.rs @@ -41,7 +41,7 @@ use relay_substrate_client::{AccountKeyPairOf, Chain, UnsignedTransaction}; use sp_core::Pair; use structopt::StructOpt; use strum::{EnumString, EnumVariantNames, VariantNames}; -use substrate_relay_helper::finality::engine::{Engine, Grandpa as GrandpaFinalityEngine}; +use substrate_relay_helper::finality_base::engine::{Engine, Grandpa as GrandpaFinalityEngine}; /// Initialize bridge pallet. #[derive(StructOpt)] diff --git a/bridges/relays/client-kusama/Cargo.toml b/bridges/relays/client-kusama/Cargo.toml index 302f530eb4e96..767aefa5f2789 100644 --- a/bridges/relays/client-kusama/Cargo.toml +++ b/bridges/relays/client-kusama/Cargo.toml @@ -17,3 +17,4 @@ bp-runtime = { path = "../../primitives/runtime" } # Substrate Dependencies sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/bridges/relays/client-kusama/src/lib.rs b/bridges/relays/client-kusama/src/lib.rs index 477ea1b108f2d..2ea7fa39d0f07 100644 --- a/bridges/relays/client-kusama/src/lib.rs +++ b/bridges/relays/client-kusama/src/lib.rs @@ -22,6 +22,7 @@ use relay_substrate_client::{ Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider, }; use sp_core::storage::StorageKey; +use sp_session::MembershipProof; use std::time::Duration; /// Kusama header id. @@ -52,6 +53,8 @@ impl Chain for Kusama { impl ChainWithGrandpa for Kusama { const SYNCED_HEADERS_GRANDPA_INFO_METHOD: &'static str = KUSAMA_SYNCED_HEADERS_GRANDPA_INFO_METHOD; + + type KeyOwnerProof = MembershipProof; } impl ChainWithBalances for Kusama { diff --git a/bridges/relays/client-millau/Cargo.toml b/bridges/relays/client-millau/Cargo.toml index fe5482e5b6721..1cb759c5e3118 100644 --- a/bridges/relays/client-millau/Cargo.toml +++ b/bridges/relays/client-millau/Cargo.toml @@ -24,3 +24,4 @@ frame-system = { git = "https://github.com/paritytech/substrate", branch = "mast pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/bridges/relays/client-millau/src/lib.rs b/bridges/relays/client-millau/src/lib.rs index 90def2d3bd5b6..b3101c980c557 100644 --- a/bridges/relays/client-millau/src/lib.rs +++ b/bridges/relays/client-millau/src/lib.rs @@ -27,6 +27,7 @@ use relay_substrate_client::{ }; use sp_core::{storage::StorageKey, Pair}; use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount}; +use sp_session::MembershipProof; use std::time::Duration; /// Millau header id. @@ -69,6 +70,8 @@ impl Chain for Millau { impl ChainWithGrandpa for Millau { const SYNCED_HEADERS_GRANDPA_INFO_METHOD: &'static str = MILLAU_SYNCED_HEADERS_GRANDPA_INFO_METHOD; + + type KeyOwnerProof = MembershipProof; } impl ChainWithBalances for Millau { diff --git a/bridges/relays/client-polkadot/Cargo.toml b/bridges/relays/client-polkadot/Cargo.toml index 0d3a30949cbbb..7da3499a16bda 100644 --- a/bridges/relays/client-polkadot/Cargo.toml +++ b/bridges/relays/client-polkadot/Cargo.toml @@ -17,3 +17,4 @@ bp-runtime = { path = "../../primitives/runtime" } # Substrate Dependencies sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/bridges/relays/client-polkadot/src/lib.rs b/bridges/relays/client-polkadot/src/lib.rs index a906944c8f498..6e39f985c6301 100644 --- a/bridges/relays/client-polkadot/src/lib.rs +++ b/bridges/relays/client-polkadot/src/lib.rs @@ -22,6 +22,7 @@ use relay_substrate_client::{ Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider, }; use sp_core::storage::StorageKey; +use sp_session::MembershipProof; use std::time::Duration; /// Polkadot header id. @@ -52,6 +53,8 @@ impl Chain for Polkadot { impl ChainWithGrandpa for Polkadot { const SYNCED_HEADERS_GRANDPA_INFO_METHOD: &'static str = POLKADOT_SYNCED_HEADERS_GRANDPA_INFO_METHOD; + + type KeyOwnerProof = MembershipProof; } impl ChainWithBalances for Polkadot { diff --git a/bridges/relays/client-rialto/Cargo.toml b/bridges/relays/client-rialto/Cargo.toml index c1ae0dfabe7ec..9b3a987387965 100644 --- a/bridges/relays/client-rialto/Cargo.toml +++ b/bridges/relays/client-rialto/Cargo.toml @@ -24,3 +24,4 @@ frame-support = { git = "https://github.com/paritytech/substrate", branch = "mas pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/bridges/relays/client-rialto/src/lib.rs b/bridges/relays/client-rialto/src/lib.rs index c12d27bc1d916..cc5371612c346 100644 --- a/bridges/relays/client-rialto/src/lib.rs +++ b/bridges/relays/client-rialto/src/lib.rs @@ -27,6 +27,7 @@ use relay_substrate_client::{ }; use sp_core::{storage::StorageKey, Pair}; use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount}; +use sp_session::MembershipProof; use std::time::Duration; /// Rialto header id. @@ -54,6 +55,8 @@ impl Chain for Rialto { impl ChainWithGrandpa for Rialto { const SYNCED_HEADERS_GRANDPA_INFO_METHOD: &'static str = RIALTO_SYNCED_HEADERS_GRANDPA_INFO_METHOD; + + type KeyOwnerProof = MembershipProof; } impl RelayChain for Rialto { diff --git a/bridges/relays/client-rococo/Cargo.toml b/bridges/relays/client-rococo/Cargo.toml index 2a5b556dce22c..c569ab4f27efd 100644 --- a/bridges/relays/client-rococo/Cargo.toml +++ b/bridges/relays/client-rococo/Cargo.toml @@ -17,3 +17,4 @@ bp-runtime = { path = "../../primitives/runtime" } # Substrate Dependencies sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/bridges/relays/client-rococo/src/lib.rs b/bridges/relays/client-rococo/src/lib.rs index c4221e0dd004b..bf2cdd772271b 100644 --- a/bridges/relays/client-rococo/src/lib.rs +++ b/bridges/relays/client-rococo/src/lib.rs @@ -22,6 +22,7 @@ use relay_substrate_client::{ Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider, }; use sp_core::storage::StorageKey; +use sp_session::MembershipProof; use std::time::Duration; /// Rococo header id. @@ -52,6 +53,8 @@ impl Chain for Rococo { impl ChainWithGrandpa for Rococo { const SYNCED_HEADERS_GRANDPA_INFO_METHOD: &'static str = ROCOCO_SYNCED_HEADERS_GRANDPA_INFO_METHOD; + + type KeyOwnerProof = MembershipProof; } impl ChainWithBalances for Rococo { diff --git a/bridges/relays/client-substrate/src/chain.rs b/bridges/relays/client-substrate/src/chain.rs index fe0ec3ebb0a36..fb99fc7af16f7 100644 --- a/bridges/relays/client-substrate/src/chain.rs +++ b/bridges/relays/client-substrate/src/chain.rs @@ -85,6 +85,9 @@ pub trait ChainWithGrandpa: Chain + UnderlyingChainWithGrandpaProvider { /// Keep in mind that this method is normally provided by the other chain, which is /// bridged with this chain. const SYNCED_HEADERS_GRANDPA_INFO_METHOD: &'static str; + + /// The type of the key owner proof used by the grandpa engine. + type KeyOwnerProof; } /// Substrate-based parachain from minimal relay-client point of view. diff --git a/bridges/relays/client-westend/Cargo.toml b/bridges/relays/client-westend/Cargo.toml index 351013bd86446..9bc7d7adc099b 100644 --- a/bridges/relays/client-westend/Cargo.toml +++ b/bridges/relays/client-westend/Cargo.toml @@ -17,3 +17,4 @@ bp-westend = { path = "../../primitives/chain-westend" } # Substrate Dependencies sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/bridges/relays/client-westend/src/lib.rs b/bridges/relays/client-westend/src/lib.rs index e76bf65b82d89..674f4c71e82f8 100644 --- a/bridges/relays/client-westend/src/lib.rs +++ b/bridges/relays/client-westend/src/lib.rs @@ -22,6 +22,7 @@ use relay_substrate_client::{ Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider, }; use sp_core::storage::StorageKey; +use sp_session::MembershipProof; use std::time::Duration; /// Westend header id. @@ -52,6 +53,8 @@ impl Chain for Westend { impl ChainWithGrandpa for Westend { const SYNCED_HEADERS_GRANDPA_INFO_METHOD: &'static str = WESTEND_SYNCED_HEADERS_GRANDPA_INFO_METHOD; + + type KeyOwnerProof = MembershipProof; } impl RelayChain for Westend { diff --git a/bridges/relays/client-wococo/Cargo.toml b/bridges/relays/client-wococo/Cargo.toml index 78a5afe17cabc..e545111c2f636 100644 --- a/bridges/relays/client-wococo/Cargo.toml +++ b/bridges/relays/client-wococo/Cargo.toml @@ -16,3 +16,4 @@ bp-wococo = { path = "../../primitives/chain-wococo" } # Substrate Dependencies sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/bridges/relays/client-wococo/src/lib.rs b/bridges/relays/client-wococo/src/lib.rs index e4d23a27dcd39..f447fa6542e58 100644 --- a/bridges/relays/client-wococo/src/lib.rs +++ b/bridges/relays/client-wococo/src/lib.rs @@ -22,6 +22,7 @@ use relay_substrate_client::{ Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider, }; use sp_core::storage::StorageKey; +use sp_session::MembershipProof; use std::time::Duration; /// Wococo header id. @@ -52,6 +53,8 @@ impl Chain for Wococo { impl ChainWithGrandpa for Wococo { const SYNCED_HEADERS_GRANDPA_INFO_METHOD: &'static str = WOCOCO_SYNCED_HEADERS_GRANDPA_INFO_METHOD; + + type KeyOwnerProof = MembershipProof; } impl ChainWithBalances for Wococo { diff --git a/bridges/relays/lib-substrate-relay/Cargo.toml b/bridges/relays/lib-substrate-relay/Cargo.toml index d07aa936b57c6..869b6e00122ef 100644 --- a/bridges/relays/lib-substrate-relay/Cargo.toml +++ b/bridges/relays/lib-substrate-relay/Cargo.toml @@ -43,6 +43,7 @@ bp-messages = { path = "../../primitives/messages" } frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" } frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" } +pallet-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-consensus-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/bridges/relays/lib-substrate-relay/src/equivocation/mod.rs b/bridges/relays/lib-substrate-relay/src/equivocation/mod.rs new file mode 100644 index 0000000000000..31668490f90c1 --- /dev/null +++ b/bridges/relays/lib-substrate-relay/src/equivocation/mod.rs @@ -0,0 +1,83 @@ +// Copyright 2019-2023 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 . + +//! Types and functions intended to ease adding of new Substrate -> Substrate +//! equivocation detection pipelines. + +use crate::finality_base::SubstrateFinalityPipeline; +use std::marker::PhantomData; + +use crate::finality_base::engine::Engine; +use async_trait::async_trait; +use bp_runtime::{BlockNumberOf, HashOf}; +use pallet_grandpa::{Call as GrandpaCall, Config as GrandpaConfig}; +use relay_substrate_client::CallOf; +use sp_runtime::traits::{Block, Header}; + +/// Substrate -> Substrate equivocation detection pipeline. +#[async_trait] +pub trait SubstrateEquivocationDetectionPipeline: SubstrateFinalityPipeline { + /// How the `report_equivocation` call is built ? + type ReportEquivocationCallBuilder: ReportEquivocationCallBuilder; +} + +type EquivocationProofOf

= <

::FinalityEngine as Engine< +

::SourceChain, +>>::EquivocationProof; +type KeyOwnerProofOf

= <

::FinalityEngine as Engine< +

::SourceChain, +>>::KeyOwnerProof; + +/// Different ways of building `report_equivocation` calls. +pub trait ReportEquivocationCallBuilder { + /// Build a `report_equivocation` call to be executed on the source chain. + fn build_report_equivocation_call( + equivocation_proof: EquivocationProofOf

, + key_owner_proof: KeyOwnerProofOf

, + ) -> CallOf; +} + +/// Building the `report_equivocation` call when having direct access to the target chain runtime. +pub struct DirectReportGrandpaEquivocationCallBuilder { + _phantom: PhantomData<(P, R)>, +} + +impl ReportEquivocationCallBuilder

for DirectReportGrandpaEquivocationCallBuilder +where + P: SubstrateEquivocationDetectionPipeline, + P::FinalityEngine: Engine< + P::SourceChain, + EquivocationProof = sp_consensus_grandpa::EquivocationProof< + HashOf, + BlockNumberOf, + >, + >, + R: frame_system::Config> + + GrandpaConfig>, + ::Header: Header>, + CallOf: From>, +{ + fn build_report_equivocation_call( + equivocation_proof: EquivocationProofOf

, + key_owner_proof: KeyOwnerProofOf

, + ) -> CallOf { + GrandpaCall::::report_equivocation { + equivocation_proof: Box::new(equivocation_proof), + key_owner_proof, + } + .into() + } +} diff --git a/bridges/relays/lib-substrate-relay/src/finality/initialize.rs b/bridges/relays/lib-substrate-relay/src/finality/initialize.rs index 1b990f5dbda3e..5dde46c39dd67 100644 --- a/bridges/relays/lib-substrate-relay/src/finality/initialize.rs +++ b/bridges/relays/lib-substrate-relay/src/finality/initialize.rs @@ -21,7 +21,7 @@ //! and authorities set from source to target chain. The finality sync starts //! with this header. -use crate::{error::Error, finality::engine::Engine}; +use crate::{error::Error, finality_base::engine::Engine}; use sp_core::Pair; use bp_runtime::HeaderIdOf; diff --git a/bridges/relays/lib-substrate-relay/src/finality/mod.rs b/bridges/relays/lib-substrate-relay/src/finality/mod.rs index 37ee61b48f1bd..bae49efe72e01 100644 --- a/bridges/relays/lib-substrate-relay/src/finality/mod.rs +++ b/bridges/relays/lib-substrate-relay/src/finality/mod.rs @@ -19,26 +19,25 @@ use crate::{ finality::{ - engine::Engine, source::{SubstrateFinalityProof, SubstrateFinalitySource}, target::SubstrateFinalityTarget, }, TransactionParams, }; +use crate::finality_base::{engine::Engine, SubstrateFinalityPipeline}; use async_trait::async_trait; use bp_header_chain::justification::GrandpaJustification; use finality_relay::FinalitySyncPipeline; use pallet_bridge_grandpa::{Call as BridgeGrandpaCall, Config as BridgeGrandpaConfig}; use relay_substrate_client::{ - transaction_stall_timeout, AccountIdOf, AccountKeyPairOf, BlockNumberOf, CallOf, Chain, - ChainWithTransactions, Client, HashOf, HeaderOf, SyncHeader, + transaction_stall_timeout, AccountIdOf, AccountKeyPairOf, BlockNumberOf, CallOf, Chain, Client, + HashOf, HeaderOf, SyncHeader, }; use relay_utils::metrics::MetricsParams; use sp_core::Pair; use std::{fmt::Debug, marker::PhantomData}; -pub mod engine; pub mod initialize; pub mod source; pub mod target; @@ -51,14 +50,7 @@ pub(crate) const RECENT_FINALITY_PROOFS_LIMIT: usize = 4096; /// Substrate -> Substrate finality proofs synchronization pipeline. #[async_trait] -pub trait SubstrateFinalitySyncPipeline: 'static + Clone + Debug + Send + Sync { - /// Headers of this chain are submitted to the `TargetChain`. - type SourceChain: Chain; - /// Headers of the `SourceChain` are submitted to this chain. - type TargetChain: ChainWithTransactions; - - /// Finality engine. - type FinalityEngine: Engine; +pub trait SubstrateFinalitySyncPipeline: SubstrateFinalityPipeline { /// How submit finality proof call is built? type SubmitFinalityProofCallBuilder: SubmitFinalityProofCallBuilder; @@ -144,16 +136,16 @@ macro_rules! generate_submit_finality_proof_call_builder { fn build_submit_finality_proof_call( header: relay_substrate_client::SyncHeader< relay_substrate_client::HeaderOf< - <$pipeline as $crate::finality::SubstrateFinalitySyncPipeline>::SourceChain + <$pipeline as $crate::finality_base::SubstrateFinalityPipeline>::SourceChain > >, proof: bp_header_chain::justification::GrandpaJustification< relay_substrate_client::HeaderOf< - <$pipeline as $crate::finality::SubstrateFinalitySyncPipeline>::SourceChain + <$pipeline as $crate::finality_base::SubstrateFinalityPipeline>::SourceChain > >, ) -> relay_substrate_client::CallOf< - <$pipeline as $crate::finality::SubstrateFinalitySyncPipeline>::TargetChain + <$pipeline as $crate::finality_base::SubstrateFinalityPipeline>::TargetChain > { bp_runtime::paste::item! { $bridge_grandpa($submit_finality_proof { diff --git a/bridges/relays/lib-substrate-relay/src/finality/source.rs b/bridges/relays/lib-substrate-relay/src/finality/source.rs index 6227263902ae6..208299f9cb2bf 100644 --- a/bridges/relays/lib-substrate-relay/src/finality/source.rs +++ b/bridges/relays/lib-substrate-relay/src/finality/source.rs @@ -16,8 +16,12 @@ //! Default generic implementation of finality source for basic Substrate client. -use crate::finality::{engine::Engine, FinalitySyncPipelineAdapter, SubstrateFinalitySyncPipeline}; +use crate::{ + finality::{FinalitySyncPipelineAdapter, SubstrateFinalitySyncPipeline}, + finality_base::engine::Engine, +}; +use crate::finality_base::SubstrateFinalityPipeline; use async_std::sync::{Arc, Mutex}; use async_trait::async_trait; use bp_header_chain::FinalityProof; @@ -42,10 +46,9 @@ pub type SubstrateFinalityProofsStream

= Pin> + Send>>; /// Substrate finality proof. Specific to the used `FinalityEngine`. -pub type SubstrateFinalityProof

= - <

::FinalityEngine as Engine< -

::SourceChain, - >>::FinalityProof; +pub type SubstrateFinalityProof

= <

::FinalityEngine as Engine< +

::SourceChain, +>>::FinalityProof; /// Substrate node as finality source. pub struct SubstrateFinalitySource { diff --git a/bridges/relays/lib-substrate-relay/src/finality/target.rs b/bridges/relays/lib-substrate-relay/src/finality/target.rs index 2d9d4b856e025..3d1fa780eaefb 100644 --- a/bridges/relays/lib-substrate-relay/src/finality/target.rs +++ b/bridges/relays/lib-substrate-relay/src/finality/target.rs @@ -18,9 +18,10 @@ use crate::{ finality::{ - engine::Engine, source::SubstrateFinalityProof, FinalitySyncPipelineAdapter, + source::SubstrateFinalityProof, FinalitySyncPipelineAdapter, SubmitFinalityProofCallBuilder, SubstrateFinalitySyncPipeline, }, + finality_base::engine::Engine, TransactionParams, }; diff --git a/bridges/relays/lib-substrate-relay/src/finality/engine.rs b/bridges/relays/lib-substrate-relay/src/finality_base/engine.rs similarity index 97% rename from bridges/relays/lib-substrate-relay/src/finality/engine.rs rename to bridges/relays/lib-substrate-relay/src/finality_base/engine.rs index 0092db969925c..dc84ebaa4d5c6 100644 --- a/bridges/relays/lib-substrate-relay/src/finality/engine.rs +++ b/bridges/relays/lib-substrate-relay/src/finality_base/engine.rs @@ -47,6 +47,10 @@ pub trait Engine: Send { type FinalityClient: SubstrateFinalityClient; /// Type of finality proofs, used by consensus engine. type FinalityProof: FinalityProof> + Decode + Encode; + /// The type of the equivocation proof used by the consensus engine. + type EquivocationProof; + /// The type of the key owner proof used by the consensus engine. + type KeyOwnerProof; /// Type of bridge pallet initialization data. type InitializationData: std::fmt::Debug + Send + Sync + 'static; /// Type of bridge pallet operating mode. @@ -138,6 +142,8 @@ impl Engine for Grandpa { type ConsensusLogReader = GrandpaConsensusLogReader<::Number>; type FinalityClient = SubstrateGrandpaFinalityClient; type FinalityProof = GrandpaJustification>; + type EquivocationProof = sp_consensus_grandpa::EquivocationProof, BlockNumberOf>; + type KeyOwnerProof = C::KeyOwnerProof; type InitializationData = bp_header_chain::InitializationData; type OperatingMode = BasicOperatingMode; diff --git a/bridges/relays/lib-substrate-relay/src/finality_base/mod.rs b/bridges/relays/lib-substrate-relay/src/finality_base/mod.rs new file mode 100644 index 0000000000000..e48f82480325a --- /dev/null +++ b/bridges/relays/lib-substrate-relay/src/finality_base/mod.rs @@ -0,0 +1,35 @@ +// Copyright 2019-2023 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 . + +//! Types and functions intended to ease adding of new Substrate -> Substrate +//! finality pipelines. + +pub mod engine; + +use async_trait::async_trait; +use relay_substrate_client::{Chain, ChainWithTransactions}; +use std::fmt::Debug; + +/// Substrate -> Substrate finality related pipeline. +#[async_trait] +pub trait SubstrateFinalityPipeline: 'static + Clone + Debug + Send + Sync { + /// Headers of this chain are submitted to the `TargetChain`. + type SourceChain: Chain; + /// Headers of the `SourceChain` are submitted to this chain. + type TargetChain: ChainWithTransactions; + /// Finality engine. + type FinalityEngine: engine::Engine; +} diff --git a/bridges/relays/lib-substrate-relay/src/lib.rs b/bridges/relays/lib-substrate-relay/src/lib.rs index f9bd80d50793c..50a89e90e2dcd 100644 --- a/bridges/relays/lib-substrate-relay/src/lib.rs +++ b/bridges/relays/lib-substrate-relay/src/lib.rs @@ -22,8 +22,10 @@ use relay_substrate_client::{Chain, ChainWithUtilityPallet, UtilityPallet}; use std::marker::PhantomData; +pub mod equivocation; pub mod error; pub mod finality; +pub mod finality_base; pub mod messages_lane; pub mod messages_metrics; pub mod messages_source; diff --git a/bridges/relays/lib-substrate-relay/src/on_demand/headers.rs b/bridges/relays/lib-substrate-relay/src/on_demand/headers.rs index 1dfc544703c88..5014851a5b2f3 100644 --- a/bridges/relays/lib-substrate-relay/src/on_demand/headers.rs +++ b/bridges/relays/lib-substrate-relay/src/on_demand/headers.rs @@ -38,11 +38,11 @@ use relay_utils::{ use crate::{ finality::{ - engine::Engine, source::{RequiredHeaderNumberRef, SubstrateFinalitySource}, target::SubstrateFinalityTarget, SubstrateFinalitySyncPipeline, RECENT_FINALITY_PROOFS_LIMIT, }, + finality_base::engine::Engine, on_demand::OnDemandRelay, TransactionParams, };