From ac5671c1a9e7084edbdc4b0bfdfe1284540e8733 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Mon, 12 Aug 2024 18:22:34 +0200 Subject: [PATCH] required changes for gateway --- pallets/liquidity-pools-gateway/src/lib.rs | 119 +++++++----------- .../liquidity-pools-gateway/src/message.rs | 6 +- 2 files changed, 51 insertions(+), 74 deletions(-) diff --git a/pallets/liquidity-pools-gateway/src/lib.rs b/pallets/liquidity-pools-gateway/src/lib.rs index 2dfc01b85e..75c7dfb5ed 100644 --- a/pallets/liquidity-pools-gateway/src/lib.rs +++ b/pallets/liquidity-pools-gateway/src/lib.rs @@ -30,8 +30,8 @@ use core::fmt::Debug; use cfg_primitives::LP_DEFENSIVE_WEIGHT; use cfg_traits::liquidity_pools::{ - InboundMessageHandler, LPEncoding, MessageProcessor, MessageQueue, OutboundMessageHandler, - Router as DomainRouter, + InboundMessageHandler, LPEncoding, MessageProcessor, MessageQueue, MessageReceiver, + MessageSender, OutboundMessageHandler, RouterSupport, }; use cfg_types::domain_address::{Domain, DomainAddress}; use frame_support::{dispatch::DispatchResult, pallet_prelude::*}; @@ -39,7 +39,7 @@ use frame_system::pallet_prelude::{ensure_signed, OriginFor}; use message::GatewayMessage; use orml_traits::GetByKey; pub use pallet::*; -use parity_scale_codec::{EncodeLike, FullCodec}; +use parity_scale_codec::FullCodec; use sp_std::convert::TryInto; use crate::weights::WeightInfo; @@ -93,15 +93,11 @@ pub mod pallet { /// The Liquidity Pools message type. type Message: LPEncoding + Clone + Debug + PartialEq + MaxEncodedLen + TypeInfo + FullCodec; - /// The message router type that is stored for each domain. - type Router: DomainRouter - + Clone - + Debug - + MaxEncodedLen - + TypeInfo - + FullCodec - + EncodeLike - + PartialEq; + /// The target of of the messages comming from this chain + type MessageSender: MessageSender; + + /// An identification of a router + type RouterId: RouterSupport + Parameter; /// The type that processes inbound messages. type InboundMessageHandler: InboundMessageHandler< @@ -118,18 +114,15 @@ pub mod pallet { /// The sender account that will be used in the OutboundQueue /// implementation. #[pallet::constant] - type Sender: Get; + type Sender: Get; /// Type used for queueing messages. - type MessageQueue: MessageQueue>; + type MessageQueue: MessageQueue>; } #[pallet::event] #[pallet::generate_deposit(pub (super) fn deposit_event)] pub enum Event { - /// The router for a given domain was set. - DomainRouterSet { domain: Domain, router: T::Router }, - /// An instance was added to a domain. InstanceAdded { instance: DomainAddress }, @@ -143,13 +136,6 @@ pub mod pallet { }, } - /// Storage for domain routers. - /// - /// This can only be set by an admin. - #[pallet::storage] - #[pallet::getter(fn domain_routers)] - pub type DomainRouters = StorageMap<_, Blake2_128Concat, Domain, T::Router>; - /// Storage that contains a limited number of whitelisted instances of /// deployed liquidity pools for a particular domain. /// @@ -177,9 +163,6 @@ pub mod pallet { #[pallet::error] pub enum Error { - /// Router initialization failed. - RouterInitFailed, - /// The origin of the message to be processed is invalid. InvalidMessageOrigin, @@ -209,27 +192,6 @@ pub mod pallet { #[pallet::call] impl Pallet { - /// Set a domain's router, - #[pallet::weight(T::WeightInfo::set_domain_router())] - #[pallet::call_index(0)] - pub fn set_domain_router( - origin: OriginFor, - domain: Domain, - router: T::Router, - ) -> DispatchResult { - T::AdminOrigin::ensure_origin(origin)?; - - ensure!(domain != Domain::Centrifuge, Error::::DomainNotSupported); - - router.init().map_err(|_| Error::::RouterInitFailed)?; - - >::insert(domain.clone(), router.clone()); - - Self::deposit_event(Event::DomainRouterSet { domain, router }); - - Ok(()) - } - /// Add a known instance of a deployed liquidity pools integration for a /// specific domain. #[pallet::weight(T::WeightInfo::add_instance())] @@ -277,6 +239,7 @@ pub mod pallet { #[pallet::call_index(5)] pub fn receive_message( origin: OriginFor, + router_id: T::RouterId, msg: BoundedVec, ) -> DispatchResult { let GatewayOrigin::Domain(origin_address) = T::LocalEVMOrigin::ensure_origin(origin)?; @@ -285,17 +248,7 @@ pub mod pallet { return Err(Error::::InvalidMessageOrigin.into()); } - ensure!( - Allowlist::::contains_key(origin_address.domain(), origin_address.clone()), - Error::::UnknownInstance, - ); - - let gateway_message = GatewayMessage::::Inbound { - domain_address: origin_address, - message: T::Message::deserialize(&msg)?, - }; - - T::MessageQueue::submit(gateway_message) + Self::receive(router_id, origin_address, msg.into()) } /// Set the address of the domain hook @@ -379,28 +332,27 @@ pub mod pallet { /// message using the router, and calculates and returns the required /// weight for these operations in the `DispatchResultWithPostInfo`. fn process_outbound_message( - sender: T::AccountId, + sender: DomainAddress, domain: Domain, message: T::Message, ) -> (DispatchResult, Weight) { - let read_weight = T::DbWeight::get().reads(1); + let router_ids = T::RouterId::for_domain(domain); - let Some(router) = DomainRouters::::get(domain) else { - return (Err(Error::::RouterNotFound.into()), read_weight); - }; + // TODO handle router ids logic - let (result, router_weight) = match router.send(sender, message.serialize()) { - Ok(dispatch_info) => (Ok(()), dispatch_info.actual_weight), - Err(e) => (Err(e.error), e.post_info.actual_weight), - }; + for router_id in router_ids { + T::MessageSender::send(router_id, sender.clone(), message.serialize()); + // TODO handle weights + // sum up the weights from all routers + } - (result, router_weight.unwrap_or(read_weight)) + (Ok(()), 0.into()) //TODO } fn queue_message(destination: Domain, message: T::Message) -> DispatchResult { // We are using the sender specified in the pallet config so that we can // ensure that the account is funded - let gateway_message = GatewayMessage::::Outbound { + let gateway_message = GatewayMessage::::Outbound { sender: T::Sender::get(), destination, message, @@ -439,7 +391,7 @@ pub mod pallet { } impl MessageProcessor for Pallet { - type Message = GatewayMessage; + type Message = GatewayMessage; fn process(msg: Self::Message) -> (DispatchResult, Weight) { match msg { @@ -465,4 +417,29 @@ pub mod pallet { } } } + + impl MessageReceiver for Pallet { + type Middleware = T::RouterId; + type Origin = DomainAddress; + + fn receive( + router_id: T::RouterId, + origin_address: DomainAddress, + message: Vec, + ) -> DispatchResult { + // TODO handle router ids logic with votes and session_id + + ensure!( + Allowlist::::contains_key(origin_address.domain(), origin_address.clone()), + Error::::UnknownInstance, + ); + + let gateway_message = GatewayMessage::::Inbound { + domain_address: origin_address, + message: T::Message::deserialize(&message)?, + }; + + T::MessageQueue::submit(gateway_message) + } + } } diff --git a/pallets/liquidity-pools-gateway/src/message.rs b/pallets/liquidity-pools-gateway/src/message.rs index cf0bbb1a17..505e075b0f 100644 --- a/pallets/liquidity-pools-gateway/src/message.rs +++ b/pallets/liquidity-pools-gateway/src/message.rs @@ -3,19 +3,19 @@ use frame_support::pallet_prelude::{Decode, Encode, MaxEncodedLen, TypeInfo}; /// Message type used by the LP gateway. #[derive(Debug, Encode, Decode, Clone, Eq, MaxEncodedLen, PartialEq, TypeInfo)] -pub enum GatewayMessage { +pub enum GatewayMessage { Inbound { domain_address: DomainAddress, message: Message, }, Outbound { - sender: AccountId, + sender: DomainAddress, destination: Domain, message: Message, }, } -impl Default for GatewayMessage { +impl Default for GatewayMessage { fn default() -> Self { GatewayMessage::Inbound { domain_address: Default::default(),