Skip to content

Commit

Permalink
required changes for gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 12, 2024
1 parent e7a7ebe commit ac5671c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 74 deletions.
119 changes: 48 additions & 71 deletions pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ 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::*};
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;
Expand Down Expand Up @@ -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<Sender = Self::AccountId>
+ Clone
+ Debug
+ MaxEncodedLen
+ TypeInfo
+ FullCodec
+ EncodeLike
+ PartialEq;
/// The target of of the messages comming from this chain
type MessageSender: MessageSender<Middleware = Self::RouterId, Origin = DomainAddress>;

/// An identification of a router
type RouterId: RouterSupport<Domain> + Parameter;

/// The type that processes inbound messages.
type InboundMessageHandler: InboundMessageHandler<
Expand All @@ -118,18 +114,15 @@ pub mod pallet {
/// The sender account that will be used in the OutboundQueue
/// implementation.
#[pallet::constant]
type Sender: Get<Self::AccountId>;
type Sender: Get<DomainAddress>;

/// Type used for queueing messages.
type MessageQueue: MessageQueue<Message = GatewayMessage<Self::AccountId, Self::Message>>;
type MessageQueue: MessageQueue<Message = GatewayMessage<Self::Message>>;
}

#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
/// The router for a given domain was set.
DomainRouterSet { domain: Domain, router: T::Router },

/// An instance was added to a domain.
InstanceAdded { instance: DomainAddress },

Expand All @@ -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<T: Config> = StorageMap<_, Blake2_128Concat, Domain, T::Router>;

/// Storage that contains a limited number of whitelisted instances of
/// deployed liquidity pools for a particular domain.
///
Expand Down Expand Up @@ -177,9 +163,6 @@ pub mod pallet {

#[pallet::error]
pub enum Error<T> {
/// Router initialization failed.
RouterInitFailed,

/// The origin of the message to be processed is invalid.
InvalidMessageOrigin,

Expand Down Expand Up @@ -209,27 +192,6 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Set a domain's router,
#[pallet::weight(T::WeightInfo::set_domain_router())]
#[pallet::call_index(0)]
pub fn set_domain_router(
origin: OriginFor<T>,
domain: Domain,
router: T::Router,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;

ensure!(domain != Domain::Centrifuge, Error::<T>::DomainNotSupported);

router.init().map_err(|_| Error::<T>::RouterInitFailed)?;

<DomainRouters<T>>::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())]
Expand Down Expand Up @@ -277,6 +239,7 @@ pub mod pallet {
#[pallet::call_index(5)]
pub fn receive_message(
origin: OriginFor<T>,
router_id: T::RouterId,
msg: BoundedVec<u8, T::MaxIncomingMessageSize>,
) -> DispatchResult {
let GatewayOrigin::Domain(origin_address) = T::LocalEVMOrigin::ensure_origin(origin)?;
Expand All @@ -285,17 +248,7 @@ pub mod pallet {
return Err(Error::<T>::InvalidMessageOrigin.into());
}

ensure!(
Allowlist::<T>::contains_key(origin_address.domain(), origin_address.clone()),
Error::<T>::UnknownInstance,
);

let gateway_message = GatewayMessage::<T::AccountId, T::Message>::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
Expand Down Expand Up @@ -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::<T>::get(domain) else {
return (Err(Error::<T>::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::<T::AccountId, T::Message>::Outbound {
let gateway_message = GatewayMessage::<T::Message>::Outbound {
sender: T::Sender::get(),
destination,
message,
Expand Down Expand Up @@ -439,7 +391,7 @@ pub mod pallet {
}

impl<T: Config> MessageProcessor for Pallet<T> {
type Message = GatewayMessage<T::AccountId, T::Message>;
type Message = GatewayMessage<T::Message>;

fn process(msg: Self::Message) -> (DispatchResult, Weight) {
match msg {
Expand All @@ -465,4 +417,29 @@ pub mod pallet {
}
}
}

impl<T: Config> MessageReceiver for Pallet<T> {
type Middleware = T::RouterId;
type Origin = DomainAddress;

fn receive(
router_id: T::RouterId,
origin_address: DomainAddress,
message: Vec<u8>,
) -> DispatchResult {
// TODO handle router ids logic with votes and session_id

ensure!(
Allowlist::<T>::contains_key(origin_address.domain(), origin_address.clone()),
Error::<T>::UnknownInstance,
);

let gateway_message = GatewayMessage::<T::Message>::Inbound {
domain_address: origin_address,
message: T::Message::deserialize(&message)?,
};

T::MessageQueue::submit(gateway_message)
}
}
}
6 changes: 3 additions & 3 deletions pallets/liquidity-pools-gateway/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountId, Message> {
pub enum GatewayMessage<Message> {
Inbound {
domain_address: DomainAddress,
message: Message,
},
Outbound {
sender: AccountId,
sender: DomainAddress,
destination: Domain,
message: Message,
},
}

impl<AccountId, Message: Default> Default for GatewayMessage<AccountId, Message> {
impl<Message: Default> Default for GatewayMessage<Message> {
fn default() -> Self {
GatewayMessage::Inbound {
domain_address: Default::default(),
Expand Down

0 comments on commit ac5671c

Please sign in to comment.