Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Aug 13, 2024
1 parent c12b4db commit 004e7c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
10 changes: 5 additions & 5 deletions pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ pub mod pallet {
/// Inbound domain session not found.
InboundDomainSessionNotFound,

/// The router that sent the inbound message is unknown.
UnknownInboundMessageRouter,
/// Unknown router.
UnknownRouter,

/// The router that sent the message is not the first one.
MessageExpectedFromFirstRouter,
Expand Down Expand Up @@ -443,7 +443,7 @@ pub mod pallet {

match PackedMessage::<T>::take((&sender, &destination)) {
Some(msg) if msg.submessages().is_empty() => Ok(()), //No-op
Some(message) => Self::queue_message(destination, message),
Some(message) => Self::queue_outbound_message(destination, message),
None => Err(Error::<T>::MessagePackingNotStarted.into()),
}
}
Expand All @@ -467,7 +467,7 @@ pub mod pallet {

ensure!(
routers.iter().any(|x| x == &router_id),
Error::<T>::UnknownInboundMessageRouter
Error::<T>::UnknownRouter
);

PendingInboundEntries::<T>::try_mutate(
Expand Down Expand Up @@ -513,7 +513,7 @@ pub mod pallet {

PackedMessage::<T>::mutate((&from, destination.clone()), |batch| match batch {
Some(batch) => batch.pack_with(message),
None => Self::queue_message(destination, message),
None => Self::queue_outbound_message(destination, message),
})
}
}
Expand Down
41 changes: 22 additions & 19 deletions pallets/liquidity-pools-gateway/src/message_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ pub enum InboundEntry<T: Config> {
#[derive(Clone)]
pub struct InboundProcessingInfo<T: Config> {
domain_address: DomainAddress,
routers: BoundedVec<T::RouterId, T::MaxRouterCount>,
router_ids: Vec<T::RouterId, T::MaxRouterCount>,
current_session_id: T::SessionId,
expected_proof_count_per_message: u32,
}

impl<T: Config> Pallet<T> {
/// Calculates and returns the proof count required for processing one
/// inbound message.
fn get_expected_proof_count() -> Result<u32, DispatchError> {
let routers = Routers::<T>::get();
fn get_expected_proof_count(domain: Domain) -> Result<u32, DispatchError> {
let routers_count = T::RouterId::for_domain(domain).len();

let expected_proof_count = routers.len().ensure_sub(1)?;
let expected_proof_count = routers_count.ensure_sub(1)?;

Ok(expected_proof_count as u32)
}
Expand Down Expand Up @@ -96,25 +96,25 @@ impl<T: Config> Pallet<T> {
router_id: &T::RouterId,
inbound_entry: &InboundEntry<T>,
) -> DispatchResult {
let routers = inbound_processing_info.routers.clone();
let router_ids = inbound_processing_info.router_ids.clone();

ensure!(
routers.iter().any(|x| x == router_id),
Error::<T>::UnknownInboundMessageRouter
router_ids.iter().any(|x| x == router_id),
Error::<T>::UnknownRouter
);

match inbound_entry {
InboundEntry::Message { .. } => {
ensure!(
routers.get(0) == Some(&router_id),
router_ids.get(0) == Some(&router_id),
Error::<T>::MessageExpectedFromFirstRouter
);

Ok(())
}
InboundEntry::Proof { .. } => {
ensure!(
routers.get(0) != Some(&router_id),
router_ids.get(0) != Some(&router_id),
Error::<T>::ProofNotExpectedFromFirstRouter
);

Expand Down Expand Up @@ -204,12 +204,12 @@ impl<T: Config> Pallet<T> {
let mut message = None;
let mut votes = 0;

for router in &inbound_processing_info.routers {
for router_id in &inbound_processing_info.router_ids {
weight.saturating_accrue(T::DbWeight::get().reads(1));

match PendingInboundEntries::<T>::get(
inbound_processing_info.current_session_id,
(message_proof, router),
(message_proof, router_id),
) {
// We expected one InboundEntry for each router, if that's not the case,
// we can return.
Expand Down Expand Up @@ -242,12 +242,12 @@ impl<T: Config> Pallet<T> {
message_proof: Proof,
weight: &mut Weight,
) -> DispatchResult {
for router in &inbound_processing_info.routers {
for router_id in &inbound_processing_info.router_ids {
weight.saturating_accrue(T::DbWeight::get().writes(1));

match PendingInboundEntries::<T>::try_mutate(
inbound_processing_info.current_session_id,
(message_proof, router),
(message_proof, router_id),
|storage_entry| match storage_entry {
None => Err(Error::<T>::PendingInboundEntryNotFound.into()),
Some(stored_inbound_entry) => match stored_inbound_entry {
Expand Down Expand Up @@ -292,24 +292,24 @@ impl<T: Config> Pallet<T> {
/// Retrieves the information required for processing an inbound
/// message.
fn get_inbound_processing_info(
domain_address: DomainAddress,
domain: Domain,
weight: &mut Weight,
) -> Result<InboundProcessingInfo<T>, DispatchError> {
let routers = Routers::<T>::get();
let router_ids = T::RouterId::for_domain(domain.clone());

weight.saturating_accrue(T::DbWeight::get().reads(1));

let current_session_id = SessionIdStore::<T>::get();

weight.saturating_accrue(T::DbWeight::get().reads(1));

let expected_proof_count = Self::get_expected_proof_count()?;
let expected_proof_count = Self::get_expected_proof_count(domain)?;

weight.saturating_accrue(T::DbWeight::get().reads(1));

Ok(InboundProcessingInfo {
domain_address,
routers,
router_ids,
current_session_id,
expected_proof_count_per_message: expected_proof_count,
})
Expand All @@ -325,7 +325,7 @@ impl<T: Config> Pallet<T> {
let mut weight = Default::default();

let inbound_processing_info =
match Self::get_inbound_processing_info(domain_address.clone(), &mut weight) {
match Self::get_inbound_processing_info(domain_address.domain(), &mut weight) {
Ok(i) => i,
Err(e) => return (Err(e), weight),
};
Expand Down Expand Up @@ -392,7 +392,10 @@ impl<T: Config> Pallet<T> {

/// Retrieves the IDs of the routers set for a domain and queues the
/// message and proofs accordingly.
pub(crate) fn queue_message(destination: Domain, message: T::Message) -> DispatchResult {
pub(crate) fn queue_outbound_message(
destination: Domain,
message: T::Message,
) -> DispatchResult {
let router_ids = T::RouterId::for_domain(destination);

let message_proof = message.to_message_proof();
Expand Down
4 changes: 2 additions & 2 deletions pallets/liquidity-pools-gateway/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ mod message_processor_impl {
InboundMessageSessions::<Runtime>::insert(domain_address.domain(), session_id);

let (res, _) = LiquidityPoolsGateway::process(gateway_message);
assert_noop!(res, Error::<Runtime>::UnknownInboundMessageRouter);
assert_noop!(res, Error::<Runtime>::UnknownRouter);
});
}

Expand Down Expand Up @@ -2967,7 +2967,7 @@ mod execute_message_recovery {
MESSAGE_PROOF,
router_id_2
),
Error::<Runtime>::UnknownInboundMessageRouter
Error::<Runtime>::UnknownRouter
);
});
}
Expand Down

0 comments on commit 004e7c1

Please sign in to comment.