diff --git a/bridges/bin/millau/node/Cargo.toml b/bridges/bin/millau/node/Cargo.toml index 3485c8d487..7af2f6febd 100644 --- a/bridges/bin/millau/node/Cargo.toml +++ b/bridges/bin/millau/node/Cargo.toml @@ -14,8 +14,12 @@ jsonrpc-core = "15.1.0" structopt = "0.3.20" # Bridge dependencies + +bp-message-lane = { path = "../../../primitives/message-lane" } bp-rialto = { path = "../../../primitives/rialto" } +bp-runtime = { path = "../../../primitives/runtime" } millau-runtime = { path = "../runtime" } +pallet-message-lane-rpc = { path = "../../../modules/message-lane/rpc" } # Substrate Dependencies diff --git a/bridges/bin/millau/node/src/service.rs b/bridges/bin/millau/node/src/service.rs index fe1686fdda..3e3917d7bd 100644 --- a/bridges/bin/millau/node/src/service.rs +++ b/bridges/bin/millau/node/src/service.rs @@ -28,6 +28,8 @@ // ===================================================================================== // ===================================================================================== +use bp_message_lane::{LaneId, MessageNonce}; +use bp_runtime::{InstanceId, RIALTO_BRIDGE_INSTANCE}; use millau_runtime::{self, opaque::Block, RuntimeApi}; use sc_client_api::{ExecutorProvider, RemoteBackend}; use sc_executor::native_executor_instance; @@ -36,6 +38,7 @@ use sc_finality_grandpa::{FinalityProofProvider as GrandpaFinalityProofProvider, use sc_finality_grandpa_rpc::GrandpaRpcHandler; use sc_service::{error::Error as ServiceError, Configuration, TaskManager}; use sp_consensus_aura::sr25519::AuthorityPair as AuraPair; +use sp_core::storage::StorageKey; use sp_inherents::InherentDataProviders; use std::sync::Arc; use std::time::Duration; @@ -161,10 +164,40 @@ pub fn new_full(config: Configuration) -> Result { let telemetry_connection_sinks = sc_service::TelemetryConnectionSinks::default(); let rpc_extensions_builder = { + // This struct is here to ease update process. + + /// Millau runtime from message-lane RPC point of view. + struct MillauMessageLaneKeys; + + impl pallet_message_lane_rpc::Runtime for MillauMessageLaneKeys { + fn message_key(&self, instance: &InstanceId, lane: &LaneId, nonce: MessageNonce) -> Option { + match *instance { + RIALTO_BRIDGE_INSTANCE => Some(millau_runtime::rialto_messages::message_key(lane, nonce)), + _ => None, + } + } + + fn outbound_lane_data_key(&self, instance: &InstanceId, lane: &LaneId) -> Option { + match *instance { + RIALTO_BRIDGE_INSTANCE => Some(millau_runtime::rialto_messages::outbound_lane_data_key(lane)), + _ => None, + } + } + + fn inbound_lane_data_key(&self, instance: &InstanceId, lane: &LaneId) -> Option { + match *instance { + RIALTO_BRIDGE_INSTANCE => Some(millau_runtime::rialto_messages::inbound_lane_data_key(lane)), + _ => None, + } + } + } + + use pallet_message_lane_rpc::{MessageLaneApi, MessageLaneRpcHandler}; use sc_finality_grandpa_rpc::GrandpaApi; use sc_rpc::DenyUnsafe; use substrate_frame_rpc_system::{FullSystem, SystemApi}; + let backend = backend.clone(); let client = client.clone(); let pool = transaction_pool.clone(); @@ -188,6 +221,10 @@ pub fn new_full(config: Configuration) -> Result { subscription_executor, finality_proof_provider.clone(), ))); + io.extend_with(MessageLaneApi::to_delegate(MessageLaneRpcHandler::new( + backend.clone(), + Arc::new(MillauMessageLaneKeys), + ))); io }) diff --git a/bridges/bin/millau/runtime/src/rialto_messages.rs b/bridges/bin/millau/runtime/src/rialto_messages.rs index e2a41e88f9..a947d796cd 100644 --- a/bridges/bin/millau/runtime/src/rialto_messages.rs +++ b/bridges/bin/millau/runtime/src/rialto_messages.rs @@ -16,21 +16,41 @@ //! Everything required to serve Millau <-> Rialto message lanes. -use bridge_runtime_common::messages; +use crate::Runtime; use bp_message_lane::{ source_chain::TargetHeaderChain, target_chain::{ProvedMessages, SourceHeaderChain}, - InboundLaneData, LaneId, Message, MessageNonce, + InboundLaneData, LaneId, Message, MessageKey, MessageNonce, }; use bp_runtime::InstanceId; -use bridge_runtime_common::messages::MessageBridge; +use bridge_runtime_common::messages::{self, MessageBridge}; use frame_support::{ + storage::generator::StorageMap, weights::{Weight, WeightToFeePolynomial}, RuntimeDebug, }; +use pallet_message_lane::{DefaultInstance, InboundLanes, OutboundLanes, OutboundMessages}; +use sp_core::storage::StorageKey; use sp_trie::StorageProof; +/// Storage key of the Millau -> Rialto message in the runtime storage. +pub fn message_key(lane: &LaneId, nonce: MessageNonce) -> StorageKey { + let message_key = MessageKey { lane_id: *lane, nonce }; + let raw_storage_key = OutboundMessages::::storage_map_final_key(message_key); + StorageKey(raw_storage_key) +} + +/// Storage key of the Millau -> Rialto message lane state in the runtime storage. +pub fn outbound_lane_data_key(lane: &LaneId) -> StorageKey { + StorageKey(OutboundLanes::::storage_map_final_key(*lane)) +} + +/// Storage key of the Rialto -> Millau message lane state in the runtime storage. +pub fn inbound_lane_data_key(lane: &LaneId) -> StorageKey { + StorageKey(InboundLanes::::storage_map_final_key(*lane)) +} + /// Message payload for Millau -> Rialto messages. pub type ToRialtoMessagePayload = messages::source::FromThisChainMessagePayload; diff --git a/bridges/bin/rialto/node/Cargo.toml b/bridges/bin/rialto/node/Cargo.toml index be700d9353..a0394f34af 100644 --- a/bridges/bin/rialto/node/Cargo.toml +++ b/bridges/bin/rialto/node/Cargo.toml @@ -15,6 +15,8 @@ structopt = "0.3.20" # Bridge dependencies +bp-message-lane = { path = "../../../primitives/message-lane" } +bp-runtime = { path = "../../../primitives/runtime" } pallet-message-lane-rpc = { path = "../../../modules/message-lane/rpc" } rialto-runtime = { path = "../runtime" } diff --git a/bridges/bin/rialto/node/src/service.rs b/bridges/bin/rialto/node/src/service.rs index 4b89ae4782..21b2bf0bce 100644 --- a/bridges/bin/rialto/node/src/service.rs +++ b/bridges/bin/rialto/node/src/service.rs @@ -28,6 +28,8 @@ // ===================================================================================== // ===================================================================================== +use bp_message_lane::{LaneId, MessageNonce}; +use bp_runtime::{InstanceId, MILLAU_BRIDGE_INSTANCE}; use rialto_runtime::{self, opaque::Block, RuntimeApi}; use sc_client_api::{ExecutorProvider, RemoteBackend}; use sc_executor::native_executor_instance; @@ -35,6 +37,7 @@ pub use sc_executor::NativeExecutor; use sc_finality_grandpa::{FinalityProofProvider as GrandpaFinalityProofProvider, SharedVoterState}; use sc_service::{error::Error as ServiceError, Configuration, TaskManager}; use sp_consensus_aura::sr25519::AuthorityPair as AuraPair; +use sp_core::storage::StorageKey; use sp_inherents::InherentDataProviders; use std::sync::Arc; use std::time::Duration; @@ -160,9 +163,38 @@ pub fn new_full(config: Configuration) -> Result { let telemetry_connection_sinks = sc_service::TelemetryConnectionSinks::default(); let rpc_extensions_builder = { + // This struct is here to ease update process. + + /// Rialto runtime from message-lane RPC point of view. + struct RialtoMessageLaneKeys; + + impl pallet_message_lane_rpc::Runtime for RialtoMessageLaneKeys { + fn message_key(&self, instance: &InstanceId, lane: &LaneId, nonce: MessageNonce) -> Option { + match *instance { + MILLAU_BRIDGE_INSTANCE => Some(rialto_runtime::millau_messages::message_key(lane, nonce)), + _ => None, + } + } + + fn outbound_lane_data_key(&self, instance: &InstanceId, lane: &LaneId) -> Option { + match *instance { + MILLAU_BRIDGE_INSTANCE => Some(rialto_runtime::millau_messages::outbound_lane_data_key(lane)), + _ => None, + } + } + + fn inbound_lane_data_key(&self, instance: &InstanceId, lane: &LaneId) -> Option { + match *instance { + MILLAU_BRIDGE_INSTANCE => Some(rialto_runtime::millau_messages::inbound_lane_data_key(lane)), + _ => None, + } + } + } + + use pallet_message_lane_rpc::{MessageLaneApi, MessageLaneRpcHandler}; use sc_rpc::DenyUnsafe; use substrate_frame_rpc_system::{FullSystem, SystemApi}; - + let backend = backend.clone(); let client = client.clone(); let pool = transaction_pool.clone(); @@ -173,6 +205,10 @@ pub fn new_full(config: Configuration) -> Result { pool.clone(), DenyUnsafe::No, ))); + io.extend_with(MessageLaneApi::to_delegate(MessageLaneRpcHandler::new( + backend.clone(), + Arc::new(RialtoMessageLaneKeys), + ))); io }) diff --git a/bridges/bin/rialto/runtime/src/millau_messages.rs b/bridges/bin/rialto/runtime/src/millau_messages.rs index 920c3b39eb..6d38398eab 100644 --- a/bridges/bin/rialto/runtime/src/millau_messages.rs +++ b/bridges/bin/rialto/runtime/src/millau_messages.rs @@ -16,21 +16,41 @@ //! Everything required to serve Millau <-> Rialto message lanes. -use bridge_runtime_common::messages; +use crate::Runtime; use bp_message_lane::{ source_chain::TargetHeaderChain, target_chain::{ProvedMessages, SourceHeaderChain}, - InboundLaneData, LaneId, Message, MessageNonce, + InboundLaneData, LaneId, Message, MessageKey, MessageNonce, }; use bp_runtime::InstanceId; -use bridge_runtime_common::messages::MessageBridge; +use bridge_runtime_common::messages::{self, MessageBridge}; use frame_support::{ + storage::generator::StorageMap, weights::{Weight, WeightToFeePolynomial}, RuntimeDebug, }; +use pallet_message_lane::{DefaultInstance, InboundLanes, OutboundLanes, OutboundMessages}; +use sp_core::storage::StorageKey; use sp_trie::StorageProof; +/// Storage key of the Rialto -> Millau message in the runtime storage. +pub fn message_key(lane: &LaneId, nonce: MessageNonce) -> StorageKey { + let message_key = MessageKey { lane_id: *lane, nonce }; + let raw_storage_key = OutboundMessages::::storage_map_final_key(message_key); + StorageKey(raw_storage_key) +} + +/// Storage key of the Rialto -> Millau message lane state in the runtime storage. +pub fn outbound_lane_data_key(lane: &LaneId) -> StorageKey { + StorageKey(OutboundLanes::::storage_map_final_key(*lane)) +} + +/// Storage key of the Millau -> Rialto message lane state in the runtime storage. +pub fn inbound_lane_data_key(lane: &LaneId) -> StorageKey { + StorageKey(InboundLanes::::storage_map_final_key(*lane)) +} + /// Message payload for Rialto -> Millau messages. pub type ToMillauMessagePayload = messages::source::FromThisChainMessagePayload; diff --git a/bridges/modules/message-lane/src/lib.rs b/bridges/modules/message-lane/src/lib.rs index 6e7a6add2f..696340c6ff 100644 --- a/bridges/modules/message-lane/src/lib.rs +++ b/bridges/modules/message-lane/src/lib.rs @@ -138,11 +138,11 @@ decl_error! { decl_storage! { trait Store for Module, I: Instance = DefaultInstance> as MessageLane { /// Map of lane id => inbound lane data. - InboundLanes: map hasher(blake2_128_concat) LaneId => InboundLaneData; + pub InboundLanes: map hasher(blake2_128_concat) LaneId => InboundLaneData; /// Map of lane id => outbound lane data. - OutboundLanes: map hasher(blake2_128_concat) LaneId => OutboundLaneData; + pub OutboundLanes: map hasher(blake2_128_concat) LaneId => OutboundLaneData; /// All queued outbound messages. - OutboundMessages: map hasher(blake2_128_concat) MessageKey => Option>; + pub OutboundMessages: map hasher(blake2_128_concat) MessageKey => Option>; } } diff --git a/bridges/primitives/runtime/src/lib.rs b/bridges/primitives/runtime/src/lib.rs index a7894554c3..df0975f981 100644 --- a/bridges/primitives/runtime/src/lib.rs +++ b/bridges/primitives/runtime/src/lib.rs @@ -28,6 +28,12 @@ mod chain; /// Use this when something must be shared among all instances. pub const NO_INSTANCE_ID: InstanceId = [0, 0, 0, 0]; +/// Bridge-with-Rialto instance id. +pub const RIALTO_BRIDGE_INSTANCE: InstanceId = *b"rlto"; + +/// Bridge-with-Millau instance id. +pub const MILLAU_BRIDGE_INSTANCE: InstanceId = *b"mlau"; + /// Call-dispatch module prefix. pub const CALL_DISPATCH_MODULE_PREFIX: &[u8] = b"pallet-bridge/call-dispatch";