mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 22:11:02 +00:00
Adding Bridges code as git subtree. (#2515)
* Add instructions. * Squashed 'bridges/' content from commit 345e84a21 git-subtree-dir: bridges git-subtree-split: 345e84a2146b56628e9888c9f5e129cb40e868a9 * Remove bridges workspace file to avoid confusing Cargo. * Add some bridges primitives to Polkadot workspace. * Improve docs.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
[package]
|
||||
name = "substrate-relay"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
async-std = "1.9.0"
|
||||
async-trait = "0.1.42"
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0" }
|
||||
futures = "0.3.12"
|
||||
hex = "0.4"
|
||||
log = "0.4.14"
|
||||
num-traits = "0.2"
|
||||
paste = "1.0"
|
||||
structopt = "0.3"
|
||||
|
||||
# Bridge dependencies
|
||||
|
||||
bp-header-chain = { path = "../../primitives/header-chain" }
|
||||
bp-kusama = { path = "../../primitives/kusama" }
|
||||
bp-message-lane = { path = "../../primitives/message-lane" }
|
||||
bp-millau = { path = "../../primitives/millau" }
|
||||
bp-polkadot = { path = "../../primitives/polkadot" }
|
||||
bp-runtime = { path = "../../primitives/runtime" }
|
||||
bp-rialto = { path = "../../primitives/rialto" }
|
||||
bridge-runtime-common = { path = "../../bin/runtime-common" }
|
||||
headers-relay = { path = "../headers-relay" }
|
||||
messages-relay = { path = "../messages-relay" }
|
||||
millau-runtime = { path = "../../bin/millau/runtime" }
|
||||
pallet-bridge-call-dispatch = { path = "../../modules/call-dispatch" }
|
||||
pallet-message-lane = { path = "../../modules/message-lane" }
|
||||
pallet-substrate-bridge = { path = "../../modules/substrate" }
|
||||
relay-kusama-client = { path = "../kusama-client" }
|
||||
relay-millau-client = { path = "../millau-client" }
|
||||
relay-polkadot-client = { path = "../polkadot-client" }
|
||||
relay-rialto-client = { path = "../rialto-client" }
|
||||
relay-substrate-client = { path = "../substrate-client" }
|
||||
relay-utils = { path = "../utils" }
|
||||
rialto-runtime = { path = "../../bin/rialto/runtime" }
|
||||
|
||||
# Substrate Dependencies
|
||||
|
||||
frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
|
||||
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
|
||||
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
|
||||
sp-trie = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
|
||||
@@ -0,0 +1,360 @@
|
||||
// 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/>.
|
||||
|
||||
//! Deal with CLI args of substrate-to-substrate relay.
|
||||
|
||||
use bp_message_lane::LaneId;
|
||||
use frame_support::weights::Weight;
|
||||
use sp_core::Bytes;
|
||||
use sp_finality_grandpa::SetId as GrandpaAuthoritiesSetId;
|
||||
use structopt::{clap::arg_enum, StructOpt};
|
||||
|
||||
/// Parse relay CLI args.
|
||||
pub fn parse_args() -> Command {
|
||||
Command::from_args()
|
||||
}
|
||||
|
||||
/// Substrate-to-Substrate bridge utilities.
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(about = "Substrate-to-Substrate relay")]
|
||||
pub enum Command {
|
||||
/// Start headers relay between two chains.
|
||||
///
|
||||
/// The on-chain bridge component should have been already initialized with
|
||||
/// `init-bridge` sub-command.
|
||||
RelayHeaders(RelayHeaders),
|
||||
/// Start messages relay between two chains.
|
||||
///
|
||||
/// Ties up to `MessageLane` pallets on both chains and starts relaying messages.
|
||||
/// Requires the header relay to be already running.
|
||||
RelayMessages(RelayMessages),
|
||||
/// Initialize on-chain bridge pallet with current header data.
|
||||
///
|
||||
/// Sends initialization transaction to bootstrap the bridge with current finalized block data.
|
||||
InitBridge(InitBridge),
|
||||
/// Send custom message over the bridge.
|
||||
///
|
||||
/// Allows interacting with the bridge by sending messages over `MessageLane` component.
|
||||
/// The message is being sent to the source chain, delivered to the target chain and dispatched
|
||||
/// there.
|
||||
SendMessage(SendMessage),
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
pub enum RelayHeaders {
|
||||
/// Relay Millau headers to Rialto.
|
||||
MillauToRialto {
|
||||
#[structopt(flatten)]
|
||||
millau: MillauConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
rialto: RialtoConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
rialto_sign: RialtoSigningParams,
|
||||
#[structopt(flatten)]
|
||||
prometheus_params: PrometheusParams,
|
||||
},
|
||||
/// Relay Rialto headers to Millau.
|
||||
RialtoToMillau {
|
||||
#[structopt(flatten)]
|
||||
rialto: RialtoConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
millau: MillauConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
millau_sign: MillauSigningParams,
|
||||
#[structopt(flatten)]
|
||||
prometheus_params: PrometheusParams,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
pub enum RelayMessages {
|
||||
/// Serve given lane of Millau -> Rialto messages.
|
||||
MillauToRialto {
|
||||
#[structopt(flatten)]
|
||||
millau: MillauConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
millau_sign: MillauSigningParams,
|
||||
#[structopt(flatten)]
|
||||
rialto: RialtoConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
rialto_sign: RialtoSigningParams,
|
||||
#[structopt(flatten)]
|
||||
prometheus_params: PrometheusParams,
|
||||
/// Hex-encoded id of lane that should be served by relay.
|
||||
#[structopt(long)]
|
||||
lane: HexLaneId,
|
||||
},
|
||||
/// Serve given lane of Rialto -> Millau messages.
|
||||
RialtoToMillau {
|
||||
#[structopt(flatten)]
|
||||
rialto: RialtoConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
rialto_sign: RialtoSigningParams,
|
||||
#[structopt(flatten)]
|
||||
millau: MillauConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
millau_sign: MillauSigningParams,
|
||||
#[structopt(flatten)]
|
||||
prometheus_params: PrometheusParams,
|
||||
/// Hex-encoded id of lane that should be served by relay.
|
||||
#[structopt(long)]
|
||||
lane: HexLaneId,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
pub enum InitBridge {
|
||||
/// Initialize Millau headers bridge in Rialto.
|
||||
MillauToRialto {
|
||||
#[structopt(flatten)]
|
||||
millau: MillauConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
rialto: RialtoConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
rialto_sign: RialtoSigningParams,
|
||||
#[structopt(flatten)]
|
||||
millau_bridge_params: MillauBridgeInitializationParams,
|
||||
},
|
||||
/// Initialize Rialto headers bridge in Millau.
|
||||
RialtoToMillau {
|
||||
#[structopt(flatten)]
|
||||
rialto: RialtoConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
millau: MillauConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
millau_sign: MillauSigningParams,
|
||||
#[structopt(flatten)]
|
||||
rialto_bridge_params: RialtoBridgeInitializationParams,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
pub enum SendMessage {
|
||||
/// Submit message to given Millau -> Rialto lane.
|
||||
MillauToRialto {
|
||||
#[structopt(flatten)]
|
||||
millau: MillauConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
millau_sign: MillauSigningParams,
|
||||
#[structopt(flatten)]
|
||||
rialto_sign: RialtoSigningParams,
|
||||
/// Hex-encoded lane id.
|
||||
#[structopt(long)]
|
||||
lane: HexLaneId,
|
||||
/// Dispatch weight of the message. If not passed, determined automatically.
|
||||
#[structopt(long)]
|
||||
dispatch_weight: Option<ExplicitOrMaximal<Weight>>,
|
||||
/// Delivery and dispatch fee. If not passed, determined automatically.
|
||||
#[structopt(long)]
|
||||
fee: Option<bp_millau::Balance>,
|
||||
/// Message type.
|
||||
#[structopt(subcommand)]
|
||||
message: ToRialtoMessage,
|
||||
/// The origin to use when dispatching the message on the target chain.
|
||||
#[structopt(long, possible_values = &Origins::variants())]
|
||||
origin: Origins,
|
||||
},
|
||||
/// Submit message to given Rialto -> Millau lane.
|
||||
RialtoToMillau {
|
||||
#[structopt(flatten)]
|
||||
rialto: RialtoConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
rialto_sign: RialtoSigningParams,
|
||||
#[structopt(flatten)]
|
||||
millau_sign: MillauSigningParams,
|
||||
/// Hex-encoded lane id.
|
||||
#[structopt(long)]
|
||||
lane: HexLaneId,
|
||||
/// Dispatch weight of the message. If not passed, determined automatically.
|
||||
#[structopt(long)]
|
||||
dispatch_weight: Option<ExplicitOrMaximal<Weight>>,
|
||||
/// Delivery and dispatch fee. If not passed, determined automatically.
|
||||
#[structopt(long)]
|
||||
fee: Option<bp_rialto::Balance>,
|
||||
/// Message type.
|
||||
#[structopt(subcommand)]
|
||||
message: ToMillauMessage,
|
||||
/// The origin to use when dispatching the message on the target chain.
|
||||
#[structopt(long, possible_values = &Origins::variants())]
|
||||
origin: Origins,
|
||||
},
|
||||
}
|
||||
|
||||
/// All possible messages that may be delivered to the Rialto chain.
|
||||
#[derive(StructOpt, Debug)]
|
||||
pub enum ToRialtoMessage {
|
||||
/// Make an on-chain remark (comment).
|
||||
Remark {
|
||||
/// Remark size. If not passed, small UTF8-encoded string is generated by relay as remark.
|
||||
#[structopt(long)]
|
||||
remark_size: Option<ExplicitOrMaximal<usize>>,
|
||||
},
|
||||
/// Transfer the specified `amount` of native tokens to a particular `recipient`.
|
||||
Transfer {
|
||||
#[structopt(long)]
|
||||
recipient: bp_rialto::AccountId,
|
||||
#[structopt(long)]
|
||||
amount: bp_rialto::Balance,
|
||||
},
|
||||
}
|
||||
|
||||
/// All possible messages that may be delivered to the Millau chain.
|
||||
#[derive(StructOpt, Debug)]
|
||||
pub enum ToMillauMessage {
|
||||
/// Make an on-chain remark (comment).
|
||||
Remark {
|
||||
/// Size of the remark. If not passed, small UTF8-encoded string is generated by relay as remark.
|
||||
#[structopt(long)]
|
||||
remark_size: Option<ExplicitOrMaximal<usize>>,
|
||||
},
|
||||
/// Transfer the specified `amount` of native tokens to a particular `recipient`.
|
||||
Transfer {
|
||||
#[structopt(long)]
|
||||
recipient: bp_millau::AccountId,
|
||||
#[structopt(long)]
|
||||
amount: bp_millau::Balance,
|
||||
},
|
||||
}
|
||||
|
||||
arg_enum! {
|
||||
#[derive(Debug)]
|
||||
/// The origin to use when dispatching the message on the target chain.
|
||||
///
|
||||
/// - `Target` uses account existing on the target chain (requires target private key).
|
||||
/// - `Origin` uses account derived from the source-chain account.
|
||||
pub enum Origins {
|
||||
Target,
|
||||
Source,
|
||||
}
|
||||
}
|
||||
|
||||
/// Lane id.
|
||||
#[derive(Debug)]
|
||||
pub struct HexLaneId(LaneId);
|
||||
|
||||
impl From<HexLaneId> for LaneId {
|
||||
fn from(lane_id: HexLaneId) -> LaneId {
|
||||
lane_id.0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for HexLaneId {
|
||||
type Err = hex::FromHexError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut lane_id = LaneId::default();
|
||||
hex::decode_to_slice(s, &mut lane_id)?;
|
||||
Ok(HexLaneId(lane_id))
|
||||
}
|
||||
}
|
||||
|
||||
/// Prometheus metrics params.
|
||||
#[derive(StructOpt)]
|
||||
pub struct PrometheusParams {
|
||||
/// Do not expose a Prometheus metric endpoint.
|
||||
#[structopt(long)]
|
||||
pub no_prometheus: bool,
|
||||
/// Expose Prometheus endpoint at given interface.
|
||||
#[structopt(long, default_value = "127.0.0.1")]
|
||||
pub prometheus_host: String,
|
||||
/// Expose Prometheus endpoint at given port.
|
||||
#[structopt(long, default_value = "9616")]
|
||||
pub prometheus_port: u16,
|
||||
}
|
||||
|
||||
impl From<PrometheusParams> for Option<relay_utils::metrics::MetricsParams> {
|
||||
fn from(cli_params: PrometheusParams) -> Option<relay_utils::metrics::MetricsParams> {
|
||||
if !cli_params.no_prometheus {
|
||||
Some(relay_utils::metrics::MetricsParams {
|
||||
host: cli_params.prometheus_host,
|
||||
port: cli_params.prometheus_port,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Either explicit or maximal allowed value.
|
||||
#[derive(Debug)]
|
||||
pub enum ExplicitOrMaximal<V> {
|
||||
/// User has explicitly specified argument value.
|
||||
Explicit(V),
|
||||
/// Maximal allowed value for this argument.
|
||||
Maximal,
|
||||
}
|
||||
|
||||
impl<V: std::str::FromStr> std::str::FromStr for ExplicitOrMaximal<V>
|
||||
where
|
||||
V::Err: std::fmt::Debug,
|
||||
{
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.to_lowercase() == "max" {
|
||||
return Ok(ExplicitOrMaximal::Maximal);
|
||||
}
|
||||
|
||||
V::from_str(s)
|
||||
.map(ExplicitOrMaximal::Explicit)
|
||||
.map_err(|e| format!("Failed to parse '{:?}'. Expected 'max' or explicit value", e))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! declare_chain_options {
|
||||
($chain:ident, $chain_prefix:ident) => {
|
||||
paste::item! {
|
||||
#[doc = $chain " connection params."]
|
||||
#[derive(StructOpt)]
|
||||
pub struct [<$chain ConnectionParams>] {
|
||||
#[doc = "Connect to " $chain " node at given host."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _host>]: String,
|
||||
#[doc = "Connect to " $chain " node websocket server at given port."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _port>]: u16,
|
||||
}
|
||||
|
||||
#[doc = $chain " signing params."]
|
||||
#[derive(StructOpt)]
|
||||
pub struct [<$chain SigningParams>] {
|
||||
#[doc = "The SURI of secret key to use when transactions are submitted to the " $chain " node."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _signer>]: String,
|
||||
#[doc = "The password for the SURI of secret key to use when transactions are submitted to the " $chain " node."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _signer_password>]: Option<String>,
|
||||
}
|
||||
|
||||
#[doc = $chain " headers bridge initialization params."]
|
||||
#[derive(StructOpt)]
|
||||
pub struct [<$chain BridgeInitializationParams>] {
|
||||
#[doc = "Hex-encoded " $chain " header to initialize bridge with. If not specified, genesis header is used."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _initial_header>]: Option<Bytes>,
|
||||
#[doc = "Hex-encoded " $chain " GRANDPA authorities set to initialize bridge with. If not specified, set from genesis block is used."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _initial_authorities>]: Option<Bytes>,
|
||||
#[doc = "Id of the " $chain " GRANDPA authorities set to initialize bridge with. If not specified, zero is used."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _initial_authorities_set_id>]: Option<GrandpaAuthoritiesSetId>,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
declare_chain_options!(Rialto, rialto);
|
||||
declare_chain_options!(Millau, millau);
|
||||
@@ -0,0 +1,141 @@
|
||||
// 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/>.
|
||||
|
||||
//! Initialize Substrate -> Substrate headers bridge.
|
||||
//!
|
||||
//! Initialization is a transaction that calls `initialize()` function of the
|
||||
//! `pallet-substrate-bridge` pallet. This transaction brings initial header
|
||||
//! and authorities set from source to target chain. The headers sync starts
|
||||
//! with this header.
|
||||
|
||||
use codec::Decode;
|
||||
use pallet_substrate_bridge::InitializationData;
|
||||
use relay_substrate_client::{Chain, Client};
|
||||
use sp_core::Bytes;
|
||||
use sp_finality_grandpa::{AuthorityList as GrandpaAuthoritiesSet, SetId as GrandpaAuthoritiesSetId};
|
||||
|
||||
/// Submit headers-bridge initialization transaction.
|
||||
pub async fn initialize<SourceChain: Chain, TargetChain: Chain>(
|
||||
source_client: Client<SourceChain>,
|
||||
target_client: Client<TargetChain>,
|
||||
raw_initial_header: Option<Bytes>,
|
||||
raw_initial_authorities_set: Option<Bytes>,
|
||||
initial_authorities_set_id: Option<GrandpaAuthoritiesSetId>,
|
||||
prepare_initialize_transaction: impl FnOnce(InitializationData<SourceChain::Header>) -> Result<Bytes, String>,
|
||||
) {
|
||||
let result = do_initialize(
|
||||
source_client,
|
||||
target_client,
|
||||
raw_initial_header,
|
||||
raw_initial_authorities_set,
|
||||
initial_authorities_set_id,
|
||||
prepare_initialize_transaction,
|
||||
)
|
||||
.await;
|
||||
|
||||
match result {
|
||||
Ok(tx_hash) => log::info!(
|
||||
target: "bridge",
|
||||
"Successfully submitted {}-headers bridge initialization transaction to {}: {:?}",
|
||||
SourceChain::NAME,
|
||||
TargetChain::NAME,
|
||||
tx_hash,
|
||||
),
|
||||
Err(err) => log::error!(
|
||||
target: "bridge",
|
||||
"Failed to submit {}-headers bridge initialization transaction to {}: {:?}",
|
||||
SourceChain::NAME,
|
||||
TargetChain::NAME,
|
||||
err,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Craft and submit initialization transaction, returning any error that may occur.
|
||||
async fn do_initialize<SourceChain: Chain, TargetChain: Chain>(
|
||||
source_client: Client<SourceChain>,
|
||||
target_client: Client<TargetChain>,
|
||||
raw_initial_header: Option<Bytes>,
|
||||
raw_initial_authorities_set: Option<Bytes>,
|
||||
initial_authorities_set_id: Option<GrandpaAuthoritiesSetId>,
|
||||
prepare_initialize_transaction: impl FnOnce(InitializationData<SourceChain::Header>) -> Result<Bytes, String>,
|
||||
) -> Result<TargetChain::Hash, String> {
|
||||
let initialization_data = prepare_initialization_data(
|
||||
source_client,
|
||||
raw_initial_header,
|
||||
raw_initial_authorities_set,
|
||||
initial_authorities_set_id,
|
||||
)
|
||||
.await?;
|
||||
let initialization_tx = prepare_initialize_transaction(initialization_data)?;
|
||||
let initialization_tx_hash = target_client
|
||||
.submit_extrinsic(initialization_tx)
|
||||
.await
|
||||
.map_err(|err| format!("Failed to submit {} transaction: {:?}", TargetChain::NAME, err))?;
|
||||
Ok(initialization_tx_hash)
|
||||
}
|
||||
|
||||
/// Prepare initialization data for the headers-bridge pallet.
|
||||
async fn prepare_initialization_data<SourceChain: Chain>(
|
||||
source_client: Client<SourceChain>,
|
||||
raw_initial_header: Option<Bytes>,
|
||||
raw_initial_authorities_set: Option<Bytes>,
|
||||
initial_authorities_set_id: Option<GrandpaAuthoritiesSetId>,
|
||||
) -> Result<InitializationData<SourceChain::Header>, String> {
|
||||
let source_genesis_hash = *source_client.genesis_hash();
|
||||
|
||||
let initial_header = match raw_initial_header {
|
||||
Some(raw_initial_header) => SourceChain::Header::decode(&mut &raw_initial_header.0[..])
|
||||
.map_err(|err| format!("Failed to decode {} initial header: {:?}", SourceChain::NAME, err))?,
|
||||
None => source_client
|
||||
.header_by_hash(source_genesis_hash)
|
||||
.await
|
||||
.map_err(|err| format!("Failed to retrive {} genesis header: {:?}", SourceChain::NAME, err))?,
|
||||
};
|
||||
|
||||
let raw_initial_authorities_set = match raw_initial_authorities_set {
|
||||
Some(raw_initial_authorities_set) => raw_initial_authorities_set.0,
|
||||
None => source_client
|
||||
.grandpa_authorities_set(source_genesis_hash)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
format!(
|
||||
"Failed to retrive {} authorities set at genesis header: {:?}",
|
||||
SourceChain::NAME,
|
||||
err
|
||||
)
|
||||
})?,
|
||||
};
|
||||
let initial_authorities_set =
|
||||
GrandpaAuthoritiesSet::decode(&mut &raw_initial_authorities_set[..]).map_err(|err| {
|
||||
format!(
|
||||
"Failed to decode {} initial authorities set: {:?}",
|
||||
SourceChain::NAME,
|
||||
err
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(InitializationData {
|
||||
header: initial_header,
|
||||
authority_list: initial_authorities_set,
|
||||
set_id: initial_authorities_set_id.unwrap_or(0),
|
||||
// There may be multiple scheduled changes, so on real chains we should select proper
|
||||
// moment, when there's nothing scheduled. On ephemeral (temporary) chains, it is ok to
|
||||
// start with genesis.
|
||||
scheduled_change: None,
|
||||
is_halted: false,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
// 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/>.
|
||||
|
||||
//! Substrate-to-Substrate headers synchronization maintain procedure.
|
||||
//!
|
||||
//! Regular headers synchronization only depends on persistent justifications
|
||||
//! that are generated when authorities set changes. This happens rarely on
|
||||
//! real-word chains. So some other way to finalize headers is required.
|
||||
//!
|
||||
//! Full nodes are listening to GRANDPA messages, so they may have track authorities
|
||||
//! votes on their own. They're returning both persistent and ephemeral justifications
|
||||
//! (justifications that are not stored in the database and not broadcasted over network)
|
||||
//! throught `grandpa_subscribeJustifications` RPC subscription.
|
||||
//!
|
||||
//! The idea of this maintain procedure is that when we see justification that 'improves'
|
||||
//! best finalized header on the target chain, we submit this justification to the target
|
||||
//! node.
|
||||
|
||||
use crate::headers_pipeline::SubstrateHeadersSyncPipeline;
|
||||
|
||||
use async_std::sync::{Arc, Mutex};
|
||||
use async_trait::async_trait;
|
||||
use codec::{Decode, Encode};
|
||||
use futures::future::{poll_fn, FutureExt, TryFutureExt};
|
||||
use headers_relay::{
|
||||
sync::HeadersSync,
|
||||
sync_loop::SyncMaintain,
|
||||
sync_types::{HeaderIdOf, HeaderStatus},
|
||||
};
|
||||
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, Justification};
|
||||
use std::{collections::VecDeque, marker::PhantomData, task::Poll};
|
||||
|
||||
/// Substrate-to-Substrate headers synchronization maintain procedure.
|
||||
pub struct SubstrateHeadersToSubstrateMaintain<P: SubstrateHeadersSyncPipeline, SourceChain, TargetChain: Chain> {
|
||||
pipeline: P,
|
||||
target_client: Client<TargetChain>,
|
||||
justifications: Arc<Mutex<Justifications<P>>>,
|
||||
_marker: PhantomData<SourceChain>,
|
||||
}
|
||||
|
||||
/// Future and already received justifications from the source chain.
|
||||
struct Justifications<P: SubstrateHeadersSyncPipeline> {
|
||||
/// Justifications stream.
|
||||
stream: JustificationsSubscription,
|
||||
/// Justifications that we have read from the stream but have not sent to the
|
||||
/// target node, because their targets were still not synced.
|
||||
queue: VecDeque<(HeaderIdOf<P>, Justification)>,
|
||||
}
|
||||
|
||||
impl<P: SubstrateHeadersSyncPipeline, SourceChain, TargetChain: Chain>
|
||||
SubstrateHeadersToSubstrateMaintain<P, SourceChain, TargetChain>
|
||||
{
|
||||
/// Create new maintain procedure.
|
||||
pub fn new(pipeline: P, target_client: Client<TargetChain>, justifications: JustificationsSubscription) -> Self {
|
||||
SubstrateHeadersToSubstrateMaintain {
|
||||
pipeline,
|
||||
target_client,
|
||||
justifications: Arc::new(Mutex::new(Justifications {
|
||||
stream: justifications,
|
||||
queue: VecDeque::new(),
|
||||
})),
|
||||
_marker: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<P: SubstrateHeadersSyncPipeline, SourceChain, TargetChain: Chain> Clone
|
||||
for SubstrateHeadersToSubstrateMaintain<P, SourceChain, TargetChain>
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
SubstrateHeadersToSubstrateMaintain {
|
||||
pipeline: self.pipeline.clone(),
|
||||
target_client: self.target_client.clone(),
|
||||
justifications: self.justifications.clone(),
|
||||
_marker: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<P, SourceChain, TargetChain> SyncMaintain<P> for SubstrateHeadersToSubstrateMaintain<P, SourceChain, TargetChain>
|
||||
where
|
||||
SourceChain: Chain,
|
||||
<SourceChain::Header as HeaderT>::Number: Into<P::Number>,
|
||||
<SourceChain::Header as HeaderT>::Hash: Into<P::Hash>,
|
||||
TargetChain: Chain,
|
||||
P::Number: Decode,
|
||||
P::Hash: Decode,
|
||||
P: SubstrateHeadersSyncPipeline<Completion = Justification, Extra = ()>,
|
||||
{
|
||||
async fn maintain(&self, sync: &mut HeadersSync<P>) {
|
||||
// lock justifications before doing anything else
|
||||
let mut justifications = match self.justifications.try_lock() {
|
||||
Some(justifications) => justifications,
|
||||
None => {
|
||||
// this should never happen, as we use single-thread executor
|
||||
log::warn!(target: "bridge", "Failed to acquire {} justifications lock", P::SOURCE_NAME);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// we need to read best finalized header from the target node to be able to
|
||||
// choose justification to submit
|
||||
let best_finalized = match best_finalized_header_id::<P, _>(&self.target_client).await {
|
||||
Ok(best_finalized) => best_finalized,
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
target: "bridge",
|
||||
"Failed to read best finalized {} block from maintain: {:?}",
|
||||
P::SOURCE_NAME,
|
||||
error,
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
log::debug!(
|
||||
target: "bridge",
|
||||
"Read best finalized {} block from {}: {:?}",
|
||||
P::SOURCE_NAME,
|
||||
P::TARGET_NAME,
|
||||
best_finalized,
|
||||
);
|
||||
|
||||
// Select justification to submit to the target node. We're submitting at most one justification
|
||||
// on every maintain call. So maintain rate directly affects finalization rate.
|
||||
let justification_to_submit = poll_fn(|context| {
|
||||
// read justifications from the stream and push to the queue
|
||||
justifications.read_from_stream::<SourceChain::Header>(context);
|
||||
|
||||
// remove all obsolete justifications from the queue
|
||||
remove_obsolete::<P>(&mut justifications.queue, best_finalized);
|
||||
|
||||
// select justification to submit
|
||||
Poll::Ready(select_justification(&mut justifications.queue, sync))
|
||||
})
|
||||
.await;
|
||||
|
||||
// finally - submit selected justification
|
||||
if let Some((target, justification)) = justification_to_submit {
|
||||
let submit_result = self
|
||||
.pipeline
|
||||
.make_complete_header_transaction(target, justification)
|
||||
.and_then(|tx| self.target_client.submit_extrinsic(Bytes(tx.encode())))
|
||||
.await;
|
||||
|
||||
match submit_result {
|
||||
Ok(_) => log::debug!(
|
||||
target: "bridge",
|
||||
"Submitted justification received over {} subscription. Target: {:?}",
|
||||
P::SOURCE_NAME,
|
||||
target,
|
||||
),
|
||||
Err(error) => log::warn!(
|
||||
target: "bridge",
|
||||
"Failed to submit justification received over {} subscription for {:?}: {:?}",
|
||||
P::SOURCE_NAME,
|
||||
target,
|
||||
error,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> Justifications<P>
|
||||
where
|
||||
P::Number: Decode,
|
||||
P::Hash: Decode,
|
||||
P: SubstrateHeadersSyncPipeline<Completion = Justification, Extra = ()>,
|
||||
{
|
||||
/// Read justifications from the subscription stream without blocking.
|
||||
fn read_from_stream<'a, SourceHeader>(&mut self, context: &mut std::task::Context<'a>)
|
||||
where
|
||||
SourceHeader: HeaderT,
|
||||
SourceHeader::Number: Into<P::Number>,
|
||||
SourceHeader::Hash: Into<P::Hash>,
|
||||
{
|
||||
loop {
|
||||
let maybe_next_justification = self.stream.next();
|
||||
futures::pin_mut!(maybe_next_justification);
|
||||
|
||||
let maybe_next_justification = maybe_next_justification.poll_unpin(context);
|
||||
let justification = match maybe_next_justification {
|
||||
Poll::Ready(justification) => justification,
|
||||
Poll::Pending => return,
|
||||
};
|
||||
|
||||
// decode justification target
|
||||
let target = bp_header_chain::justification::decode_justification_target::<SourceHeader>(&justification);
|
||||
let target = match target {
|
||||
Ok((target_hash, target_number)) => HeaderId(target_number.into(), target_hash.into()),
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
target: "bridge",
|
||||
"Failed to decode justification from {} subscription: {:?}",
|
||||
P::SOURCE_NAME,
|
||||
error,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
log::debug!(
|
||||
target: "bridge",
|
||||
"Received {} justification over subscription. Target: {:?}",
|
||||
P::SOURCE_NAME,
|
||||
target,
|
||||
);
|
||||
|
||||
self.queue.push_back((target, justification.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Clean queue of all justifications that are justifying already finalized blocks.
|
||||
fn remove_obsolete<P: SubstrateHeadersSyncPipeline>(
|
||||
queue: &mut VecDeque<(HeaderIdOf<P>, Justification)>,
|
||||
best_finalized: HeaderIdOf<P>,
|
||||
) {
|
||||
while queue
|
||||
.front()
|
||||
.map(|(target, _)| target.0 <= best_finalized.0)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
queue.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
/// Select appropriate justification that would improve best finalized block on target node.
|
||||
///
|
||||
/// It is assumed that the selected justification will be submitted to the target node. The
|
||||
/// justification itself and all preceeding justifications are removed from the queue.
|
||||
fn select_justification<P>(
|
||||
queue: &mut VecDeque<(HeaderIdOf<P>, Justification)>,
|
||||
sync: &mut HeadersSync<P>,
|
||||
) -> Option<(HeaderIdOf<P>, Justification)>
|
||||
where
|
||||
P: SubstrateHeadersSyncPipeline<Completion = Justification>,
|
||||
{
|
||||
let mut selected_justification = None;
|
||||
while let Some((target, justification)) = queue.pop_front() {
|
||||
// if we're waiting for this justification, report it
|
||||
if sync.headers().requires_completion_data(&target) {
|
||||
sync.headers_mut().completion_response(&target, Some(justification));
|
||||
// we won't submit previous justifications as we going to submit justification for
|
||||
// next header
|
||||
selected_justification = None;
|
||||
// we won't submit next justifications as we need to submit previous justifications
|
||||
// first
|
||||
break;
|
||||
}
|
||||
|
||||
// if we know that the header is already synced (it is known to the target node), let's
|
||||
// select it for submission. We still may select better justification on the next iteration.
|
||||
if sync.headers().status(&target) == HeaderStatus::Synced {
|
||||
selected_justification = Some((target, justification));
|
||||
continue;
|
||||
}
|
||||
|
||||
// finally - return justification back to the queue
|
||||
queue.push_back((target, justification));
|
||||
break;
|
||||
}
|
||||
|
||||
selected_justification
|
||||
}
|
||||
|
||||
/// Returns best finalized source header on the target chain.
|
||||
async fn best_finalized_header_id<P, C>(client: &Client<C>) -> Result<HeaderIdOf<P>, SubstrateError>
|
||||
where
|
||||
P: SubstrateHeadersSyncPipeline,
|
||||
P::Number: Decode,
|
||||
P::Hash: Decode,
|
||||
C: Chain,
|
||||
{
|
||||
let call = P::FINALIZED_BLOCK_METHOD.into();
|
||||
let data = Bytes(Vec::new());
|
||||
|
||||
let encoded_response = client.state_call(call, data, None).await?;
|
||||
let decoded_response: (P::Number, P::Hash) =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
|
||||
let best_header_id = HeaderId(decoded_response.0, decoded_response.1);
|
||||
Ok(best_header_id)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::headers_pipeline::sync_params;
|
||||
use crate::millau_headers_to_rialto::MillauHeadersToRialto;
|
||||
|
||||
fn parent_hash(index: u8) -> bp_millau::Hash {
|
||||
if index == 1 {
|
||||
Default::default()
|
||||
} else {
|
||||
header(index - 1).hash()
|
||||
}
|
||||
}
|
||||
|
||||
fn header_hash(index: u8) -> bp_millau::Hash {
|
||||
header(index).hash()
|
||||
}
|
||||
|
||||
fn header(index: u8) -> bp_millau::Header {
|
||||
bp_millau::Header::new(
|
||||
index as _,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
parent_hash(index),
|
||||
Default::default(),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn obsolete_justifications_are_removed() {
|
||||
let mut queue = vec![
|
||||
(HeaderId(1, header_hash(1)), vec![1]),
|
||||
(HeaderId(2, header_hash(2)), vec![2]),
|
||||
(HeaderId(3, header_hash(3)), vec![3]),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
remove_obsolete::<MillauHeadersToRialto>(&mut queue, HeaderId(2, header_hash(2)));
|
||||
|
||||
assert_eq!(
|
||||
queue,
|
||||
vec![(HeaderId(3, header_hash(3)), vec![3])]
|
||||
.into_iter()
|
||||
.collect::<VecDeque<_>>(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn latest_justification_is_selected() {
|
||||
let mut queue = vec![
|
||||
(HeaderId(1, header_hash(1)), vec![1]),
|
||||
(HeaderId(2, header_hash(2)), vec![2]),
|
||||
(HeaderId(3, header_hash(3)), vec![3]),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
let mut sync = HeadersSync::<MillauHeadersToRialto>::new(sync_params());
|
||||
sync.headers_mut().header_response(header(1).into());
|
||||
sync.headers_mut().header_response(header(2).into());
|
||||
sync.headers_mut().header_response(header(3).into());
|
||||
sync.target_best_header_response(HeaderId(2, header_hash(2)));
|
||||
|
||||
assert_eq!(
|
||||
select_justification(&mut queue, &mut sync),
|
||||
Some((HeaderId(2, header_hash(2)), vec![2])),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn required_justification_is_reported() {
|
||||
let mut queue = vec![
|
||||
(HeaderId(1, header_hash(1)), vec![1]),
|
||||
(HeaderId(2, header_hash(2)), vec![2]),
|
||||
(HeaderId(3, header_hash(3)), vec![3]),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
let mut sync = HeadersSync::<MillauHeadersToRialto>::new(sync_params());
|
||||
sync.headers_mut().header_response(header(1).into());
|
||||
sync.headers_mut().header_response(header(2).into());
|
||||
sync.headers_mut().header_response(header(3).into());
|
||||
sync.headers_mut()
|
||||
.incomplete_headers_response(vec![HeaderId(2, header_hash(2))].into_iter().collect());
|
||||
sync.target_best_header_response(HeaderId(2, header_hash(2)));
|
||||
|
||||
assert_eq!(sync.headers_mut().header_to_complete(), None,);
|
||||
|
||||
assert_eq!(select_justification(&mut queue, &mut sync), None,);
|
||||
|
||||
assert_eq!(
|
||||
sync.headers_mut().header_to_complete(),
|
||||
Some((HeaderId(2, header_hash(2)), &vec![2])),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
// 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/>.
|
||||
|
||||
//! Substrate-to-Substrate headers sync entrypoint.
|
||||
|
||||
use crate::{headers_maintain::SubstrateHeadersToSubstrateMaintain, headers_target::SubstrateHeadersTarget};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use codec::Encode;
|
||||
use headers_relay::{
|
||||
sync::{HeadersSyncParams, TargetTransactionMode},
|
||||
sync_types::{HeaderIdOf, HeadersSyncPipeline, QueuedHeader, SourceHeader},
|
||||
};
|
||||
use relay_substrate_client::{
|
||||
headers_source::HeadersSource, BlockNumberOf, Chain, Client, Error as SubstrateError, HashOf,
|
||||
};
|
||||
use relay_utils::BlockNumberBase;
|
||||
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> {
|
||||
/// Client for the target chain.
|
||||
pub(crate) target_client: Client<TargetChain>,
|
||||
/// Data required to sign target chain transactions.
|
||||
pub(crate) target_sign: TargetSign,
|
||||
/// Unused generic arguments dump.
|
||||
_marker: PhantomData<(SourceChain, SourceSyncHeader)>,
|
||||
}
|
||||
|
||||
impl<SourceChain, SourceSyncHeader, TargetChain: Chain, TargetSign>
|
||||
SubstrateHeadersToSubstrate<SourceChain, SourceSyncHeader, TargetChain, TargetSign>
|
||||
{
|
||||
/// Create new Substrate-to-Substrate headers pipeline.
|
||||
pub fn new(target_client: Client<TargetChain>, target_sign: TargetSign) -> Self {
|
||||
SubstrateHeadersToSubstrate {
|
||||
target_client,
|
||||
target_sign,
|
||||
_marker: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<SourceChain, SourceSyncHeader, TargetChain, TargetSign> HeadersSyncPipeline
|
||||
for SubstrateHeadersToSubstrate<SourceChain, SourceSyncHeader, TargetChain, TargetSign>
|
||||
where
|
||||
SourceChain: Clone + Chain,
|
||||
BlockNumberOf<SourceChain>: BlockNumberBase,
|
||||
SourceSyncHeader:
|
||||
SourceHeader<HashOf<SourceChain>, BlockNumberOf<SourceChain>> + std::ops::Deref<Target = SourceChain::Header>,
|
||||
TargetChain: Clone + Chain,
|
||||
TargetSign: Clone + Send + Sync,
|
||||
{
|
||||
const SOURCE_NAME: &'static str = SourceChain::NAME;
|
||||
const TARGET_NAME: &'static str = TargetChain::NAME;
|
||||
|
||||
type Hash = HashOf<SourceChain>;
|
||||
type Number = BlockNumberOf<SourceChain>;
|
||||
type Header = SourceSyncHeader;
|
||||
type Extra = ();
|
||||
type Completion = Justification;
|
||||
|
||||
fn estimate_size(source: &QueuedHeader<Self>) -> usize {
|
||||
source.header().encode().len()
|
||||
}
|
||||
}
|
||||
|
||||
/// Return sync parameters for Substrate-to-Substrate headers sync.
|
||||
pub fn sync_params() -> HeadersSyncParams {
|
||||
HeadersSyncParams {
|
||||
max_future_headers_to_download: 32,
|
||||
max_headers_in_submitted_status: 8,
|
||||
max_headers_in_single_submit: 1,
|
||||
max_headers_size_in_single_submit: 1024 * 1024,
|
||||
prune_depth: 256,
|
||||
target_tx_mode: TargetTransactionMode::Signed,
|
||||
}
|
||||
}
|
||||
|
||||
/// Run Substrate-to-Substrate headers sync.
|
||||
pub async fn run<SourceChain, TargetChain, P>(
|
||||
pipeline: P,
|
||||
source_client: Client<SourceChain>,
|
||||
target_client: Client<TargetChain>,
|
||||
metrics_params: Option<relay_utils::metrics::MetricsParams>,
|
||||
) where
|
||||
P: SubstrateHeadersSyncPipeline<
|
||||
Hash = HashOf<SourceChain>,
|
||||
Number = BlockNumberOf<SourceChain>,
|
||||
Completion = Justification,
|
||||
Extra = (),
|
||||
>,
|
||||
P::Header: SourceHeader<HashOf<SourceChain>, BlockNumberOf<SourceChain>>,
|
||||
SourceChain: Clone + Chain,
|
||||
SourceChain::Header: Into<P::Header>,
|
||||
BlockNumberOf<SourceChain>: BlockNumberBase,
|
||||
TargetChain: Clone + Chain,
|
||||
{
|
||||
let source_justifications = match source_client.clone().subscribe_justifications().await {
|
||||
Ok(source_justifications) => source_justifications,
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
target: "bridge",
|
||||
"Failed to subscribe to {} justifications: {:?}",
|
||||
SourceChain::NAME,
|
||||
error,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let sync_maintain = SubstrateHeadersToSubstrateMaintain::<_, SourceChain, _>::new(
|
||||
pipeline.clone(),
|
||||
target_client.clone(),
|
||||
source_justifications,
|
||||
);
|
||||
|
||||
log::info!(
|
||||
target: "bridge",
|
||||
"Starting {} -> {} headers relay",
|
||||
SourceChain::NAME,
|
||||
TargetChain::NAME,
|
||||
);
|
||||
|
||||
headers_relay::sync_loop::run(
|
||||
HeadersSource::new(source_client),
|
||||
SourceChain::AVERAGE_BLOCK_INTERVAL,
|
||||
SubstrateHeadersTarget::new(target_client, pipeline),
|
||||
TargetChain::AVERAGE_BLOCK_INTERVAL,
|
||||
sync_maintain,
|
||||
sync_params(),
|
||||
metrics_params,
|
||||
futures::future::pending(),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
// 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/>.
|
||||
|
||||
//! Substrate client as Substrate headers target. The chain we connect to should have
|
||||
//! 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, QueuedHeader, SubmittedHeaders},
|
||||
};
|
||||
use relay_substrate_client::{Chain, Client, Error as SubstrateError};
|
||||
use relay_utils::{relay_loop::Client as RelayClient, HeaderId};
|
||||
use sp_core::Bytes;
|
||||
use sp_runtime::Justification;
|
||||
use std::collections::HashSet;
|
||||
|
||||
/// Substrate client as Substrate headers target.
|
||||
pub struct SubstrateHeadersTarget<C: Chain, P> {
|
||||
client: Client<C>,
|
||||
pipeline: P,
|
||||
}
|
||||
|
||||
impl<C: Chain, P> SubstrateHeadersTarget<C, P> {
|
||||
/// Create new Substrate headers target.
|
||||
pub fn new(client: Client<C>, pipeline: P) -> Self {
|
||||
SubstrateHeadersTarget { client, pipeline }
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Chain, P: SubstrateHeadersSyncPipeline> Clone for SubstrateHeadersTarget<C, P> {
|
||||
fn clone(&self) -> Self {
|
||||
SubstrateHeadersTarget {
|
||||
client: self.client.clone(),
|
||||
pipeline: self.pipeline.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<C: Chain, P: SubstrateHeadersSyncPipeline> RelayClient for SubstrateHeadersTarget<C, P> {
|
||||
type Error = SubstrateError;
|
||||
|
||||
async fn reconnect(&mut self) -> Result<(), SubstrateError> {
|
||||
self.client.reconnect().await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<C, P> TargetClient<P> for SubstrateHeadersTarget<C, P>
|
||||
where
|
||||
C: Chain,
|
||||
P::Number: Decode,
|
||||
P::Hash: Decode + Encode,
|
||||
P: SubstrateHeadersSyncPipeline<Completion = Justification, Extra = ()>,
|
||||
{
|
||||
async fn best_header_id(&self) -> Result<HeaderIdOf<P>, SubstrateError> {
|
||||
// we can't continue to relay headers if target node is out of sync, because
|
||||
// it may have already received (some of) headers that we're going to relay
|
||||
self.client.ensure_synced().await?;
|
||||
|
||||
let call = P::BEST_BLOCK_METHOD.into();
|
||||
let data = Bytes(Vec::new());
|
||||
|
||||
let encoded_response = self.client.state_call(call, data, None).await?;
|
||||
let decoded_response: Vec<(P::Number, P::Hash)> =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
|
||||
// If we parse an empty list of headers it means that bridge pallet has not been initalized
|
||||
// yet. Otherwise we expect to always have at least one header.
|
||||
decoded_response
|
||||
.last()
|
||||
.ok_or(SubstrateError::UninitializedBridgePallet)
|
||||
.map(|(num, hash)| HeaderId(*num, *hash))
|
||||
}
|
||||
|
||||
async fn is_known_header(&self, id: HeaderIdOf<P>) -> Result<(HeaderIdOf<P>, bool), SubstrateError> {
|
||||
let call = P::IS_KNOWN_BLOCK_METHOD.into();
|
||||
let data = Bytes(id.1.encode());
|
||||
|
||||
let encoded_response = self.client.state_call(call, data, None).await?;
|
||||
let is_known_block: bool =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
|
||||
Ok((id, is_known_block))
|
||||
}
|
||||
|
||||
async fn submit_headers(
|
||||
&self,
|
||||
mut headers: Vec<QueuedHeader<P>>,
|
||||
) -> SubmittedHeaders<HeaderIdOf<P>, SubstrateError> {
|
||||
debug_assert_eq!(
|
||||
headers.len(),
|
||||
1,
|
||||
"Substrate pallet only supports single header / transaction"
|
||||
);
|
||||
|
||||
let header = headers.remove(0);
|
||||
let id = header.id();
|
||||
let submit_transaction_result = self
|
||||
.pipeline
|
||||
.make_submit_header_transaction(header)
|
||||
.and_then(|tx| self.client.submit_extrinsic(Bytes(tx.encode())))
|
||||
.await;
|
||||
|
||||
match submit_transaction_result {
|
||||
Ok(_) => SubmittedHeaders {
|
||||
submitted: vec![id],
|
||||
incomplete: Vec::new(),
|
||||
rejected: Vec::new(),
|
||||
fatal_error: None,
|
||||
},
|
||||
Err(error) => SubmittedHeaders {
|
||||
submitted: Vec::new(),
|
||||
incomplete: Vec::new(),
|
||||
rejected: vec![id],
|
||||
fatal_error: Some(error),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn incomplete_headers_ids(&self) -> Result<HashSet<HeaderIdOf<P>>, SubstrateError> {
|
||||
let call = P::INCOMPLETE_HEADERS_METHOD.into();
|
||||
let data = Bytes(Vec::new());
|
||||
|
||||
let encoded_response = self.client.state_call(call, data, None).await?;
|
||||
let decoded_response: Vec<(P::Number, P::Hash)> =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
|
||||
let incomplete_headers = decoded_response
|
||||
.into_iter()
|
||||
.map(|(number, hash)| HeaderId(number, hash))
|
||||
.collect();
|
||||
Ok(incomplete_headers)
|
||||
}
|
||||
|
||||
async fn complete_header(
|
||||
&self,
|
||||
id: HeaderIdOf<P>,
|
||||
completion: Justification,
|
||||
) -> Result<HeaderIdOf<P>, SubstrateError> {
|
||||
let tx = self.pipeline.make_complete_header_transaction(id, completion).await?;
|
||||
self.client.submit_extrinsic(Bytes(tx.encode())).await?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
async fn requires_extra(&self, header: QueuedHeader<P>) -> Result<(HeaderIdOf<P>, bool), SubstrateError> {
|
||||
Ok((header.id(), false))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,709 @@
|
||||
// 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/>.
|
||||
|
||||
//! Substrate-to-substrate relay entrypoint.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::weights::{GetDispatchInfo, Weight};
|
||||
use pallet_bridge_call_dispatch::{CallOrigin, MessagePayload};
|
||||
use relay_kusama_client::Kusama;
|
||||
use relay_millau_client::{Millau, SigningParams as MillauSigningParams};
|
||||
use relay_rialto_client::{Rialto, SigningParams as RialtoSigningParams};
|
||||
use relay_substrate_client::{Chain, ConnectionParams, TransactionSignScheme};
|
||||
use relay_utils::initialize::initialize_relay;
|
||||
use sp_core::{Bytes, Pair};
|
||||
use sp_runtime::traits::IdentifyAccount;
|
||||
|
||||
/// Kusama node client.
|
||||
pub type KusamaClient = relay_substrate_client::Client<Kusama>;
|
||||
/// Millau node client.
|
||||
pub type MillauClient = relay_substrate_client::Client<Millau>;
|
||||
/// Rialto node client.
|
||||
pub type RialtoClient = relay_substrate_client::Client<Rialto>;
|
||||
|
||||
mod cli;
|
||||
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;
|
||||
mod millau_messages_to_rialto;
|
||||
mod rialto_headers_to_millau;
|
||||
mod rialto_messages_to_millau;
|
||||
|
||||
fn main() {
|
||||
initialize_relay();
|
||||
|
||||
let result = async_std::task::block_on(run_command(cli::parse_args()));
|
||||
if let Err(error) = result {
|
||||
log::error!(target: "bridge", "Failed to start relay: {}", error);
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_command(command: cli::Command) -> Result<(), String> {
|
||||
match command {
|
||||
cli::Command::InitBridge(arg) => run_init_bridge(arg).await,
|
||||
cli::Command::RelayHeaders(arg) => run_relay_headers(arg).await,
|
||||
cli::Command::RelayMessages(arg) => run_relay_messages(arg).await,
|
||||
cli::Command::SendMessage(arg) => run_send_message(arg).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_init_bridge(command: cli::InitBridge) -> Result<(), String> {
|
||||
match command {
|
||||
cli::InitBridge::MillauToRialto {
|
||||
millau,
|
||||
rialto,
|
||||
rialto_sign,
|
||||
millau_bridge_params,
|
||||
} => {
|
||||
let millau_client = millau.into_client().await?;
|
||||
let rialto_client = rialto.into_client().await?;
|
||||
let rialto_sign = rialto_sign.parse()?;
|
||||
|
||||
let rialto_signer_next_index = rialto_client
|
||||
.next_account_index(rialto_sign.signer.public().into())
|
||||
.await?;
|
||||
|
||||
headers_initialize::initialize(
|
||||
millau_client,
|
||||
rialto_client.clone(),
|
||||
millau_bridge_params.millau_initial_header,
|
||||
millau_bridge_params.millau_initial_authorities,
|
||||
millau_bridge_params.millau_initial_authorities_set_id,
|
||||
move |initialization_data| {
|
||||
Ok(Bytes(
|
||||
Rialto::sign_transaction(
|
||||
&rialto_client,
|
||||
&rialto_sign.signer,
|
||||
rialto_signer_next_index,
|
||||
rialto_runtime::SudoCall::sudo(Box::new(
|
||||
rialto_runtime::BridgeMillauCall::initialize(initialization_data).into(),
|
||||
))
|
||||
.into(),
|
||||
)
|
||||
.encode(),
|
||||
))
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
cli::InitBridge::RialtoToMillau {
|
||||
rialto,
|
||||
millau,
|
||||
millau_sign,
|
||||
rialto_bridge_params,
|
||||
} => {
|
||||
let rialto_client = rialto.into_client().await?;
|
||||
let millau_client = millau.into_client().await?;
|
||||
let millau_sign = millau_sign.parse()?;
|
||||
let millau_signer_next_index = millau_client
|
||||
.next_account_index(millau_sign.signer.public().into())
|
||||
.await?;
|
||||
|
||||
headers_initialize::initialize(
|
||||
rialto_client,
|
||||
millau_client.clone(),
|
||||
rialto_bridge_params.rialto_initial_header,
|
||||
rialto_bridge_params.rialto_initial_authorities,
|
||||
rialto_bridge_params.rialto_initial_authorities_set_id,
|
||||
move |initialization_data| {
|
||||
Ok(Bytes(
|
||||
Millau::sign_transaction(
|
||||
&millau_client,
|
||||
&millau_sign.signer,
|
||||
millau_signer_next_index,
|
||||
millau_runtime::SudoCall::sudo(Box::new(
|
||||
millau_runtime::BridgeRialtoCall::initialize(initialization_data).into(),
|
||||
))
|
||||
.into(),
|
||||
)
|
||||
.encode(),
|
||||
))
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_relay_headers(command: cli::RelayHeaders) -> Result<(), String> {
|
||||
match command {
|
||||
cli::RelayHeaders::MillauToRialto {
|
||||
millau,
|
||||
rialto,
|
||||
rialto_sign,
|
||||
prometheus_params,
|
||||
} => {
|
||||
let millau_client = millau.into_client().await?;
|
||||
let rialto_client = rialto.into_client().await?;
|
||||
let rialto_sign = rialto_sign.parse()?;
|
||||
millau_headers_to_rialto::run(millau_client, rialto_client, rialto_sign, prometheus_params.into()).await;
|
||||
}
|
||||
cli::RelayHeaders::RialtoToMillau {
|
||||
rialto,
|
||||
millau,
|
||||
millau_sign,
|
||||
prometheus_params,
|
||||
} => {
|
||||
let rialto_client = rialto.into_client().await?;
|
||||
let millau_client = millau.into_client().await?;
|
||||
let millau_sign = millau_sign.parse()?;
|
||||
rialto_headers_to_millau::run(rialto_client, millau_client, millau_sign, prometheus_params.into()).await;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_relay_messages(command: cli::RelayMessages) -> Result<(), String> {
|
||||
match command {
|
||||
cli::RelayMessages::MillauToRialto {
|
||||
millau,
|
||||
millau_sign,
|
||||
rialto,
|
||||
rialto_sign,
|
||||
prometheus_params,
|
||||
lane,
|
||||
} => {
|
||||
let millau_client = millau.into_client().await?;
|
||||
let millau_sign = millau_sign.parse()?;
|
||||
let rialto_client = rialto.into_client().await?;
|
||||
let rialto_sign = rialto_sign.parse()?;
|
||||
|
||||
millau_messages_to_rialto::run(
|
||||
millau_client,
|
||||
millau_sign,
|
||||
rialto_client,
|
||||
rialto_sign,
|
||||
lane.into(),
|
||||
prometheus_params.into(),
|
||||
);
|
||||
}
|
||||
cli::RelayMessages::RialtoToMillau {
|
||||
rialto,
|
||||
rialto_sign,
|
||||
millau,
|
||||
millau_sign,
|
||||
prometheus_params,
|
||||
lane,
|
||||
} => {
|
||||
let rialto_client = rialto.into_client().await?;
|
||||
let rialto_sign = rialto_sign.parse()?;
|
||||
let millau_client = millau.into_client().await?;
|
||||
let millau_sign = millau_sign.parse()?;
|
||||
|
||||
rialto_messages_to_millau::run(
|
||||
rialto_client,
|
||||
rialto_sign,
|
||||
millau_client,
|
||||
millau_sign,
|
||||
lane.into(),
|
||||
prometheus_params.into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_send_message(command: cli::SendMessage) -> Result<(), String> {
|
||||
match command {
|
||||
cli::SendMessage::MillauToRialto {
|
||||
millau,
|
||||
millau_sign,
|
||||
rialto_sign,
|
||||
lane,
|
||||
message,
|
||||
dispatch_weight,
|
||||
fee,
|
||||
origin,
|
||||
..
|
||||
} => {
|
||||
let millau_client = millau.into_client().await?;
|
||||
let millau_sign = millau_sign.parse()?;
|
||||
let rialto_sign = rialto_sign.parse()?;
|
||||
let rialto_call = message.into_call();
|
||||
|
||||
let payload =
|
||||
millau_to_rialto_message_payload(&millau_sign, &rialto_sign, &rialto_call, origin, dispatch_weight);
|
||||
let dispatch_weight = payload.weight;
|
||||
|
||||
let lane = lane.into();
|
||||
let fee = get_fee(fee, || {
|
||||
estimate_message_delivery_and_dispatch_fee(
|
||||
&millau_client,
|
||||
bp_rialto::TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD,
|
||||
lane,
|
||||
payload.clone(),
|
||||
)
|
||||
})
|
||||
.await?;
|
||||
|
||||
let millau_call = millau_runtime::Call::BridgeRialtoMessageLane(
|
||||
millau_runtime::MessageLaneCall::send_message(lane, payload, fee),
|
||||
);
|
||||
|
||||
let signed_millau_call = Millau::sign_transaction(
|
||||
&millau_client,
|
||||
&millau_sign.signer,
|
||||
millau_client
|
||||
.next_account_index(millau_sign.signer.public().clone().into())
|
||||
.await?,
|
||||
millau_call,
|
||||
)
|
||||
.encode();
|
||||
|
||||
log::info!(
|
||||
target: "bridge",
|
||||
"Sending message to Rialto. Size: {}. Dispatch weight: {}. Fee: {}",
|
||||
signed_millau_call.len(),
|
||||
dispatch_weight,
|
||||
fee,
|
||||
);
|
||||
|
||||
millau_client.submit_extrinsic(Bytes(signed_millau_call)).await?;
|
||||
}
|
||||
cli::SendMessage::RialtoToMillau {
|
||||
rialto,
|
||||
rialto_sign,
|
||||
millau_sign,
|
||||
lane,
|
||||
message,
|
||||
dispatch_weight,
|
||||
fee,
|
||||
origin,
|
||||
..
|
||||
} => {
|
||||
let rialto_client = rialto.into_client().await?;
|
||||
let rialto_sign = rialto_sign.parse()?;
|
||||
let millau_sign = millau_sign.parse()?;
|
||||
let millau_call = message.into_call();
|
||||
|
||||
let payload =
|
||||
rialto_to_millau_message_payload(&rialto_sign, &millau_sign, &millau_call, origin, dispatch_weight);
|
||||
let dispatch_weight = payload.weight;
|
||||
|
||||
let lane = lane.into();
|
||||
let fee = get_fee(fee, || {
|
||||
estimate_message_delivery_and_dispatch_fee(
|
||||
&rialto_client,
|
||||
bp_millau::TO_MILLAU_ESTIMATE_MESSAGE_FEE_METHOD,
|
||||
lane,
|
||||
payload.clone(),
|
||||
)
|
||||
})
|
||||
.await?;
|
||||
|
||||
let rialto_call = rialto_runtime::Call::BridgeMillauMessageLane(
|
||||
rialto_runtime::MessageLaneCall::send_message(lane, payload, fee),
|
||||
);
|
||||
|
||||
let signed_rialto_call = Rialto::sign_transaction(
|
||||
&rialto_client,
|
||||
&rialto_sign.signer,
|
||||
rialto_client
|
||||
.next_account_index(rialto_sign.signer.public().clone().into())
|
||||
.await?,
|
||||
rialto_call,
|
||||
)
|
||||
.encode();
|
||||
|
||||
log::info!(
|
||||
target: "bridge",
|
||||
"Sending message to Millau. Size: {}. Dispatch weight: {}. Fee: {}",
|
||||
signed_rialto_call.len(),
|
||||
dispatch_weight,
|
||||
fee,
|
||||
);
|
||||
|
||||
rialto_client.submit_extrinsic(Bytes(signed_rialto_call)).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn estimate_message_delivery_and_dispatch_fee<Fee: Decode, C: Chain, P: Encode>(
|
||||
client: &relay_substrate_client::Client<C>,
|
||||
estimate_fee_method: &str,
|
||||
lane: bp_message_lane::LaneId,
|
||||
payload: P,
|
||||
) -> Result<Option<Fee>, relay_substrate_client::Error> {
|
||||
let encoded_response = client
|
||||
.state_call(estimate_fee_method.into(), (lane, payload).encode().into(), None)
|
||||
.await?;
|
||||
let decoded_response: Option<Fee> =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(relay_substrate_client::Error::ResponseParseFailed)?;
|
||||
Ok(decoded_response)
|
||||
}
|
||||
|
||||
fn remark_payload(remark_size: Option<cli::ExplicitOrMaximal<usize>>, maximal_allowed_size: u32) -> Vec<u8> {
|
||||
match remark_size {
|
||||
Some(cli::ExplicitOrMaximal::Explicit(remark_size)) => vec![0; remark_size],
|
||||
Some(cli::ExplicitOrMaximal::Maximal) => vec![0; maximal_allowed_size as _],
|
||||
None => format!(
|
||||
"Unix time: {}",
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs(),
|
||||
)
|
||||
.as_bytes()
|
||||
.to_vec(),
|
||||
}
|
||||
}
|
||||
|
||||
fn rialto_to_millau_message_payload(
|
||||
rialto_sign: &RialtoSigningParams,
|
||||
millau_sign: &MillauSigningParams,
|
||||
millau_call: &millau_runtime::Call,
|
||||
origin: cli::Origins,
|
||||
user_specified_dispatch_weight: Option<cli::ExplicitOrMaximal<Weight>>,
|
||||
) -> rialto_runtime::millau_messages::ToMillauMessagePayload {
|
||||
let millau_call_weight = prepare_call_dispatch_weight(
|
||||
user_specified_dispatch_weight,
|
||||
cli::ExplicitOrMaximal::Explicit(millau_call.get_dispatch_info().weight),
|
||||
compute_maximal_message_dispatch_weight(bp_millau::max_extrinsic_weight()),
|
||||
);
|
||||
let rialto_sender_public: bp_rialto::AccountSigner = rialto_sign.signer.public().clone().into();
|
||||
let rialto_account_id: bp_rialto::AccountId = rialto_sender_public.into_account();
|
||||
let millau_origin_public = millau_sign.signer.public();
|
||||
|
||||
MessagePayload {
|
||||
spec_version: millau_runtime::VERSION.spec_version,
|
||||
weight: millau_call_weight,
|
||||
origin: match origin {
|
||||
cli::Origins::Source => CallOrigin::SourceAccount(rialto_account_id),
|
||||
cli::Origins::Target => {
|
||||
let digest = rialto_runtime::millau_account_ownership_digest(
|
||||
&millau_call,
|
||||
rialto_account_id.clone(),
|
||||
millau_runtime::VERSION.spec_version,
|
||||
);
|
||||
|
||||
let digest_signature = millau_sign.signer.sign(&digest);
|
||||
|
||||
CallOrigin::TargetAccount(rialto_account_id, millau_origin_public.into(), digest_signature.into())
|
||||
}
|
||||
},
|
||||
call: millau_call.encode(),
|
||||
}
|
||||
}
|
||||
|
||||
fn millau_to_rialto_message_payload(
|
||||
millau_sign: &MillauSigningParams,
|
||||
rialto_sign: &RialtoSigningParams,
|
||||
rialto_call: &rialto_runtime::Call,
|
||||
origin: cli::Origins,
|
||||
user_specified_dispatch_weight: Option<cli::ExplicitOrMaximal<Weight>>,
|
||||
) -> millau_runtime::rialto_messages::ToRialtoMessagePayload {
|
||||
let rialto_call_weight = prepare_call_dispatch_weight(
|
||||
user_specified_dispatch_weight,
|
||||
cli::ExplicitOrMaximal::Explicit(rialto_call.get_dispatch_info().weight),
|
||||
compute_maximal_message_dispatch_weight(bp_rialto::max_extrinsic_weight()),
|
||||
);
|
||||
let millau_sender_public: bp_millau::AccountSigner = millau_sign.signer.public().clone().into();
|
||||
let millau_account_id: bp_millau::AccountId = millau_sender_public.into_account();
|
||||
let rialto_origin_public = rialto_sign.signer.public();
|
||||
|
||||
MessagePayload {
|
||||
spec_version: rialto_runtime::VERSION.spec_version,
|
||||
weight: rialto_call_weight,
|
||||
origin: match origin {
|
||||
cli::Origins::Source => CallOrigin::SourceAccount(millau_account_id),
|
||||
cli::Origins::Target => {
|
||||
let digest = millau_runtime::rialto_account_ownership_digest(
|
||||
&rialto_call,
|
||||
millau_account_id.clone(),
|
||||
rialto_runtime::VERSION.spec_version,
|
||||
);
|
||||
|
||||
let digest_signature = rialto_sign.signer.sign(&digest);
|
||||
|
||||
CallOrigin::TargetAccount(millau_account_id, rialto_origin_public.into(), digest_signature.into())
|
||||
}
|
||||
},
|
||||
call: rialto_call.encode(),
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare_call_dispatch_weight(
|
||||
user_specified_dispatch_weight: Option<cli::ExplicitOrMaximal<Weight>>,
|
||||
weight_from_pre_dispatch_call: cli::ExplicitOrMaximal<Weight>,
|
||||
maximal_allowed_weight: Weight,
|
||||
) -> Weight {
|
||||
match user_specified_dispatch_weight.unwrap_or(weight_from_pre_dispatch_call) {
|
||||
cli::ExplicitOrMaximal::Explicit(weight) => weight,
|
||||
cli::ExplicitOrMaximal::Maximal => maximal_allowed_weight,
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_fee<Fee, F, R, E>(fee: Option<Fee>, f: F) -> Result<Fee, String>
|
||||
where
|
||||
Fee: Decode,
|
||||
F: FnOnce() -> R,
|
||||
R: std::future::Future<Output = Result<Option<Fee>, E>>,
|
||||
E: std::fmt::Debug,
|
||||
{
|
||||
match fee {
|
||||
Some(fee) => Ok(fee),
|
||||
None => match f().await {
|
||||
Ok(Some(fee)) => Ok(fee),
|
||||
Ok(None) => Err("Failed to estimate message fee. Message is too heavy?".into()),
|
||||
Err(error) => Err(format!("Failed to estimate message fee: {:?}", error)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_maximal_message_dispatch_weight(maximal_extrinsic_weight: Weight) -> Weight {
|
||||
bridge_runtime_common::messages::target::maximal_incoming_message_dispatch_weight(maximal_extrinsic_weight)
|
||||
}
|
||||
|
||||
fn compute_maximal_message_arguments_size(
|
||||
maximal_source_extrinsic_size: u32,
|
||||
maximal_target_extrinsic_size: u32,
|
||||
) -> u32 {
|
||||
// assume that both signed extensions and other arguments fit 1KB
|
||||
let service_tx_bytes_on_source_chain = 1024;
|
||||
let maximal_source_extrinsic_size = maximal_source_extrinsic_size - service_tx_bytes_on_source_chain;
|
||||
let maximal_call_size =
|
||||
bridge_runtime_common::messages::target::maximal_incoming_message_size(maximal_target_extrinsic_size);
|
||||
let maximal_call_size = if maximal_call_size > maximal_source_extrinsic_size {
|
||||
maximal_source_extrinsic_size
|
||||
} else {
|
||||
maximal_call_size
|
||||
};
|
||||
|
||||
// bytes in Call encoding that are used to encode everything except arguments
|
||||
let service_bytes = 1 + 1 + 4;
|
||||
maximal_call_size - service_bytes
|
||||
}
|
||||
|
||||
impl crate::cli::RialtoSigningParams {
|
||||
/// Parse CLI parameters into typed signing params.
|
||||
pub fn parse(self) -> Result<RialtoSigningParams, String> {
|
||||
RialtoSigningParams::from_suri(&self.rialto_signer, self.rialto_signer_password.as_deref())
|
||||
.map_err(|e| format!("Failed to parse rialto-signer: {:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::cli::MillauSigningParams {
|
||||
/// Parse CLI parameters into typed signing params.
|
||||
pub fn parse(self) -> Result<MillauSigningParams, String> {
|
||||
MillauSigningParams::from_suri(&self.millau_signer, self.millau_signer_password.as_deref())
|
||||
.map_err(|e| format!("Failed to parse millau-signer: {:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::cli::MillauConnectionParams {
|
||||
/// Convert CLI connection parameters into Millau RPC Client.
|
||||
pub async fn into_client(self) -> relay_substrate_client::Result<MillauClient> {
|
||||
MillauClient::new(ConnectionParams {
|
||||
host: self.millau_host,
|
||||
port: self.millau_port,
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
impl crate::cli::RialtoConnectionParams {
|
||||
/// Convert CLI connection parameters into Rialto RPC Client.
|
||||
pub async fn into_client(self) -> relay_substrate_client::Result<RialtoClient> {
|
||||
RialtoClient::new(ConnectionParams {
|
||||
host: self.rialto_host,
|
||||
port: self.rialto_port,
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::cli::ToRialtoMessage {
|
||||
/// Convert CLI call request into runtime `Call` instance.
|
||||
pub fn into_call(self) -> rialto_runtime::Call {
|
||||
match self {
|
||||
cli::ToRialtoMessage::Remark { remark_size } => {
|
||||
rialto_runtime::Call::System(rialto_runtime::SystemCall::remark(remark_payload(
|
||||
remark_size,
|
||||
compute_maximal_message_arguments_size(
|
||||
bp_millau::max_extrinsic_size(),
|
||||
bp_rialto::max_extrinsic_size(),
|
||||
),
|
||||
)))
|
||||
}
|
||||
cli::ToRialtoMessage::Transfer { recipient, amount } => {
|
||||
rialto_runtime::Call::Balances(rialto_runtime::BalancesCall::transfer(recipient, amount))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::cli::ToMillauMessage {
|
||||
/// Convert CLI call request into runtime `Call` instance.
|
||||
pub fn into_call(self) -> millau_runtime::Call {
|
||||
match self {
|
||||
cli::ToMillauMessage::Remark { remark_size } => {
|
||||
millau_runtime::Call::System(millau_runtime::SystemCall::remark(remark_payload(
|
||||
remark_size,
|
||||
compute_maximal_message_arguments_size(
|
||||
bp_rialto::max_extrinsic_size(),
|
||||
bp_millau::max_extrinsic_size(),
|
||||
),
|
||||
)))
|
||||
}
|
||||
cli::ToMillauMessage::Transfer { recipient, amount } => {
|
||||
millau_runtime::Call::Balances(millau_runtime::BalancesCall::transfer(recipient, amount))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use bp_message_lane::source_chain::TargetHeaderChain;
|
||||
use sp_core::Pair;
|
||||
use sp_runtime::traits::{IdentifyAccount, Verify};
|
||||
|
||||
#[test]
|
||||
fn millau_signature_is_valid_on_rialto() {
|
||||
let millau_sign = relay_millau_client::SigningParams::from_suri("//Dave", None).unwrap();
|
||||
|
||||
let call = rialto_runtime::Call::System(rialto_runtime::SystemCall::remark(vec![]));
|
||||
|
||||
let millau_public: bp_millau::AccountSigner = millau_sign.signer.public().clone().into();
|
||||
let millau_account_id: bp_millau::AccountId = millau_public.into_account();
|
||||
|
||||
let digest = millau_runtime::rialto_account_ownership_digest(
|
||||
&call,
|
||||
millau_account_id,
|
||||
rialto_runtime::VERSION.spec_version,
|
||||
);
|
||||
|
||||
let rialto_signer = relay_rialto_client::SigningParams::from_suri("//Dave", None).unwrap();
|
||||
let signature = rialto_signer.signer.sign(&digest);
|
||||
|
||||
assert!(signature.verify(&digest[..], &rialto_signer.signer.public()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rialto_signature_is_valid_on_millau() {
|
||||
let rialto_sign = relay_rialto_client::SigningParams::from_suri("//Dave", None).unwrap();
|
||||
|
||||
let call = millau_runtime::Call::System(millau_runtime::SystemCall::remark(vec![]));
|
||||
|
||||
let rialto_public: bp_rialto::AccountSigner = rialto_sign.signer.public().clone().into();
|
||||
let rialto_account_id: bp_rialto::AccountId = rialto_public.into_account();
|
||||
|
||||
let digest = rialto_runtime::millau_account_ownership_digest(
|
||||
&call,
|
||||
rialto_account_id,
|
||||
millau_runtime::VERSION.spec_version,
|
||||
);
|
||||
|
||||
let millau_signer = relay_millau_client::SigningParams::from_suri("//Dave", None).unwrap();
|
||||
let signature = millau_signer.signer.sign(&digest);
|
||||
|
||||
assert!(signature.verify(&digest[..], &millau_signer.signer.public()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maximal_rialto_to_millau_message_arguments_size_is_computed_correctly() {
|
||||
use rialto_runtime::millau_messages::Millau;
|
||||
|
||||
let maximal_remark_size =
|
||||
compute_maximal_message_arguments_size(bp_rialto::max_extrinsic_size(), bp_millau::max_extrinsic_size());
|
||||
|
||||
let call: millau_runtime::Call = millau_runtime::SystemCall::remark(vec![42; maximal_remark_size as _]).into();
|
||||
let payload = pallet_bridge_call_dispatch::MessagePayload {
|
||||
spec_version: Default::default(),
|
||||
weight: call.get_dispatch_info().weight,
|
||||
origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot,
|
||||
call: call.encode(),
|
||||
};
|
||||
assert_eq!(Millau::verify_message(&payload), Ok(()));
|
||||
|
||||
let call: millau_runtime::Call =
|
||||
millau_runtime::SystemCall::remark(vec![42; (maximal_remark_size + 1) as _]).into();
|
||||
let payload = pallet_bridge_call_dispatch::MessagePayload {
|
||||
spec_version: Default::default(),
|
||||
weight: call.get_dispatch_info().weight,
|
||||
origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot,
|
||||
call: call.encode(),
|
||||
};
|
||||
assert!(Millau::verify_message(&payload).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maximal_size_remark_to_rialto_is_generated_correctly() {
|
||||
assert!(
|
||||
bridge_runtime_common::messages::target::maximal_incoming_message_size(
|
||||
bp_rialto::max_extrinsic_size()
|
||||
) > bp_millau::max_extrinsic_size(),
|
||||
"We can't actually send maximal messages to Rialto from Millau, because Millau extrinsics can't be that large",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maximal_rialto_to_millau_message_dispatch_weight_is_computed_correctly() {
|
||||
use rialto_runtime::millau_messages::Millau;
|
||||
|
||||
let maximal_dispatch_weight = compute_maximal_message_dispatch_weight(bp_millau::max_extrinsic_weight());
|
||||
let call: millau_runtime::Call = rialto_runtime::SystemCall::remark(vec![]).into();
|
||||
|
||||
let payload = pallet_bridge_call_dispatch::MessagePayload {
|
||||
spec_version: Default::default(),
|
||||
weight: maximal_dispatch_weight,
|
||||
origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot,
|
||||
call: call.encode(),
|
||||
};
|
||||
assert_eq!(Millau::verify_message(&payload), Ok(()));
|
||||
|
||||
let payload = pallet_bridge_call_dispatch::MessagePayload {
|
||||
spec_version: Default::default(),
|
||||
weight: maximal_dispatch_weight + 1,
|
||||
origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot,
|
||||
call: call.encode(),
|
||||
};
|
||||
assert!(Millau::verify_message(&payload).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maximal_weight_fill_block_to_rialto_is_generated_correctly() {
|
||||
use millau_runtime::rialto_messages::Rialto;
|
||||
|
||||
let maximal_dispatch_weight = compute_maximal_message_dispatch_weight(bp_rialto::max_extrinsic_weight());
|
||||
let call: rialto_runtime::Call = millau_runtime::SystemCall::remark(vec![]).into();
|
||||
|
||||
let payload = pallet_bridge_call_dispatch::MessagePayload {
|
||||
spec_version: Default::default(),
|
||||
weight: maximal_dispatch_weight,
|
||||
origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot,
|
||||
call: call.encode(),
|
||||
};
|
||||
assert_eq!(Rialto::verify_message(&payload), Ok(()));
|
||||
|
||||
let payload = pallet_bridge_call_dispatch::MessagePayload {
|
||||
spec_version: Default::default(),
|
||||
weight: maximal_dispatch_weight + 1,
|
||||
origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot,
|
||||
call: call.encode(),
|
||||
};
|
||||
assert!(Rialto::verify_message(&payload).is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
// 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/>.
|
||||
|
||||
use crate::messages_source::SubstrateMessagesProof;
|
||||
use crate::messages_target::SubstrateMessagesReceivingProof;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bp_message_lane::MessageNonce;
|
||||
use codec::Encode;
|
||||
use frame_support::weights::Weight;
|
||||
use messages_relay::message_lane::{MessageLane, SourceHeaderIdOf, TargetHeaderIdOf};
|
||||
use relay_substrate_client::{BlockNumberOf, Chain, Client, Error as SubstrateError, HashOf};
|
||||
use relay_utils::BlockNumberBase;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
/// Message sync pipeline for Substrate <-> Substrate relays.
|
||||
#[async_trait]
|
||||
pub trait SubstrateMessageLane: MessageLane {
|
||||
/// Name of the runtime method that returns dispatch weight of outbound messages at the source chain.
|
||||
const OUTBOUND_LANE_MESSAGES_DISPATCH_WEIGHT_METHOD: &'static str;
|
||||
/// Name of the runtime method that returns latest generated nonce at the source chain.
|
||||
const OUTBOUND_LANE_LATEST_GENERATED_NONCE_METHOD: &'static str;
|
||||
/// Name of the runtime method that returns latest received (confirmed) nonce at the the source chain.
|
||||
const OUTBOUND_LANE_LATEST_RECEIVED_NONCE_METHOD: &'static str;
|
||||
|
||||
/// Name of the runtime method that returns latest received nonce at the target chain.
|
||||
const INBOUND_LANE_LATEST_RECEIVED_NONCE_METHOD: &'static str;
|
||||
/// Name of the runtime method that returns latest confirmed (reward-paid) nonce at the target chain.
|
||||
const INBOUND_LANE_LATEST_CONFIRMED_NONCE_METHOD: &'static str;
|
||||
/// Numebr of the runtime method that returns state of "unrewarded relayers" set at the target chain.
|
||||
const INBOUND_LANE_UNREWARDED_RELAYERS_STATE: &'static str;
|
||||
|
||||
/// Name of the runtime method that returns id of best finalized source header at target chain.
|
||||
const BEST_FINALIZED_SOURCE_HEADER_ID_AT_TARGET: &'static str;
|
||||
/// Name of the runtime method that returns id of best finalized target header at source chain.
|
||||
const BEST_FINALIZED_TARGET_HEADER_ID_AT_SOURCE: &'static str;
|
||||
|
||||
/// Signed transaction type of the source chain.
|
||||
type SourceSignedTransaction: Send + Sync + Encode;
|
||||
/// Signed transaction type of the target chain.
|
||||
type TargetSignedTransaction: Send + Sync + Encode;
|
||||
|
||||
/// Make messages delivery transaction.
|
||||
async fn make_messages_delivery_transaction(
|
||||
&self,
|
||||
generated_at_header: SourceHeaderIdOf<Self>,
|
||||
nonces: RangeInclusive<MessageNonce>,
|
||||
proof: Self::MessagesProof,
|
||||
) -> Result<Self::TargetSignedTransaction, SubstrateError>;
|
||||
|
||||
/// Make messages receiving proof transaction.
|
||||
async fn make_messages_receiving_proof_transaction(
|
||||
&self,
|
||||
generated_at_header: TargetHeaderIdOf<Self>,
|
||||
proof: Self::MessagesReceivingProof,
|
||||
) -> Result<Self::SourceSignedTransaction, SubstrateError>;
|
||||
}
|
||||
|
||||
/// Substrate-to-Substrate message lane.
|
||||
#[derive(Debug)]
|
||||
pub struct SubstrateMessageLaneToSubstrate<Source: Chain, SourceSignParams, Target: Chain, TargetSignParams> {
|
||||
/// Client for the source Substrate chain.
|
||||
pub(crate) source_client: Client<Source>,
|
||||
/// Parameters required to sign transactions for source chain.
|
||||
pub(crate) source_sign: SourceSignParams,
|
||||
/// Client for the target Substrate chain.
|
||||
pub(crate) target_client: Client<Target>,
|
||||
/// Parameters required to sign transactions for target chain.
|
||||
pub(crate) target_sign: TargetSignParams,
|
||||
/// Account id of relayer at the source chain.
|
||||
pub(crate) relayer_id_at_source: Source::AccountId,
|
||||
}
|
||||
|
||||
impl<Source: Chain, SourceSignParams: Clone, Target: Chain, TargetSignParams: Clone> Clone
|
||||
for SubstrateMessageLaneToSubstrate<Source, SourceSignParams, Target, TargetSignParams>
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
source_client: self.source_client.clone(),
|
||||
source_sign: self.source_sign.clone(),
|
||||
target_client: self.target_client.clone(),
|
||||
target_sign: self.target_sign.clone(),
|
||||
relayer_id_at_source: self.relayer_id_at_source.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Source: Chain, SourceSignParams, Target: Chain, TargetSignParams> MessageLane
|
||||
for SubstrateMessageLaneToSubstrate<Source, SourceSignParams, Target, TargetSignParams>
|
||||
where
|
||||
SourceSignParams: Clone + Send + Sync + 'static,
|
||||
TargetSignParams: Clone + Send + Sync + 'static,
|
||||
BlockNumberOf<Source>: BlockNumberBase,
|
||||
BlockNumberOf<Target>: BlockNumberBase,
|
||||
{
|
||||
const SOURCE_NAME: &'static str = Source::NAME;
|
||||
const TARGET_NAME: &'static str = Target::NAME;
|
||||
|
||||
type MessagesProof = SubstrateMessagesProof<Source>;
|
||||
type MessagesReceivingProof = SubstrateMessagesReceivingProof<Target>;
|
||||
|
||||
type SourceHeaderNumber = BlockNumberOf<Source>;
|
||||
type SourceHeaderHash = HashOf<Source>;
|
||||
|
||||
type TargetHeaderNumber = BlockNumberOf<Target>;
|
||||
type TargetHeaderHash = HashOf<Target>;
|
||||
}
|
||||
|
||||
/// Returns maximal number of messages and their maximal cumulative dispatch weight, based
|
||||
/// on given chain parameters.
|
||||
pub fn select_delivery_transaction_limits<W: pallet_message_lane::WeightInfoExt>(
|
||||
max_extrinsic_weight: Weight,
|
||||
max_unconfirmed_messages_at_inbound_lane: MessageNonce,
|
||||
) -> (MessageNonce, Weight) {
|
||||
// We may try to guess accurate value, based on maximal number of messages and per-message
|
||||
// weight overhead, but the relay loop isn't using this info in a super-accurate way anyway.
|
||||
// So just a rough guess: let's say 1/3 of max tx weight is for tx itself and the rest is
|
||||
// for messages dispatch.
|
||||
|
||||
// Another thing to keep in mind is that our runtimes (when this code was written) accept
|
||||
// messages with dispatch weight <= max_extrinsic_weight/2. So we can't reserve less than
|
||||
// that for dispatch.
|
||||
|
||||
let weight_for_delivery_tx = max_extrinsic_weight / 3;
|
||||
let weight_for_messages_dispatch = max_extrinsic_weight - weight_for_delivery_tx;
|
||||
|
||||
let delivery_tx_base_weight =
|
||||
W::receive_messages_proof_overhead() + W::receive_messages_proof_outbound_lane_state_overhead();
|
||||
let delivery_tx_weight_rest = weight_for_delivery_tx - delivery_tx_base_weight;
|
||||
let max_number_of_messages = std::cmp::min(
|
||||
delivery_tx_weight_rest / W::receive_messages_proof_messages_overhead(1),
|
||||
max_unconfirmed_messages_at_inbound_lane,
|
||||
);
|
||||
|
||||
assert!(
|
||||
max_number_of_messages > 0,
|
||||
"Relay should fit at least one message in every delivery transaction",
|
||||
);
|
||||
assert!(
|
||||
weight_for_messages_dispatch >= max_extrinsic_weight / 2,
|
||||
"Relay shall be able to deliver messages with dispatch weight = max_extrinsic_weight / 2",
|
||||
);
|
||||
|
||||
(max_number_of_messages, weight_for_messages_dispatch)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
type RialtoToMillauMessageLaneWeights = pallet_message_lane::weights::RialtoWeight<rialto_runtime::Runtime>;
|
||||
|
||||
#[test]
|
||||
fn select_delivery_transaction_limits_works() {
|
||||
let (max_count, max_weight) = select_delivery_transaction_limits::<RialtoToMillauMessageLaneWeights>(
|
||||
bp_millau::max_extrinsic_weight(),
|
||||
bp_millau::MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE,
|
||||
);
|
||||
assert_eq!(
|
||||
(max_count, max_weight),
|
||||
// We don't actually care about these values, so feel free to update them whenever test
|
||||
// fails. The only thing to do before that is to ensure that new values looks sane: i.e. weight
|
||||
// reserved for messages dispatch allows dispatch of non-trivial messages.
|
||||
//
|
||||
// Any significant change in this values should attract additional attention.
|
||||
(955, 216_583_333_334),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
// 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/>.
|
||||
|
||||
//! Substrate client as Substrate messages source. The chain we connect to should have
|
||||
//! runtime that implements `<BridgedChainName>HeaderApi` to allow bridging with
|
||||
//! <BridgedName> chain.
|
||||
|
||||
use crate::messages_lane::SubstrateMessageLane;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bp_message_lane::{LaneId, MessageNonce};
|
||||
use bp_runtime::InstanceId;
|
||||
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::weights::Weight;
|
||||
use messages_relay::{
|
||||
message_lane::{SourceHeaderIdOf, TargetHeaderIdOf},
|
||||
message_lane_loop::{
|
||||
ClientState, MessageProofParameters, MessageWeights, MessageWeightsMap, SourceClient, SourceClientState,
|
||||
},
|
||||
};
|
||||
use relay_substrate_client::{Chain, Client, Error as SubstrateError, HashOf, HeaderIdOf};
|
||||
use relay_utils::{relay_loop::Client as RelayClient, BlockNumberBase, HeaderId};
|
||||
use sp_core::Bytes;
|
||||
use sp_runtime::{traits::Header as HeaderT, DeserializeOwned};
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
/// Intermediate message proof returned by the source Substrate node. Includes everything
|
||||
/// required to submit to the target node: cumulative dispatch weight of bundled messages and
|
||||
/// the proof itself.
|
||||
pub type SubstrateMessagesProof<C> = (Weight, FromBridgedChainMessagesProof<HashOf<C>>);
|
||||
|
||||
/// Substrate client as Substrate messages source.
|
||||
pub struct SubstrateMessagesSource<C: Chain, P> {
|
||||
client: Client<C>,
|
||||
lane: P,
|
||||
lane_id: LaneId,
|
||||
instance: InstanceId,
|
||||
}
|
||||
|
||||
impl<C: Chain, P> SubstrateMessagesSource<C, P> {
|
||||
/// Create new Substrate headers source.
|
||||
pub fn new(client: Client<C>, lane: P, lane_id: LaneId, instance: InstanceId) -> Self {
|
||||
SubstrateMessagesSource {
|
||||
client,
|
||||
lane,
|
||||
lane_id,
|
||||
instance,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Chain, P: SubstrateMessageLane> Clone for SubstrateMessagesSource<C, P> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
client: self.client.clone(),
|
||||
lane: self.lane.clone(),
|
||||
lane_id: self.lane_id,
|
||||
instance: self.instance,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<C: Chain, P: SubstrateMessageLane> RelayClient for SubstrateMessagesSource<C, P> {
|
||||
type Error = SubstrateError;
|
||||
|
||||
async fn reconnect(&mut self) -> Result<(), SubstrateError> {
|
||||
self.client.reconnect().await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<C, P> SourceClient<P> for SubstrateMessagesSource<C, P>
|
||||
where
|
||||
C: Chain,
|
||||
C::Header: DeserializeOwned,
|
||||
C::Index: DeserializeOwned,
|
||||
C::BlockNumber: BlockNumberBase,
|
||||
P: SubstrateMessageLane<
|
||||
MessagesProof = SubstrateMessagesProof<C>,
|
||||
SourceHeaderNumber = <C::Header as HeaderT>::Number,
|
||||
SourceHeaderHash = <C::Header as HeaderT>::Hash,
|
||||
>,
|
||||
P::TargetHeaderNumber: Decode,
|
||||
P::TargetHeaderHash: Decode,
|
||||
{
|
||||
async fn state(&self) -> Result<SourceClientState<P>, SubstrateError> {
|
||||
// we can't continue to deliver confirmations if source node is out of sync, because
|
||||
// it may have already received confirmations that we're going to deliver
|
||||
self.client.ensure_synced().await?;
|
||||
|
||||
read_client_state::<_, P::TargetHeaderHash, P::TargetHeaderNumber>(
|
||||
&self.client,
|
||||
P::BEST_FINALIZED_TARGET_HEADER_ID_AT_SOURCE,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn latest_generated_nonce(
|
||||
&self,
|
||||
id: SourceHeaderIdOf<P>,
|
||||
) -> Result<(SourceHeaderIdOf<P>, MessageNonce), SubstrateError> {
|
||||
let encoded_response = self
|
||||
.client
|
||||
.state_call(
|
||||
P::OUTBOUND_LANE_LATEST_GENERATED_NONCE_METHOD.into(),
|
||||
Bytes(self.lane_id.encode()),
|
||||
Some(id.1),
|
||||
)
|
||||
.await?;
|
||||
let latest_generated_nonce: MessageNonce =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
Ok((id, latest_generated_nonce))
|
||||
}
|
||||
|
||||
async fn latest_confirmed_received_nonce(
|
||||
&self,
|
||||
id: SourceHeaderIdOf<P>,
|
||||
) -> Result<(SourceHeaderIdOf<P>, MessageNonce), SubstrateError> {
|
||||
let encoded_response = self
|
||||
.client
|
||||
.state_call(
|
||||
P::OUTBOUND_LANE_LATEST_RECEIVED_NONCE_METHOD.into(),
|
||||
Bytes(self.lane_id.encode()),
|
||||
Some(id.1),
|
||||
)
|
||||
.await?;
|
||||
let latest_received_nonce: MessageNonce =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
Ok((id, latest_received_nonce))
|
||||
}
|
||||
|
||||
async fn generated_messages_weights(
|
||||
&self,
|
||||
id: SourceHeaderIdOf<P>,
|
||||
nonces: RangeInclusive<MessageNonce>,
|
||||
) -> Result<MessageWeightsMap, SubstrateError> {
|
||||
let encoded_response = self
|
||||
.client
|
||||
.state_call(
|
||||
P::OUTBOUND_LANE_MESSAGES_DISPATCH_WEIGHT_METHOD.into(),
|
||||
Bytes((self.lane_id, nonces.start(), nonces.end()).encode()),
|
||||
Some(id.1),
|
||||
)
|
||||
.await?;
|
||||
|
||||
make_message_weights_map::<C>(
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?,
|
||||
nonces,
|
||||
)
|
||||
}
|
||||
|
||||
async fn prove_messages(
|
||||
&self,
|
||||
id: SourceHeaderIdOf<P>,
|
||||
nonces: RangeInclusive<MessageNonce>,
|
||||
proof_parameters: MessageProofParameters,
|
||||
) -> Result<(SourceHeaderIdOf<P>, RangeInclusive<MessageNonce>, P::MessagesProof), SubstrateError> {
|
||||
let proof = self
|
||||
.client
|
||||
.prove_messages(
|
||||
self.instance,
|
||||
self.lane_id,
|
||||
nonces.clone(),
|
||||
proof_parameters.outbound_state_proof_required,
|
||||
id.1,
|
||||
)
|
||||
.await?
|
||||
.iter_nodes()
|
||||
.collect();
|
||||
let proof = FromBridgedChainMessagesProof {
|
||||
bridged_header_hash: id.1,
|
||||
storage_proof: proof,
|
||||
lane: self.lane_id,
|
||||
nonces_start: *nonces.start(),
|
||||
nonces_end: *nonces.end(),
|
||||
};
|
||||
Ok((id, nonces, (proof_parameters.dispatch_weight, proof)))
|
||||
}
|
||||
|
||||
async fn submit_messages_receiving_proof(
|
||||
&self,
|
||||
generated_at_block: TargetHeaderIdOf<P>,
|
||||
proof: P::MessagesReceivingProof,
|
||||
) -> Result<(), SubstrateError> {
|
||||
let tx = self
|
||||
.lane
|
||||
.make_messages_receiving_proof_transaction(generated_at_block, proof)
|
||||
.await?;
|
||||
self.client.submit_extrinsic(Bytes(tx.encode())).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn read_client_state<SelfChain, BridgedHeaderHash, BridgedHeaderNumber>(
|
||||
self_client: &Client<SelfChain>,
|
||||
best_finalized_header_id_method_name: &str,
|
||||
) -> Result<ClientState<HeaderIdOf<SelfChain>, HeaderId<BridgedHeaderHash, BridgedHeaderNumber>>, SubstrateError>
|
||||
where
|
||||
SelfChain: Chain,
|
||||
SelfChain::Header: DeserializeOwned,
|
||||
SelfChain::Index: DeserializeOwned,
|
||||
BridgedHeaderHash: Decode,
|
||||
BridgedHeaderNumber: Decode,
|
||||
{
|
||||
// let's read our state first: we need best finalized header hash on **this** chain
|
||||
let self_best_finalized_header_hash = self_client.best_finalized_header_hash().await?;
|
||||
let self_best_finalized_header = self_client.header_by_hash(self_best_finalized_header_hash).await?;
|
||||
let self_best_finalized_id = HeaderId(*self_best_finalized_header.number(), self_best_finalized_header_hash);
|
||||
|
||||
// now let's read our best header on **this** chain
|
||||
let self_best_header = self_client.best_header().await?;
|
||||
let self_best_hash = self_best_header.hash();
|
||||
let self_best_id = HeaderId(*self_best_header.number(), self_best_hash);
|
||||
|
||||
// now let's read id of best finalized peer header at our best finalized block
|
||||
let encoded_best_finalized_peer_on_self = self_client
|
||||
.state_call(
|
||||
best_finalized_header_id_method_name.into(),
|
||||
Bytes(Vec::new()),
|
||||
Some(self_best_hash),
|
||||
)
|
||||
.await?;
|
||||
let decoded_best_finalized_peer_on_self: (BridgedHeaderNumber, BridgedHeaderHash) =
|
||||
Decode::decode(&mut &encoded_best_finalized_peer_on_self.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
let peer_on_self_best_finalized_id = HeaderId(
|
||||
decoded_best_finalized_peer_on_self.0,
|
||||
decoded_best_finalized_peer_on_self.1,
|
||||
);
|
||||
|
||||
Ok(ClientState {
|
||||
best_self: self_best_id,
|
||||
best_finalized_self: self_best_finalized_id,
|
||||
best_finalized_peer_at_best_self: peer_on_self_best_finalized_id,
|
||||
})
|
||||
}
|
||||
|
||||
fn make_message_weights_map<C: Chain>(
|
||||
weights: Vec<(MessageNonce, Weight, u32)>,
|
||||
nonces: RangeInclusive<MessageNonce>,
|
||||
) -> Result<MessageWeightsMap, SubstrateError> {
|
||||
let make_missing_nonce_error = |expected_nonce| {
|
||||
Err(SubstrateError::Custom(format!(
|
||||
"Missing nonce {} in messages_dispatch_weight call result. Expected all nonces from {:?}",
|
||||
expected_nonce, nonces,
|
||||
)))
|
||||
};
|
||||
|
||||
let mut weights_map = MessageWeightsMap::new();
|
||||
|
||||
// this is actually prevented by external logic
|
||||
if nonces.is_empty() {
|
||||
return Ok(weights_map);
|
||||
}
|
||||
|
||||
// check if last nonce is missing - loop below is not checking this
|
||||
let last_nonce_is_missing = weights
|
||||
.last()
|
||||
.map(|(last_nonce, _, _)| last_nonce != nonces.end())
|
||||
.unwrap_or(true);
|
||||
if last_nonce_is_missing {
|
||||
return make_missing_nonce_error(*nonces.end());
|
||||
}
|
||||
|
||||
let mut expected_nonce = *nonces.start();
|
||||
let mut is_at_head = true;
|
||||
|
||||
for (nonce, weight, size) in weights {
|
||||
match (nonce == expected_nonce, is_at_head) {
|
||||
(true, _) => (),
|
||||
(false, true) => {
|
||||
// this may happen if some messages were already pruned from the source node
|
||||
//
|
||||
// this is not critical error and will be auto-resolved by messages lane (and target node)
|
||||
log::info!(
|
||||
target: "bridge",
|
||||
"Some messages are missing from the {} node: {:?}. Target node may be out of sync?",
|
||||
C::NAME,
|
||||
expected_nonce..nonce,
|
||||
);
|
||||
}
|
||||
(false, false) => {
|
||||
// some nonces are missing from the middle/tail of the range
|
||||
//
|
||||
// this is critical error, because we can't miss any nonces
|
||||
return make_missing_nonce_error(expected_nonce);
|
||||
}
|
||||
}
|
||||
|
||||
weights_map.insert(
|
||||
nonce,
|
||||
MessageWeights {
|
||||
weight,
|
||||
size: size as _,
|
||||
},
|
||||
);
|
||||
expected_nonce = nonce + 1;
|
||||
is_at_head = false;
|
||||
}
|
||||
|
||||
Ok(weights_map)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn make_message_weights_map_succeeds_if_no_messages_are_missing() {
|
||||
assert_eq!(
|
||||
make_message_weights_map::<relay_rialto_client::Rialto>(vec![(1, 0, 0), (2, 0, 0), (3, 0, 0)], 1..=3,)
|
||||
.unwrap(),
|
||||
vec![
|
||||
(1, MessageWeights { weight: 0, size: 0 }),
|
||||
(2, MessageWeights { weight: 0, size: 0 }),
|
||||
(3, MessageWeights { weight: 0, size: 0 }),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_message_weights_map_succeeds_if_head_messages_are_missing() {
|
||||
assert_eq!(
|
||||
make_message_weights_map::<relay_rialto_client::Rialto>(vec![(2, 0, 0), (3, 0, 0)], 1..=3,).unwrap(),
|
||||
vec![
|
||||
(2, MessageWeights { weight: 0, size: 0 }),
|
||||
(3, MessageWeights { weight: 0, size: 0 }),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_message_weights_map_fails_if_mid_messages_are_missing() {
|
||||
assert!(matches!(
|
||||
make_message_weights_map::<relay_rialto_client::Rialto>(vec![(1, 0, 0), (3, 0, 0)], 1..=3,),
|
||||
Err(SubstrateError::Custom(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_message_weights_map_fails_if_tail_messages_are_missing() {
|
||||
assert!(matches!(
|
||||
make_message_weights_map::<relay_rialto_client::Rialto>(vec![(1, 0, 0), (2, 0, 0)], 1..=3,),
|
||||
Err(SubstrateError::Custom(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_message_weights_map_fails_if_all_messages_are_missing() {
|
||||
assert!(matches!(
|
||||
make_message_weights_map::<relay_rialto_client::Rialto>(vec![], 1..=3),
|
||||
Err(SubstrateError::Custom(_))
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
// 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/>.
|
||||
|
||||
//! Substrate client as Substrate messages target. The chain we connect to should have
|
||||
//! runtime that implements `<BridgedChainName>HeaderApi` to allow bridging with
|
||||
//! <BridgedName> chain.
|
||||
|
||||
use crate::messages_lane::SubstrateMessageLane;
|
||||
use crate::messages_source::read_client_state;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bp_message_lane::{LaneId, MessageNonce, UnrewardedRelayersState};
|
||||
use bp_runtime::InstanceId;
|
||||
use bridge_runtime_common::messages::source::FromBridgedChainMessagesDeliveryProof;
|
||||
use codec::{Decode, Encode};
|
||||
use messages_relay::{
|
||||
message_lane::{SourceHeaderIdOf, TargetHeaderIdOf},
|
||||
message_lane_loop::{TargetClient, TargetClientState},
|
||||
};
|
||||
use relay_substrate_client::{Chain, Client, Error as SubstrateError, HashOf};
|
||||
use relay_utils::{relay_loop::Client as RelayClient, BlockNumberBase};
|
||||
use sp_core::Bytes;
|
||||
use sp_runtime::{traits::Header as HeaderT, DeserializeOwned};
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
/// Message receiving proof returned by the target Substrate node.
|
||||
pub type SubstrateMessagesReceivingProof<C> = (
|
||||
UnrewardedRelayersState,
|
||||
FromBridgedChainMessagesDeliveryProof<HashOf<C>>,
|
||||
);
|
||||
|
||||
/// Substrate client as Substrate messages target.
|
||||
pub struct SubstrateMessagesTarget<C: Chain, P> {
|
||||
client: Client<C>,
|
||||
lane: P,
|
||||
lane_id: LaneId,
|
||||
instance: InstanceId,
|
||||
}
|
||||
|
||||
impl<C: Chain, P> SubstrateMessagesTarget<C, P> {
|
||||
/// Create new Substrate headers target.
|
||||
pub fn new(client: Client<C>, lane: P, lane_id: LaneId, instance: InstanceId) -> Self {
|
||||
SubstrateMessagesTarget {
|
||||
client,
|
||||
lane,
|
||||
lane_id,
|
||||
instance,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Chain, P: SubstrateMessageLane> Clone for SubstrateMessagesTarget<C, P> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
client: self.client.clone(),
|
||||
lane: self.lane.clone(),
|
||||
lane_id: self.lane_id,
|
||||
instance: self.instance,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<C: Chain, P: SubstrateMessageLane> RelayClient for SubstrateMessagesTarget<C, P> {
|
||||
type Error = SubstrateError;
|
||||
|
||||
async fn reconnect(&mut self) -> Result<(), SubstrateError> {
|
||||
self.client.reconnect().await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<C, P> TargetClient<P> for SubstrateMessagesTarget<C, P>
|
||||
where
|
||||
C: Chain,
|
||||
C::Header: DeserializeOwned,
|
||||
C::Index: DeserializeOwned,
|
||||
<C::Header as HeaderT>::Number: BlockNumberBase,
|
||||
P: SubstrateMessageLane<
|
||||
MessagesReceivingProof = SubstrateMessagesReceivingProof<C>,
|
||||
TargetHeaderNumber = <C::Header as HeaderT>::Number,
|
||||
TargetHeaderHash = <C::Header as HeaderT>::Hash,
|
||||
>,
|
||||
P::SourceHeaderNumber: Decode,
|
||||
P::SourceHeaderHash: Decode,
|
||||
{
|
||||
async fn state(&self) -> Result<TargetClientState<P>, SubstrateError> {
|
||||
// we can't continue to deliver messages if target node is out of sync, because
|
||||
// it may have already received (some of) messages that we're going to deliver
|
||||
self.client.ensure_synced().await?;
|
||||
|
||||
read_client_state::<_, P::SourceHeaderHash, P::SourceHeaderNumber>(
|
||||
&self.client,
|
||||
P::BEST_FINALIZED_SOURCE_HEADER_ID_AT_TARGET,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn latest_received_nonce(
|
||||
&self,
|
||||
id: TargetHeaderIdOf<P>,
|
||||
) -> Result<(TargetHeaderIdOf<P>, MessageNonce), SubstrateError> {
|
||||
let encoded_response = self
|
||||
.client
|
||||
.state_call(
|
||||
P::INBOUND_LANE_LATEST_RECEIVED_NONCE_METHOD.into(),
|
||||
Bytes(self.lane_id.encode()),
|
||||
Some(id.1),
|
||||
)
|
||||
.await?;
|
||||
let latest_received_nonce: MessageNonce =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
Ok((id, latest_received_nonce))
|
||||
}
|
||||
|
||||
async fn latest_confirmed_received_nonce(
|
||||
&self,
|
||||
id: TargetHeaderIdOf<P>,
|
||||
) -> Result<(TargetHeaderIdOf<P>, MessageNonce), SubstrateError> {
|
||||
let encoded_response = self
|
||||
.client
|
||||
.state_call(
|
||||
P::INBOUND_LANE_LATEST_CONFIRMED_NONCE_METHOD.into(),
|
||||
Bytes(self.lane_id.encode()),
|
||||
Some(id.1),
|
||||
)
|
||||
.await?;
|
||||
let latest_received_nonce: MessageNonce =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
Ok((id, latest_received_nonce))
|
||||
}
|
||||
|
||||
async fn unrewarded_relayers_state(
|
||||
&self,
|
||||
id: TargetHeaderIdOf<P>,
|
||||
) -> Result<(TargetHeaderIdOf<P>, UnrewardedRelayersState), SubstrateError> {
|
||||
let encoded_response = self
|
||||
.client
|
||||
.state_call(
|
||||
P::INBOUND_LANE_UNREWARDED_RELAYERS_STATE.into(),
|
||||
Bytes(self.lane_id.encode()),
|
||||
Some(id.1),
|
||||
)
|
||||
.await?;
|
||||
let unrewarded_relayers_state: UnrewardedRelayersState =
|
||||
Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?;
|
||||
Ok((id, unrewarded_relayers_state))
|
||||
}
|
||||
|
||||
async fn prove_messages_receiving(
|
||||
&self,
|
||||
id: TargetHeaderIdOf<P>,
|
||||
) -> Result<(TargetHeaderIdOf<P>, P::MessagesReceivingProof), SubstrateError> {
|
||||
let (id, relayers_state) = self.unrewarded_relayers_state(id).await?;
|
||||
let proof = self
|
||||
.client
|
||||
.prove_messages_delivery(self.instance, self.lane_id, id.1)
|
||||
.await?;
|
||||
let proof = FromBridgedChainMessagesDeliveryProof {
|
||||
bridged_header_hash: id.1,
|
||||
storage_proof: proof,
|
||||
lane: self.lane_id,
|
||||
};
|
||||
Ok((id, (relayers_state, proof)))
|
||||
}
|
||||
|
||||
async fn submit_messages_proof(
|
||||
&self,
|
||||
generated_at_header: SourceHeaderIdOf<P>,
|
||||
nonces: RangeInclusive<MessageNonce>,
|
||||
proof: P::MessagesProof,
|
||||
) -> Result<RangeInclusive<MessageNonce>, SubstrateError> {
|
||||
let tx = self
|
||||
.lane
|
||||
.make_messages_delivery_transaction(generated_at_header, nonces.clone(), proof)
|
||||
.await?;
|
||||
self.client.submit_extrinsic(Bytes(tx.encode())).await?;
|
||||
Ok(nonces)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
// 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/>.
|
||||
|
||||
//! Millau-to-Rialto headers sync entrypoint.
|
||||
|
||||
use crate::{
|
||||
headers_pipeline::{SubstrateHeadersSyncPipeline, SubstrateHeadersToSubstrate},
|
||||
MillauClient, RialtoClient,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bp_millau::{
|
||||
BEST_MILLAU_BLOCKS_METHOD, FINALIZED_MILLAU_BLOCK_METHOD, INCOMPLETE_MILLAU_HEADERS_METHOD,
|
||||
IS_KNOWN_MILLAU_BLOCK_METHOD,
|
||||
};
|
||||
use headers_relay::sync_types::QueuedHeader;
|
||||
use relay_millau_client::{HeaderId as MillauHeaderId, Millau, SyncHeader as MillauSyncHeader};
|
||||
use relay_rialto_client::{BridgeMillauCall, Rialto, SigningParams as RialtoSigningParams};
|
||||
use relay_substrate_client::{Error as SubstrateError, TransactionSignScheme};
|
||||
use sp_core::Pair;
|
||||
use sp_runtime::Justification;
|
||||
|
||||
/// Millau-to-Rialto headers sync pipeline.
|
||||
pub(crate) type MillauHeadersToRialto =
|
||||
SubstrateHeadersToSubstrate<Millau, MillauSyncHeader, Rialto, RialtoSigningParams>;
|
||||
/// Millau header in-the-queue.
|
||||
type QueuedMillauHeader = QueuedHeader<MillauHeadersToRialto>;
|
||||
|
||||
#[async_trait]
|
||||
impl SubstrateHeadersSyncPipeline for MillauHeadersToRialto {
|
||||
const BEST_BLOCK_METHOD: &'static str = BEST_MILLAU_BLOCKS_METHOD;
|
||||
const FINALIZED_BLOCK_METHOD: &'static str = FINALIZED_MILLAU_BLOCK_METHOD;
|
||||
const IS_KNOWN_BLOCK_METHOD: &'static str = IS_KNOWN_MILLAU_BLOCK_METHOD;
|
||||
const INCOMPLETE_HEADERS_METHOD: &'static str = INCOMPLETE_MILLAU_HEADERS_METHOD;
|
||||
|
||||
type SignedTransaction = <Rialto as TransactionSignScheme>::SignedTransaction;
|
||||
|
||||
async fn make_submit_header_transaction(
|
||||
&self,
|
||||
header: QueuedMillauHeader,
|
||||
) -> Result<Self::SignedTransaction, SubstrateError> {
|
||||
let account_id = self.target_sign.signer.public().as_array_ref().clone().into();
|
||||
let nonce = self.target_client.next_account_index(account_id).await?;
|
||||
let call = BridgeMillauCall::import_signed_header(header.header().clone().into_inner()).into();
|
||||
let transaction = Rialto::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call);
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
async fn make_complete_header_transaction(
|
||||
&self,
|
||||
id: MillauHeaderId,
|
||||
completion: Justification,
|
||||
) -> Result<Self::SignedTransaction, SubstrateError> {
|
||||
let account_id = self.target_sign.signer.public().as_array_ref().clone().into();
|
||||
let nonce = self.target_client.next_account_index(account_id).await?;
|
||||
let call = BridgeMillauCall::finalize_header(id.1, completion).into();
|
||||
let transaction = Rialto::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call);
|
||||
Ok(transaction)
|
||||
}
|
||||
}
|
||||
|
||||
/// Run Millau-to-Rialto headers sync.
|
||||
pub async fn run(
|
||||
millau_client: MillauClient,
|
||||
rialto_client: RialtoClient,
|
||||
rialto_sign: RialtoSigningParams,
|
||||
metrics_params: Option<relay_utils::metrics::MetricsParams>,
|
||||
) {
|
||||
crate::headers_pipeline::run(
|
||||
MillauHeadersToRialto::new(rialto_client.clone(), rialto_sign),
|
||||
millau_client,
|
||||
rialto_client,
|
||||
metrics_params,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
// 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/>.
|
||||
|
||||
//! Millau-to-Rialto messages sync entrypoint.
|
||||
|
||||
use crate::messages_lane::{select_delivery_transaction_limits, SubstrateMessageLane, SubstrateMessageLaneToSubstrate};
|
||||
use crate::messages_source::SubstrateMessagesSource;
|
||||
use crate::messages_target::SubstrateMessagesTarget;
|
||||
use crate::{MillauClient, RialtoClient};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bp_message_lane::{LaneId, MessageNonce};
|
||||
use bp_runtime::{MILLAU_BRIDGE_INSTANCE, RIALTO_BRIDGE_INSTANCE};
|
||||
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
|
||||
use codec::Encode;
|
||||
use frame_support::dispatch::GetDispatchInfo;
|
||||
use messages_relay::message_lane::MessageLane;
|
||||
use relay_millau_client::{HeaderId as MillauHeaderId, Millau, SigningParams as MillauSigningParams};
|
||||
use relay_rialto_client::{HeaderId as RialtoHeaderId, Rialto, SigningParams as RialtoSigningParams};
|
||||
use relay_substrate_client::{Chain, Error as SubstrateError, TransactionSignScheme};
|
||||
use relay_utils::metrics::MetricsParams;
|
||||
use sp_core::Pair;
|
||||
use std::{ops::RangeInclusive, time::Duration};
|
||||
|
||||
/// Millau-to-Rialto message lane.
|
||||
type MillauMessagesToRialto = SubstrateMessageLaneToSubstrate<Millau, MillauSigningParams, Rialto, RialtoSigningParams>;
|
||||
|
||||
#[async_trait]
|
||||
impl SubstrateMessageLane for MillauMessagesToRialto {
|
||||
const OUTBOUND_LANE_MESSAGES_DISPATCH_WEIGHT_METHOD: &'static str =
|
||||
bp_rialto::TO_RIALTO_MESSAGES_DISPATCH_WEIGHT_METHOD;
|
||||
const OUTBOUND_LANE_LATEST_GENERATED_NONCE_METHOD: &'static str =
|
||||
bp_rialto::TO_RIALTO_LATEST_GENERATED_NONCE_METHOD;
|
||||
const OUTBOUND_LANE_LATEST_RECEIVED_NONCE_METHOD: &'static str = bp_rialto::TO_RIALTO_LATEST_RECEIVED_NONCE_METHOD;
|
||||
|
||||
const INBOUND_LANE_LATEST_RECEIVED_NONCE_METHOD: &'static str = bp_millau::FROM_MILLAU_LATEST_RECEIVED_NONCE_METHOD;
|
||||
const INBOUND_LANE_LATEST_CONFIRMED_NONCE_METHOD: &'static str =
|
||||
bp_millau::FROM_MILLAU_LATEST_CONFIRMED_NONCE_METHOD;
|
||||
const INBOUND_LANE_UNREWARDED_RELAYERS_STATE: &'static str = bp_millau::FROM_MILLAU_UNREWARDED_RELAYERS_STATE;
|
||||
|
||||
const BEST_FINALIZED_SOURCE_HEADER_ID_AT_TARGET: &'static str = bp_millau::FINALIZED_MILLAU_BLOCK_METHOD;
|
||||
const BEST_FINALIZED_TARGET_HEADER_ID_AT_SOURCE: &'static str = bp_rialto::FINALIZED_RIALTO_BLOCK_METHOD;
|
||||
|
||||
type SourceSignedTransaction = <Millau as TransactionSignScheme>::SignedTransaction;
|
||||
type TargetSignedTransaction = <Rialto as TransactionSignScheme>::SignedTransaction;
|
||||
|
||||
async fn make_messages_receiving_proof_transaction(
|
||||
&self,
|
||||
_generated_at_block: RialtoHeaderId,
|
||||
proof: <Self as MessageLane>::MessagesReceivingProof,
|
||||
) -> Result<Self::SourceSignedTransaction, SubstrateError> {
|
||||
let (relayers_state, proof) = proof;
|
||||
let account_id = self.source_sign.signer.public().as_array_ref().clone().into();
|
||||
let nonce = self.source_client.next_account_index(account_id).await?;
|
||||
let call: millau_runtime::Call =
|
||||
millau_runtime::MessageLaneCall::receive_messages_delivery_proof(proof, relayers_state).into();
|
||||
let call_weight = call.get_dispatch_info().weight;
|
||||
let transaction = Millau::sign_transaction(&self.source_client, &self.source_sign.signer, nonce, call);
|
||||
log::trace!(
|
||||
target: "bridge",
|
||||
"Prepared Rialto -> Millau confirmation transaction. Weight: {}/{}, size: {}/{}",
|
||||
call_weight,
|
||||
bp_millau::max_extrinsic_weight(),
|
||||
transaction.encode().len(),
|
||||
bp_millau::max_extrinsic_size(),
|
||||
);
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
async fn make_messages_delivery_transaction(
|
||||
&self,
|
||||
_generated_at_header: MillauHeaderId,
|
||||
_nonces: RangeInclusive<MessageNonce>,
|
||||
proof: <Self as MessageLane>::MessagesProof,
|
||||
) -> Result<Self::TargetSignedTransaction, SubstrateError> {
|
||||
let (dispatch_weight, proof) = proof;
|
||||
let FromBridgedChainMessagesProof {
|
||||
ref nonces_start,
|
||||
ref nonces_end,
|
||||
..
|
||||
} = proof;
|
||||
let messages_count = nonces_end - nonces_start + 1;
|
||||
let account_id = self.target_sign.signer.public().as_array_ref().clone().into();
|
||||
let nonce = self.target_client.next_account_index(account_id).await?;
|
||||
let call: rialto_runtime::Call = rialto_runtime::MessageLaneCall::receive_messages_proof(
|
||||
self.relayer_id_at_source.clone(),
|
||||
proof,
|
||||
messages_count as _,
|
||||
dispatch_weight,
|
||||
)
|
||||
.into();
|
||||
let call_weight = call.get_dispatch_info().weight;
|
||||
let transaction = Rialto::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call);
|
||||
log::trace!(
|
||||
target: "bridge",
|
||||
"Prepared Millau -> Rialto delivery transaction. Weight: {}/{}, size: {}/{}",
|
||||
call_weight,
|
||||
bp_rialto::max_extrinsic_weight(),
|
||||
transaction.encode().len(),
|
||||
bp_rialto::max_extrinsic_size(),
|
||||
);
|
||||
Ok(transaction)
|
||||
}
|
||||
}
|
||||
|
||||
/// Millau node as messages source.
|
||||
type MillauSourceClient = SubstrateMessagesSource<Millau, MillauMessagesToRialto>;
|
||||
|
||||
/// Rialto node as messages target.
|
||||
type RialtoTargetClient = SubstrateMessagesTarget<Rialto, MillauMessagesToRialto>;
|
||||
|
||||
/// Run Millau-to-Rialto messages sync.
|
||||
pub fn run(
|
||||
millau_client: MillauClient,
|
||||
millau_sign: MillauSigningParams,
|
||||
rialto_client: RialtoClient,
|
||||
rialto_sign: RialtoSigningParams,
|
||||
lane_id: LaneId,
|
||||
metrics_params: Option<MetricsParams>,
|
||||
) {
|
||||
let stall_timeout = Duration::from_secs(5 * 60);
|
||||
let relayer_id_at_millau = millau_sign.signer.public().as_array_ref().clone().into();
|
||||
|
||||
let lane = MillauMessagesToRialto {
|
||||
source_client: millau_client.clone(),
|
||||
source_sign: millau_sign,
|
||||
target_client: rialto_client.clone(),
|
||||
target_sign: rialto_sign,
|
||||
relayer_id_at_source: relayer_id_at_millau,
|
||||
};
|
||||
|
||||
// 2/3 is reserved for proofs and tx overhead
|
||||
let max_messages_size_in_single_batch = bp_rialto::max_extrinsic_size() as usize / 3;
|
||||
// TODO: use Millau weights after https://github.com/paritytech/parity-bridges-common/issues/390
|
||||
let (max_messages_in_single_batch, max_messages_weight_in_single_batch) =
|
||||
select_delivery_transaction_limits::<pallet_message_lane::weights::RialtoWeight<millau_runtime::Runtime>>(
|
||||
bp_rialto::max_extrinsic_weight(),
|
||||
bp_rialto::MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE,
|
||||
);
|
||||
|
||||
log::info!(
|
||||
target: "bridge",
|
||||
"Starting Millau -> Rialto messages relay.\n\t\
|
||||
Millau relayer account id: {:?}\n\t\
|
||||
Max messages in single transaction: {}\n\t\
|
||||
Max messages size in single transaction: {}\n\t\
|
||||
Max messages weight in single transaction: {}",
|
||||
lane.relayer_id_at_source,
|
||||
max_messages_in_single_batch,
|
||||
max_messages_size_in_single_batch,
|
||||
max_messages_weight_in_single_batch,
|
||||
);
|
||||
|
||||
messages_relay::message_lane_loop::run(
|
||||
messages_relay::message_lane_loop::Params {
|
||||
lane: lane_id,
|
||||
source_tick: Millau::AVERAGE_BLOCK_INTERVAL,
|
||||
target_tick: Rialto::AVERAGE_BLOCK_INTERVAL,
|
||||
reconnect_delay: relay_utils::relay_loop::RECONNECT_DELAY,
|
||||
stall_timeout,
|
||||
delivery_params: messages_relay::message_lane_loop::MessageDeliveryParams {
|
||||
max_unrewarded_relayer_entries_at_target: bp_rialto::MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE,
|
||||
max_unconfirmed_nonces_at_target: bp_rialto::MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE,
|
||||
max_messages_in_single_batch,
|
||||
max_messages_weight_in_single_batch,
|
||||
max_messages_size_in_single_batch,
|
||||
},
|
||||
},
|
||||
MillauSourceClient::new(millau_client, lane.clone(), lane_id, RIALTO_BRIDGE_INSTANCE),
|
||||
RialtoTargetClient::new(rialto_client, lane, lane_id, MILLAU_BRIDGE_INSTANCE),
|
||||
metrics_params,
|
||||
futures::future::pending(),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
// 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/>.
|
||||
|
||||
//! Rialto-to-Millau headers sync entrypoint.
|
||||
|
||||
use crate::{
|
||||
headers_pipeline::{SubstrateHeadersSyncPipeline, SubstrateHeadersToSubstrate},
|
||||
MillauClient, RialtoClient,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bp_rialto::{
|
||||
BEST_RIALTO_BLOCKS_METHOD, FINALIZED_RIALTO_BLOCK_METHOD, INCOMPLETE_RIALTO_HEADERS_METHOD,
|
||||
IS_KNOWN_RIALTO_BLOCK_METHOD,
|
||||
};
|
||||
use headers_relay::sync_types::QueuedHeader;
|
||||
use relay_millau_client::{BridgeRialtoCall, Millau, SigningParams as MillauSigningParams};
|
||||
use relay_rialto_client::{HeaderId as RialtoHeaderId, Rialto, SyncHeader as RialtoSyncHeader};
|
||||
use relay_substrate_client::{Error as SubstrateError, TransactionSignScheme};
|
||||
use sp_core::Pair;
|
||||
use sp_runtime::Justification;
|
||||
|
||||
/// Rialto-to-Millau headers sync pipeline.
|
||||
type RialtoHeadersToMillau = SubstrateHeadersToSubstrate<Rialto, RialtoSyncHeader, Millau, MillauSigningParams>;
|
||||
/// Rialto header in-the-queue.
|
||||
type QueuedRialtoHeader = QueuedHeader<RialtoHeadersToMillau>;
|
||||
|
||||
#[async_trait]
|
||||
impl SubstrateHeadersSyncPipeline for RialtoHeadersToMillau {
|
||||
const BEST_BLOCK_METHOD: &'static str = BEST_RIALTO_BLOCKS_METHOD;
|
||||
const FINALIZED_BLOCK_METHOD: &'static str = FINALIZED_RIALTO_BLOCK_METHOD;
|
||||
const IS_KNOWN_BLOCK_METHOD: &'static str = IS_KNOWN_RIALTO_BLOCK_METHOD;
|
||||
const INCOMPLETE_HEADERS_METHOD: &'static str = INCOMPLETE_RIALTO_HEADERS_METHOD;
|
||||
|
||||
type SignedTransaction = <Millau as TransactionSignScheme>::SignedTransaction;
|
||||
|
||||
async fn make_submit_header_transaction(
|
||||
&self,
|
||||
header: QueuedRialtoHeader,
|
||||
) -> Result<Self::SignedTransaction, SubstrateError> {
|
||||
let account_id = self.target_sign.signer.public().as_array_ref().clone().into();
|
||||
let nonce = self.target_client.next_account_index(account_id).await?;
|
||||
let call = BridgeRialtoCall::import_signed_header(header.header().clone().into_inner()).into();
|
||||
let transaction = Millau::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call);
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
async fn make_complete_header_transaction(
|
||||
&self,
|
||||
id: RialtoHeaderId,
|
||||
completion: Justification,
|
||||
) -> Result<Self::SignedTransaction, SubstrateError> {
|
||||
let account_id = self.target_sign.signer.public().as_array_ref().clone().into();
|
||||
let nonce = self.target_client.next_account_index(account_id).await?;
|
||||
let call = BridgeRialtoCall::finalize_header(id.1, completion).into();
|
||||
let transaction = Millau::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call);
|
||||
Ok(transaction)
|
||||
}
|
||||
}
|
||||
|
||||
/// Run Rialto-to-Millau headers sync.
|
||||
pub async fn run(
|
||||
rialto_client: RialtoClient,
|
||||
millau_client: MillauClient,
|
||||
millau_sign: MillauSigningParams,
|
||||
metrics_params: Option<relay_utils::metrics::MetricsParams>,
|
||||
) {
|
||||
crate::headers_pipeline::run(
|
||||
RialtoHeadersToMillau::new(millau_client.clone(), millau_sign),
|
||||
rialto_client,
|
||||
millau_client,
|
||||
metrics_params,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
// 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/>.
|
||||
|
||||
//! Rialto-to-Millau messages sync entrypoint.
|
||||
|
||||
use crate::messages_lane::{select_delivery_transaction_limits, SubstrateMessageLane, SubstrateMessageLaneToSubstrate};
|
||||
use crate::messages_source::SubstrateMessagesSource;
|
||||
use crate::messages_target::SubstrateMessagesTarget;
|
||||
use crate::{MillauClient, RialtoClient};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bp_message_lane::{LaneId, MessageNonce};
|
||||
use bp_runtime::{MILLAU_BRIDGE_INSTANCE, RIALTO_BRIDGE_INSTANCE};
|
||||
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
|
||||
use codec::Encode;
|
||||
use frame_support::dispatch::GetDispatchInfo;
|
||||
use messages_relay::message_lane::MessageLane;
|
||||
use relay_millau_client::{HeaderId as MillauHeaderId, Millau, SigningParams as MillauSigningParams};
|
||||
use relay_rialto_client::{HeaderId as RialtoHeaderId, Rialto, SigningParams as RialtoSigningParams};
|
||||
use relay_substrate_client::{Chain, Error as SubstrateError, TransactionSignScheme};
|
||||
use relay_utils::metrics::MetricsParams;
|
||||
use sp_core::Pair;
|
||||
use std::{ops::RangeInclusive, time::Duration};
|
||||
|
||||
/// Rialto-to-Millau message lane.
|
||||
type RialtoMessagesToMillau = SubstrateMessageLaneToSubstrate<Rialto, RialtoSigningParams, Millau, MillauSigningParams>;
|
||||
|
||||
#[async_trait]
|
||||
impl SubstrateMessageLane for RialtoMessagesToMillau {
|
||||
const OUTBOUND_LANE_MESSAGES_DISPATCH_WEIGHT_METHOD: &'static str =
|
||||
bp_millau::TO_MILLAU_MESSAGES_DISPATCH_WEIGHT_METHOD;
|
||||
const OUTBOUND_LANE_LATEST_GENERATED_NONCE_METHOD: &'static str =
|
||||
bp_millau::TO_MILLAU_LATEST_GENERATED_NONCE_METHOD;
|
||||
const OUTBOUND_LANE_LATEST_RECEIVED_NONCE_METHOD: &'static str = bp_millau::TO_MILLAU_LATEST_RECEIVED_NONCE_METHOD;
|
||||
|
||||
const INBOUND_LANE_LATEST_RECEIVED_NONCE_METHOD: &'static str = bp_rialto::FROM_RIALTO_LATEST_RECEIVED_NONCE_METHOD;
|
||||
const INBOUND_LANE_LATEST_CONFIRMED_NONCE_METHOD: &'static str =
|
||||
bp_rialto::FROM_RIALTO_LATEST_CONFIRMED_NONCE_METHOD;
|
||||
const INBOUND_LANE_UNREWARDED_RELAYERS_STATE: &'static str = bp_rialto::FROM_RIALTO_UNREWARDED_RELAYERS_STATE;
|
||||
|
||||
const BEST_FINALIZED_SOURCE_HEADER_ID_AT_TARGET: &'static str = bp_rialto::FINALIZED_RIALTO_BLOCK_METHOD;
|
||||
const BEST_FINALIZED_TARGET_HEADER_ID_AT_SOURCE: &'static str = bp_millau::FINALIZED_MILLAU_BLOCK_METHOD;
|
||||
|
||||
type SourceSignedTransaction = <Rialto as TransactionSignScheme>::SignedTransaction;
|
||||
type TargetSignedTransaction = <Millau as TransactionSignScheme>::SignedTransaction;
|
||||
|
||||
async fn make_messages_receiving_proof_transaction(
|
||||
&self,
|
||||
_generated_at_block: MillauHeaderId,
|
||||
proof: <Self as MessageLane>::MessagesReceivingProof,
|
||||
) -> Result<Self::SourceSignedTransaction, SubstrateError> {
|
||||
let (relayers_state, proof) = proof;
|
||||
let account_id = self.source_sign.signer.public().as_array_ref().clone().into();
|
||||
let nonce = self.source_client.next_account_index(account_id).await?;
|
||||
let call: rialto_runtime::Call =
|
||||
rialto_runtime::MessageLaneCall::receive_messages_delivery_proof(proof, relayers_state).into();
|
||||
let call_weight = call.get_dispatch_info().weight;
|
||||
let transaction = Rialto::sign_transaction(&self.source_client, &self.source_sign.signer, nonce, call);
|
||||
log::trace!(
|
||||
target: "bridge",
|
||||
"Prepared Millau -> Rialto confirmation transaction. Weight: {}/{}, size: {}/{}",
|
||||
call_weight,
|
||||
bp_rialto::max_extrinsic_weight(),
|
||||
transaction.encode().len(),
|
||||
bp_rialto::max_extrinsic_size(),
|
||||
);
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
async fn make_messages_delivery_transaction(
|
||||
&self,
|
||||
_generated_at_header: RialtoHeaderId,
|
||||
_nonces: RangeInclusive<MessageNonce>,
|
||||
proof: <Self as MessageLane>::MessagesProof,
|
||||
) -> Result<Self::TargetSignedTransaction, SubstrateError> {
|
||||
let (dispatch_weight, proof) = proof;
|
||||
let FromBridgedChainMessagesProof {
|
||||
ref nonces_start,
|
||||
ref nonces_end,
|
||||
..
|
||||
} = proof;
|
||||
let messages_count = nonces_end - nonces_start + 1;
|
||||
let account_id = self.target_sign.signer.public().as_array_ref().clone().into();
|
||||
let nonce = self.target_client.next_account_index(account_id).await?;
|
||||
let call: millau_runtime::Call = millau_runtime::MessageLaneCall::receive_messages_proof(
|
||||
self.relayer_id_at_source.clone(),
|
||||
proof,
|
||||
messages_count as _,
|
||||
dispatch_weight,
|
||||
)
|
||||
.into();
|
||||
let call_weight = call.get_dispatch_info().weight;
|
||||
let transaction = Millau::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call);
|
||||
log::trace!(
|
||||
target: "bridge",
|
||||
"Prepared Rialto -> Millau delivery transaction. Weight: {}/{}, size: {}/{}",
|
||||
call_weight,
|
||||
bp_millau::max_extrinsic_weight(),
|
||||
transaction.encode().len(),
|
||||
bp_millau::max_extrinsic_size(),
|
||||
);
|
||||
Ok(transaction)
|
||||
}
|
||||
}
|
||||
|
||||
/// Rialto node as messages source.
|
||||
type RialtoSourceClient = SubstrateMessagesSource<Rialto, RialtoMessagesToMillau>;
|
||||
|
||||
/// Millau node as messages target.
|
||||
type MillauTargetClient = SubstrateMessagesTarget<Millau, RialtoMessagesToMillau>;
|
||||
|
||||
/// Run Rialto-to-Millau messages sync.
|
||||
pub fn run(
|
||||
rialto_client: RialtoClient,
|
||||
rialto_sign: RialtoSigningParams,
|
||||
millau_client: MillauClient,
|
||||
millau_sign: MillauSigningParams,
|
||||
lane_id: LaneId,
|
||||
metrics_params: Option<MetricsParams>,
|
||||
) {
|
||||
let stall_timeout = Duration::from_secs(5 * 60);
|
||||
let relayer_id_at_rialto = rialto_sign.signer.public().as_array_ref().clone().into();
|
||||
|
||||
let lane = RialtoMessagesToMillau {
|
||||
source_client: rialto_client.clone(),
|
||||
source_sign: rialto_sign,
|
||||
target_client: millau_client.clone(),
|
||||
target_sign: millau_sign,
|
||||
relayer_id_at_source: relayer_id_at_rialto,
|
||||
};
|
||||
|
||||
// 2/3 is reserved for proofs and tx overhead
|
||||
let max_messages_size_in_single_batch = bp_millau::max_extrinsic_size() as usize / 3;
|
||||
let (max_messages_in_single_batch, max_messages_weight_in_single_batch) =
|
||||
select_delivery_transaction_limits::<pallet_message_lane::weights::RialtoWeight<rialto_runtime::Runtime>>(
|
||||
bp_millau::max_extrinsic_weight(),
|
||||
bp_millau::MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE,
|
||||
);
|
||||
|
||||
log::info!(
|
||||
target: "bridge",
|
||||
"Starting Rialto -> Millau messages relay.\n\t\
|
||||
Rialto relayer account id: {:?}\n\t\
|
||||
Max messages in single transaction: {}\n\t\
|
||||
Max messages size in single transaction: {}\n\t\
|
||||
Max messages weight in single transaction: {}",
|
||||
lane.relayer_id_at_source,
|
||||
max_messages_in_single_batch,
|
||||
max_messages_size_in_single_batch,
|
||||
max_messages_weight_in_single_batch,
|
||||
);
|
||||
|
||||
messages_relay::message_lane_loop::run(
|
||||
messages_relay::message_lane_loop::Params {
|
||||
lane: lane_id,
|
||||
source_tick: Rialto::AVERAGE_BLOCK_INTERVAL,
|
||||
target_tick: Millau::AVERAGE_BLOCK_INTERVAL,
|
||||
reconnect_delay: relay_utils::relay_loop::RECONNECT_DELAY,
|
||||
stall_timeout,
|
||||
delivery_params: messages_relay::message_lane_loop::MessageDeliveryParams {
|
||||
max_unrewarded_relayer_entries_at_target: bp_millau::MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE,
|
||||
max_unconfirmed_nonces_at_target: bp_millau::MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE,
|
||||
max_messages_in_single_batch,
|
||||
max_messages_weight_in_single_batch,
|
||||
max_messages_size_in_single_batch,
|
||||
},
|
||||
},
|
||||
RialtoSourceClient::new(rialto_client, lane.clone(), lane_id, MILLAU_BRIDGE_INSTANCE),
|
||||
MillauTargetClient::new(millau_client, lane, lane_id, RIALTO_BRIDGE_INSTANCE),
|
||||
metrics_params,
|
||||
futures::future::pending(),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user