Skip to content

Commit

Permalink
Rialto -> Millau headers relay (paritytech#477)
Browse files Browse the repository at this point in the history
* Rialto -> Millau headers relay

* removed more constraints

* removed file from other PR

* Update primitives/rialto/src/lib.rs

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
  • Loading branch information
2 people authored and bkchr committed Apr 10, 2024
1 parent b027c81 commit 3e45356
Show file tree
Hide file tree
Showing 20 changed files with 337 additions and 128 deletions.
2 changes: 1 addition & 1 deletion bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub use frame_support::{

pub use pallet_balances::Call as BalancesCall;
pub use pallet_message_lane::Call as MessageLaneCall;
pub use pallet_substrate_bridge::Call as BridgeSubstrateCall;
pub use pallet_substrate_bridge::Call as BridgeRialtoCall;
pub use pallet_timestamp::Call as TimestampCall;

#[cfg(any(feature = "std", test))]
Expand Down
13 changes: 11 additions & 2 deletions bridges/primitives/rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use sp_runtime::{
};
use sp_std::prelude::*;

/// Maximal weight of single Millau block.
/// Maximal weight of single Rialto block.
pub const MAXIMUM_BLOCK_WEIGHT: Weight = 2_000_000_000_000;
/// Portion of block reserved for regular transactions.
pub const AVAILABLE_BLOCK_RATIO: u32 = 75;
/// Maximal weight of single Millau extrinsic (65% of maximum block weight = 75% for regular
/// Maximal weight of single Rialto extrinsic (65% of maximum block weight = 75% for regular
/// transactions minus 10% for initialization).
pub const MAXIMUM_EXTRINSIC_WEIGHT: Weight = MAXIMUM_BLOCK_WEIGHT / 100 * (AVAILABLE_BLOCK_RATIO as Weight - 10);

Expand All @@ -60,6 +60,15 @@ impl Chain for Rialto {
type Header = Header;
}

/// Name of the `RialtoHeaderApi::best_blocks` runtime method.
pub const BEST_RIALTO_BLOCKS_METHOD: &str = "RialtoHeaderApi_best_blocks";
/// Name of the `RialtoHeaderApi::finalized_block` runtime method.
pub const FINALIZED_RIALTO_BLOCK_METHOD: &str = "RialtoHeaderApi_finalized_block";
/// Name of the `RialtoHeaderApi::is_known_block` runtime method.
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";

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

Expand Down
2 changes: 1 addition & 1 deletion bridges/primitives/runtime/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub trait Chain: Send + Sync + 'static {
/// A type that fulfills the abstract idea of what a Substrate header is.
// See here for more info:
// https://crates.parity.io/sp_runtime/traits/trait.Header.html
type Header: Parameter + HeaderT<Number = Self::BlockNumber, Hash = Self::Hash>;
type Header: Parameter + HeaderT<Number = Self::BlockNumber, Hash = Self::Hash> + MaybeSerializeDeserialize;
}

/// Block number used by the chain.
Expand Down
8 changes: 4 additions & 4 deletions bridges/relays/ethereum/src/ethereum_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ use relay_ethereum_client::{
Client as EthereumClient, ConnectionParams as EthereumConnectionParams,
};
use relay_rialto_client::{Rialto, SigningParams as RialtoSigningParams};
use relay_substrate_client::{Client as SubstrateClient, ConnectionParams as SubstrateConnectionParams};
use relay_substrate_client::{
Chain as SubstrateChain, Client as SubstrateClient, ConnectionParams as SubstrateConnectionParams,
};
use relay_utils::{metrics::MetricsParams, HeaderId};
use rialto_runtime::exchange::EthereumTransactionInclusionProof;
use std::{sync::Arc, time::Duration};

/// Interval at which we ask Ethereum node for updates.
const ETHEREUM_TICK_INTERVAL: Duration = Duration::from_secs(10);
/// Interval at which we ask Substrate node for updates.
const SUBSTRATE_TICK_INTERVAL: Duration = Duration::from_secs(5);

/// Exchange relay mode.
#[derive(Debug)]
Expand Down Expand Up @@ -210,7 +210,7 @@ impl TargetClient<EthereumToSubstrateExchange> for SubstrateTransactionsTarget {
type Error = RpcError;

async fn tick(&self) {
async_std::task::sleep(SUBSTRATE_TICK_INTERVAL).await;
async_std::task::sleep(Rialto::AVERAGE_BLOCK_INTERVAL).await;
}

async fn is_header_known(&self, id: &EthereumHeaderId) -> Result<bool, Self::Error> {
Expand Down
8 changes: 4 additions & 4 deletions bridges/relays/ethereum/src/ethereum_sync_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ use relay_ethereum_client::{
Client as EthereumClient, ConnectionParams as EthereumConnectionParams,
};
use relay_rialto_client::{Rialto, SigningParams as RialtoSigningParams};
use relay_substrate_client::{Client as SubstrateClient, ConnectionParams as SubstrateConnectionParams};
use relay_substrate_client::{
Chain as SubstrateChain, Client as SubstrateClient, ConnectionParams as SubstrateConnectionParams,
};
use relay_utils::metrics::MetricsParams;

use std::fmt::Debug;
Expand All @@ -45,8 +47,6 @@ pub mod consts {

/// Interval at which we check new Ethereum headers when we are synced/almost synced.
pub const ETHEREUM_TICK_INTERVAL: Duration = Duration::from_secs(10);
/// Interval at which we check new Substrate blocks.
pub const SUBSTRATE_TICK_INTERVAL: Duration = Duration::from_secs(5);
/// Max number of headers in single submit transaction.
pub const MAX_HEADERS_IN_SINGLE_SUBMIT: usize = 32;
/// Max total size of headers in single submit transaction. This only affects signed
Expand Down Expand Up @@ -253,7 +253,7 @@ pub fn run(params: EthereumSyncParams) -> Result<(), RpcError> {
source,
consts::ETHEREUM_TICK_INTERVAL,
target,
consts::SUBSTRATE_TICK_INTERVAL,
Rialto::AVERAGE_BLOCK_INTERVAL,
(),
sync_params,
metrics_params,
Expand Down
7 changes: 3 additions & 4 deletions bridges/relays/ethereum/src/substrate_sync_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ use relay_ethereum_client::{
};
use relay_rialto_client::{HeaderId as RialtoHeaderId, Rialto, SyncHeader as RialtoSyncHeader};
use relay_substrate_client::{
headers_source::HeadersSource, Client as SubstrateClient, ConnectionParams as SubstrateConnectionParams,
headers_source::HeadersSource, Chain as SubstrateChain, Client as SubstrateClient,
ConnectionParams as SubstrateConnectionParams,
};
use relay_utils::metrics::MetricsParams;
use sp_runtime::Justification;
Expand All @@ -43,8 +44,6 @@ use std::{collections::HashSet, time::Duration};
pub mod consts {
use super::*;

/// Interval at which we check new Substrate headers when we are synced/almost synced.
pub const SUBSTRATE_TICK_INTERVAL: Duration = Duration::from_secs(10);
/// Interval at which we check new Ethereum blocks.
pub const ETHEREUM_TICK_INTERVAL: Duration = Duration::from_secs(5);
/// Max Ethereum headers we want to have in all 'before-submitted' states.
Expand Down Expand Up @@ -174,7 +173,7 @@ pub fn run(params: SubstrateSyncParams) -> Result<(), RpcError> {

headers_relay::sync_loop::run(
source,
consts::SUBSTRATE_TICK_INTERVAL,
Rialto::AVERAGE_BLOCK_INTERVAL,
target,
consts::ETHEREUM_TICK_INTERVAL,
(),
Expand Down
4 changes: 2 additions & 2 deletions bridges/relays/headers-relay/src/sync_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub trait HeadersSyncPipeline: Clone + Send + Sync {
+ num_traits::One
+ Into<u64>;
/// Type of header that we're syncing.
type Header: Clone + std::fmt::Debug + PartialEq + SourceHeader<Self::Hash, Self::Number> + Send + Sync;
type Header: SourceHeader<Self::Hash, Self::Number>;
/// 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 All @@ -94,7 +94,7 @@ pub trait HeadersSyncPipeline: Clone + Send + Sync {
pub type HeaderIdOf<P> = HeaderId<<P as HeadersSyncPipeline>::Hash, <P as HeadersSyncPipeline>::Number>;

/// Header that we're receiving from source node.
pub trait SourceHeader<Hash, Number> {
pub trait SourceHeader<Hash, Number>: Clone + std::fmt::Debug + PartialEq + Send + Sync {
/// Returns ID of header.
fn id(&self) -> HeaderId<Hash, Number>;
/// Returns ID of parent header.
Expand Down
2 changes: 2 additions & 0 deletions bridges/relays/millau-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use sp_runtime::{
};
use std::time::Duration;

pub use millau_runtime::BridgeRialtoCall;

/// Millau header id.
pub type HeaderId = relay_utils::HeaderId<millau_runtime::Hash, millau_runtime::BlockNumber>;

Expand Down
6 changes: 6 additions & 0 deletions bridges/relays/rialto-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ impl From<rialto_runtime::Header> for SyncHeader {
}
}

impl From<SyncHeader> for rialto_runtime::Header {
fn from(header: SyncHeader) -> Self {
header.0
}
}

impl SourceHeader<rialto_runtime::Hash, rialto_runtime::BlockNumber> for SyncHeader {
fn id(&self) -> HeaderId {
relay_utils::HeaderId(*self.number(), self.hash())
Expand Down
13 changes: 11 additions & 2 deletions bridges/relays/substrate-client/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@ pub trait Chain: ChainBase {
const NAME: &'static str;
/// Average block interval.
///
/// How often blocks are produced on that chain. It's suggested to set this value to match the block time of the chain.
/// How often blocks are produced on that chain. It's suggested to set this value
/// to match the block time of the chain.
const AVERAGE_BLOCK_INTERVAL: Duration;

/// The user account identifier type for the runtime.
type AccountId: Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay + Ord + Default;
/// Account index (aka nonce) type. This stores the number of previous transactions associated
/// with a sender account.
type Index: Parameter + Member + MaybeSerialize + Debug + Default + MaybeDisplay + AtLeast32Bit + Copy;
type Index: Parameter
+ Member
+ MaybeSerialize
+ Debug
+ Default
+ MaybeDisplay
+ DeserializeOwned
+ AtLeast32Bit
+ Copy;
/// Block type.
type SignedBlock: Member + Serialize + DeserializeOwned + BlockWithJustification;
/// The aggregated `Call` type.
Expand Down
5 changes: 1 addition & 4 deletions bridges/relays/substrate-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ impl<C: Chain> Client<C> {
/// Get the nonce of the given Substrate account.
///
/// Note: It's the caller's responsibility to make sure `account` is a valid ss58 address.
pub async fn next_account_index(&self, account: C::AccountId) -> Result<C::Index>
where
C::Index: DeserializeOwned,
{
pub async fn next_account_index(&self, account: C::AccountId) -> Result<C::Index> {
Ok(Substrate::<C, _, _>::system_account_next_index(&self.client, account).await?)
}

Expand Down
4 changes: 1 addition & 3 deletions bridges/relays/substrate-client/src/headers_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use headers_relay::{
sync_loop::SourceClient,
sync_types::{HeaderIdOf, HeadersSyncPipeline, QueuedHeader, SourceHeader},
};
use jsonrpsee::common::DeserializeOwned;
use num_traits::Saturating;
use sp_runtime::{traits::Header as HeaderT, Justification};
use std::marker::PhantomData;
Expand All @@ -51,8 +50,7 @@ impl<C, P> SourceClient<P> for HeadersSource<C, P>
where
C: Chain,
C::BlockNumber: Into<u64> + Saturating,
C::Header: DeserializeOwned + Into<P::Header>,
C::Index: DeserializeOwned,
C::Header: Into<P::Header>,
P: HeadersSyncPipeline<Extra = (), Completion = Justification, Hash = C::Hash, Number = C::BlockNumber>,
P::Header: SourceHeader<C::Hash, C::BlockNumber>,
{
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ codec = { package = "parity-scale-codec", version = "1.3.4" }
futures = "0.3.7"
hex = "0.4"
log = "0.4.11"
num-traits = "0.2"
paste = "1.0"
structopt = "0.3"

Expand Down
11 changes: 11 additions & 0 deletions bridges/relays/substrate/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ pub enum Command {
#[structopt(flatten)]
prometheus_params: PrometheusParams,
},
/// Relay Rialto headers to Millau.
RialtoHeadersToMillau {
#[structopt(flatten)]
rialto: RialtoConnectionParams,
#[structopt(flatten)]
millau: MillauConnectionParams,
#[structopt(flatten)]
millau_sign: MillauSigningParams,
#[structopt(flatten)]
prometheus_params: PrometheusParams,
},
/// Submit message to given Rialto -> Millau lane.
SubmitMillauToRialtoMessage {
#[structopt(flatten)]
Expand Down
9 changes: 3 additions & 6 deletions bridges/relays/substrate/src/headers_maintain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use headers_relay::{
use relay_substrate_client::{Chain, Client, Error as SubstrateError, JustificationsSubscription};
use relay_utils::HeaderId;
use sp_core::Bytes;
use sp_runtime::{traits::Header as HeaderT, DeserializeOwned, Justification};
use sp_runtime::{traits::Header as HeaderT, Justification};
use std::{collections::VecDeque, task::Poll};

/// Substrate-to-Substrate headers synchronization maintain procedure.
Expand Down Expand Up @@ -80,8 +80,6 @@ impl<P: SubstrateHeadersSyncPipeline, C: Chain> SubstrateHeadersToSubstrateMaint
impl<P, C> SyncMaintain<P> for SubstrateHeadersToSubstrateMaintain<P, C>
where
C: Chain,
C::Header: DeserializeOwned,
C::Index: DeserializeOwned,
P::Number: Decode + From<C::BlockNumber>,
P::Hash: Decode + From<C::Hash>,
P: SubstrateHeadersSyncPipeline<Completion = Justification, Extra = ()>,
Expand Down Expand Up @@ -271,8 +269,6 @@ where
P::Number: Decode + From<C::BlockNumber>,
P::Hash: Decode + From<C::Hash>,
C: Chain,
C::Header: DeserializeOwned,
C::Index: DeserializeOwned,
{
let call = P::FINALIZED_BLOCK_METHOD.into();
let data = Bytes(Vec::new());
Expand All @@ -288,7 +284,8 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::millau_headers_to_rialto::{sync_params, MillauHeadersToRialto};
use crate::headers_pipeline::sync_params;
use crate::millau_headers_to_rialto::MillauHeadersToRialto;

fn parent_hash(index: u8) -> bp_millau::Hash {
if index == 1 {
Expand Down
Loading

0 comments on commit 3e45356

Please sign in to comment.