Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lp-gateway: Add multi-router support #1948

Closed
wants to merge 11 commits into from
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl-trait-for-tuples = "0.2.2"
num-traits = { version = "0.2.17", default-features = false }
num_enum = { version = "0.5.3", default-features = false }
chrono = { version = "0.4", default-features = false }
itertools = { version = "0.13.0", default-features = false }

# Cumulus
cumulus-pallet-aura-ext = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.7.2" }
Expand Down
8 changes: 5 additions & 3 deletions libs/mocks/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub mod pallet {
use cfg_traits::liquidity_pools::InboundMessageHandler;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};
use mock_builder::{execute_call, register_call, CallHandler};

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

impl<T: Config> Pallet<T> {
pub fn mock_handle(f: impl Fn(T::DomainAddress, T::Message) -> DispatchResult + 'static) {
register_call!(move |(sender, msg)| f(sender, msg));
pub fn mock_handle(
f: impl Fn(T::DomainAddress, T::Message) -> DispatchResult + 'static,
) -> CallHandler {
register_call!(move |(sender, msg)| f(sender, msg))
}
}

Expand Down
22 changes: 22 additions & 0 deletions libs/mocks/src/liquidity_pools_gateway_routers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ pub mod pallet {
) {
register_call!(move |(sender, message)| f(sender, message));
}

pub fn mock_hash(f: impl Fn() -> T::Hash + 'static) {
register_call!(move |()| f());
}
}

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

fn init() -> DispatchResult {
Expand All @@ -38,6 +43,10 @@ pub mod pallet {
fn send(sender: Self::Sender, message: Vec<u8>) -> DispatchResultWithPostInfo {
execute_call!((sender, message))
}

fn hash() -> Self::Hash {
execute_call!(())
}
}
}

Expand Down Expand Up @@ -68,11 +77,16 @@ impl<T: pallet::Config> RouterMock<T> {
) {
pallet::Pallet::<T>::mock_send(f)
}

pub fn mock_hash(&self, f: impl Fn() -> <RouterMock<T> as Router>::Hash + 'static) {
pallet::Pallet::<T>::mock_hash(f)
}
}

/// 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 Hash = T::Hash;
type Sender = T::AccountId;

fn init(&self) -> DispatchResult {
Expand All @@ -82,6 +96,10 @@ impl<T: pallet::Config> Router for RouterMock<T> {
fn send(&self, sender: Self::Sender, message: Vec<u8>) -> DispatchResultWithPostInfo {
pallet::Pallet::<T>::send(sender, message)
}

fn hash(&self) -> Self::Hash {
pallet::Pallet::<T>::hash()
}
}

/// A mocked Router trait that emulates the actual Router trait but without
Expand All @@ -94,9 +112,13 @@ trait MockedRouter {
/// The sender type of the outbound message.
type Sender;

type Hash;

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

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

fn hash() -> Self::Hash;
}
14 changes: 14 additions & 0 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use frame_support::{
use sp_runtime::DispatchError;
use sp_std::vec::Vec;

pub type Proof = [u8; 32];

/// An encoding & decoding trait for the purpose of meeting the
/// LiquidityPools General Message Passing Format
pub trait LPEncoding: Sized {
Expand All @@ -34,18 +36,30 @@ pub trait LPEncoding: Sized {
/// Creates an empty message.
/// It's the identity message for composing messages with pack_with
fn empty() -> Self;

/// Retrieves the message proof, if any.
fn get_message_proof(&self) -> Option<Proof>;

/// Converts the message into a message proof type.
fn to_message_proof(&self) -> Self;
}

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

/// The router hash type.
type Hash;

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

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

/// Generate a hash for this router.
fn hash(&self) -> Self::Hash;
}

/// The trait required for queueing messages.
Expand Down
5 changes: 5 additions & 0 deletions pallets/liquidity-pools-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ scale-info = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
sp-arithmetic = { workspace = true }

# Benchmarking
frame-benchmarking = { workspace = true, optional = true }
Expand All @@ -35,6 +36,9 @@ cfg-utils = { workspace = true }
[dev-dependencies]
cfg-mocks = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }
itertools = { workspace = true, default-features = true }
lazy_static = { workspace = true, default-features = true }
mock-builder = { workspace = true, default-features = true }

[features]
default = ["std"]
Expand All @@ -53,6 +57,7 @@ std = [
"cfg-utils/std",
"hex/std",
"cfg-primitives/std",
"sp-arithmetic/std",
]
try-runtime = [
"cfg-traits/try-runtime",
Expand Down
7 changes: 7 additions & 0 deletions pallets/liquidity-pools-gateway/routers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ where
OriginFor<T>:
From<pallet_ethereum::Origin> + Into<Result<pallet_ethereum::Origin, OriginFor<T>>>,
{
type Hash = T::Hash;
type Sender = T::AccountId;

fn init(&self) -> DispatchResult {
Expand All @@ -89,6 +90,12 @@ where
DomainRouter::AxelarEVM(r) => r.do_send(sender, message),
}
}

fn hash(&self) -> Self::Hash {
match self {
DomainRouter::AxelarEVM(r) => r.hash(),
}
}
}

/// A generic router used for executing EVM calls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use scale_info::{
prelude::{format, string::String},
TypeInfo,
};
use sp_core::{bounded::BoundedVec, ConstU32, H160};
use sp_core::{bounded::BoundedVec, ConstU32, Hasher, H160};
use sp_runtime::traits::BlakeTwo256;
use sp_std::{collections::btree_map::BTreeMap, vec, vec::Vec};

use crate::{
Expand Down Expand Up @@ -77,6 +78,10 @@ where

self.router.do_send(sender, eth_msg)
}

pub fn hash(&self) -> T::Hash {
BlakeTwo256::hash(self.evm_chain.encode().as_slice())
}
Comment on lines +82 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure: The router hash is solely used locally and not as part of any dispatched message right? Asking because Solidity usually uses keccak256.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is mainly for the LP gateway to map a router hash to a router in storage.

}

/// Encodes the provided message into the format required for submitting it
Expand Down
Loading