Skip to content

Commit

Permalink
lp-gateway: Add queue for outbound messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Jan 18, 2024
1 parent ca18a90 commit d4f41a8
Show file tree
Hide file tree
Showing 17 changed files with 575 additions and 151 deletions.
4 changes: 2 additions & 2 deletions libs/mocks/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use cfg_traits::liquidity_pools::Codec;
use parity_scale_codec::{Decode, Encode, Error, Input};
use parity_scale_codec::{Decode, Encode, Error, Input, MaxEncodedLen};
use scale_info::TypeInfo;

#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, TypeInfo)]
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub enum MessageMock {
First,
Second,
Expand Down
15 changes: 10 additions & 5 deletions libs/mocks/src/liquidity_pools_gateway_routers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ pub mod pallet {
register_call!(move |()| f());
}

pub fn mock_send(f: impl Fn(T::AccountId, MessageMock) -> DispatchResult + 'static) {
pub fn mock_send(
f: impl Fn(T::AccountId, MessageMock) -> DispatchResultWithPostInfo + 'static,
) {
register_call!(move |(sender, message)| f(sender, message));
}
}
Expand All @@ -41,7 +43,7 @@ pub mod pallet {
execute_call!(())
}

fn send(sender: Self::Sender, message: MessageMock) -> DispatchResult {
fn send(sender: Self::Sender, message: MessageMock) -> DispatchResultWithPostInfo {
execute_call!((sender, message))
}
}
Expand All @@ -68,7 +70,10 @@ impl<T: pallet::Config> RouterMock<T> {
pallet::Pallet::<T>::mock_init(f)
}

pub fn mock_send(&self, f: impl Fn(T::AccountId, MessageMock) -> DispatchResult + 'static) {
pub fn mock_send(
&self,
f: impl Fn(T::AccountId, MessageMock) -> DispatchResultWithPostInfo + 'static,
) {
pallet::Pallet::<T>::mock_send(f)
}
}
Expand All @@ -83,7 +88,7 @@ impl<T: pallet::Config> Router for RouterMock<T> {
pallet::Pallet::<T>::init()
}

fn send(&self, sender: Self::Sender, message: Self::Message) -> DispatchResult {
fn send(&self, sender: Self::Sender, message: Self::Message) -> DispatchResultWithPostInfo {
pallet::Pallet::<T>::send(sender, message)
}
}
Expand All @@ -105,5 +110,5 @@ trait MockedRouter {
fn init() -> DispatchResult;

/// Send the message to the router's destination.
fn send(sender: Self::Sender, message: Self::Message) -> DispatchResult;
fn send(sender: Self::Sender, message: Self::Message) -> DispatchResultWithPostInfo;
}
4 changes: 2 additions & 2 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use frame_support::dispatch::DispatchResult;
use frame_support::dispatch::{DispatchResult, DispatchResultWithPostInfo};
use parity_scale_codec::Input;
use sp_std::vec::Vec;

Expand All @@ -34,7 +34,7 @@ pub trait Router {
fn init(&self) -> DispatchResult;

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

/// The trait required for processing outbound messages.
Expand Down
21 changes: 10 additions & 11 deletions pallets/liquidity-pools-gateway/routers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ pub const GAS_TO_WEIGHT_MULTIPLIER: u64 = 25_000;

use cfg_traits::{ethereum::EthereumTransactor, liquidity_pools::Router};
use frame_support::{
dispatch::{DispatchError, DispatchResult, Weight},
dispatch::{
DispatchError, DispatchResult, DispatchResultWithPostInfo, PostDispatchInfo, Weight,
},
ensure,
traits::OriginTrait,
};
Expand Down Expand Up @@ -113,7 +115,7 @@ where
}
}

fn send(&self, sender: Self::Sender, message: Self::Message) -> DispatchResult {
fn send(&self, sender: Self::Sender, message: Self::Message) -> DispatchResultWithPostInfo {
match self {
DomainRouter::EthereumXCM(r) => r.do_send(sender, message),
DomainRouter::AxelarEVM(r) => r.do_send(sender, message),
Expand Down Expand Up @@ -159,12 +161,9 @@ where
/// pallet, this EVM address will be converted back into a substrate account
/// which will be charged for the transaction. This converted substrate
/// account is not the same as the original account.
pub fn do_send(&self, sender: T::AccountId, msg: Vec<u8>) -> DispatchResult {
pub fn do_send(&self, sender: T::AccountId, msg: Vec<u8>) -> DispatchResultWithPostInfo {
let sender_evm_address = H160::from_slice(&sender.as_ref()[0..20]);

// TODO(cdamian): This returns a `DispatchResultWithPostInfo`. Should we
// propagate that to another layer that will eventually charge for the
// weight in the PostDispatchInfo?
<pallet_ethereum_transaction::Pallet<T> as EthereumTransactor>::call(
sender_evm_address,
self.evm_domain.target_contract_address,
Expand All @@ -173,9 +172,6 @@ where
self.evm_domain.fee_values.gas_price,
self.evm_domain.fee_values.gas_limit,
)
.map_err(|e| e.error)?;

Ok(())
}
}

Expand Down Expand Up @@ -231,7 +227,7 @@ where

/// Encodes the message to the required format and executes the
/// call via the XCM transactor pallet.
pub fn do_send(&self, sender: T::AccountId, msg: Vec<u8>) -> DispatchResult {
pub fn do_send(&self, sender: T::AccountId, msg: Vec<u8>) -> DispatchResultWithPostInfo {
let ethereum_xcm_call = get_encoded_ethereum_xcm_call::<T>(self.xcm_domain.clone(), msg)
.map_err(|_| DispatchError::Other("encoded ethereum xcm call retrieval"))?;

Expand All @@ -257,7 +253,10 @@ where
true,
)?;

Ok(())
Ok(PostDispatchInfo {
actual_weight: Some(self.xcm_domain.overall_weight),
pays_fee: Default::default(),
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// GNU General Public License for more details.
use cfg_traits::liquidity_pools::Codec;
use ethabi::{Contract, Function, Param, ParamType, Token};
use frame_support::dispatch::{DispatchError, DispatchResult};
use frame_support::dispatch::{DispatchError, DispatchResult, DispatchResultWithPostInfo};
use frame_system::pallet_prelude::OriginFor;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::{
Expand Down Expand Up @@ -61,7 +61,7 @@ where

/// Encodes the message to the required format,
/// then executes the EVM call using the generic EVM router.
pub fn do_send(&self, sender: AccountIdOf<T>, msg: MessageOf<T>) -> DispatchResult {
pub fn do_send(&self, sender: AccountIdOf<T>, msg: MessageOf<T>) -> DispatchResultWithPostInfo {
let eth_msg = get_axelar_encoded_msg(
msg.serialize(),
self.evm_chain.clone().into_inner(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// GNU General Public License for more details.

use cfg_traits::liquidity_pools::Codec;
use frame_support::dispatch::DispatchResult;
use frame_support::dispatch::{DispatchResult, DispatchResultWithPostInfo};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::{bounded::BoundedVec, ConstU32, H160};
Expand Down Expand Up @@ -53,7 +53,7 @@ where

/// Encodes the message to the required format,
/// then executes the EVM call using the generic XCM router.
pub fn do_send(&self, sender: AccountIdOf<T>, msg: MessageOf<T>) -> DispatchResult {
pub fn do_send(&self, sender: AccountIdOf<T>, msg: MessageOf<T>) -> DispatchResultWithPostInfo {
let contract_call = get_axelar_encoded_msg(
msg.serialize(),
self.axelar_target_chain.clone().into_inner(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
// GNU General Public License for more details.
use cfg_traits::liquidity_pools::Codec;
use ethabi::{Bytes, Contract};
use frame_support::{dispatch::DispatchResult, sp_runtime::DispatchError};
use frame_support::{
dispatch::{DispatchResult, DispatchResultWithPostInfo},
sp_runtime::DispatchError,
};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_std::{collections::btree_map::BTreeMap, marker::PhantomData, vec, vec::Vec};
Expand Down Expand Up @@ -43,7 +46,7 @@ where

/// Encodes the message to the required format and executes the
/// call via the XCM router.
pub fn do_send(&self, sender: AccountIdOf<T>, msg: MessageOf<T>) -> DispatchResult {
pub fn do_send(&self, sender: AccountIdOf<T>, msg: MessageOf<T>) -> DispatchResultWithPostInfo {
let contract_call = get_encoded_contract_call(msg.serialize())
.map_err(|_| DispatchError::Other("encoded contract call retrieval"))?;

Expand Down
4 changes: 2 additions & 2 deletions pallets/liquidity-pools-gateway/routers/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ mod evm_router {
let res = router.do_send(test_data.sender, test_data.msg);

assert_eq!(
res.err().unwrap(),
res.err().unwrap().error,
pallet_evm::Error::<Runtime>::BalanceLow.into()
);
});
Expand Down Expand Up @@ -483,7 +483,7 @@ mod axelar_evm {
let res = domain_router.send(test_data.sender, test_data.msg);

assert_eq!(
res.err().unwrap(),
res.err().unwrap().error,
pallet_evm::Error::<Runtime>::BalanceLow.into()
);
});
Expand Down
Loading

0 comments on commit d4f41a8

Please sign in to comment.