Skip to content

Commit

Permalink
CLI: naming clean-up. (paritytech#897)
Browse files Browse the repository at this point in the history
* Small naming clean up.

* Small naming clean up.

* Add Rococo module to CLI

Co-authored-by: Svyatoslav Nikolsky <[email protected]>
Co-authored-by: Hernando Castano <[email protected]>
  • Loading branch information
3 people authored and serban300 committed Apr 9, 2024
1 parent bc5fdd2 commit 88f9cca
Show file tree
Hide file tree
Showing 16 changed files with 306 additions and 214 deletions.
101 changes: 101 additions & 0 deletions bridges/relays/bin-substrate/src/chains/millau.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2019-2021 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 chain specification for CLI.

use crate::cli::{
bridge,
encode_call::{self, Call, CliEncodeCall},
encode_message, send_message, CliChain,
};
use codec::Decode;
use frame_support::weights::{GetDispatchInfo, Weight};
use pallet_bridge_dispatch::{CallOrigin, MessagePayload};
use relay_millau_client::Millau;
use sp_version::RuntimeVersion;

impl CliEncodeCall for Millau {
fn max_extrinsic_size() -> u32 {
bp_millau::max_extrinsic_size()
}

fn encode_call(call: &Call) -> anyhow::Result<Self::Call> {
Ok(match call {
Call::Raw { data } => Decode::decode(&mut &*data.0)?,
Call::Remark { remark_payload, .. } => millau_runtime::Call::System(millau_runtime::SystemCall::remark(
remark_payload.as_ref().map(|x| x.0.clone()).unwrap_or_default(),
)),
Call::Transfer { recipient, amount } => millau_runtime::Call::Balances(
millau_runtime::BalancesCall::transfer(recipient.raw_id(), amount.cast()),
),
Call::BridgeSendMessage {
lane,
payload,
fee,
bridge_instance_index,
} => match *bridge_instance_index {
bridge::MILLAU_TO_RIALTO_INDEX => {
let payload = Decode::decode(&mut &*payload.0)?;
millau_runtime::Call::BridgeRialtoMessages(millau_runtime::MessagesCall::send_message(
lane.0,
payload,
fee.cast(),
))
}
_ => anyhow::bail!(
"Unsupported target bridge pallet with instance index: {}",
bridge_instance_index
),
},
})
}
}

impl CliChain for Millau {
const RUNTIME_VERSION: RuntimeVersion = millau_runtime::VERSION;

type KeyPair = sp_core::sr25519::Pair;
type MessagePayload = MessagePayload<bp_millau::AccountId, bp_rialto::AccountSigner, bp_rialto::Signature, Vec<u8>>;

fn ss58_format() -> u16 {
millau_runtime::SS58Prefix::get() as u16
}

fn max_extrinsic_weight() -> Weight {
bp_millau::max_extrinsic_weight()
}

// TODO [#854|#843] support multiple bridges?
fn encode_message(message: encode_message::MessagePayload) -> Result<Self::MessagePayload, String> {
match message {
encode_message::MessagePayload::Raw { data } => MessagePayload::decode(&mut &*data.0)
.map_err(|e| format!("Failed to decode Millau's MessagePayload: {:?}", e)),
encode_message::MessagePayload::Call { mut call, mut sender } => {
type Source = Millau;
type Target = relay_rialto_client::Rialto;

sender.enforce_chain::<Source>();
let spec_version = Target::RUNTIME_VERSION.spec_version;
let origin = CallOrigin::SourceAccount(sender.raw_id());
encode_call::preprocess_call::<Source, Target>(&mut call, bridge::MILLAU_TO_RIALTO_INDEX);
let call = Target::encode_call(&call).map_err(|e| e.to_string())?;
let weight = call.get_dispatch_info().weight;

Ok(send_message::message_payload(spec_version, weight, origin, &call))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// 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 <> Millau Bridge commands.
//! Chain-specific relayer configuration.

pub mod millau_headers_to_rialto;
pub mod millau_messages_to_rialto;
Expand All @@ -24,20 +24,12 @@ pub mod rococo_headers_to_westend;
pub mod westend_headers_to_millau;
pub mod westend_headers_to_rococo;

use crate::cli::{
bridge,
encode_call::{self, Call, CliEncodeCall},
encode_message, send_message, CliChain,
};
use codec::Decode;
use frame_support::weights::{GetDispatchInfo, Weight};
use pallet_bridge_dispatch::{CallOrigin, MessagePayload};
use relay_millau_client::Millau;
use relay_rialto_client::Rialto;
use relay_rococo_client::Rococo;
mod millau;
mod rialto;
mod rococo;
mod westend;

use relay_utils::metrics::{FloatJsonValueMetric, MetricsParams};
use relay_westend_client::Westend;
use sp_version::RuntimeVersion;

pub(crate) fn add_polkadot_kusama_price_metrics<T: finality_relay::FinalitySyncPipeline>(
params: MetricsParams,
Expand Down Expand Up @@ -72,192 +64,14 @@ pub(crate) fn add_polkadot_kusama_price_metrics<T: finality_relay::FinalitySyncP
)
}

impl CliEncodeCall for Millau {
fn max_extrinsic_size() -> u32 {
bp_millau::max_extrinsic_size()
}

fn encode_call(call: &Call) -> anyhow::Result<Self::Call> {
Ok(match call {
Call::Raw { data } => Decode::decode(&mut &*data.0)?,
Call::Remark { remark_payload, .. } => millau_runtime::Call::System(millau_runtime::SystemCall::remark(
remark_payload.as_ref().map(|x| x.0.clone()).unwrap_or_default(),
)),
Call::Transfer { recipient, amount } => millau_runtime::Call::Balances(
millau_runtime::BalancesCall::transfer(recipient.raw_id(), amount.cast()),
),
Call::BridgeSendMessage {
lane,
payload,
fee,
bridge_instance_index,
} => match *bridge_instance_index {
bridge::MILLAU_TO_RIALTO_INDEX => {
let payload = Decode::decode(&mut &*payload.0)?;
millau_runtime::Call::BridgeRialtoMessages(millau_runtime::MessagesCall::send_message(
lane.0,
payload,
fee.cast(),
))
}
_ => anyhow::bail!(
"Unsupported target bridge pallet with instance index: {}",
bridge_instance_index
),
},
})
}
}

impl CliChain for Millau {
const RUNTIME_VERSION: RuntimeVersion = millau_runtime::VERSION;

type KeyPair = sp_core::sr25519::Pair;
type MessagePayload = MessagePayload<bp_millau::AccountId, bp_rialto::AccountSigner, bp_rialto::Signature, Vec<u8>>;

fn ss58_format() -> u16 {
millau_runtime::SS58Prefix::get() as u16
}

fn max_extrinsic_weight() -> Weight {
bp_millau::max_extrinsic_weight()
}

// TODO [#854|#843] support multiple bridges?
fn encode_message(message: encode_message::MessagePayload) -> Result<Self::MessagePayload, String> {
match message {
encode_message::MessagePayload::Raw { data } => MessagePayload::decode(&mut &*data.0)
.map_err(|e| format!("Failed to decode Millau's MessagePayload: {:?}", e)),
encode_message::MessagePayload::Call { mut call, mut sender } => {
type Source = Millau;
type Target = Rialto;

sender.enforce_chain::<Source>();
let spec_version = Target::RUNTIME_VERSION.spec_version;
let origin = CallOrigin::SourceAccount(sender.raw_id());
encode_call::preprocess_call::<Source, Target>(&mut call, bridge::MILLAU_TO_RIALTO_INDEX);
let call = Target::encode_call(&call).map_err(|e| e.to_string())?;
let weight = call.get_dispatch_info().weight;

Ok(send_message::message_payload(spec_version, weight, origin, &call))
}
}
}
}

impl CliEncodeCall for Rialto {
fn max_extrinsic_size() -> u32 {
bp_rialto::max_extrinsic_size()
}

fn encode_call(call: &Call) -> anyhow::Result<Self::Call> {
Ok(match call {
Call::Raw { data } => Decode::decode(&mut &*data.0)?,
Call::Remark { remark_payload, .. } => rialto_runtime::Call::System(rialto_runtime::SystemCall::remark(
remark_payload.as_ref().map(|x| x.0.clone()).unwrap_or_default(),
)),
Call::Transfer { recipient, amount } => {
rialto_runtime::Call::Balances(rialto_runtime::BalancesCall::transfer(recipient.raw_id(), amount.0))
}
Call::BridgeSendMessage {
lane,
payload,
fee,
bridge_instance_index,
} => match *bridge_instance_index {
bridge::RIALTO_TO_MILLAU_INDEX => {
let payload = Decode::decode(&mut &*payload.0)?;
rialto_runtime::Call::BridgeMillauMessages(rialto_runtime::MessagesCall::send_message(
lane.0, payload, fee.0,
))
}
_ => anyhow::bail!(
"Unsupported target bridge pallet with instance index: {}",
bridge_instance_index
),
},
})
}
}

impl CliChain for Rialto {
const RUNTIME_VERSION: RuntimeVersion = rialto_runtime::VERSION;

type KeyPair = sp_core::sr25519::Pair;
type MessagePayload = MessagePayload<bp_rialto::AccountId, bp_millau::AccountSigner, bp_millau::Signature, Vec<u8>>;

fn ss58_format() -> u16 {
rialto_runtime::SS58Prefix::get() as u16
}

fn max_extrinsic_weight() -> Weight {
bp_rialto::max_extrinsic_weight()
}

fn encode_message(message: encode_message::MessagePayload) -> Result<Self::MessagePayload, String> {
match message {
encode_message::MessagePayload::Raw { data } => MessagePayload::decode(&mut &*data.0)
.map_err(|e| format!("Failed to decode Rialto's MessagePayload: {:?}", e)),
encode_message::MessagePayload::Call { mut call, mut sender } => {
type Source = Rialto;
type Target = Millau;

sender.enforce_chain::<Source>();
let spec_version = Target::RUNTIME_VERSION.spec_version;
let origin = CallOrigin::SourceAccount(sender.raw_id());
encode_call::preprocess_call::<Source, Target>(&mut call, bridge::RIALTO_TO_MILLAU_INDEX);
let call = Target::encode_call(&call).map_err(|e| e.to_string())?;
let weight = call.get_dispatch_info().weight;

Ok(send_message::message_payload(spec_version, weight, origin, &call))
}
}
}
}

impl CliChain for Westend {
const RUNTIME_VERSION: RuntimeVersion = bp_westend::VERSION;

type KeyPair = sp_core::sr25519::Pair;
type MessagePayload = ();

fn ss58_format() -> u16 {
42
}

fn max_extrinsic_weight() -> Weight {
0
}

fn encode_message(_message: encode_message::MessagePayload) -> Result<Self::MessagePayload, String> {
Err("Sending messages from Westend is not yet supported.".into())
}
}

impl CliChain for Rococo {
const RUNTIME_VERSION: RuntimeVersion = bp_rococo::VERSION;

type KeyPair = sp_core::sr25519::Pair;
type MessagePayload = ();

fn ss58_format() -> u16 {
42
}

fn max_extrinsic_weight() -> Weight {
0
}

fn encode_message(_message: encode_message::MessagePayload) -> Result<Self::MessagePayload, String> {
Err("Sending messages from Rococo is not yet supported.".into())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::cli::{encode_call, send_message};
use bp_messages::source_chain::TargetHeaderChain;
use codec::Encode;
use frame_support::dispatch::GetDispatchInfo;
use relay_millau_client::Millau;
use relay_rialto_client::Rialto;
use relay_substrate_client::TransactionSignScheme;
use sp_core::Pair;
use sp_runtime::traits::{IdentifyAccount, Verify};
Expand Down
Loading

0 comments on commit 88f9cca

Please sign in to comment.