Skip to content

Commit

Permalink
Substrate relay stub (paritytech#376)
Browse files Browse the repository at this point in the history
* substrate-relay: initial commit

* MillauHeaderApi and RialtoHeaderApi

* post-merge fixes + TODOs + compilation
  • Loading branch information
svyatonik authored and serban300 committed Apr 8, 2024
1 parent b2db3ac commit 14d2aae
Show file tree
Hide file tree
Showing 22 changed files with 652 additions and 71 deletions.
2 changes: 2 additions & 0 deletions bridges/bin/millau-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository = "https://github.com/paritytech/parity-bridges-common/"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
bp-millau = { path = "../../primitives/millau", default-features = false }
hex-literal = "0.3"

[dependencies.codec]
Expand Down Expand Up @@ -217,6 +218,7 @@ git = "https://github.com/paritytech/substrate/"
default = ["std"]
std = [
"bp-message-lane/std",
"bp-millau/std",
"codec/std",
"frame-benchmarking/std",
"frame-executive/std",
Expand Down
4 changes: 2 additions & 2 deletions bridges/bin/millau-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub use sp_runtime::BuildStorage;
pub use sp_runtime::{Perbill, Permill};

/// An index to a block.
pub type BlockNumber = u32;
pub type BlockNumber = bp_millau::BlockNumber;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;
Expand All @@ -81,7 +81,7 @@ pub type Balance = u128;
pub type Index = u32;

/// A hash of some data used by the chain.
pub type Hash = sp_core::H256;
pub type Hash = bp_millau::Hash;

/// Digest item type.
pub type DigestItem = generic::DigestItem<Hash>;
Expand Down
4 changes: 4 additions & 0 deletions bridges/bin/rialto-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ repository = "https://github.com/paritytech/parity-bridges-common/"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
bp-millau = { path = "../../primitives/millau", default-features = false }
bp-rialto = { path = "../../primitives/rialto", default-features = false }
hex-literal = "0.3"

[dependencies.codec]
Expand Down Expand Up @@ -256,6 +258,8 @@ std = [
"bp-currency-exchange/std",
"bp-eth-poa/std",
"bp-message-lane/std",
"bp-millau/std",
"bp-rialto/std",
"codec/std",
"frame-benchmarking/std",
"frame-executive/std",
Expand Down
21 changes: 19 additions & 2 deletions bridges/bin/rialto-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub use sp_runtime::BuildStorage;
pub use sp_runtime::{Perbill, Permill};

/// An index to a block.
pub type BlockNumber = u32;
pub type BlockNumber = bp_rialto::BlockNumber;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;
Expand All @@ -90,7 +90,7 @@ pub type Balance = u128;
pub type Index = u32;

/// A hash of some data used by the chain.
pub type Hash = sp_core::H256;
pub type Hash = bp_rialto::Hash;

/// Digest item type.
pub type DigestItem = generic::DigestItem<Hash>;
Expand Down Expand Up @@ -574,6 +574,23 @@ impl_runtime_apis! {
}
}

impl bp_millau::MillauHeaderApi<Block> for Runtime {
fn best_block() -> (bp_millau::BlockNumber, bp_millau::Hash) {
unimplemented!("https://github.com/paritytech/parity-bridges-common/issues/368")
}

fn finalized_block() -> (bp_millau::BlockNumber, bp_millau::Hash) {
unimplemented!("https://github.com/paritytech/parity-bridges-common/issues/368")
}

fn incomplete_headers() -> Vec<(bp_millau::BlockNumber, bp_millau::Hash)> {
unimplemented!("https://github.com/paritytech/parity-bridges-common/issues/368")
}

fn is_known_block(_hash: bp_millau::Hash) -> bool {
unimplemented!("https://github.com/paritytech/parity-bridges-common/issues/368")
}
}

impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {
fn validate_transaction(
Expand Down
22 changes: 22 additions & 0 deletions bridges/primitives/millau/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "bp-millau"
description = "Primitives of Millau runtime."
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]

# Substrate Based Dependencies
sp-api = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", tag = 'v2.0.0', default-features = false }
sp-core = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", tag = 'v2.0.0', default-features = false }
sp-std = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", tag = 'v2.0.0', default-features = false }

[features]
default = ["std"]
std = [
"sp-api/std",
"sp-core/std",
"sp-std/std",
]
49 changes: 49 additions & 0 deletions bridges/primitives/millau/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019-2020 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/>.

#![cfg_attr(not(feature = "std"), no_std)]
// RuntimeApi generated functions
#![allow(clippy::too_many_arguments)]
// Runtime-generated DecodeLimit::decode_all_With_depth_limit
#![allow(clippy::unnecessary_mut_passed)]

use sp_std::prelude::*;

/// Block number type used in Millau.
pub type BlockNumber = u32;

/// Hash type used in Millau.
pub type Hash = sp_core::H256;

sp_api::decl_runtime_apis! {
/// API for querying information about Millau headers from the Bridge Pallet instance.
///
/// This API is implemented by runtimes that are bridging with Millau chain, not the
/// Millau runtime itself.
pub trait MillauHeaderApi {
/// Returns number and hash of the best block known to the bridge module.
///
/// The caller should only submit an `import_header` transaction that makes
/// (or leads to making) other header the best one.
fn best_block() -> (BlockNumber, Hash);
/// Returns number and hash of the best finalized block known to the bridge module.
fn finalized_block() -> (BlockNumber, Hash);
/// Returns numbers and hashes of headers that require finality proofs.
fn incomplete_headers() -> Vec<(BlockNumber, Hash)>;
/// Returns true if header is known to the runtime.
fn is_known_block(hash: Hash) -> bool;
}
}
22 changes: 22 additions & 0 deletions bridges/primitives/rialto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "bp-rialto"
description = "Primitives of Rialto runtime."
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]

# Substrate Based Dependencies
sp-api = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", tag = 'v2.0.0', default-features = false }
sp-core = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", tag = 'v2.0.0', default-features = false }
sp-std = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", tag = 'v2.0.0', default-features = false }

[features]
default = ["std"]
std = [
"sp-api/std",
"sp-core/std",
"sp-std/std",
]
49 changes: 49 additions & 0 deletions bridges/primitives/rialto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019-2020 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/>.

#![cfg_attr(not(feature = "std"), no_std)]
// RuntimeApi generated functions
#![allow(clippy::too_many_arguments)]
// Runtime-generated DecodeLimit::decode_all_With_depth_limit
#![allow(clippy::unnecessary_mut_passed)]

use sp_std::prelude::*;

/// Block number type used in Rialto.
pub type BlockNumber = u32;

/// Hash type used in Rialto.
pub type Hash = sp_core::H256;

sp_api::decl_runtime_apis! {
/// API for querying information about Rialto headers from the Bridge Pallet instance.
///
/// This API is implemented by runtimes that are bridging with Rialto chain, not the
/// Rialto runtime itself.
pub trait RialtoHeaderApi {
/// Returns number and hash of the best block known to the bridge module.
///
/// The caller should only submit an `import_header` transaction that makes
/// (or leads to making) other header the best one.
fn best_block() -> (BlockNumber, Hash);
/// Returns number and hash of the best finalized block known to the bridge module.
fn finalized_block() -> (BlockNumber, Hash);
/// Returns numbers and hashes of headers that require finality proofs.
fn incomplete_headers() -> Vec<(BlockNumber, Hash)>;
/// Returns true if header is known to the runtime.
fn is_known_block(hash: Hash) -> bool;
}
}
63 changes: 6 additions & 57 deletions bridges/relays/ethereum/src/substrate_sync_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ use async_trait::async_trait;
use codec::Encode;
use headers_relay::{
sync::HeadersSyncParams,
sync_loop::{SourceClient, TargetClient},
sync_loop::TargetClient,
sync_types::{HeadersSyncPipeline, QueuedHeader, SourceHeader, SubmittedHeaders},
};
use relay_ethereum_client::{
types::Address, Client as EthereumClient, ConnectionParams as EthereumConnectionParams,
SigningParams as EthereumSigningParams,
};
use relay_rialto_client::{HeaderId as RialtoHeaderId, Rialto, SyncHeader as RialtoSyncHeader};
use relay_substrate_client::{Client as SubstrateClient, ConnectionParams as SubstrateConnectionParams};
use relay_substrate_client::{
headers_source::HeadersSource, Client as SubstrateClient, ConnectionParams as SubstrateConnectionParams,
};
use relay_utils::metrics::MetricsParams;
use sp_runtime::Justification;

Expand Down Expand Up @@ -93,61 +95,8 @@ impl HeadersSyncPipeline for SubstrateHeadersSyncPipeline {
/// Queued substrate header ID.
pub type QueuedRialtoHeader = QueuedHeader<SubstrateHeadersSyncPipeline>;

/// Substrate client as headers source.
struct SubstrateHeadersSource {
/// Substrate node client.
client: SubstrateClient<Rialto>,
}

impl SubstrateHeadersSource {
fn new(client: SubstrateClient<Rialto>) -> Self {
Self { client }
}
}

#[async_trait]
impl SourceClient<SubstrateHeadersSyncPipeline> for SubstrateHeadersSource {
type Error = RpcError;

async fn best_block_number(&self) -> Result<rialto_runtime::BlockNumber, Self::Error> {
Ok(self.client.best_header().await?.number)
}

async fn header_by_hash(&self, hash: rialto_runtime::Hash) -> Result<RialtoSyncHeader, Self::Error> {
self.client
.header_by_hash(hash)
.await
.map(Into::into)
.map_err(Into::into)
}

async fn header_by_number(&self, number: rialto_runtime::BlockNumber) -> Result<RialtoSyncHeader, Self::Error> {
self.client
.header_by_number(number)
.await
.map(Into::into)
.map_err(Into::into)
}

async fn header_completion(
&self,
id: RialtoHeaderId,
) -> Result<(RialtoHeaderId, Option<Justification>), Self::Error> {
let hash = id.1;
let signed_block = self.client.get_block(Some(hash)).await?;
let grandpa_justification = signed_block.justification;

Ok((id, grandpa_justification))
}

async fn header_extra(
&self,
id: RialtoHeaderId,
_header: QueuedRialtoHeader,
) -> Result<(RialtoHeaderId, ()), Self::Error> {
Ok((id, ()))
}
}
/// Rialto node as headers source.
type SubstrateHeadersSource = HeadersSource<Rialto, SubstrateHeadersSyncPipeline>;

/// Ethereum client as Substrate headers target.
struct EthereumHeadersTarget {
Expand Down
8 changes: 5 additions & 3 deletions bridges/relays/headers-relay/src/sync_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ pub enum HeaderStatus {
}

/// Headers synchronization pipeline.
pub trait HeadersSyncPipeline: Clone + Copy {
pub trait HeadersSyncPipeline: Clone + Copy + Send + Sync {
/// Name of the headers source.
const SOURCE_NAME: &'static str;
/// Name of the headers target.
const TARGET_NAME: &'static str;

/// Headers we're syncing are identified by this hash.
type Hash: Eq + Clone + Copy + std::fmt::Debug + std::fmt::Display + std::hash::Hash;
type Hash: Eq + Clone + Copy + Send + Sync + std::fmt::Debug + std::fmt::Display + std::hash::Hash;
/// Headers we're syncing are identified by this number.
type Number: From<u32>
+ Ord
+ Clone
+ Copy
+ Send
+ Sync
+ std::fmt::Debug
+ std::fmt::Display
+ std::hash::Hash
Expand All @@ -66,7 +68,7 @@ pub trait HeadersSyncPipeline: Clone + Copy {
+ num_traits::One
+ Into<u64>;
/// Type of header that we're syncing.
type Header: Clone + std::fmt::Debug + PartialEq + SourceHeader<Self::Hash, Self::Number>;
type Header: Clone + std::fmt::Debug + PartialEq + SourceHeader<Self::Hash, Self::Number> + Send + Sync;
/// Type of extra data for the header that we're receiving from the source node:
/// 1) extra data is required for some headers;
/// 2) target node may answer if it'll require extra data before header is submitted;
Expand Down
13 changes: 12 additions & 1 deletion bridges/relays/millau-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.4" }
headers-relay = { path = "../headers-relay" }
relay-substrate-client = { path = "../substrate-client" }
relay-utils = { path = "../utils" }

# Supported Chains

millau-runtime = { path = "../../bin/millau-runtime" }
substrate-client = { path = "../substrate-client" }

# Substrate Dependencies

frame-system = { version = "2.0.0", tag = 'v2.0.0', git = "https://github.com/paritytech/substrate.git" }
sp-runtime = { version = "2.0.0", tag = 'v2.0.0', git = "https://github.com/paritytech/substrate.git" }

0 comments on commit 14d2aae

Please sign in to comment.