Skip to content

Commit

Permalink
Use indirect runtime calls for RialtoParachain (paritytech#1753)
Browse files Browse the repository at this point in the history
* Use indirect runtime calls for RialtoParachain

* Code review fixes
  • Loading branch information
serban300 committed Apr 8, 2024
1 parent 1b08569 commit 80ad4f9
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 95 deletions.
1 change: 0 additions & 1 deletion bridges/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub use storage_types::BoundedStorageValue;
#[cfg(feature = "std")]
pub use storage_proof::craft_valid_storage_proof;

pub mod calls;
pub mod messages;

mod chain;
Expand Down
1 change: 0 additions & 1 deletion bridges/relays/bin-substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ relay-substrate-client = { path = "../client-substrate" }
relay-utils = { path = "../utils" }
relay-westend-client = { path = "../client-westend" }
relay-wococo-client = { path = "../client-wococo" }
rialto-parachain-runtime = { path = "../../bin/rialto-parachain/runtime" }
rialto-runtime = { path = "../../bin/rialto/runtime" }
substrate-relay-helper = { path = "../lib-substrate-relay" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
//! BridgeHubRococo-to-BridgeHubWococo messages sync entrypoint.

use crate::cli::bridge::{CliBridgeBase, MessagesCliBridge};
use bp_messages::Weight;
use relay_bridge_hub_rococo_client::BridgeHubRococo;
use relay_bridge_hub_wococo_client::BridgeHubWococo;
use substrate_relay_helper::messages_lane::SubstrateMessageLane;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
//! BridgeHubWococo-to-BridgeHubRococo messages sync entrypoint.

use crate::cli::bridge::{CliBridgeBase, MessagesCliBridge};
use bp_messages::Weight;
use relay_bridge_hub_rococo_client::BridgeHubRococo;
use relay_bridge_hub_wococo_client::BridgeHubWococo;
use substrate_relay_helper::messages_lane::SubstrateMessageLane;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@

use crate::cli::bridge::{CliBridgeBase, MessagesCliBridge, RelayToRelayHeadersCliBridge};
use substrate_relay_helper::finality::{
engine::Grandpa as GrandpaFinalityEngine, DirectSubmitGrandpaFinalityProofCallBuilder,
SubstrateFinalitySyncPipeline,
engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline,
};

substrate_relay_helper::generate_mocked_submit_finality_proof_call_builder!(
MillauFinalityToRialtoParachain,
MillauFinalityToRialtoParachainCallBuilder,
relay_rialto_parachain_client::runtime::Call::BridgeMillauGrandpa,
relay_rialto_parachain_client::runtime::BridgeMillauGrandpaCall::submit_finality_proof
);

/// Description of Millau -> Rialto finalized headers bridge.
#[derive(Clone, Debug)]
pub struct MillauFinalityToRialtoParachain;
Expand All @@ -49,11 +55,7 @@ impl SubstrateFinalitySyncPipeline for MillauFinalityToRialtoParachain {
type TargetChain = relay_rialto_parachain_client::RialtoParachain;

type FinalityEngine = GrandpaFinalityEngine<Self::SourceChain>;
type SubmitFinalityProofCallBuilder = DirectSubmitGrandpaFinalityProofCallBuilder<
Self,
rialto_parachain_runtime::Runtime,
rialto_parachain_runtime::MillauGrandpaInstance,
>;
type SubmitFinalityProofCallBuilder = MillauFinalityToRialtoParachainCallBuilder;
}

//// `Millau` to `RialtoParachain` bridge definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
use relay_millau_client::Millau;
use relay_rialto_parachain_client::RialtoParachain;
use substrate_relay_helper::messages_lane::{
DirectReceiveMessagesDeliveryProofCallBuilder, DirectReceiveMessagesProofCallBuilder,
SubstrateMessageLane,
DirectReceiveMessagesDeliveryProofCallBuilder, SubstrateMessageLane,
};

substrate_relay_helper::generate_mocked_receive_message_proof_call_builder!(
MillauMessagesToRialtoParachain,
MillauMessagesToRialtoParachainReceiveMessagesProofCallBuilder,
relay_rialto_parachain_client::runtime::Call::BridgeMillauMessages,
relay_rialto_parachain_client::runtime::BridgeMillauMessagesCall::receive_messages_proof
);

/// Description of Millau -> RialtoParachain messages bridge.
#[derive(Clone, Debug)]
pub struct MillauMessagesToRialtoParachain;
Expand All @@ -31,11 +37,8 @@ impl SubstrateMessageLane for MillauMessagesToRialtoParachain {
type SourceChain = Millau;
type TargetChain = RialtoParachain;

type ReceiveMessagesProofCallBuilder = DirectReceiveMessagesProofCallBuilder<
Self,
rialto_parachain_runtime::Runtime,
rialto_parachain_runtime::WithMillauMessagesInstance,
>;
type ReceiveMessagesProofCallBuilder =
MillauMessagesToRialtoParachainReceiveMessagesProofCallBuilder;
type ReceiveMessagesDeliveryProofCallBuilder = DirectReceiveMessagesDeliveryProofCallBuilder<
Self,
millau_runtime::Runtime,
Expand Down
16 changes: 7 additions & 9 deletions bridges/relays/bin-substrate/src/chains/rialto_parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

use crate::cli::{bridge, encode_message::CliEncodeMessage, CliChain};
use bp_runtime::EncodedOrDecodedCall;
use bridge_runtime_common::CustomNetworkId;
use relay_rialto_parachain_client::RialtoParachain;
use relay_substrate_client::calls::XcmCall;
use sp_version::RuntimeVersion;
use xcm::latest::prelude::*;

Expand All @@ -29,23 +31,19 @@ impl CliEncodeMessage for RialtoParachain {
) -> anyhow::Result<EncodedOrDecodedCall<Self::Call>> {
let dest = match bridge_instance_index {
bridge::RIALTO_PARACHAIN_TO_MILLAU_INDEX =>
(Parent, X1(GlobalConsensus(rialto_parachain_runtime::MillauNetwork::get()))),
(Parent, X1(GlobalConsensus(CustomNetworkId::Millau.as_network_id()))),
_ => anyhow::bail!(
"Unsupported target bridge pallet with instance index: {}",
bridge_instance_index
),
};

Ok(rialto_parachain_runtime::RuntimeCall::PolkadotXcm(
rialto_parachain_runtime::XcmCall::send {
dest: Box::new(dest.into()),
message: Box::new(message),
},
)
.into())
let xcm_call = XcmCall::send(Box::new(dest.into()), Box::new(message));

Ok(relay_rialto_parachain_client::runtime::Call::PolkadotXcm(xcm_call).into())
}
}

impl CliChain for RialtoParachain {
const RUNTIME_VERSION: Option<RuntimeVersion> = Some(rialto_parachain_runtime::VERSION);
const RUNTIME_VERSION: Option<RuntimeVersion> = None;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
use relay_millau_client::Millau;
use relay_rialto_parachain_client::RialtoParachain;
use substrate_relay_helper::{
messages_lane::{
DirectReceiveMessagesDeliveryProofCallBuilder, DirectReceiveMessagesProofCallBuilder,
SubstrateMessageLane,
},
messages_lane::{DirectReceiveMessagesProofCallBuilder, SubstrateMessageLane},
UtilityPalletBatchCallBuilder,
};

substrate_relay_helper::generate_mocked_receive_message_delivery_proof_call_builder!(
RialtoParachainMessagesToMillau,
RialtoParachainMessagesToMillauReceiveMessagesDeliveryProofCallBuilder,
relay_rialto_parachain_client::runtime::Call::BridgeMillauMessages,
relay_rialto_parachain_client::runtime::BridgeMillauMessagesCall::receive_messages_delivery_proof
);

/// Description of RialtoParachain -> Millau messages bridge.
#[derive(Clone, Debug)]
pub struct RialtoParachainMessagesToMillau;
Expand All @@ -39,11 +43,8 @@ impl SubstrateMessageLane for RialtoParachainMessagesToMillau {
millau_runtime::Runtime,
millau_runtime::WithRialtoParachainMessagesInstance,
>;
type ReceiveMessagesDeliveryProofCallBuilder = DirectReceiveMessagesDeliveryProofCallBuilder<
Self,
rialto_parachain_runtime::Runtime,
rialto_parachain_runtime::WithMillauMessagesInstance,
>;
type ReceiveMessagesDeliveryProofCallBuilder =
RialtoParachainMessagesToMillauReceiveMessagesDeliveryProofCallBuilder;

type SourceBatchCallBuilder = ();
type TargetBatchCallBuilder = UtilityPalletBatchCallBuilder<Millau>;
Expand Down
16 changes: 8 additions & 8 deletions bridges/relays/bin-substrate/src/cli/init_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
cli::{bridge::CliBridgeBase, chain_schema::*},
};
use bp_runtime::Chain as ChainBase;
use relay_substrate_client::{AccountKeyPairOf, Chain, UnsignedTransaction};
use relay_substrate_client::{calls::SudoCall, AccountKeyPairOf, Chain, UnsignedTransaction};
use sp_core::Pair;
use structopt::StructOpt;
use strum::{EnumString, EnumVariantNames, VariantNames};
Expand Down Expand Up @@ -123,13 +123,13 @@ impl BridgeInitializer for MillauToRialtoParachainCliBridge {
fn encode_init_bridge(
init_data: <Self::Engine as Engine<Self::Source>>::InitializationData,
) -> <Self::Target as Chain>::Call {
let initialize_call = rialto_parachain_runtime::BridgeGrandpaCall::<
rialto_parachain_runtime::Runtime,
rialto_parachain_runtime::MillauGrandpaInstance,
>::initialize {
init_data,
};
rialto_parachain_runtime::SudoCall::sudo { call: Box::new(initialize_call.into()) }.into()
use relay_rialto_parachain_client::runtime;

let initialize_call = runtime::Call::BridgeMillauGrandpa(
runtime::BridgeMillauGrandpaCall::initialize(init_data),
);
let sudo_call = SudoCall::sudo(Box::new(initialize_call));
runtime::Call::Sudo(sudo_call)
}
}

Expand Down
1 change: 1 addition & 0 deletions bridges/relays/client-bridge-hub-rococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bp-runtime = { path = "../../primitives/runtime" }
bp-wococo = { path = "../../primitives/chain-wococo" }

bridge-runtime-common = { path = "../../bin/runtime-common" }

# Substrate Dependencies

sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/client-bridge-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod tests {
#[test]
fn parse_transaction_works() {
let unsigned = UnsignedTransaction {
call: runtime::Call::System(bp_runtime::calls::SystemCall::remark(
call: runtime::Call::System(relay_substrate_client::calls::SystemCall::remark(
b"Hello world!".to_vec(),
))
.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use scale_info::TypeInfo;

pub use bp_header_chain::BridgeGrandpaCallOf;
pub use bp_parachains::BridgeParachainCall;
pub use bp_runtime::calls::SystemCall;
pub use bridge_runtime_common::messages::BridgeMessagesCallOf;
pub use relay_substrate_client::calls::SystemCall;

// TODO:check-parameter - check SignedExtension
/// Unchecked BridgeHubRococo extrinsic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use scale_info::TypeInfo;

pub use bp_header_chain::BridgeGrandpaCallOf;
pub use bp_parachains::BridgeParachainCall;
pub use bp_runtime::calls::SystemCall;
pub use bridge_runtime_common::messages::BridgeMessagesCallOf;
pub use relay_substrate_client::calls::SystemCall;

// TODO:check-parameter - check SignedExtension
/// Unchecked BridgeHubWococo extrinsic.
Expand Down
12 changes: 9 additions & 3 deletions bridges/relays/client-rialto-parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5" }
relay-substrate-client = { path = "../client-substrate" }
relay-utils = { path = "../utils" }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }

# Bridge dependencies

bp-header-chain = { path = "../../primitives/header-chain" }
bp-messages = { path = "../../primitives/messages" }
bp-millau = { path = "../../primitives/chain-millau" }
bp-polkadot-core = { path = "../../primitives/polkadot-core" }
bp-rialto-parachain = { path = "../../primitives/chain-rialto-parachain" }
rialto-parachain-runtime = { path = "../../bin/rialto-parachain/runtime" }
bp-runtime = { path = "../../primitives/runtime" }

bridge-runtime-common = { path = "../../bin/runtime-common" }
relay-substrate-client = { path = "../client-substrate" }
relay-utils = { path = "../utils" }

# Substrate Dependencies

Expand Down
68 changes: 25 additions & 43 deletions bridges/relays/client-rialto-parachain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@

//! Types used to connect to the Rialto-Substrate chain.

pub mod runtime_wrapper;

use bp_messages::MessageNonce;
use codec::Encode;
use relay_substrate_client::{
Chain, ChainWithBalances, ChainWithMessages, ChainWithTransactions, Error as SubstrateError,
SignParam, UnderlyingChainProvider, UnsignedTransaction,
};
use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount, MultiAddress};
use std::time::Duration;

/// Rialto header id.
pub type HeaderId =
relay_utils::HeaderId<rialto_parachain_runtime::Hash, rialto_parachain_runtime::BlockNumber>;
pub use runtime_wrapper as runtime;

/// The address format for describing accounts.
pub type Address = MultiAddress<bp_rialto_parachain::AccountId, ()>;

/// Rialto parachain definition
#[derive(Debug, Clone, Copy)]
Expand All @@ -46,18 +49,13 @@ impl Chain for RialtoParachain {
bp_rialto_parachain::BEST_FINALIZED_RIALTO_PARACHAIN_HEADER_METHOD;
const AVERAGE_BLOCK_INTERVAL: Duration = Duration::from_secs(5);

type SignedBlock = rialto_parachain_runtime::SignedBlock;
type Call = rialto_parachain_runtime::RuntimeCall;
type SignedBlock = bp_polkadot_core::SignedBlock;
type Call = runtime::Call;
}

impl ChainWithBalances for RialtoParachain {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
use frame_support::storage::generator::StorageMap;
StorageKey(
frame_system::Account::<rialto_parachain_runtime::Runtime>::storage_map_final_key(
account_id,
),
)
bp_polkadot_core::AccountInfoStorageMapKeyProvider::final_key(account_id)
}
}

Expand All @@ -77,45 +75,30 @@ impl ChainWithMessages for RialtoParachain {

impl ChainWithTransactions for RialtoParachain {
type AccountKeyPair = sp_core::sr25519::Pair;
type SignedTransaction = rialto_parachain_runtime::UncheckedExtrinsic;
type SignedTransaction = bp_polkadot_core::UncheckedExtrinsic<Self::Call>;

fn sign_transaction(
param: SignParam<Self>,
unsigned: UnsignedTransaction<Self>,
) -> Result<Self::SignedTransaction, SubstrateError> {
let raw_payload = SignedPayload::from_raw(
let raw_payload = SignedPayload::new(
unsigned.call,
(
frame_system::CheckNonZeroSender::<rialto_parachain_runtime::Runtime>::new(),
frame_system::CheckSpecVersion::<rialto_parachain_runtime::Runtime>::new(),
frame_system::CheckTxVersion::<rialto_parachain_runtime::Runtime>::new(),
frame_system::CheckGenesis::<rialto_parachain_runtime::Runtime>::new(),
frame_system::CheckEra::<rialto_parachain_runtime::Runtime>::from(
unsigned.era.frame_era(),
),
frame_system::CheckNonce::<rialto_parachain_runtime::Runtime>::from(unsigned.nonce),
frame_system::CheckWeight::<rialto_parachain_runtime::Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<
rialto_parachain_runtime::Runtime,
>::from(unsigned.tip),
),
(
(),
bp_polkadot_core::SignedExtensions::new(
param.spec_version,
param.transaction_version,
unsigned.era,
param.genesis_hash,
unsigned.era.signed_payload(param.genesis_hash),
(),
(),
(),
unsigned.nonce,
unsigned.tip,
),
);
)?;

let signature = raw_payload.using_encoded(|payload| param.signer.sign(payload));
let signer: sp_runtime::MultiSigner = param.signer.public().into();
let (call, extra, _) = raw_payload.deconstruct();

Ok(rialto_parachain_runtime::UncheckedExtrinsic::new_signed(
call.into_decoded()?,
Ok(Self::SignedTransaction::new_signed(
call,
signer.into_account().into(),
signature.into(),
extra,
Expand All @@ -129,19 +112,18 @@ impl ChainWithTransactions for RialtoParachain {
fn is_signed_by(signer: &Self::AccountKeyPair, tx: &Self::SignedTransaction) -> bool {
tx.signature
.as_ref()
.map(|(address, _, _)| {
*address == rialto_parachain_runtime::Address::Id(signer.public().into())
})
.map(|(address, _, _)| *address == Address::Id(signer.public().into()))
.unwrap_or(false)
}

fn parse_transaction(_tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self>> {
None
fn parse_transaction(tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self>> {
let extra = &tx.signature.as_ref()?.2;
Some(UnsignedTransaction::new(tx.function, extra.nonce()).tip(extra.tip()))
}
}

/// RialtoParachain signing params.
pub type SigningParams = sp_core::sr25519::Pair;

/// RialtoParachain header type used in headers sync.
pub type SyncHeader = relay_substrate_client::SyncHeader<rialto_parachain_runtime::Header>;
pub type SyncHeader = relay_substrate_client::SyncHeader<bp_rialto_parachain::Header>;
Loading

0 comments on commit 80ad4f9

Please sign in to comment.