Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain specific message lane apis #503

Merged
merged 5 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

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

6 changes: 2 additions & 4 deletions bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,7 @@ impl_runtime_apis! {
}
}

// TODO: runtime should support several chains (https://github.com/paritytech/parity-bridges-common/issues/457)
impl bp_message_lane::OutboundLaneApi<Block> for Runtime {
impl bp_rialto::ToRialtoOutboundLaneApi<Block> for Runtime {
fn messages_dispatch_weight(
lane: bp_message_lane::LaneId,
begin: bp_message_lane::MessageNonce,
Expand All @@ -566,8 +565,7 @@ impl_runtime_apis! {
}
}

// TODO: runtime should support several chains (https://github.com/paritytech/parity-bridges-common/issues/457)
impl bp_message_lane::InboundLaneApi<Block> for Runtime {
impl bp_rialto::FromRialtoInboundLaneApi<Block> for Runtime {
fn latest_received_nonce(lane: bp_message_lane::LaneId) -> bp_message_lane::MessageNonce {
BridgeRialtoMessageLane::inbound_latest_received_nonce(lane)
}
Expand Down
6 changes: 2 additions & 4 deletions bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,7 @@ impl_runtime_apis! {
}
}

// TODO: runtime should support several chains (https://github.com/paritytech/parity-bridges-common/issues/457)
impl bp_message_lane::OutboundLaneApi<Block> for Runtime {
impl bp_millau::ToMillauOutboundLaneApi<Block> for Runtime {
fn messages_dispatch_weight(
lane: bp_message_lane::LaneId,
begin: bp_message_lane::MessageNonce,
Expand All @@ -730,8 +729,7 @@ impl_runtime_apis! {
}
}

// TODO: runtime should support several chains (https://github.com/paritytech/parity-bridges-common/issues/457)
impl bp_message_lane::InboundLaneApi<Block> for Runtime {
impl bp_millau::FromMillauInboundLaneApi<Block> for Runtime {
fn latest_received_nonce(lane: bp_message_lane::LaneId) -> bp_message_lane::MessageNonce {
BridgeMillauMessageLane::inbound_latest_received_nonce(lane)
}
Expand Down
2 changes: 0 additions & 2 deletions primitives/message-lane/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ codec = { package = "parity-scale-codec", version = "1.3.1", default-features =
# Substrate Dependencies

frame-support = { version = "2.0", default-features = false }
sp-api = { version = "2.0", default-features = false }
sp-std = { version = "2.0", default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"sp-api/std",
"sp-std/std"
]
28 changes: 0 additions & 28 deletions primitives/message-lane/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

use codec::{Decode, Encode};
use frame_support::RuntimeDebug;
use sp_api::decl_runtime_apis;
use sp_std::{collections::vec_deque::VecDeque, prelude::*};

pub mod source_chain;
Expand Down Expand Up @@ -125,30 +124,3 @@ impl Default for OutboundLaneData {
}
}
}

decl_runtime_apis! {
/// Outbound message lane API.
pub trait OutboundLaneApi {
/// Returns dispatch weight of all messages in given inclusive range.
///
/// If some (or all) messages are missing from the storage, they'll also will
/// be missing from the resulting vector. The vector is ordered by the nonce.
fn messages_dispatch_weight(
lane: LaneId,
begin: MessageNonce,
end: MessageNonce,
) -> Vec<(MessageNonce, Weight)>;
/// Returns nonce of the latest message, received by bridged chain.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Returns nonce of the latest message, generated by given lane.
fn latest_generated_nonce(lane: LaneId) -> MessageNonce;
}

/// Inbound message lane API.
pub trait InboundLaneApi {
/// Returns nonce of the latest message, received by given lane.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Nonce of latest message that has been confirmed to the bridged chain.
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
}
}
45 changes: 44 additions & 1 deletion primitives/millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// Runtime-generated DecodeLimit::decode_all_With_depth_limit
#![allow(clippy::unnecessary_mut_passed)]

use bp_message_lane::MessageNonce;
use bp_message_lane::{LaneId, MessageNonce};
use bp_runtime::Chain;
use frame_support::{weights::Weight, RuntimeDebug};
use sp_core::Hasher as HasherT;
Expand Down Expand Up @@ -73,6 +73,18 @@ pub const IS_KNOWN_MILLAU_BLOCK_METHOD: &str = "MillauHeaderApi_is_known_block";
/// Name of the `MillauHeaderApi::incomplete_headers` runtime method.
pub const INCOMPLETE_MILLAU_HEADERS_METHOD: &str = "MillauHeaderApi_incomplete_headers";

/// Name of the `ToMillauOutboundLaneApi::messages_dispatch_weight` runtime method.
pub const TO_MILLAU_MESSAGES_DISPATCH_WEIGHT_METHOD: &str = "ToMillauOutboundLaneApi_messages_dispatch_weight";
/// Name of the `ToMillauOutboundLaneApi::latest_received_nonce` runtime method.
pub const TO_MILLAU_LATEST_RECEIVED_NONCE_METHOD: &str = "ToMillauOutboundLaneApi_latest_received_nonce";
/// Name of the `ToMillauOutboundLaneApi::latest_generated_nonce` runtime method.
pub const TO_MILLAU_LATEST_GENERATED_NONCE_METHOD: &str = "ToMillauOutboundLaneApi_latest_generated_nonce";

/// Name of the `FromMillauInboundLaneApi::latest_received_nonce` runtime method.
pub const FROM_MILLAU_LATEST_RECEIVED_NONCE_METHOD: &str = "FromMillauInboundLaneApi_latest_received_nonce";
/// Name of the `FromMillauInboundLaneApi::latest_onfirmed_nonce` runtime method.
pub const FROM_MILLAU_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromMillauInboundLaneApi_latest_confirmed_nonce";

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;

Expand Down Expand Up @@ -111,4 +123,35 @@ sp_api::decl_runtime_apis! {
/// Returns true if the header is considered finalized by the runtime.
fn is_finalized_block(hash: Hash) -> bool;
}

/// Outbound message lane API for messages that are sent to Millau chain.
///
/// This API is implemented by runtimes that are sending messages to Millau chain, not the
/// Millau runtime itself.
pub trait ToMillauOutboundLaneApi {
/// Returns dispatch weight of all messages in given inclusive range.
///
/// If some (or all) messages are missing from the storage, they'll also will
/// be missing from the resulting vector. The vector is ordered by the nonce.
fn messages_dispatch_weight(
lane: LaneId,
begin: MessageNonce,
end: MessageNonce,
) -> Vec<(MessageNonce, Weight)>;
/// Returns nonce of the latest message, received by bridged chain.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Returns nonce of the latest message, generated by given lane.
fn latest_generated_nonce(lane: LaneId) -> MessageNonce;
}

/// Inbound message lane API for messages sent by Millau chain.
///
/// This API is implemented by runtimes that are receiving messages from Millau chain, not the
/// Millau runtime itself.
pub trait FromMillauInboundLaneApi {
/// Returns nonce of the latest message, received by given lane.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Nonce of latest message that has been confirmed to the bridged chain.
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
}
}
45 changes: 44 additions & 1 deletion primitives/rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// Runtime-generated DecodeLimit::decode_all_With_depth_limit
#![allow(clippy::unnecessary_mut_passed)]

use bp_message_lane::MessageNonce;
use bp_message_lane::{LaneId, MessageNonce};
use bp_runtime::Chain;
use frame_support::{weights::Weight, RuntimeDebug};
use sp_core::Hasher as HasherT;
Expand Down Expand Up @@ -73,6 +73,18 @@ pub const IS_KNOWN_RIALTO_BLOCK_METHOD: &str = "RialtoHeaderApi_is_known_block";
/// Name of the `RialtoHeaderApi::incomplete_headers` runtime method.
pub const INCOMPLETE_RIALTO_HEADERS_METHOD: &str = "RialtoHeaderApi_incomplete_headers";

/// Name of the `ToRialtoOutboundLaneApi::messages_dispatch_weight` runtime method.
pub const TO_RIALTO_MESSAGES_DISPATCH_WEIGHT_METHOD: &str = "ToRialtoOutboundLaneApi_messages_dispatch_weight";
/// Name of the `ToRialtoOutboundLaneApi::latest_generated_nonce` runtime method.
pub const TO_RIALTO_LATEST_GENERATED_NONCE_METHOD: &str = "ToRialtoOutboundLaneApi_latest_generated_nonce";
/// Name of the `ToRialtoOutboundLaneApi::latest_received_nonce` runtime method.
pub const TO_RIALTO_LATEST_RECEIVED_NONCE_METHOD: &str = "ToRialtoOutboundLaneApi_latest_received_nonce";

/// Name of the `FromRialtoInboundLaneApi::latest_received_nonce` runtime method.
pub const FROM_RIALTO_LATEST_RECEIVED_NONCE_METHOD: &str = "FromRialtoInboundLaneApi_latest_received_nonce";
/// Name of the `FromRialtoInboundLaneApi::latest_onfirmed_nonce` runtime method.
pub const FROM_RIALTO_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromRialtoInboundLaneApi_latest_confirmed_nonce";

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;

Expand Down Expand Up @@ -111,4 +123,35 @@ sp_api::decl_runtime_apis! {
/// Returns true if the header is considered finalized by the runtime.
fn is_finalized_block(hash: Hash) -> bool;
}

/// Outbound message lane API for messages that are sent to Rialto chain.
///
/// This API is implemented by runtimes that are sending messages to Rialto chain, not the
/// Rialto runtime itself.
pub trait ToRialtoOutboundLaneApi {
/// Returns dispatch weight of all messages in given inclusive range.
///
/// If some (or all) messages are missing from the storage, they'll also will
/// be missing from the resulting vector. The vector is ordered by the nonce.
fn messages_dispatch_weight(
lane: LaneId,
begin: MessageNonce,
end: MessageNonce,
) -> Vec<(MessageNonce, Weight)>;
/// Returns nonce of the latest message, received by bridged chain.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Returns nonce of the latest message, generated by given lane.
fn latest_generated_nonce(lane: LaneId) -> MessageNonce;
}

/// Inbound message lane API for messages sent by Rialto chain.
///
/// This API is implemented by runtimes that are receiving messages from Rialto chain, not the
/// Rialto runtime itself.
pub trait FromRialtoInboundLaneApi {
/// Returns nonce of the latest message, received by given lane.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Nonce of latest message that has been confirmed to the bridged chain.
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
}
}
2 changes: 1 addition & 1 deletion relays/substrate/src/headers_maintain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! best finalized header on the target chain, we submit this justification to the target
//! node.

use crate::headers_target::SubstrateHeadersSyncPipeline;
use crate::headers_pipeline::SubstrateHeadersSyncPipeline;

use async_std::sync::{Arc, Mutex};
use async_trait::async_trait;
Expand Down
41 changes: 35 additions & 6 deletions relays/substrate/src/headers_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,50 @@

//! Substrate-to-Substrate headers sync entrypoint.

use crate::{
headers_maintain::SubstrateHeadersToSubstrateMaintain,
headers_target::{SubstrateHeadersSyncPipeline, SubstrateHeadersTarget},
};
use crate::{headers_maintain::SubstrateHeadersToSubstrateMaintain, headers_target::SubstrateHeadersTarget};

use async_trait::async_trait;
use codec::Encode;
use headers_relay::{
sync::{HeadersSyncParams, TargetTransactionMode},
sync_types::{HeadersSyncPipeline, QueuedHeader, SourceHeader},
sync_types::{HeaderIdOf, HeadersSyncPipeline, QueuedHeader, SourceHeader},
};
use num_traits::Saturating;
use relay_substrate_client::{headers_source::HeadersSource, BlockNumberOf, Chain, Client, HashOf};
use relay_substrate_client::{
headers_source::HeadersSource, BlockNumberOf, Chain, Client, Error as SubstrateError, HashOf,
};
use sp_runtime::Justification;
use std::marker::PhantomData;

/// Headers sync pipeline for Substrate <-> Substrate relays.
#[async_trait]
pub trait SubstrateHeadersSyncPipeline: HeadersSyncPipeline {
/// Name of the `best_block` runtime method.
const BEST_BLOCK_METHOD: &'static str;
/// Name of the `finalized_block` runtime method.
const FINALIZED_BLOCK_METHOD: &'static str;
/// Name of the `is_known_block` runtime method.
const IS_KNOWN_BLOCK_METHOD: &'static str;
/// Name of the `incomplete_headers` runtime method.
const INCOMPLETE_HEADERS_METHOD: &'static str;

/// Signed transaction type.
type SignedTransaction: Send + Sync + Encode;

/// Make submit header transaction.
async fn make_submit_header_transaction(
&self,
header: QueuedHeader<Self>,
) -> Result<Self::SignedTransaction, SubstrateError>;

/// Make completion transaction for the header.
async fn make_complete_header_transaction(
&self,
id: HeaderIdOf<Self>,
completion: Justification,
) -> Result<Self::SignedTransaction, SubstrateError>;
}

/// Substrate-to-Substrate headers pipeline.
#[derive(Debug, Clone)]
pub struct SubstrateHeadersToSubstrate<SourceChain, SourceSyncHeader, TargetChain: Chain, TargetSign> {
Expand Down
33 changes: 3 additions & 30 deletions relays/substrate/src/headers_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,21 @@
//! runtime that implements `<BridgedChainName>HeaderApi` to allow bridging with
//! <BridgedName> chain.

use crate::headers_pipeline::SubstrateHeadersSyncPipeline;

use async_trait::async_trait;
use codec::{Decode, Encode};
use futures::TryFutureExt;
use headers_relay::{
sync_loop::TargetClient,
sync_types::{HeaderIdOf, HeadersSyncPipeline, QueuedHeader, SubmittedHeaders},
sync_types::{HeaderIdOf, QueuedHeader, SubmittedHeaders},
};
use relay_substrate_client::{Chain, Client, Error as SubstrateError};
use relay_utils::HeaderId;
use sp_core::Bytes;
use sp_runtime::Justification;
use std::collections::HashSet;

/// Headers sync pipeline for Substrate <-> Substrate relays.
#[async_trait]
pub trait SubstrateHeadersSyncPipeline: HeadersSyncPipeline {
/// Name of the `best_block` runtime method.
const BEST_BLOCK_METHOD: &'static str;
/// Name of the `finalized_block` runtime method.
const FINALIZED_BLOCK_METHOD: &'static str;
/// Name of the `is_known_block` runtime method.
const IS_KNOWN_BLOCK_METHOD: &'static str;
/// Name of the `incomplete_headers` runtime method.
const INCOMPLETE_HEADERS_METHOD: &'static str;

/// Signed transaction type.
type SignedTransaction: Send + Sync + Encode;

/// Make submit header transaction.
async fn make_submit_header_transaction(
&self,
header: QueuedHeader<Self>,
) -> Result<Self::SignedTransaction, SubstrateError>;

/// Make completion transaction for the header.
async fn make_complete_header_transaction(
&self,
id: HeaderIdOf<Self>,
completion: Justification,
) -> Result<Self::SignedTransaction, SubstrateError>;
}

/// Substrate client as Substrate headers target.
pub struct SubstrateHeadersTarget<C: Chain, P> {
client: Client<C>,
Expand Down
1 change: 1 addition & 0 deletions relays/substrate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod headers_initialize;
mod headers_maintain;
mod headers_pipeline;
mod headers_target;
mod messages_lane;
mod messages_source;
mod messages_target;
mod millau_headers_to_rialto;
Expand Down
Loading