Skip to content

Commit

Permalink
Feat/lp v2 use gateway queue (#1943)
Browse files Browse the repository at this point in the history
* liquidity-pools: Use LP gateway queue for inbound and outbound messages (wip)

* tests: wip

* lp-gateway-queue: Add TODO for benchmarks

* lp-gateway: Add MaxEncodedLen of inbound Message

* gateway: Move GatewayMessage to gateway pallet

* integration-tests: Adapt existing tests to LP gateway queue usage, add specific inbound tests

* integration-tests: Add LP gateway queue tests

* unit-tests: Fix

* integration-tests: Enable all runtimes for LP gateway queue tests

* integration-tests: Skip the gateway queue for LP gateway router tests

* integration-tests: Reduce visibility for LP utils module

* integration-tests: Drop fudge from router evm tests

* integration-tests: Drop fudge from foreign investments tests

* lp-gateway: Replace DispatchResultWithPostInfo with (DispatchResult, Weight)

* integration-tests: Fix LP tests (wip)

* lp-gateway: Drop AddAssign

* integration-tests: Fix failing tests

* lp-gateway-queue: Add defensive weight to extrinsics

* lp-gateway: Add defensive weight when processing inbound message

* unit-tests: Update expected weight in LP gateway tests

* lp: Add constants for LP defensive weights

* deps: Add cfg-primitives

* LP: Add and use LP_DEFENSIVE_WEIGHT

* lp-gateway-queue: Drop benchmarks and use defensive weights instead
  • Loading branch information
cdamian authored Aug 5, 2024
1 parent 7070f0f commit fc2b451
Show file tree
Hide file tree
Showing 47 changed files with 1,142 additions and 1,342 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub mod liquidity_pools;
pub mod liquidity_pools_gateway;
pub mod liquidity_pools_gateway_queue;
pub mod liquidity_pools_gateway_routers;
pub mod outbound_queue;
pub mod pay_fee;
pub mod permissions;
pub mod pools;
Expand Down
8 changes: 4 additions & 4 deletions libs/mocks/src/liquidity_pools.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::InboundQueue;
use cfg_traits::liquidity_pools::InboundMessageHandler;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

Expand All @@ -17,16 +17,16 @@ 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));
}
}

impl<T: Config> InboundQueue for Pallet<T> {
impl<T: Config> InboundMessageHandler for Pallet<T> {
type Message = T::Message;
type Sender = T::DomainAddress;

fn submit(sender: Self::Sender, msg: Self::Message) -> DispatchResult {
fn handle(sender: Self::Sender, msg: Self::Message) -> DispatchResult {
execute_call!((sender, msg))
}
}
Expand Down
38 changes: 35 additions & 3 deletions libs/mocks/src/liquidity_pools_gateway.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::liquidity_pools::MessageProcessor;
use cfg_traits::liquidity_pools::{MessageProcessor, OutboundMessageHandler};
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};
use orml_traits::GetByKey;

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

#[pallet::pallet]
Expand All @@ -16,16 +18,46 @@ pub mod pallet {
type CallIds<T: Config> = StorageMap<_, _, String, mock_builder::CallId>;

impl<T: Config> Pallet<T> {
pub fn mock_process(f: impl Fn(T::Message) -> DispatchResultWithPostInfo + 'static) {
pub fn mock_process(f: impl Fn(T::Message) -> (DispatchResult, Weight) + 'static) {
register_call!(f);
}

pub fn mock_get(f: impl Fn(&T::Destination) -> Option<[u8; 20]> + 'static) {
register_call!(f);
}

pub fn mock_handle(
f: impl Fn(T::AccountId, T::Destination, T::Message) -> DispatchResult + 'static,
) {
register_call!(move |(sender, destination, msg)| f(sender, destination, msg));
}
}

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

fn process(msg: Self::Message) -> DispatchResultWithPostInfo {
fn process(msg: Self::Message) -> (DispatchResult, Weight) {
execute_call!(msg)
}
}

impl<T: Config> GetByKey<T::Destination, Option<[u8; 20]>> for Pallet<T> {
fn get(a: &T::Destination) -> Option<[u8; 20]> {
execute_call!(a)
}
}

impl<T: Config> OutboundMessageHandler for Pallet<T> {
type Destination = T::Destination;
type Message = T::Message;
type Sender = T::AccountId;

fn handle(
sender: Self::Sender,
destination: Self::Destination,
msg: Self::Message,
) -> DispatchResult {
execute_call!((sender, destination, msg))
}
}
}
48 changes: 0 additions & 48 deletions libs/mocks/src/outbound_queue.rs

This file was deleted.

11 changes: 8 additions & 3 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ pub mod types {
/// The representation of a pool fee identifier
pub type PoolFeeId = u64;

/// The type for outbound LP message nonces.
pub type OutboundMessageNonce = u64;

/// The type for LP gateway message nonces.
pub type LPGatewayQueueMessageNonce = u64;
}
Expand Down Expand Up @@ -284,6 +281,14 @@ pub mod constants {
pub const TRACK_INDEX_REF_CANCELLER: u16 = 20;
/// The index of the referendum killer OpenGov track
pub const TRACK_INDEX_REF_KILLER: u16 = 21;

/// Defensive weight parts that are used in LP related pallets.
pub const LP_DEFENSIVE_WEIGHT_REF_TIME: u64 = 5_000_000_000;
pub const LP_DEFENSIVE_WEIGHT_POV: u64 = 4096;

/// Defensive weight for processing LP messages;
pub const LP_DEFENSIVE_WEIGHT: Weight =
Weight::from_parts(LP_DEFENSIVE_WEIGHT_REF_TIME, LP_DEFENSIVE_WEIGHT_POV);
}

/// Listing of parachains we integrate with.
Expand Down
64 changes: 34 additions & 30 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use frame_support::dispatch::{DispatchResult, DispatchResultWithPostInfo};
use frame_support::{
dispatch::{DispatchResult, DispatchResultWithPostInfo},
weights::Weight,
};
use sp_runtime::DispatchError;
use sp_std::vec::Vec;

Expand Down Expand Up @@ -58,50 +61,51 @@ pub trait Router {
fn send(&self, sender: Self::Sender, message: Vec<u8>) -> DispatchResultWithPostInfo;
}

/// The trait required for processing outbound messages.
pub trait OutboundQueue {
/// The sender type of the outgoing message.
type Sender;
/// The trait required for queueing messages.
pub trait MessageQueue {
/// The message type.
type Message;

/// The destination this message should go to.
type Destination;
/// Submit a message to the queue.
fn submit(msg: Self::Message) -> DispatchResult;
}

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

/// Submit a message to the outbound queue.
fn submit(
sender: Self::Sender,
destination: Self::Destination,
msg: Self::Message,
) -> DispatchResult;
/// Process a message.
fn process(msg: Self::Message) -> (DispatchResult, Weight);
}

/// The trait required for processing incoming messages.
pub trait InboundQueue {
/// The sender type of the incoming message.
/// The trait required for handling outbound LP messages.
pub trait OutboundMessageHandler {
/// The sender type of the outbound message.
type Sender;

/// The liquidityPools message type.
type Message;

/// Submit a message to the inbound queue.
fn submit(sender: Self::Sender, msg: Self::Message) -> DispatchResult;
}
/// The destination type of the outbound message.
type Destination;

/// 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;
/// Handle an outbound message.
fn handle(
sender: Self::Sender,
destination: Self::Destination,
msg: Self::Message,
) -> DispatchResult;
}

pub trait MessageProcessor {
/// The trait required for handling inbound LP messages.
pub trait InboundMessageHandler {
/// The sender type of the inbound message.
type Sender;

/// The message type.
type Message;

/// Process a message.
fn process(msg: Self::Message) -> DispatchResultWithPostInfo;
/// Handle an inbound message.
fn handle(sender: Self::Sender, msg: Self::Message) -> DispatchResult;
}
6 changes: 6 additions & 0 deletions libs/types/src/domain_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ pub enum DomainAddress {
EVM(EVMChainId, [u8; 20]),
}

impl Default for DomainAddress {
fn default() -> Self {
DomainAddress::Centrifuge([0; 32])
}
}

impl DomainAddress {
pub fn evm(chain_id: EVMChainId, address: [u8; 20]) -> Self {
Self::EVM(chain_id, address)
Expand Down
4 changes: 4 additions & 0 deletions pallets/liquidity-pools-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sp-std = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }

# Our custom pallets
cfg-primitives = { workspace = true }
cfg-traits = { workspace = true }
cfg-types = { workspace = true }
cfg-utils = { workspace = true }
Expand Down Expand Up @@ -56,6 +57,7 @@ std = [
"scale-info/std",
"cfg-utils/std",
"hex/std",
"cfg-primitives/std",
]
try-runtime = [
"cfg-traits/try-runtime",
Expand All @@ -65,6 +67,7 @@ try-runtime = [
"sp-runtime/try-runtime",
"cfg-utils/try-runtime",
"cfg-mocks/try-runtime",
"cfg-primitives/try-runtime",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
Expand All @@ -75,4 +78,5 @@ runtime-benchmarks = [
"sp-runtime/runtime-benchmarks",
"cfg-utils/runtime-benchmarks",
"cfg-mocks/runtime-benchmarks",
"cfg-primitives/runtime-benchmarks",
]
3 changes: 2 additions & 1 deletion pallets/liquidity-pools-gateway/queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }

cfg-primitives = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
Expand All @@ -26,7 +27,7 @@ cfg-traits = { workspace = true }

[dev-dependencies]
cfg-mocks = { workspace = true, default-features = true }
cfg-primitives = { workspace = true, default-features = true }
cfg-types = { workspace = true, default-features = true }

pallet-balances = { workspace = true, default-features = true }

Expand Down
Loading

0 comments on commit fc2b451

Please sign in to comment.