Skip to content

Commit

Permalink
Add equivocation detection pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
serban300 committed Aug 10, 2023
1 parent c79631c commit f09f758
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions relays/client-kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ bp-kusama = { path = "../../primitives/chain-kusama" }
# Substrate Dependencies

sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" }
3 changes: 3 additions & 0 deletions relays/client-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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.
Expand Down Expand Up @@ -50,6 +51,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 {
Expand Down
1 change: 1 addition & 0 deletions relays/client-millau/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,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" }
3 changes: 3 additions & 0 deletions relays/client-millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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.
Expand Down Expand Up @@ -60,6 +61,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 {
Expand Down
1 change: 1 addition & 0 deletions relays/client-polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ bp-polkadot = { path = "../../primitives/chain-polkadot" }
# Substrate Dependencies

sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" }
3 changes: 3 additions & 0 deletions relays/client-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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.
Expand Down Expand Up @@ -50,6 +51,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 {
Expand Down
1 change: 1 addition & 0 deletions relays/client-rialto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,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" }
3 changes: 3 additions & 0 deletions relays/client-rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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.
Expand All @@ -51,6 +52,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 {
Expand Down
1 change: 1 addition & 0 deletions relays/client-rococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ bp-rococo = { path = "../../primitives/chain-rococo" }
# Substrate Dependencies

sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-session = { git = "https://github.com/paritytech/substrate", branch = "master" }
3 changes: 3 additions & 0 deletions relays/client-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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.
Expand Down Expand Up @@ -50,6 +51,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 {
Expand Down
3 changes: 3 additions & 0 deletions relays/client-substrate/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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.
Expand Down
1 change: 1 addition & 0 deletions relays/client-westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,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" }
3 changes: 3 additions & 0 deletions relays/client-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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.
Expand Down Expand Up @@ -50,6 +51,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 {
Expand Down
1 change: 1 addition & 0 deletions relays/client-wococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,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" }
3 changes: 3 additions & 0 deletions relays/client-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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.
Expand Down Expand Up @@ -50,6 +51,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 {
Expand Down
1 change: 1 addition & 0 deletions relays/lib-substrate-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
83 changes: 83 additions & 0 deletions relays/lib-substrate-relay/src/equivocation/mod.rs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

//! 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<Self>;
}

type EquivocationProofOf<P> = <<P as SubstrateFinalityPipeline>::FinalityEngine as Engine<
<P as SubstrateFinalityPipeline>::SourceChain,
>>::EquivocationProof;
type KeyOwnerProofOf<P> = <<P as SubstrateFinalityPipeline>::FinalityEngine as Engine<
<P as SubstrateFinalityPipeline>::SourceChain,
>>::KeyOwnerProof;

/// Different ways of building `report_equivocation` calls.
pub trait ReportEquivocationCallBuilder<P: SubstrateEquivocationDetectionPipeline> {
/// Build a `report_equivocation` call to be executed on the source chain.
fn build_report_equivocation_call(
equivocation_proof: EquivocationProofOf<P>,
key_owner_proof: KeyOwnerProofOf<P>,
) -> CallOf<P::SourceChain>;
}

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

impl<P, R> ReportEquivocationCallBuilder<P> for DirectReportGrandpaEquivocationCallBuilder<P, R>
where
P: SubstrateEquivocationDetectionPipeline,
P::FinalityEngine: Engine<
P::SourceChain,
EquivocationProof = sp_consensus_grandpa::EquivocationProof<
HashOf<P::SourceChain>,
BlockNumberOf<P::SourceChain>,
>,
>,
R: frame_system::Config<Hash = HashOf<P::SourceChain>>
+ GrandpaConfig<KeyOwnerProof = KeyOwnerProofOf<P>>,
<R::Block as Block>::Header: Header<Number = BlockNumberOf<P::SourceChain>>,
CallOf<P::SourceChain>: From<GrandpaCall<R>>,
{
fn build_report_equivocation_call(
equivocation_proof: EquivocationProofOf<P>,
key_owner_proof: KeyOwnerProofOf<P>,
) -> CallOf<P::SourceChain> {
GrandpaCall::<R>::report_equivocation {
equivocation_proof: Box::new(equivocation_proof),
key_owner_proof,
}
.into()
}
}
4 changes: 1 addition & 3 deletions relays/lib-substrate-relay/src/finality/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ pub(crate) const RECENT_FINALITY_PROOFS_LIMIT: usize = 4096;

/// Substrate -> Substrate finality proofs synchronization pipeline.
#[async_trait]
pub trait SubstrateFinalitySyncPipeline:
'static + SubstrateFinalityPipeline + Clone + Debug + Send + Sync
{
pub trait SubstrateFinalitySyncPipeline: SubstrateFinalityPipeline {
/// How submit finality proof call is built?
type SubmitFinalityProofCallBuilder: SubmitFinalityProofCallBuilder<Self>;

Expand Down
6 changes: 6 additions & 0 deletions relays/lib-substrate-relay/src/finality_base/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub trait Engine<C: Chain>: Send {
type ConsensusLogReader: ConsensusLogReader;
/// Type of finality proofs, used by consensus engine.
type FinalityProof: FinalityProof<BlockNumberOf<C>> + 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.
Expand Down Expand Up @@ -137,6 +141,8 @@ impl<C: ChainWithGrandpa> Engine<C> for Grandpa<C> {
const ID: ConsensusEngineId = GRANDPA_ENGINE_ID;
type ConsensusLogReader = GrandpaConsensusLogReader<<C::Header as Header>::Number>;
type FinalityProof = GrandpaJustification<HeaderOf<C>>;
type EquivocationProof = sp_consensus_grandpa::EquivocationProof<HashOf<C>, BlockNumberOf<C>>;
type KeyOwnerProof = C::KeyOwnerProof;
type InitializationData = bp_header_chain::InitializationData<C::Header>;
type OperatingMode = BasicOperatingMode;

Expand Down
1 change: 1 addition & 0 deletions relays/lib-substrate-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use relay_substrate_client::{Chain, ChainWithUtilityPallet, UtilityPallet};

use std::marker::PhantomData;

pub mod equivocation;
pub mod error;
pub mod finality;
pub mod finality_base;
Expand Down

0 comments on commit f09f758

Please sign in to comment.