Skip to content

Commit

Permalink
LPv2: Fix weights with batch messages (#1951)
Browse files Browse the repository at this point in the history
* fix weights with limits

* tests on_idle with weights

* fix namings

* fix clippy
  • Loading branch information
lemunozm authored Aug 9, 2024
1 parent 4bfeabe commit e0d2230
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 122 deletions.
14 changes: 12 additions & 2 deletions libs/mocks/src/liquidity_pools_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub mod pallet {
use cfg_traits::liquidity_pools::{MessageProcessor, OutboundMessageHandler};
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};
use mock_builder::{execute_call, register_call, CallHandler};
use orml_traits::GetByKey;

#[pallet::config]
Expand All @@ -18,7 +18,13 @@ 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) -> (DispatchResult, Weight) + 'static) {
pub fn mock_process(
f: impl Fn(T::Message) -> (DispatchResult, Weight) + 'static,
) -> CallHandler {
register_call!(f)
}

pub fn mock_max_processing_weight(f: impl Fn(&T::Message) -> Weight + 'static) {
register_call!(f);
}

Expand All @@ -39,6 +45,10 @@ pub mod pallet {
fn process(msg: Self::Message) -> (DispatchResult, Weight) {
execute_call!(msg)
}

fn max_processing_weight(msg: &Self::Message) -> Weight {
execute_call!(msg)
}
}

impl<T: Config> GetByKey<T::Destination, Option<[u8; 20]>> for Pallet<T> {
Expand Down
3 changes: 3 additions & 0 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pub trait MessageProcessor {

/// 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.
Expand Down
56 changes: 31 additions & 25 deletions pallets/liquidity-pools-gateway/queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use core::fmt::Debug;

use cfg_traits::liquidity_pools::{MessageProcessor, MessageQueue as MessageQueueT};
use frame_support::pallet_prelude::*;
use frame_support::{dispatch::PostDispatchInfo, pallet_prelude::*};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use parity_scale_codec::FullCodec;
Expand All @@ -29,10 +29,6 @@ mod mock;
#[cfg(test)]
mod tests;

pub mod weights;

pub use weights::WeightInfo;

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -56,9 +52,6 @@ pub mod pallet {

/// Type used for processing messages.
type MessageProcessor: MessageProcessor<Message = Self::Message>;

/// Weight information.
type WeightInfo: WeightInfo;
}

#[pallet::pallet]
Expand Down Expand Up @@ -128,21 +121,25 @@ 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())]
#[pallet::weight(MessageQueue::<T>::get(nonce)
.map(|msg| T::MessageProcessor::max_processing_weight(&msg))
.unwrap_or(T::DbWeight::get().reads(1)))]
#[pallet::call_index(0)]
pub fn process_message(origin: OriginFor<T>, nonce: T::MessageNonce) -> DispatchResult {
pub fn process_message(
origin: OriginFor<T>,
nonce: T::MessageNonce,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;

let message = MessageQueue::<T>::take(nonce).ok_or(Error::<T>::MessageNotFound)?;

match Self::process_message_and_deposit_event(nonce, message.clone()) {
(Ok(_), _) => Ok(()),
(Err(e), _) => {
FailedMessageQueue::<T>::insert(nonce, (message, e));
let (result, weight) = Self::process_message_and_deposit_event(nonce, message.clone());

Ok(())
}
if let Err(e) = result {
FailedMessageQueue::<T>::insert(nonce, (message, e));
}

Ok(PostDispatchInfo::from(Some(weight)))
}

/// Convenience method for manually processing a failed message.
Expand All @@ -156,25 +153,26 @@ 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())]
#[pallet::weight(FailedMessageQueue::<T>::get(nonce)
.map(|(msg, _)| T::MessageProcessor::max_processing_weight(&msg))
.unwrap_or(T::DbWeight::get().reads(1)))]
#[pallet::call_index(1)]
pub fn process_failed_message(
origin: OriginFor<T>,
nonce: T::MessageNonce,
) -> DispatchResult {
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;

let (message, _) =
FailedMessageQueue::<T>::get(nonce).ok_or(Error::<T>::MessageNotFound)?;

match Self::process_message_and_deposit_event(nonce, message.clone()) {
(Ok(_), _) => {
FailedMessageQueue::<T>::remove(nonce);
let (result, weight) = Self::process_message_and_deposit_event(nonce, message);

Ok(())
}
(Err(_), _) => Ok(()),
if result.is_ok() {
FailedMessageQueue::<T>::remove(nonce);
}

Ok(PostDispatchInfo::from(Some(weight)))
}
}

Expand Down Expand Up @@ -207,7 +205,13 @@ pub mod pallet {
let mut processed_entries = Vec::new();

for (nonce, message) in MessageQueue::<T>::iter() {
processed_entries.push(nonce);
let remaining_weight = max_weight.saturating_sub(weight_used);
let next_weight = T::MessageProcessor::max_processing_weight(&message);

// We ensure we have still capacity in the block before processing the message
if remaining_weight.any_lt(next_weight) {
break;
}

let weight = match Self::process_message_and_deposit_event(nonce, message.clone()) {
(Ok(()), weight) => {
Expand All @@ -229,6 +233,8 @@ pub mod pallet {
}
};

processed_entries.push(nonce);

weight_used = weight_used.saturating_add(weight);

if weight_used.all_gte(max_weight) {
Expand Down
16 changes: 2 additions & 14 deletions pallets/liquidity-pools-gateway/queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ use cfg_mocks::pallet_mock_liquidity_pools_gateway;
use cfg_primitives::LPGatewayQueueMessageNonce;
use cfg_types::domain_address::Domain;
use frame_support::derive_impl;
use sp_runtime::traits::ConstU128;

use crate::{self as pallet_liquidity_pools_gateway_queue, Config};

frame_support::construct_runtime!(
pub enum Runtime {
System: frame_system,
Balances: pallet_balances,
LPGatewayMock: pallet_mock_liquidity_pools_gateway,
LPGatewayQueue: pallet_liquidity_pools_gateway_queue,
}
Expand All @@ -34,26 +32,16 @@ impl frame_system::Config for Runtime {
type Block = frame_system::mocking::MockBlock<Runtime>;
}

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)]
impl pallet_balances::Config for Runtime {
type AccountStore = System;
type Balance = u128;
type DustRemoval = ();
type ExistentialDeposit = ConstU128<1>;
type RuntimeHoldReason = ();
}

impl pallet_mock_liquidity_pools_gateway::Config for Runtime {
type Destination = Domain;
type Message = ();
type Message = u32;
}

impl Config for Runtime {
type Message = ();
type Message = u32;
type MessageNonce = LPGatewayQueueMessageNonce;
type MessageProcessor = LPGatewayMock;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
Loading

0 comments on commit e0d2230

Please sign in to comment.