From 60bb6e8342a8bbf30a3594684de6e52c99b26773 Mon Sep 17 00:00:00 2001 From: Cosmin Damian <17934949+cdamian@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:23:27 +0300 Subject: [PATCH] lp: Add constants for LP defensive weights --- libs/primitives/src/lib.rs | 4 ++++ pallets/liquidity-pools-gateway/queue/src/lib.rs | 5 +++-- .../liquidity-pools-gateway/queue/src/mock.rs | 16 +++++++++++----- pallets/liquidity-pools-gateway/src/lib.rs | 14 ++++++-------- pallets/liquidity-pools-gateway/src/tests.rs | 8 ++++++-- runtime/integration-tests/src/cases/lp/utils.rs | 6 +++--- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/libs/primitives/src/lib.rs b/libs/primitives/src/lib.rs index 54e1194993..f51eb578db 100644 --- a/libs/primitives/src/lib.rs +++ b/libs/primitives/src/lib.rs @@ -281,6 +281,10 @@ 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; } /// Listing of parachains we integrate with. diff --git a/pallets/liquidity-pools-gateway/queue/src/lib.rs b/pallets/liquidity-pools-gateway/queue/src/lib.rs index a68c5565b4..ad363c7ad4 100644 --- a/pallets/liquidity-pools-gateway/queue/src/lib.rs +++ b/pallets/liquidity-pools-gateway/queue/src/lib.rs @@ -14,6 +14,7 @@ use core::fmt::Debug; +use cfg_primitives::constants::{LP_DEFENSIVE_WEIGHT_POV, LP_DEFENSIVE_WEIGHT_REF_TIME}; use cfg_traits::liquidity_pools::{MessageProcessor, MessageQueue as MessageQueueT}; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; @@ -130,7 +131,7 @@ pub mod pallet { /// are not reverted. /// - an extra defensive weight is added in order to cover the weight /// used when processing the message. - #[pallet::weight(T::WeightInfo::process_message().saturating_add(Weight::from_parts(200_000_000, 4096)))] + #[pallet::weight(T::WeightInfo::process_message().saturating_add(Weight::from_parts(LP_DEFENSIVE_WEIGHT_REF_TIME, LP_DEFENSIVE_WEIGHT_POV)))] #[pallet::call_index(0)] pub fn process_message(origin: OriginFor, nonce: T::MessageNonce) -> DispatchResult { ensure_signed(origin)?; @@ -158,7 +159,7 @@ pub mod pallet { /// are not reverted. /// - an extra defensive weight is added in order to cover the weight /// used when processing the message. - #[pallet::weight(T::WeightInfo::process_failed_message().saturating_add(Weight::from_parts(200_000_000, 4096)))] + #[pallet::weight(T::WeightInfo::process_failed_message().saturating_add(Weight::from_parts(LP_DEFENSIVE_WEIGHT_REF_TIME, LP_DEFENSIVE_WEIGHT_POV)))] #[pallet::call_index(1)] pub fn process_failed_message( origin: OriginFor, diff --git a/pallets/liquidity-pools-gateway/queue/src/mock.rs b/pallets/liquidity-pools-gateway/queue/src/mock.rs index ceff4f1bfc..f5511b507f 100644 --- a/pallets/liquidity-pools-gateway/queue/src/mock.rs +++ b/pallets/liquidity-pools-gateway/queue/src/mock.rs @@ -12,13 +12,18 @@ // GNU General Public License for more details. use cfg_mocks::pallet_mock_liquidity_pools_gateway; -use cfg_primitives::LPGatewayQueueMessageNonce; +use cfg_primitives::{ + LPGatewayQueueMessageNonce, LP_DEFENSIVE_WEIGHT_POV, LP_DEFENSIVE_WEIGHT_REF_TIME, +}; use cfg_traits::liquidity_pools::test_util::Message as LPTestMessage; use cfg_types::domain_address::Domain; use frame_support::{derive_impl, pallet_prelude::Weight}; use sp_runtime::traits::ConstU128; -use crate::{self as pallet_liquidity_pools_gateway_queue, Config}; +use crate::{ + self as pallet_liquidity_pools_gateway_queue, Config, DEFENSIVE_WEIGHT_REF_POV, + DEFENSIVE_WEIGHT_REF_TIME, +}; frame_support::construct_runtime!( pub enum Runtime { @@ -57,11 +62,12 @@ impl Config for Runtime { type WeightInfo = (); } -const DEFENSIVE_WEIGHT_REF_TIME: u64 = 5_000_000_000; - pub fn mock_lp_gateway_process_success() { LPGatewayMock::mock_process(move |_| { - (Ok(()), Weight::from_parts(DEFENSIVE_WEIGHT_REF_TIME, 256)) + ( + Ok(()), + Weight::from_parts(LP_DEFENSIVE_WEIGHT_REF_TIME, LP_DEFENSIVE_WEIGHT_POV), + ) }); } diff --git a/pallets/liquidity-pools-gateway/src/lib.rs b/pallets/liquidity-pools-gateway/src/lib.rs index a4340e0300..e2a543bb83 100644 --- a/pallets/liquidity-pools-gateway/src/lib.rs +++ b/pallets/liquidity-pools-gateway/src/lib.rs @@ -79,11 +79,7 @@ pub mod pallet { const BYTES_U32: usize = 4; const BYTES_ACCOUNT_20: usize = 20; - /// Some gateway routers do not return an actual weight when sending a - /// message, thus, this default is required, and it's based on: - /// - /// https://github.com/centrifuge/centrifuge-chain/pull/1696#discussion_r1456370592 - const DEFAULT_WEIGHT_REF_TIME: u64 = 5_000_000_000; + use cfg_primitives::{LP_DEFENSIVE_WEIGHT_POV, LP_DEFENSIVE_WEIGHT_REF_TIME}; use super::*; use crate::RelayerMessageDecodingError::{ @@ -535,8 +531,10 @@ pub mod pallet { domain_address: DomainAddress, message: T::Message, ) -> (DispatchResult, Weight) { - let weight = Weight::from_parts(0, T::Message::max_encoded_len() as u64) - .saturating_add(Weight::from_parts(200_000_000, 4096)); + let weight = + Weight::from_parts(0, T::Message::max_encoded_len() as u64).saturating_add( + Weight::from_parts(LP_DEFENSIVE_WEIGHT_REF_TIME, LP_DEFENSIVE_WEIGHT_POV), + ); match T::InboundMessageHandler::handle(domain_address, message) { Ok(_) => (Ok(()), weight), @@ -599,7 +597,7 @@ pub mod pallet { .expect("can calculate outbound message POV weight"); router_call_weight - .unwrap_or(Weight::from_parts(DEFAULT_WEIGHT_REF_TIME, 0)) + .unwrap_or(Weight::from_parts(LP_DEFENSIVE_WEIGHT_REF_TIME, 0)) .saturating_add(Weight::from_parts(0, pov_weight)) } } diff --git a/pallets/liquidity-pools-gateway/src/tests.rs b/pallets/liquidity-pools-gateway/src/tests.rs index f26e13d0d2..a70083c95e 100644 --- a/pallets/liquidity-pools-gateway/src/tests.rs +++ b/pallets/liquidity-pools-gateway/src/tests.rs @@ -1043,6 +1043,8 @@ mod message_processor_impl { use super::*; mod inbound { + use cfg_primitives::{LP_DEFENSIVE_WEIGHT_POV, LP_DEFENSIVE_WEIGHT_REF_TIME}; + use super::*; #[test] @@ -1086,8 +1088,10 @@ mod message_processor_impl { Err(err) }); - let expected_weight = Weight::from_parts(0, Message::max_encoded_len() as u64) - .saturating_add(Weight::from_parts(200_000_000, 4096)); + let expected_weight = + Weight::from_parts(0, Message::max_encoded_len() as u64).saturating_add( + Weight::from_parts(LP_DEFENSIVE_WEIGHT_REF_TIME, LP_DEFENSIVE_WEIGHT_POV), + ); let (res, weight) = LiquidityPoolsGateway::process(gateway_message); assert_noop!(res, err); diff --git a/runtime/integration-tests/src/cases/lp/utils.rs b/runtime/integration-tests/src/cases/lp/utils.rs index 629227da6a..191f8ddb16 100644 --- a/runtime/integration-tests/src/cases/lp/utils.rs +++ b/runtime/integration-tests/src/cases/lp/utils.rs @@ -119,9 +119,9 @@ pub fn verify_gateway_message_success( .. } if { match &processed_message { - GatewayMessage::Inbound{ message, .. } => *message == lp_message, - GatewayMessage::Outbound{ message, .. } => *message == lp_message, - } + GatewayMessage::Inbound{ message, .. } + | GatewayMessage::Outbound{ message, .. } => *message == lp_message, + } } )); }