Skip to content

Commit

Permalink
messages bridge between RialtoParachain and Millau
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik committed Dec 13, 2021
1 parent adebeea commit 6aece4f
Show file tree
Hide file tree
Showing 36 changed files with 1,930 additions and 70 deletions.
21 changes: 20 additions & 1 deletion Cargo.lock

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

6 changes: 6 additions & 0 deletions bin/millau/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use beefy_primitives::crypto::AuthorityId as BeefyId;
use bp_millau::derive_account_from_rialto_id;
use millau_runtime::{
AccountId, AuraConfig, BalancesConfig, BeefyConfig, BridgeRialtoMessagesConfig,
BridgeRialtoParachainMessagesConfig,
BridgeWestendGrandpaConfig, GenesisConfig, GrandpaConfig, SessionConfig, SessionKeys,
Signature, SudoConfig, SystemConfig, WASM_BINARY,
};
Expand Down Expand Up @@ -139,6 +140,7 @@ impl Alternative {
get_account_id_from_seed::<sr25519::Public>("Harry//stash"),
get_account_id_from_seed::<sr25519::Public>("RialtoMessagesOwner"),
get_account_id_from_seed::<sr25519::Public>("WithRialtoTokenSwap"),
get_account_id_from_seed::<sr25519::Public>("RialtoParachainMessagesOwner"),
pallet_bridge_messages::relayer_fund_account_id::<
bp_millau::AccountId,
bp_millau::AccountIdConverter,
Expand Down Expand Up @@ -215,6 +217,10 @@ fn testnet_genesis(
owner: Some(get_account_id_from_seed::<sr25519::Public>("RialtoMessagesOwner")),
..Default::default()
},
bridge_rialto_parachain_messages: BridgeRialtoParachainMessagesConfig {
owner: Some(get_account_id_from_seed::<sr25519::Public>("RialtoParachainMessagesOwner")),
..Default::default()
},
}
}

Expand Down
4 changes: 4 additions & 0 deletions bin/millau/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ serde = { version = "1.0", optional = true, features = ["derive"] }
bp-header-chain = { path = "../../../primitives/header-chain", default-features = false }
bp-messages = { path = "../../../primitives/messages", default-features = false }
bp-millau = { path = "../../../primitives/chain-millau", default-features = false }
bp-polkadot-core = { path = "../../../primitives/polkadot-core", default-features = false }
bp-rialto = { path = "../../../primitives/chain-rialto", default-features = false }
bp-rialto-parachain = { path = "../../../primitives/chain-rialto-parachain", default-features = false }
bp-runtime = { path = "../../../primitives/runtime", default-features = false }
bp-westend = { path = "../../../primitives/chain-westend", default-features = false }
bridge-runtime-common = { path = "../../runtime-common", default-features = false }
Expand Down Expand Up @@ -74,7 +76,9 @@ std = [
"bp-header-chain/std",
"bp-messages/std",
"bp-millau/std",
"bp-polkadot-core/std",
"bp-rialto/std",
"bp-rialto-parachain/std",
"bp-runtime/std",
"bp-westend/std",
"bridge-runtime-common/std",
Expand Down
143 changes: 141 additions & 2 deletions bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

pub mod rialto_messages;
pub mod rialto_parachain_messages;

use crate::rialto_messages::{ToRialtoMessagePayload, WithRialtoMessageBridge};
use crate::rialto_parachain_messages::{ToRialtoParachainMessagePayload, WithRialtoParachainMessageBridge};

use beefy_primitives::{crypto::AuthorityId as BeefyId, mmr::MmrLeafVersion, ValidatorSet};
use bridge_runtime_common::messages::{
Expand Down Expand Up @@ -403,6 +405,16 @@ impl pallet_bridge_grandpa::Config<WestendGrandpaInstance> for Runtime {
type WeightInfo = pallet_bridge_grandpa::weights::RialtoWeight<Runtime>;
}

pub type RialtoParachainGrandpaInstance = pallet_bridge_grandpa::Instance2;
impl pallet_bridge_grandpa::Config<RialtoParachainGrandpaInstance> for Runtime {
type BridgedChain = bp_rialto_parachain::RialtoParachain;
type MaxRequests = MaxRequests;
type HeadersToKeep = HeadersToKeep;

// TODO [#391]: Use weights generated for the Millau runtime instead of Rialto ones.
type WeightInfo = pallet_bridge_grandpa::weights::RialtoWeight<Runtime>;
}

impl pallet_shift_session_manager::Config for Runtime {}

parameter_types! {
Expand All @@ -416,6 +428,7 @@ parameter_types! {
bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT as _;
pub const RootAccountForPayments: Option<AccountId> = None;
pub const RialtoChainId: bp_runtime::ChainId = bp_runtime::RIALTO_CHAIN_ID;
pub const RialtoParachainChainId: bp_runtime::ChainId = bp_runtime::RIALTO_PARACHAIN_CHAIN_ID;
}

/// Instance of the messages pallet used to relay messages to/from Rialto chain.
Expand Down Expand Up @@ -444,7 +457,7 @@ impl pallet_bridge_messages::Config<WithRialtoMessagesInstance> for Runtime {
type MessageDeliveryAndDispatchPayment =
pallet_bridge_messages::instant_payments::InstantCurrencyPayments<
Runtime,
(),
WithRialtoMessagesInstance,
pallet_balances::Pallet<Runtime>,
GetDeliveryConfirmationTransactionFee,
RootAccountForPayments,
Expand All @@ -458,6 +471,45 @@ impl pallet_bridge_messages::Config<WithRialtoMessagesInstance> for Runtime {
type BridgedChainId = RialtoChainId;
}

/// Instance of the messages pallet used to relay messages to/from RialtoParachain chain.
pub type WithRialtoParachainMessagesInstance = pallet_bridge_messages::Instance1;

impl pallet_bridge_messages::Config<WithRialtoParachainMessagesInstance> for Runtime {
type Event = Event;
// TODO: https://github.com/paritytech/parity-bridges-common/issues/390
type WeightInfo = pallet_bridge_messages::weights::RialtoWeight<Runtime>;
type Parameter = rialto_parachain_messages::MillauToRialtoParachainMessagesParameter;
type MaxMessagesToPruneAtOnce = MaxMessagesToPruneAtOnce;
type MaxUnrewardedRelayerEntriesAtInboundLane = MaxUnrewardedRelayerEntriesAtInboundLane;
type MaxUnconfirmedMessagesAtInboundLane = MaxUnconfirmedMessagesAtInboundLane;

type OutboundPayload = crate::rialto_parachain_messages::ToRialtoParachainMessagePayload;
type OutboundMessageFee = Balance;

type InboundPayload = crate::rialto_parachain_messages::FromRialtoParachainMessagePayload;
type InboundMessageFee = bp_rialto_parachain::Balance;
type InboundRelayer = bp_rialto_parachain::AccountId;

type AccountIdConverter = bp_millau::AccountIdConverter;

type TargetHeaderChain = crate::rialto_parachain_messages::RialtoParachain;
type LaneMessageVerifier = crate::rialto_parachain_messages::ToRialtoParachainMessageVerifier;
type MessageDeliveryAndDispatchPayment =
pallet_bridge_messages::instant_payments::InstantCurrencyPayments<
Runtime,
WithRialtoParachainMessagesInstance,
pallet_balances::Pallet<Runtime>,
GetDeliveryConfirmationTransactionFee,
RootAccountForPayments,
>;
type OnMessageAccepted = ();
type OnDeliveryConfirmed = ();

type SourceHeaderChain = crate::rialto_parachain_messages::RialtoParachain;
type MessageDispatch = crate::rialto_parachain_messages::FromRialtoParachainMessageDispatch;
type BridgedChainId = RialtoParachainChainId;
}

parameter_types! {
pub const TokenSwapMessagesLane: bp_messages::LaneId = *b"swap";
}
Expand Down Expand Up @@ -531,8 +583,9 @@ construct_runtime!(
// Westend bridge modules.
BridgeWestendGrandpa: pallet_bridge_grandpa::<Instance1>::{Pallet, Call, Config<T>, Storage},

// Rialto parachains bridge modules.
// RialtoParachain bridge modules.
BridgeRialtoParachains: pallet_bridge_parachains::{Pallet, Call, Storage},
BridgeRialtoParachainMessages: pallet_bridge_messages::<Instance1>::{Pallet, Call, Storage, Event<T>, Config<T>},
}
);

Expand Down Expand Up @@ -757,6 +810,22 @@ impl_runtime_apis! {
}
}

impl bp_rialto_parachain::RialtoParachainFinalityApi<Block> for Runtime {
fn best_finalized() -> (bp_rialto::BlockNumber, bp_rialto::Hash) {
// TODO: this API should be removed in https://github.com/paritytech/parity-bridges-common/issues/1246
// now let's hack something that will work
use codec::Decode;
use sp_runtime::traits::Header;
use crate::rialto_parachain_messages::RIALTO_PARACHAIN_ID;
let best_rialto_parachain_head = pallet_bridge_parachains::Pallet::<Runtime, WitRialtoParachainsInstance>::best_parachain_head(RIALTO_PARACHAIN_ID.into())
.and_then(|encoded_header| bp_rialto_parachain::Header::decode(&mut &encoded_header.0[..]).ok());
match best_rialto_parachain_head {
Some(head) => (*head.number(), head.hash()),
None => (Default::default(), Default::default()),
}
}
}

impl bp_rialto::ToRialtoOutboundLaneApi<Block, Balance, ToRialtoMessagePayload> for Runtime {
fn estimate_message_delivery_and_dispatch_fee(
_lane_id: bp_messages::LaneId,
Expand Down Expand Up @@ -803,6 +872,52 @@ impl_runtime_apis! {
}
}

impl bp_rialto_parachain::ToRialtoParachainOutboundLaneApi<Block, Balance, ToRialtoParachainMessagePayload> for Runtime {
fn estimate_message_delivery_and_dispatch_fee(
_lane_id: bp_messages::LaneId,
payload: ToRialtoParachainMessagePayload,
) -> Option<Balance> {
estimate_message_dispatch_and_delivery_fee::<WithRialtoParachainMessageBridge>(
&payload,
WithRialtoParachainMessageBridge::RELAYER_FEE_PERCENT,
).ok()
}

fn message_details(
lane: bp_messages::LaneId,
begin: bp_messages::MessageNonce,
end: bp_messages::MessageNonce,
) -> Vec<bp_messages::MessageDetails<Balance>> {
bridge_runtime_common::messages_api::outbound_message_details::<
Runtime,
WithRialtoParachainMessagesInstance,
WithRialtoParachainMessageBridge,
>(lane, begin, end)
}

fn latest_received_nonce(lane: bp_messages::LaneId) -> bp_messages::MessageNonce {
BridgeRialtoParachainMessages::outbound_latest_received_nonce(lane)
}

fn latest_generated_nonce(lane: bp_messages::LaneId) -> bp_messages::MessageNonce {
BridgeRialtoParachainMessages::outbound_latest_generated_nonce(lane)
}
}

impl bp_rialto_parachain::FromRialtoParachainInboundLaneApi<Block> for Runtime {
fn latest_received_nonce(lane: bp_messages::LaneId) -> bp_messages::MessageNonce {
BridgeRialtoParachainMessages::inbound_latest_received_nonce(lane)
}

fn latest_confirmed_nonce(lane: bp_messages::LaneId) -> bp_messages::MessageNonce {
BridgeRialtoParachainMessages::inbound_latest_confirmed_nonce(lane)
}

fn unrewarded_relayers_state(lane: bp_messages::LaneId) -> bp_messages::UnrewardedRelayersState {
BridgeRialtoParachainMessages::inbound_unrewarded_relayers_state(lane)
}
}

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn benchmark_metadata(extra: bool) -> (
Expand Down Expand Up @@ -889,6 +1004,30 @@ where
)
}

/// RialtoParachain account ownership digest from Millau.
///
/// The byte vector returned by this function should be signed with a RialtoParachain account private key.
/// This way, the owner of `millau_account_id` on Millau proves that the RialtoParachain account private key
/// is also under his control.
pub fn millau_to_rialto_parachain_account_ownership_digest<Call, AccountId, SpecVersion>(
rialto_parachain_call: &Call,
millau_account_id: AccountId,
rialto_spec_version: SpecVersion,
) -> sp_std::vec::Vec<u8>
where
Call: codec::Encode,
AccountId: codec::Encode,
SpecVersion: codec::Encode,
{
pallet_bridge_dispatch::account_ownership_digest(
rialto_parachain_call,
millau_account_id,
rialto_spec_version,
bp_runtime::MILLAU_CHAIN_ID,
bp_runtime::RIALTO_PARACHAIN_CHAIN_ID,
)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 6aece4f

Please sign in to comment.