Skip to content

Commit

Permalink
Refactor: Decoupling routers from gateway (#1891)
Browse files Browse the repository at this point in the history
* decoupling routers from gateway. Use Vec<u8>

* gateway adapted

* fix imports

* move test msg to cfg-traits

* fix imports
  • Loading branch information
lemunozm authored Jun 28, 2024
1 parent eb38d05 commit aaa6bb6
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 308 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use currency_conversion::pallet as pallet_mock_currency_conversion;
pub use data::pallet as pallet_mock_data;
pub use fees::pallet as pallet_mock_fees;
pub use investment::pallet as pallet_mock_investment;
pub use liquidity_pools::{pallet as pallet_mock_liquidity_pools, MessageMock};
pub use liquidity_pools::pallet as pallet_mock_liquidity_pools;
pub use liquidity_pools_gateway_routers::{pallet as pallet_mock_routers, RouterMock};
pub use pay_fee::pallet as pallet_mock_pay_fee;
pub use permissions::pallet as pallet_mock_permissions;
Expand Down
39 changes: 1 addition & 38 deletions libs/mocks/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,9 @@
use cfg_traits::liquidity_pools::Codec;
use parity_scale_codec::{Decode, Encode, Error, Input, MaxEncodedLen};
use scale_info::TypeInfo;

#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub enum MessageMock {
First,
Second,
}

impl MessageMock {
fn call_type(&self) -> u8 {
match self {
MessageMock::First => 0,
MessageMock::Second => 1,
}
}
}

impl Codec for MessageMock {
fn serialize(&self) -> Vec<u8> {
vec![self.call_type()]
}

fn deserialize<I: Input>(input: &mut I) -> Result<Self, Error> {
let call_type = input.read_byte()?;

match call_type {
0 => Ok(MessageMock::First),
1 => Ok(MessageMock::Second),
_ => Err("unsupported message".into()),
}
}
}

#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::liquidity_pools::InboundQueue;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

use crate::liquidity_pools::MessageMock;

#[pallet::config]
pub trait Config: frame_system::Config {
type DomainAddress;
Expand All @@ -54,7 +17,7 @@ pub mod pallet {
type CallIds<T: Config> = StorageMap<_, _, String, mock_builder::CallId>;

impl<T: Config> Pallet<T> {
pub fn mock_submit(f: impl Fn(T::DomainAddress, MessageMock) -> DispatchResult + 'static) {
pub fn mock_submit(f: impl Fn(T::DomainAddress, T::Message) -> DispatchResult + 'static) {
register_call!(move |(sender, msg)| f(sender, msg));
}
}
Expand Down
17 changes: 5 additions & 12 deletions libs/mocks/src/liquidity_pools_gateway_routers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
use mock_builder::{execute_call, register_call};
use sp_std::default::Default;

use crate::MessageMock;

#[frame_support::pallet(dev_mode)]
pub mod pallet {
use super::*;
Expand All @@ -24,21 +22,20 @@ pub mod pallet {
}

pub fn mock_send(
f: impl Fn(T::AccountId, MessageMock) -> DispatchResultWithPostInfo + 'static,
f: impl Fn(T::AccountId, Vec<u8>) -> DispatchResultWithPostInfo + 'static,
) {
register_call!(move |(sender, message)| f(sender, message));
}
}

impl<T: Config> MockedRouter for Pallet<T> {
type Message = MessageMock;
type Sender = T::AccountId;

fn init() -> DispatchResult {
execute_call!(())
}

fn send(sender: Self::Sender, message: MessageMock) -> DispatchResultWithPostInfo {
fn send(sender: Self::Sender, message: Vec<u8>) -> DispatchResultWithPostInfo {
execute_call!((sender, message))
}
}
Expand Down Expand Up @@ -67,7 +64,7 @@ impl<T: pallet::Config> RouterMock<T> {

pub fn mock_send(
&self,
f: impl Fn(T::AccountId, MessageMock) -> DispatchResultWithPostInfo + 'static,
f: impl Fn(T::AccountId, Vec<u8>) -> DispatchResultWithPostInfo + 'static,
) {
pallet::Pallet::<T>::mock_send(f)
}
Expand All @@ -76,14 +73,13 @@ impl<T: pallet::Config> RouterMock<T> {
/// Here we implement the actual Router trait for the `RouterMock` which in turn
/// calls the `MockedRouter` trait implementation.
impl<T: pallet::Config> Router for RouterMock<T> {
type Message = MessageMock;
type Sender = T::AccountId;

fn init(&self) -> DispatchResult {
pallet::Pallet::<T>::init()
}

fn send(&self, sender: Self::Sender, message: Self::Message) -> DispatchResultWithPostInfo {
fn send(&self, sender: Self::Sender, message: Vec<u8>) -> DispatchResultWithPostInfo {
pallet::Pallet::<T>::send(sender, message)
}
}
Expand All @@ -98,12 +94,9 @@ trait MockedRouter {
/// The sender type of the outbound message.
type Sender;

/// The outbound message type.
type Message;

/// Initialize the router.
fn init() -> DispatchResult;

/// Send the message to the router's destination.
fn send(sender: Self::Sender, message: Self::Message) -> DispatchResultWithPostInfo;
fn send(sender: Self::Sender, message: Vec<u8>) -> DispatchResultWithPostInfo;
}
28 changes: 24 additions & 4 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,39 @@ pub trait Codec: Sized {
fn deserialize<I: Input>(input: &mut I) -> Result<Self, parity_scale_codec::Error>;
}

#[cfg(any(test, feature = "std"))]
pub mod test_util {
use parity_scale_codec::{Decode, Encode, Input, MaxEncodedLen};
use scale_info::TypeInfo;

use super::Codec;

#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct Message;
impl Codec for Message {
fn serialize(&self) -> Vec<u8> {
vec![0x42]
}

fn deserialize<I: Input>(input: &mut I) -> Result<Self, parity_scale_codec::Error> {
match input.read_byte()? {
0x42 => Ok(Self),
_ => Err("unsupported message".into()),
}
}
}
}

/// The trait required for sending outbound messages.
pub trait Router {
/// The sender type of the outbound message.
type Sender;

/// The outbound message type.
type Message;

/// Initialize the router.
fn init(&self) -> DispatchResult;

/// Send the message to the router's destination.
fn send(&self, sender: Self::Sender, message: Self::Message) -> DispatchResultWithPostInfo;
fn send(&self, sender: Self::Sender, message: Vec<u8>) -> DispatchResultWithPostInfo;
}

/// The trait required for processing outbound messages.
Expand Down
4 changes: 0 additions & 4 deletions pallets/liquidity-pools-gateway/routers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ cfg-traits = { workspace = true }

# Local pallets
pallet-ethereum-transaction = { workspace = true }
pallet-liquidity-pools-gateway = { workspace = true }

[dev-dependencies]
cfg-types = { workspace = true, default-features = true }
Expand Down Expand Up @@ -73,7 +72,6 @@ std = [
"sp-std/std",
"sp-core/std",
"staging-xcm/std",
"pallet-liquidity-pools-gateway/std",
"pallet-xcm-transactor/std",
"pallet-ethereum/std",
"pallet-ethereum-transaction/std",
Expand All @@ -90,7 +88,6 @@ runtime-benchmarks = [
"cfg-primitives/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-liquidity-pools-gateway/runtime-benchmarks",
"pallet-ethereum/runtime-benchmarks",
"pallet-ethereum-transaction/runtime-benchmarks",
"pallet-xcm-transactor/runtime-benchmarks",
Expand All @@ -107,7 +104,6 @@ try-runtime = [
"cfg-primitives/try-runtime",
"cfg-types/try-runtime",
"cfg-mocks/try-runtime",
"pallet-liquidity-pools-gateway/try-runtime",
"pallet-ethereum/try-runtime",
"pallet-ethereum-transaction/try-runtime",
"pallet-xcm-transactor/try-runtime",
Expand Down
49 changes: 18 additions & 31 deletions pallets/liquidity-pools-gateway/routers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ mod mock;
#[cfg(test)]
mod tests;

pub mod routers;

pub use routers::*;
pub mod routers {
pub mod axelar_evm;
pub mod axelar_xcm;
pub mod ethereum_xcm;
}

type CurrencyIdOf<T> = <T as pallet_xcm_transactor::Config>::CurrencyId;
type MessageOf<T> = <T as pallet_liquidity_pools_gateway::Config>::Message;
type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub use routers::{
axelar_evm::AxelarEVMRouter, axelar_xcm::AxelarXCMRouter, ethereum_xcm::EthereumXCMRouter,
};

/// Maximum size allowed for a byte representation of an Axelar EVM chain
/// string, as found below:
Expand All @@ -87,11 +89,7 @@ const AXELAR_PAYLOAD_PARAM: &str = "payload";
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, MaxEncodedLen)]
pub enum DomainRouter<T>
where
T: frame_system::Config
+ pallet_xcm_transactor::Config
+ pallet_liquidity_pools_gateway::Config
+ pallet_ethereum_transaction::Config
+ pallet_evm::Config,
T: pallet_xcm_transactor::Config + pallet_ethereum_transaction::Config + pallet_evm::Config,
T::AccountId: AsRef<[u8; 32]>,
OriginFor<T>:
From<pallet_ethereum::Origin> + Into<Result<pallet_ethereum::Origin, OriginFor<T>>>,
Expand All @@ -103,17 +101,12 @@ where

impl<T> Router for DomainRouter<T>
where
T: frame_system::Config
+ pallet_xcm_transactor::Config
+ pallet_liquidity_pools_gateway::Config
+ pallet_ethereum_transaction::Config
+ pallet_evm::Config,
T: pallet_xcm_transactor::Config + pallet_ethereum_transaction::Config + pallet_evm::Config,
T::AccountId: AsRef<[u8; 32]>,
OriginFor<T>:
From<pallet_ethereum::Origin> + Into<Result<pallet_ethereum::Origin, OriginFor<T>>>,
{
type Message = MessageOf<T>;
type Sender = AccountIdOf<T>;
type Sender = T::AccountId;

fn init(&self) -> DispatchResult {
match self {
Expand All @@ -123,7 +116,7 @@ where
}
}

fn send(&self, sender: Self::Sender, message: Self::Message) -> DispatchResultWithPostInfo {
fn send(&self, sender: Self::Sender, message: Vec<u8>) -> DispatchResultWithPostInfo {
match self {
DomainRouter::EthereumXCM(r) => r.do_send(sender, message),
DomainRouter::AxelarEVM(r) => r.do_send(sender, message),
Expand All @@ -136,7 +129,7 @@ where
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, MaxEncodedLen)]
pub struct EVMRouter<T>
where
T: frame_system::Config + pallet_ethereum_transaction::Config + pallet_evm::Config,
T: pallet_ethereum_transaction::Config + pallet_evm::Config,
OriginFor<T>:
From<pallet_ethereum::Origin> + Into<Result<pallet_ethereum::Origin, OriginFor<T>>>,
{
Expand All @@ -146,7 +139,7 @@ where

impl<T> EVMRouter<T>
where
T: frame_system::Config + pallet_ethereum_transaction::Config + pallet_evm::Config,
T: pallet_ethereum_transaction::Config + pallet_evm::Config,
OriginFor<T>:
From<pallet_ethereum::Origin> + Into<Result<pallet_ethereum::Origin, OriginFor<T>>>,
{
Expand All @@ -160,7 +153,7 @@ where

impl<T> EVMRouter<T>
where
T: frame_system::Config + pallet_ethereum_transaction::Config + pallet_evm::Config,
T: pallet_ethereum_transaction::Config + pallet_evm::Config,
T::AccountId: AsRef<[u8; 32]>,
OriginFor<T>:
From<pallet_ethereum::Origin> + Into<Result<pallet_ethereum::Origin, OriginFor<T>>>,
Expand Down Expand Up @@ -229,17 +222,11 @@ pub struct FeeValues {

/// A generic router used for executing XCM calls.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, MaxEncodedLen)]
pub struct XCMRouter<T>
where
T: frame_system::Config + pallet_xcm_transactor::Config,
{
pub struct XCMRouter<T: pallet_xcm_transactor::Config> {
pub xcm_domain: XcmDomain<T::CurrencyId>,
}

impl<T> XCMRouter<T>
where
T: frame_system::Config + pallet_xcm_transactor::Config,
{
impl<T: pallet_xcm_transactor::Config> XCMRouter<T> {
/// Sets the weight information for the provided XCM domain location, and
/// the fee per second for the provided fee asset location.
pub fn do_init(&self) -> DispatchResult {
Expand Down Expand Up @@ -286,7 +273,7 @@ pub(crate) fn get_encoded_ethereum_xcm_call<T>(
msg: Vec<u8>,
) -> Result<Vec<u8>, ()>
where
T: frame_system::Config + pallet_xcm_transactor::Config,
T: pallet_xcm_transactor::Config,
{
let input =
BoundedVec::<u8, ConstU32<{ xcm_primitives::MAX_ETHEREUM_XCM_INPUT_SIZE }>>::try_from(msg)
Expand Down
Loading

0 comments on commit aaa6bb6

Please sign in to comment.