Skip to content

Commit 6aece4f

Browse files
committed
messages bridge between RialtoParachain and Millau
1 parent adebeea commit 6aece4f

36 files changed

Lines changed: 1930 additions & 70 deletions

File tree

Cargo.lock

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/millau/node/src/chain_spec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use beefy_primitives::crypto::AuthorityId as BeefyId;
1818
use bp_millau::derive_account_from_rialto_id;
1919
use millau_runtime::{
2020
AccountId, AuraConfig, BalancesConfig, BeefyConfig, BridgeRialtoMessagesConfig,
21+
BridgeRialtoParachainMessagesConfig,
2122
BridgeWestendGrandpaConfig, GenesisConfig, GrandpaConfig, SessionConfig, SessionKeys,
2223
Signature, SudoConfig, SystemConfig, WASM_BINARY,
2324
};
@@ -139,6 +140,7 @@ impl Alternative {
139140
get_account_id_from_seed::<sr25519::Public>("Harry//stash"),
140141
get_account_id_from_seed::<sr25519::Public>("RialtoMessagesOwner"),
141142
get_account_id_from_seed::<sr25519::Public>("WithRialtoTokenSwap"),
143+
get_account_id_from_seed::<sr25519::Public>("RialtoParachainMessagesOwner"),
142144
pallet_bridge_messages::relayer_fund_account_id::<
143145
bp_millau::AccountId,
144146
bp_millau::AccountIdConverter,
@@ -215,6 +217,10 @@ fn testnet_genesis(
215217
owner: Some(get_account_id_from_seed::<sr25519::Public>("RialtoMessagesOwner")),
216218
..Default::default()
217219
},
220+
bridge_rialto_parachain_messages: BridgeRialtoParachainMessagesConfig {
221+
owner: Some(get_account_id_from_seed::<sr25519::Public>("RialtoParachainMessagesOwner")),
222+
..Default::default()
223+
},
218224
}
219225
}
220226

bin/millau/runtime/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ serde = { version = "1.0", optional = true, features = ["derive"] }
1818
bp-header-chain = { path = "../../../primitives/header-chain", default-features = false }
1919
bp-messages = { path = "../../../primitives/messages", default-features = false }
2020
bp-millau = { path = "../../../primitives/chain-millau", default-features = false }
21+
bp-polkadot-core = { path = "../../../primitives/polkadot-core", default-features = false }
2122
bp-rialto = { path = "../../../primitives/chain-rialto", default-features = false }
23+
bp-rialto-parachain = { path = "../../../primitives/chain-rialto-parachain", default-features = false }
2224
bp-runtime = { path = "../../../primitives/runtime", default-features = false }
2325
bp-westend = { path = "../../../primitives/chain-westend", default-features = false }
2426
bridge-runtime-common = { path = "../../runtime-common", default-features = false }
@@ -74,7 +76,9 @@ std = [
7476
"bp-header-chain/std",
7577
"bp-messages/std",
7678
"bp-millau/std",
79+
"bp-polkadot-core/std",
7780
"bp-rialto/std",
81+
"bp-rialto-parachain/std",
7882
"bp-runtime/std",
7983
"bp-westend/std",
8084
"bridge-runtime-common/std",

bin/millau/runtime/src/lib.rs

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
3232

3333
pub mod rialto_messages;
34+
pub mod rialto_parachain_messages;
3435

3536
use crate::rialto_messages::{ToRialtoMessagePayload, WithRialtoMessageBridge};
37+
use crate::rialto_parachain_messages::{ToRialtoParachainMessagePayload, WithRialtoParachainMessageBridge};
3638

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

408+
pub type RialtoParachainGrandpaInstance = pallet_bridge_grandpa::Instance2;
409+
impl pallet_bridge_grandpa::Config<RialtoParachainGrandpaInstance> for Runtime {
410+
type BridgedChain = bp_rialto_parachain::RialtoParachain;
411+
type MaxRequests = MaxRequests;
412+
type HeadersToKeep = HeadersToKeep;
413+
414+
// TODO [#391]: Use weights generated for the Millau runtime instead of Rialto ones.
415+
type WeightInfo = pallet_bridge_grandpa::weights::RialtoWeight<Runtime>;
416+
}
417+
406418
impl pallet_shift_session_manager::Config for Runtime {}
407419

408420
parameter_types! {
@@ -416,6 +428,7 @@ parameter_types! {
416428
bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT as _;
417429
pub const RootAccountForPayments: Option<AccountId> = None;
418430
pub const RialtoChainId: bp_runtime::ChainId = bp_runtime::RIALTO_CHAIN_ID;
431+
pub const RialtoParachainChainId: bp_runtime::ChainId = bp_runtime::RIALTO_PARACHAIN_CHAIN_ID;
419432
}
420433

421434
/// Instance of the messages pallet used to relay messages to/from Rialto chain.
@@ -444,7 +457,7 @@ impl pallet_bridge_messages::Config<WithRialtoMessagesInstance> for Runtime {
444457
type MessageDeliveryAndDispatchPayment =
445458
pallet_bridge_messages::instant_payments::InstantCurrencyPayments<
446459
Runtime,
447-
(),
460+
WithRialtoMessagesInstance,
448461
pallet_balances::Pallet<Runtime>,
449462
GetDeliveryConfirmationTransactionFee,
450463
RootAccountForPayments,
@@ -458,6 +471,45 @@ impl pallet_bridge_messages::Config<WithRialtoMessagesInstance> for Runtime {
458471
type BridgedChainId = RialtoChainId;
459472
}
460473

474+
/// Instance of the messages pallet used to relay messages to/from RialtoParachain chain.
475+
pub type WithRialtoParachainMessagesInstance = pallet_bridge_messages::Instance1;
476+
477+
impl pallet_bridge_messages::Config<WithRialtoParachainMessagesInstance> for Runtime {
478+
type Event = Event;
479+
// TODO: https://github.com/paritytech/parity-bridges-common/issues/390
480+
type WeightInfo = pallet_bridge_messages::weights::RialtoWeight<Runtime>;
481+
type Parameter = rialto_parachain_messages::MillauToRialtoParachainMessagesParameter;
482+
type MaxMessagesToPruneAtOnce = MaxMessagesToPruneAtOnce;
483+
type MaxUnrewardedRelayerEntriesAtInboundLane = MaxUnrewardedRelayerEntriesAtInboundLane;
484+
type MaxUnconfirmedMessagesAtInboundLane = MaxUnconfirmedMessagesAtInboundLane;
485+
486+
type OutboundPayload = crate::rialto_parachain_messages::ToRialtoParachainMessagePayload;
487+
type OutboundMessageFee = Balance;
488+
489+
type InboundPayload = crate::rialto_parachain_messages::FromRialtoParachainMessagePayload;
490+
type InboundMessageFee = bp_rialto_parachain::Balance;
491+
type InboundRelayer = bp_rialto_parachain::AccountId;
492+
493+
type AccountIdConverter = bp_millau::AccountIdConverter;
494+
495+
type TargetHeaderChain = crate::rialto_parachain_messages::RialtoParachain;
496+
type LaneMessageVerifier = crate::rialto_parachain_messages::ToRialtoParachainMessageVerifier;
497+
type MessageDeliveryAndDispatchPayment =
498+
pallet_bridge_messages::instant_payments::InstantCurrencyPayments<
499+
Runtime,
500+
WithRialtoParachainMessagesInstance,
501+
pallet_balances::Pallet<Runtime>,
502+
GetDeliveryConfirmationTransactionFee,
503+
RootAccountForPayments,
504+
>;
505+
type OnMessageAccepted = ();
506+
type OnDeliveryConfirmed = ();
507+
508+
type SourceHeaderChain = crate::rialto_parachain_messages::RialtoParachain;
509+
type MessageDispatch = crate::rialto_parachain_messages::FromRialtoParachainMessageDispatch;
510+
type BridgedChainId = RialtoParachainChainId;
511+
}
512+
461513
parameter_types! {
462514
pub const TokenSwapMessagesLane: bp_messages::LaneId = *b"swap";
463515
}
@@ -531,8 +583,9 @@ construct_runtime!(
531583
// Westend bridge modules.
532584
BridgeWestendGrandpa: pallet_bridge_grandpa::<Instance1>::{Pallet, Call, Config<T>, Storage},
533585

534-
// Rialto parachains bridge modules.
586+
// RialtoParachain bridge modules.
535587
BridgeRialtoParachains: pallet_bridge_parachains::{Pallet, Call, Storage},
588+
BridgeRialtoParachainMessages: pallet_bridge_messages::<Instance1>::{Pallet, Call, Storage, Event<T>, Config<T>},
536589
}
537590
);
538591

@@ -757,6 +810,22 @@ impl_runtime_apis! {
757810
}
758811
}
759812

813+
impl bp_rialto_parachain::RialtoParachainFinalityApi<Block> for Runtime {
814+
fn best_finalized() -> (bp_rialto::BlockNumber, bp_rialto::Hash) {
815+
// TODO: this API should be removed in https://github.com/paritytech/parity-bridges-common/issues/1246
816+
// now let's hack something that will work
817+
use codec::Decode;
818+
use sp_runtime::traits::Header;
819+
use crate::rialto_parachain_messages::RIALTO_PARACHAIN_ID;
820+
let best_rialto_parachain_head = pallet_bridge_parachains::Pallet::<Runtime, WitRialtoParachainsInstance>::best_parachain_head(RIALTO_PARACHAIN_ID.into())
821+
.and_then(|encoded_header| bp_rialto_parachain::Header::decode(&mut &encoded_header.0[..]).ok());
822+
match best_rialto_parachain_head {
823+
Some(head) => (*head.number(), head.hash()),
824+
None => (Default::default(), Default::default()),
825+
}
826+
}
827+
}
828+
760829
impl bp_rialto::ToRialtoOutboundLaneApi<Block, Balance, ToRialtoMessagePayload> for Runtime {
761830
fn estimate_message_delivery_and_dispatch_fee(
762831
_lane_id: bp_messages::LaneId,
@@ -803,6 +872,52 @@ impl_runtime_apis! {
803872
}
804873
}
805874

875+
impl bp_rialto_parachain::ToRialtoParachainOutboundLaneApi<Block, Balance, ToRialtoParachainMessagePayload> for Runtime {
876+
fn estimate_message_delivery_and_dispatch_fee(
877+
_lane_id: bp_messages::LaneId,
878+
payload: ToRialtoParachainMessagePayload,
879+
) -> Option<Balance> {
880+
estimate_message_dispatch_and_delivery_fee::<WithRialtoParachainMessageBridge>(
881+
&payload,
882+
WithRialtoParachainMessageBridge::RELAYER_FEE_PERCENT,
883+
).ok()
884+
}
885+
886+
fn message_details(
887+
lane: bp_messages::LaneId,
888+
begin: bp_messages::MessageNonce,
889+
end: bp_messages::MessageNonce,
890+
) -> Vec<bp_messages::MessageDetails<Balance>> {
891+
bridge_runtime_common::messages_api::outbound_message_details::<
892+
Runtime,
893+
WithRialtoParachainMessagesInstance,
894+
WithRialtoParachainMessageBridge,
895+
>(lane, begin, end)
896+
}
897+
898+
fn latest_received_nonce(lane: bp_messages::LaneId) -> bp_messages::MessageNonce {
899+
BridgeRialtoParachainMessages::outbound_latest_received_nonce(lane)
900+
}
901+
902+
fn latest_generated_nonce(lane: bp_messages::LaneId) -> bp_messages::MessageNonce {
903+
BridgeRialtoParachainMessages::outbound_latest_generated_nonce(lane)
904+
}
905+
}
906+
907+
impl bp_rialto_parachain::FromRialtoParachainInboundLaneApi<Block> for Runtime {
908+
fn latest_received_nonce(lane: bp_messages::LaneId) -> bp_messages::MessageNonce {
909+
BridgeRialtoParachainMessages::inbound_latest_received_nonce(lane)
910+
}
911+
912+
fn latest_confirmed_nonce(lane: bp_messages::LaneId) -> bp_messages::MessageNonce {
913+
BridgeRialtoParachainMessages::inbound_latest_confirmed_nonce(lane)
914+
}
915+
916+
fn unrewarded_relayers_state(lane: bp_messages::LaneId) -> bp_messages::UnrewardedRelayersState {
917+
BridgeRialtoParachainMessages::inbound_unrewarded_relayers_state(lane)
918+
}
919+
}
920+
806921
#[cfg(feature = "runtime-benchmarks")]
807922
impl frame_benchmarking::Benchmark<Block> for Runtime {
808923
fn benchmark_metadata(extra: bool) -> (
@@ -889,6 +1004,30 @@ where
8891004
)
8901005
}
8911006

1007+
/// RialtoParachain account ownership digest from Millau.
1008+
///
1009+
/// The byte vector returned by this function should be signed with a RialtoParachain account private key.
1010+
/// This way, the owner of `millau_account_id` on Millau proves that the RialtoParachain account private key
1011+
/// is also under his control.
1012+
pub fn millau_to_rialto_parachain_account_ownership_digest<Call, AccountId, SpecVersion>(
1013+
rialto_parachain_call: &Call,
1014+
millau_account_id: AccountId,
1015+
rialto_spec_version: SpecVersion,
1016+
) -> sp_std::vec::Vec<u8>
1017+
where
1018+
Call: codec::Encode,
1019+
AccountId: codec::Encode,
1020+
SpecVersion: codec::Encode,
1021+
{
1022+
pallet_bridge_dispatch::account_ownership_digest(
1023+
rialto_parachain_call,
1024+
millau_account_id,
1025+
rialto_spec_version,
1026+
bp_runtime::MILLAU_CHAIN_ID,
1027+
bp_runtime::RIALTO_PARACHAIN_CHAIN_ID,
1028+
)
1029+
}
1030+
8921031
#[cfg(test)]
8931032
mod tests {
8941033
use super::*;

0 commit comments

Comments
 (0)