Skip to content

Commit

Permalink
queue traits to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 17, 2024
1 parent 74d957c commit d902431
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 62 deletions.
3 changes: 1 addition & 2 deletions libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub mod foreign_investment_hooks;
pub mod investment;
pub mod liquidity_pools;
pub mod liquidity_pools_gateway;
pub mod liquidity_pools_gateway_queue;
pub mod pay_fee;
pub mod permissions;
pub mod pools;
pub mod pre_conditions;
pub mod queue;
pub mod rewards;
pub mod router_message;
pub mod status_notification;
Expand All @@ -30,7 +30,6 @@ 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::pallet as pallet_mock_liquidity_pools_gateway;
pub use liquidity_pools_gateway_queue::pallet as pallet_mock_liquidity_pools_gateway_queue;
pub use pay_fee::pallet as pallet_mock_pay_fee;
pub use permissions::pallet as pallet_mock_permissions;
pub use pools::pallet as pallet_mock_pools;
Expand Down
2 changes: 1 addition & 1 deletion libs/mocks/src/liquidity_pools_gateway.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::liquidity_pools::{MessageProcessor, OutboundMessageHandler};
use cfg_traits::{liquidity_pools::OutboundMessageHandler, queue::MessageProcessor};
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call, CallHandler};
use orml_traits::GetByKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::liquidity_pools::MessageQueue;
use cfg_traits::queue::MessageQueue;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call, CallHandler};

Expand Down
11 changes: 1 addition & 10 deletions libs/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,16 @@ use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use sp_runtime::{traits::Member, DispatchError};
use sp_std::{fmt::Debug, marker::PhantomData, vec::Vec};

/// Traits related to checked changes.
pub mod changes;
/// Traits related to data registry and collection.
pub mod data;
/// Traits related to Ethereum/EVM.
pub mod ethereum;
/// Traits related to pool fees.
pub mod fee;
/// Traits related to fees payment.
pub mod fees;
/// Traits related to interest rates.
pub mod interest;
/// Traits related to investments.
pub mod investments;
/// Traits related to liquidity pools.
pub mod liquidity_pools;
/// Traits related to rewards.
pub mod queue;
pub mod rewards;
/// Traits related to swaps.
pub mod swaps;

#[cfg(feature = "runtime-benchmarks")]
Expand Down
23 changes: 1 addition & 22 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, weights::Weight};
use frame_support::dispatch::DispatchResult;
use sp_runtime::DispatchError;
use sp_std::vec::Vec;

Expand Down Expand Up @@ -78,27 +78,6 @@ pub trait MessageReceiver {
) -> DispatchResult;
}

/// The trait required for queueing messages.
pub trait MessageQueue {
/// The message type.
type Message;

/// Submit a message to the queue.
fn submit(msg: Self::Message) -> DispatchResult;
}

/// The trait required for processing queued messages.
pub trait MessageProcessor {
/// The message type.
type Message;

/// Process a message.
fn process(msg: Self::Message) -> (DispatchResult, Weight);

/// Process a message.
fn max_processing_weight(msg: &Self::Message) -> Weight;
}

/// The trait required for handling outbound LP messages.
pub trait OutboundMessageHandler {
/// The sender type of the outbound message.
Expand Down
22 changes: 22 additions & 0 deletions libs/traits/src/queue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use frame_support::{dispatch::DispatchResult, weights::Weight};

/// The trait required for queueing messages.
pub trait MessageQueue {
/// The message type.
type Message;

/// Submit a message to the queue.
fn submit(msg: Self::Message) -> DispatchResult;
}

/// The trait required for processing queued messages.
pub trait MessageProcessor {
/// The message type.
type Message;

/// Process a message.
fn process(msg: Self::Message) -> (DispatchResult, Weight);

/// Max weight that processing a message can take
fn max_processing_weight(msg: &Self::Message) -> Weight;
}
2 changes: 1 addition & 1 deletion pallets/liquidity-pools-gateway-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use core::fmt::Debug;

use cfg_traits::liquidity_pools::{MessageProcessor, MessageQueue as MessageQueueT};
use cfg_traits::queue::{MessageProcessor, MessageQueue as MessageQueueT};
use frame_support::{dispatch::PostDispatchInfo, pallet_prelude::*};
use frame_system::pallet_prelude::*;
pub use pallet::*;
Expand Down
2 changes: 1 addition & 1 deletion pallets/liquidity-pools-gateway-queue/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cfg_primitives::LPGatewayQueueMessageNonce;
use cfg_traits::liquidity_pools::MessageQueue as MessageQueueT;
use cfg_traits::queue::MessageQueue as _;
use frame_support::{
assert_noop, assert_ok, dispatch::RawOrigin, pallet_prelude::Hooks, weights::Weight,
};
Expand Down
9 changes: 6 additions & 3 deletions pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
use core::fmt::Debug;

use cfg_primitives::LP_DEFENSIVE_WEIGHT;
use cfg_traits::liquidity_pools::{
InboundMessageHandler, LPEncoding, MessageProcessor, MessageQueue, MessageReceiver,
MessageSender, OutboundMessageHandler, Proof, RouterProvider,
use cfg_traits::{
liquidity_pools::{
InboundMessageHandler, LPEncoding, MessageReceiver, MessageSender, OutboundMessageHandler,
Proof, RouterProvider,
},
queue::{MessageProcessor, MessageQueue},
};
use cfg_types::domain_address::{Domain, DomainAddress};
use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
Expand Down
5 changes: 3 additions & 2 deletions pallets/liquidity-pools-gateway/src/message_processing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cfg_primitives::LP_DEFENSIVE_WEIGHT;
use cfg_traits::liquidity_pools::{
InboundMessageHandler, LPEncoding, MessageQueue, Proof, RouterProvider,
use cfg_traits::{
liquidity_pools::{InboundMessageHandler, LPEncoding, Proof, RouterProvider},
queue::MessageQueue,
};
use cfg_types::domain_address::{Domain, DomainAddress};
use frame_support::{
Expand Down
6 changes: 3 additions & 3 deletions pallets/liquidity-pools-gateway/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::{Debug, Formatter};

use cfg_mocks::{pallet_mock_liquidity_pools, pallet_mock_liquidity_pools_gateway_queue};
use cfg_mocks::pallet_mock_liquidity_pools;
use cfg_traits::liquidity_pools::{LPEncoding, Proof, RouterProvider};
use cfg_types::{
domain_address::{Domain, DomainAddress},
Expand Down Expand Up @@ -133,7 +133,7 @@ frame_support::construct_runtime!(
pub enum Runtime {
System: frame_system,
MockLiquidityPools: pallet_mock_liquidity_pools,
MockLiquidityPoolsGatewayQueue: pallet_mock_liquidity_pools_gateway_queue,
MockLiquidityPoolsGatewayQueue: cfg_mocks::queue::pallet,
MockMessageSender: cfg_mocks::router_message::pallet,
LiquidityPoolsGateway: pallet_liquidity_pools_gateway,
}
Expand All @@ -152,7 +152,7 @@ impl pallet_mock_liquidity_pools::Config for Runtime {
type Message = Message;
}

impl pallet_mock_liquidity_pools_gateway_queue::Config for Runtime {
impl cfg_mocks::queue::pallet::Config for Runtime {
type Message = GatewayMessage<Message, RouterId>;
}

Expand Down
5 changes: 4 additions & 1 deletion pallets/liquidity-pools-gateway/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::collections::HashMap;

use cfg_primitives::LP_DEFENSIVE_WEIGHT;
use cfg_traits::liquidity_pools::{LPEncoding, MessageProcessor, OutboundMessageHandler};
use cfg_traits::{
liquidity_pools::{LPEncoding, OutboundMessageHandler},
queue::MessageProcessor,
};
use cfg_types::domain_address::*;
use frame_support::{assert_err, assert_noop, assert_ok};
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion runtime/integration-tests/src/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ mod ethereum_transaction;
mod example;
mod investments;
mod liquidity_pools;
mod liquidity_pools_gateway_queue;
mod loans;
mod lp;
mod oracles;
mod proxy;
mod queue;
mod restricted_transfers;
mod routers;
mod xcm_transfers;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cfg_primitives::AccountId;
use cfg_traits::liquidity_pools::MessageQueue;
use cfg_traits::queue::MessageQueue;
use cfg_types::domain_address::DomainAddress;
use frame_support::{assert_ok, traits::OriginTrait};
use pallet_liquidity_pools::Message;
Expand All @@ -16,16 +16,10 @@ use crate::{

pub const DEFAULT_ROUTER_ID: RouterId = RouterId::Axelar(AxelarId::Evm(1));

/// NOTE - we're using fudge here because in a non-fudge environment, the event
/// can only be read before block finalization. The LP gateway queue is
/// processing messages during the `on_idle` hook, just before the block is
/// finished, after the message is processed, the block is finalized and the
/// event resets.
/// Confirm that an inbound messages reaches its destination:
/// LP pallet
#[test_runtimes(all)]
fn inbound<T: Runtime>() {
fn queue_and_dequeue_inbound<T: Runtime>() {
let mut env = RuntimeEnv::<T>::default();

let expected_event = env.parachain_state_mut(|| {
Expand All @@ -34,13 +28,14 @@ fn inbound<T: Runtime>() {
BoundedVec::try_from(vec![DEFAULT_ROUTER_ID]).unwrap(),
));

let nonce = <T as pallet_liquidity_pools_gateway_queue::Config>::MessageNonce::one();
let nonce = T::MessageNonce::one();
let message = GatewayMessage::Inbound {
domain_address: DomainAddress::Evm(1, H160::repeat_byte(2)),
router_id: DEFAULT_ROUTER_ID,
message: Message::Invalid,
};

// Here we enqueue
assert_ok!(pallet_liquidity_pools_gateway_queue::Pallet::<T>::submit(
message.clone()
));
Expand All @@ -52,16 +47,17 @@ fn inbound<T: Runtime>() {
}
});

// Here we dequeue
env.pass(Blocks::UntilEvent {
event: expected_event.into(),
limit: 1,
});
}

/// Confirm that an inbound messages reaches its destination:
/// LP gateway pallet
/// Confirm that an outbound messages reaches its destination:
/// The routers
#[test_runtimes(all)]
fn outbound<T: Runtime>() {
fn queue_and_dequeue_outbound<T: Runtime>() {
let mut env = RuntimeEnv::<T>::default();

let expected_event = env.parachain_state_mut(|| {
Expand All @@ -70,13 +66,14 @@ fn outbound<T: Runtime>() {
BoundedVec::try_from(vec![DEFAULT_ROUTER_ID]).unwrap(),
));

let nonce = <T as pallet_liquidity_pools_gateway_queue::Config>::MessageNonce::one();
let nonce = T::MessageNonce::one();
let message = GatewayMessage::Outbound {
sender: DomainAddress::Centrifuge(AccountId::new([1; 32])),
router_id: DEFAULT_ROUTER_ID,
message: Message::Invalid,
};

// Here we enqueue
assert_ok!(pallet_liquidity_pools_gateway_queue::Pallet::<T>::submit(
message.clone()
));
Expand All @@ -88,6 +85,7 @@ fn outbound<T: Runtime>() {
}
});

// Here we dequeue
env.pass(Blocks::UntilEvent {
event: expected_event.into(),
limit: 1,
Expand Down
4 changes: 3 additions & 1 deletion runtime/integration-tests/src/cases/routers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cfg_primitives::Balance;
use cfg_traits::liquidity_pools::{LPEncoding, MessageProcessor};
use cfg_traits::{liquidity_pools::LPEncoding, queue::MessageProcessor};
use cfg_types::{
domain_address::{Domain, DomainAddress},
EVMChainId,
Expand Down Expand Up @@ -144,6 +144,8 @@ mod axelar_evm {
message: Message::Invalid,
};

// If the message is correctly processed, it means that the router sends
// correcly the message
assert_ok!(pallet_liquidity_pools_gateway::Pallet::<T>::process(gateway_message).0);
});
}
Expand Down

0 comments on commit d902431

Please sign in to comment.