Skip to content

Commit

Permalink
lp: Add constants for LP defensive weights
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Aug 2, 2024
1 parent 9689a41 commit 60bb6e8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
4 changes: 4 additions & 0 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions pallets/liquidity-pools-gateway/queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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<T>, nonce: T::MessageNonce) -> DispatchResult {
ensure_signed(origin)?;
Expand Down Expand Up @@ -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<T>,
Expand Down
16 changes: 11 additions & 5 deletions pallets/liquidity-pools-gateway/queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<T: Config>() {
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),
)
});
}

Expand Down
14 changes: 6 additions & 8 deletions pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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))
}
}
Expand Down
8 changes: 6 additions & 2 deletions pallets/liquidity-pools-gateway/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions runtime/integration-tests/src/cases/lp/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ pub fn verify_gateway_message_success<T: Runtime>(
..
} 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,
}
}
));
}
Expand Down

0 comments on commit 60bb6e8

Please sign in to comment.