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

Feat/lp v2 gateway queue #1930

Merged
merged 13 commits into from
Jul 30, 2024
24 changes: 24 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"pallets/liquidity-pools-gateway",
"pallets/liquidity-pools-gateway/axelar-gateway-precompile",
"pallets/liquidity-pools-gateway/routers",
"pallets/liquidity-pools-gateway/queue",
"pallets/liquidity-rewards",
"pallets/loans",
"pallets/oracle-feed",
Expand Down Expand Up @@ -236,6 +237,7 @@ pallet-investments = { path = "pallets/investments", default-features = false }
pallet-keystore = { path = "pallets/keystore", default-features = false }
pallet-liquidity-pools = { path = "pallets/liquidity-pools", default-features = false }
pallet-liquidity-pools-gateway = { path = "pallets/liquidity-pools-gateway", default-features = false }
pallet-liquidity-pools-gateway-queue = { path = "pallets/liquidity-pools-gateway/queue", default-features = false }
pallet-liquidity-rewards = { path = "pallets/liquidity-rewards", default-features = false }
pallet-loans = { path = "pallets/loans", default-features = false }
pallet-oracle-feed = { path = "pallets/oracle-feed", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ On top of the [Substrate FRAME](https://docs.substrate.io/reference/frame-pallet
- [**liquidity-pools**](https://github.com/centrifuge/centrifuge-chain/tree/main/pallets/liquidity-pools) ([docs](https://reference.centrifuge.io/pallet_liquidity_pools/index.html)): Provides the toolset to enable foreign investments on foreign domains.

- [**liquidity-pools-gateway**](https://github.com/centrifuge/centrifuge-chain/tree/main/pallets/liquidity-pools-gateway) ([docs](https://reference.centrifuge.io/pallet_liquidity_pools_gateway/index.html)): The main handler of incoming and outgoing Liquidity Pools messages.
-
- [**liquidity-pools-gateway-queue**](https://github.com/centrifuge/centrifuge-chain/tree/main/pallets/liquidity-pools-gateway/queue) ([docs](https://reference.centrifuge.io/pallet_liquidity_pools_gateway_queue/index.html)): The queue used by the Liquidity Pools Gateway for processing inbound/outbound messages.

- [**liquidity-pools-gateway-routers**](https://github.com/centrifuge/centrifuge-chain/tree/main/pallets/liquidity-pools-gateway/routers) ([docs](https://reference.centrifuge.io/liquidity_pools_gateway_routers/index.html)): This crate contains the `DomainRouters` used by the Liquidity Pools Gateway pallet.

Expand Down
4 changes: 4 additions & 0 deletions libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod foreign_investment;
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 liquidity_pools_gateway_routers;
pub mod outbound_queue;
pub mod pay_fee;
Expand All @@ -27,6 +29,8 @@ 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::pallet as pallet_mock_liquidity_pools_gateway;
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
31 changes: 31 additions & 0 deletions libs/mocks/src/liquidity_pools_gateway.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::MessageProcessor;
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_process(f: impl Fn(T::Message) -> DispatchResultWithPostInfo + 'static) {
register_call!(move |msg| f(msg));
}
}

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

fn process(msg: Self::Message) -> DispatchResultWithPostInfo {
execute_call!(msg)
}
}
}
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)
}
}
}
3 changes: 3 additions & 0 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ pub mod types {

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

/// The type for LP gateway message nonces.
pub type LPGatewayQueueMessageNonce = u64;
}

/// Common constants for all runtimes
Expand Down
17 changes: 17 additions & 0 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,20 @@ pub trait InboundQueue {
/// Submit a message to the inbound queue.
fn submit(sender: Self::Sender, msg: Self::Message) -> 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;
}

pub trait MessageProcessor {
/// The message type.
type Message;

/// Process a message.
fn process(msg: Self::Message) -> DispatchResultWithPostInfo;
}
61 changes: 61 additions & 0 deletions pallets/liquidity-pools-gateway/queue/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[package]
authors = ["Centrifuge <admin@centrifuge.io>"]
description = "Centrifuge Liquidity Pools Gateway Queue Pallet"
edition = "2021"
license = "LGPL-3.0"
name = "pallet-liquidity-pools-gateway-queue"
repository = "https://github.com/centrifuge/centrifuge-chain"
version = "0.0.1"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }

frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
sp-arithmetic = { workspace = true }

cfg-traits = { workspace = true }

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

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

sp-core = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"scale-info/std",
"frame-support/std",
"frame-system/std",
"sp-runtime/std",
"sp-std/std",
"sp-arithmetic/std",
"frame-benchmarking/std",
"cfg-primitives/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"cfg-primitives/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
"cfg-primitives/try-runtime",
]
65 changes: 65 additions & 0 deletions pallets/liquidity-pools-gateway/queue/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2024 Centrifuge Foundation (centrifuge.io).
// This file is part of Centrifuge chain project.

// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).

// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_traits::liquidity_pools::test_util::Message as LPTestMessage;
use frame_benchmarking::{account, impl_benchmark_test_suite, v2::*};
use frame_system::RawOrigin;
use parity_scale_codec::EncodeLike;

use super::*;

#[benchmarks(
where
T: Config<Message = LPTestMessage>,
Copy link
Contributor

@lemunozm lemunozm Jul 29, 2024

Choose a reason for hiding this comment

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

This will probably not pass for the real runtimes. My suggestion is to add a Default bound for Message only when benchmarking it. (which would be an Invalid one)

Option2. Add a worst-case method under runtime-benchmarks in LPEncoding, which allows creation of the worst message possible.

Option3. Add a parameter to the benchmarks that allow to passing the Message, so each message is benchmarked for itself. This will be the most accurate/clean, but not sure if it is possible to pass non-integer parameters to benchmarking functions 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would go with option 2 since that makes the most sense for benchmarks IMO.

Copy link
Contributor Author

@cdamian cdamian Jul 29, 2024

Choose a reason for hiding this comment

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

so each message is benchmarked for itself.

Would this be possible? More specifically, does the benchmark test suite execute for each variant of an enum?

Copy link
Contributor

@lemunozm lemunozm Jul 29, 2024

Choose a reason for hiding this comment

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

The benchmark doesn't know about Message itself. But if it receives the Message as a parameter, it would compute the effect of that Message for that specific call

Copy link
Contributor

Choose a reason for hiding this comment

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

But not sure if it would work. Option 2 seems good

Copy link
Contributor

Choose a reason for hiding this comment

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

Something like this you mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, added a default weight ref time that's also used in the pallet instead of default. PTAL

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, just one thing, the issue is still there because the benchmark is tied to LPTestMessage

Copy link
Contributor Author

@cdamian cdamian Jul 29, 2024

Choose a reason for hiding this comment

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

Been trying things out for a while now and I'm still a bit confused...

Option 2 doesn't really work since LPEncoding is not a trait used as a constraint for the Message type of this pallet, as per these constraints.

Ok, just one thing, the issue is still there because the benchmark is tied to LPTestMessage

This will remain true if we use the mocked runtime anyway. The message type that we use here is the LPTestMessage. nvm, this should be fine as long as the LPTestMessage is not part of the actual benchmark tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated this btw, took me a while to figure out what was happening when running tests and then the check benchmark scripts.

T::AccountId: EncodeLike<<T as frame_system::Config>::AccountId>,
)]
mod benchmarks {
use super::*;

#[benchmark]
fn process_message() -> Result<(), BenchmarkError> {
let caller: T::AccountId = account("acc_0", 0, 0);
let message = LPTestMessage {};
let nonce = T::MessageNonce::one();

MessageQueue::<T>::insert(nonce, message.clone());

#[cfg(test)]
mock::mock_lp_gateway_process_success(message);

#[extrinsic_call]
process_message(RawOrigin::Signed(caller), nonce);

Ok(())
}

#[benchmark]
fn process_failed_message() -> Result<(), BenchmarkError> {
let caller: T::AccountId = account("acc_0", 0, 0);
let message = LPTestMessage {};
let error = DispatchError::Unavailable;
let nonce = T::MessageNonce::one();

FailedMessageQueue::<T>::insert(nonce, (message.clone(), error));

#[cfg(test)]
mock::mock_lp_gateway_process_success(message);

#[extrinsic_call]
process_failed_message(RawOrigin::Signed(caller), nonce);

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
}
Loading
Loading