Skip to content

Commit

Permalink
lp-gateway: Use gateway queue pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Jul 23, 2024
1 parent 11a9fbd commit 7e40381
Show file tree
Hide file tree
Showing 8 changed files with 513 additions and 479 deletions.
2 changes: 2 additions & 0 deletions libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod fees;
pub mod foreign_investment;
pub mod investment;
pub mod liquidity_pools;
pub mod liquidity_pools_gateway_queue;
pub mod liquidity_pools_gateway_routers;
pub mod outbound_queue;
pub mod pay_fee;
Expand All @@ -26,6 +27,7 @@ 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;
pub use liquidity_pools_gateway_queue::pallet as pallet_mock_liquidity_pools_gateway_queue;
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
2 changes: 1 addition & 1 deletion libs/mocks/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,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, T::Message) -> DispatchResult + 'static) {
pub fn mock_handle(f: impl Fn(T::DomainAddress, T::Message) -> DispatchResult + 'static) {
register_call!(move |(sender, msg)| f(sender, msg));
}
}
Expand Down
31 changes: 31 additions & 0 deletions libs/mocks/src/liquidity_pools_gateway_queue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::liquidity_pools::MessageQueue;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

#[pallet::config]
pub trait Config: frame_system::Config {
type Message;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::storage]
type CallIds<T: Config> = StorageMap<_, _, String, mock_builder::CallId>;

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

impl<T: Config> MessageQueue for Pallet<T> {
type Message = T::Message;

fn submit(msg: Self::Message) -> DispatchResult {
execute_call!(msg)
}
}
}
10 changes: 10 additions & 0 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ pub mod test_util {
}
}
}

impl LPMessage for Message {
fn get_message_proof(&self) -> Option<[u8; 32]> {
Some([1; 32])
}

fn to_message_proof(&self) -> Self {
Self
}
}
}

/// The trait required for sending outbound messages.
Expand Down
46 changes: 26 additions & 20 deletions pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,14 @@
use core::fmt::Debug;

use cfg_traits::{
liquidity_pools::{
InboundMessageHandler, LPMessage, MessageQueue, OutboundQueue, Router as DomainRouter,
},
liquidity_pools::{InboundMessageHandler, LPMessage, Router as DomainRouter},
TryConvert,
};
use cfg_types::domain_address::{Domain, DomainAddress};
use frame_support::{dispatch::DispatchResult, pallet_prelude::*, PalletError};
use frame_system::{
ensure_signed,
pallet_prelude::{BlockNumberFor, OriginFor},
};
use frame_system::pallet_prelude::OriginFor;
pub use pallet::*;
use parity_scale_codec::{EncodeLike, FullCodec};
use sp_runtime::traits::{AtLeast32BitUnsigned, EnsureAdd, EnsureAddAssign, One};
use sp_std::{convert::TryInto, vec::Vec};

use crate::weights::WeightInfo;
Expand Down Expand Up @@ -87,7 +81,7 @@ pub mod pallet {
use cfg_traits::liquidity_pools::{LPEncoding, MessageProcessor, MessageQueue};
use cfg_types::gateway::GatewayMessage;
use frame_support::dispatch::PostDispatchInfo;
use sp_runtime::DispatchErrorWithPostInfo;
use sp_runtime::{traits::EnsureSub, DispatchErrorWithPostInfo};

use super::*;
use crate::RelayerMessageDecodingError::{
Expand Down Expand Up @@ -278,11 +272,8 @@ pub mod pallet {
/// signals malforming of the wrapping information.
RelayerMessageDecodingFailed { reason: RelayerMessageDecodingError },

/// Outbound message not found in storage.
OutboundMessageNotFound,

/// Failed outbound message not found in storage.
FailedOutboundMessageNotFound,
/// Message proof cannot be retrieved.
MessageProofRetrieval,
}

#[pallet::call]
Expand Down Expand Up @@ -579,11 +570,14 @@ pub mod pallet {
})?
.len();

let expected_proof_count = routers_count - 1;
let expected_proof_count = routers_count.ensure_sub(1)?;

let (message_proof, message_proof_count) = match message.get_message_proof() {
None => {
let message_proof = message.to_message_proof().get_message_proof().unwrap();
let message_proof = message
.to_message_proof()
.get_message_proof()
.ok_or(Error::<T>::MessageProofRetrieval)?;

InboundMessages::<T>::insert(message_proof, message);

Expand All @@ -607,7 +601,7 @@ pub mod pallet {
post_info.actual_weight = Some(
post_info
.actual_weight
.unwrap()
.unwrap_or_default()
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
);

Expand All @@ -619,16 +613,28 @@ pub mod pallet {
return Ok(post_info);
}

let message = InboundMessages::<T>::get(message_proof).unwrap();
post_info.actual_weight = Some(
post_info
.actual_weight
.unwrap_or_default()
.saturating_add(T::DbWeight::get().reads(1)),
);

let message = match InboundMessages::<T>::get(message_proof) {
Some(m) => m,
// Not finding the message here is not a problem. We might have the correct message proof
// count but no actual message.
None => return Ok(post_info),
};

InboundMessages::<T>::remove(message_proof);
InboundMessageProofCount::<T>::remove(message_proof);

post_info.actual_weight = Some(
post_info
.actual_weight
.unwrap()
.saturating_add(T::DbWeight::get().reads_writes(1, 2)),
.unwrap_or_default()
.saturating_add(T::DbWeight::get().writes(2)),
);

match T::InboundMessageHandler::handle(domain_address, message) {
Expand Down
17 changes: 12 additions & 5 deletions pallets/liquidity-pools-gateway/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use cfg_mocks::{pallet_mock_liquidity_pools, pallet_mock_routers, RouterMock};
use cfg_primitives::OutboundMessageNonce;
use crate::{pallet as pallet_liquidity_pools_gateway, EnsureLocal};
use cfg_mocks::{
pallet_mock_liquidity_pools, pallet_mock_liquidity_pools_gateway_queue, pallet_mock_routers,
RouterMock,
};
use cfg_traits::liquidity_pools::test_util::Message;
use cfg_types::domain_address::DomainAddress;
use cfg_types::gateway::GatewayMessage;
use frame_support::derive_impl;
use frame_system::EnsureRoot;
use sp_core::{crypto::AccountId32, H256};
use sp_runtime::traits::IdentityLookup;

use crate::{pallet as pallet_liquidity_pools_gateway, EnsureLocal};

pub const LENGTH_SOURCE_CHAIN: usize = 10;
pub const SOURCE_CHAIN: [u8; LENGTH_SOURCE_CHAIN] = *b"ethereum-2";
pub const SOURCE_CHAIN_EVM_ID: u64 = 1;
Expand All @@ -20,6 +22,7 @@ frame_support::construct_runtime!(
pub enum Runtime {
System: frame_system,
MockLiquidityPools: pallet_mock_liquidity_pools,
MockLiquidityPoolsGatewayQueue: pallet_mock_liquidity_pools_gateway_queue,
MockRouters: pallet_mock_routers,
MockOriginRecovery: cfg_mocks::converter::pallet,
LiquidityPoolsGateway: pallet_liquidity_pools_gateway,
Expand All @@ -38,6 +41,10 @@ impl pallet_mock_liquidity_pools::Config for Runtime {
type Message = Message;
}

impl pallet_mock_liquidity_pools_gateway_queue::Config for Runtime {
type Message = GatewayMessage<AccountId32, Message>;
}

impl pallet_mock_routers::Config for Runtime {}

impl cfg_mocks::converter::pallet::Config for Runtime {
Expand All @@ -59,12 +66,12 @@ impl pallet_liquidity_pools_gateway::Config for Runtime {
type MaxIncomingMessageSize = MaxIncomingMessageSize;
type MaxRouterCount = MaxRouterCount;
type OriginRecovery = MockOriginRecovery;
type OutboundMessageNonce = OutboundMessageNonce;
type Router = RouterMock<Runtime>;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type Sender = Sender;
type WeightInfo = ();
type MessageQueue = MockLiquidityPoolsGatewayQueue;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
Loading

0 comments on commit 7e40381

Please sign in to comment.