Skip to content

Commit

Permalink
isolate gateway weights (#1972)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm authored Aug 16, 2024
1 parent eb7e14b commit 771fb78
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
25 changes: 19 additions & 6 deletions pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,18 +517,31 @@ pub mod pallet {
domain_address,
message,
router_id,
} => Self::process_inbound_message(domain_address, message, router_id),
} => {
let mut counter = 0;

let res = Self::process_inbound_message(
domain_address,
message,
router_id,
&mut counter,
);

let weight = match counter {
0 => LP_DEFENSIVE_WEIGHT / 10,
n => LP_DEFENSIVE_WEIGHT.saturating_mul(n),
};

(res, weight)
}
GatewayMessage::Outbound {
sender,
message,
router_id,
} => {
let weight = LP_DEFENSIVE_WEIGHT;
let res = T::MessageSender::send(router_id, sender, message.serialize());

match T::MessageSender::send(router_id, sender, message.serialize()) {
Ok(_) => (Ok(()), weight),
Err(e) => (Err(e), weight),
}
(res, LP_DEFENSIVE_WEIGHT)
}
}
}
Expand Down
43 changes: 10 additions & 33 deletions pallets/liquidity-pools-gateway/src/message_processing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use cfg_primitives::LP_DEFENSIVE_WEIGHT;
use cfg_traits::liquidity_pools::{
InboundMessageHandler, LPEncoding, MessageQueue, Proof, RouterProvider,
};
Expand All @@ -7,7 +6,6 @@ use frame_support::{
dispatch::DispatchResult,
ensure,
pallet_prelude::{Decode, Encode, Get, TypeInfo},
weights::Weight,
};
use parity_scale_codec::MaxEncodedLen;
use sp_arithmetic::traits::{EnsureAddAssign, EnsureSub, SaturatedConversion};
Expand Down Expand Up @@ -406,27 +404,14 @@ impl<T: Config> Pallet<T> {
domain_address: DomainAddress,
message: T::Message,
router_id: T::RouterId,
) -> (DispatchResult, Weight) {
let weight = LP_DEFENSIVE_WEIGHT;

let router_ids = match Self::get_router_ids_for_domain(domain_address.domain()) {
Ok(r) => r,
Err(e) => return (Err(e), weight),
};

counter: &mut u64,
) -> DispatchResult {
let router_ids = Self::get_router_ids_for_domain(domain_address.domain())?;
let session_id = SessionIdStore::<T>::get();

let expected_proof_count = match Self::get_expected_proof_count(&router_ids) {
Ok(r) => r,
Err(e) => return (Err(e), weight),
};

let mut count = 0;
let expected_proof_count = Self::get_expected_proof_count(&router_ids)?;

for submessage in message.submessages() {
if let Err(e) = count.ensure_add_assign(1) {
return (Err(e.into()), weight.saturating_mul(count));
}
counter.ensure_add_assign(1)?;

let message_proof = Self::get_message_proof(submessage.clone());

Expand All @@ -437,27 +422,19 @@ impl<T: Config> Pallet<T> {
expected_proof_count,
);

if let Err(e) = inbound_entry.validate(&router_ids, &router_id.clone()) {
return (Err(e), weight.saturating_mul(count));
}

if let Err(e) = Self::upsert_pending_entry(message_proof, &router_id, inbound_entry) {
return (Err(e), weight.saturating_mul(count));
}
inbound_entry.validate(&router_ids, &router_id.clone())?;
Self::upsert_pending_entry(message_proof, &router_id, inbound_entry)?;

match Self::execute_if_requirements_are_met(
Self::execute_if_requirements_are_met(
message_proof,
&router_ids,
session_id,
expected_proof_count,
domain_address.clone(),
) {
Err(e) => return (Err(e), weight.saturating_mul(count)),
Ok(_) => continue,
}
)?;
}

(Ok(()), weight.saturating_mul(count))
Ok(())
}

/// Retrieves the IDs of the routers set for a domain and queues the
Expand Down

0 comments on commit 771fb78

Please sign in to comment.