Skip to content

Commit

Permalink
fix: re-add RouterProvider to forwarder pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Aug 18, 2024
1 parent 4a0f751 commit cd72182
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
45 changes: 28 additions & 17 deletions pallets/liquidity-pools-forwarder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ mod tests;

use core::fmt::Debug;

use cfg_traits::liquidity_pools::{LpMessage as LpMessageT, MessageReceiver, MessageSender};
use cfg_traits::liquidity_pools::{
LpMessage as LpMessageT, MessageReceiver, MessageSender, RouterProvider,
};
use cfg_types::domain_address::{Domain, DomainAddress};
use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
use frame_system::pallet_prelude::OriginFor;
Expand Down Expand Up @@ -100,6 +102,9 @@ pub mod pallet {

/// An identification of a router.
type RouterId: Parameter + MaxEncodedLen;

/// The type that provides the router available for a domain.
type RouterProvider: RouterProvider<Domain, RouterId = Self::RouterId>;
}

#[pallet::event]
Expand Down Expand Up @@ -219,26 +224,32 @@ pub mod pallet {
type Origin = DomainAddress;

fn receive(
router_id: T::RouterId,
domain_address: DomainAddress,
forwarder_router_id: T::RouterId,
forwarder_domain_address: DomainAddress,
message: T::Message,
) -> DispatchResult {
// Message can be unwrapped iff it was forwarded
//
// NOTE: We can rely on EVM side to ensure forwarded messages are valid such
// that it suffices to filter for the existence of forwarding info
let lp_message = match (
RouterForwarding::<T>::get(&router_id).is_some(),
message.clone().unwrap_forwarded(),
) {
(true, Some((_domain, _contract, lp_message))) => Ok(lp_message),
(true, None) => Err(Error::<T>::UnwrappingFailed),
(false, None) => Ok(message),
(false, Some((_, _, _))) => Err(Error::<T>::ForwardInfoNotFound),
if let Some((source_domain, _contract, lp_message)) = message.clone().unwrap_forwarded()
{
let router_ids = T::RouterProvider::routers_for_domain(source_domain);
for router_id in router_ids {
// NOTE: It suffices to check for forwarding existence without need to check the
// forwarding contract address. For that, we can rely on EVM side to ensure
// forwarded messages are valid
if RouterForwarding::<T>::get(&router_id).is_some() {
return T::MessageReceiver::receive(
// Since message was sent from forwarder router id, Gateway needs to
// check whether that id is whitelisted, not the source domain
forwarder_router_id,
forwarder_domain_address,
lp_message,
);
}
}
Err(Error::<T>::ForwardInfoNotFound.into())
} else {
T::MessageReceiver::receive(forwarder_router_id, forwarder_domain_address, message)
}
.map_err(|e: Error<T>| e)?;

T::MessageReceiver::receive(router_id, domain_address, lp_message)
}
}
}
21 changes: 17 additions & 4 deletions pallets/liquidity-pools-forwarder/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cfg_traits::liquidity_pools::{LpMessage, MessageHash};
use cfg_traits::liquidity_pools::{LpMessage, MessageHash, RouterProvider};
use cfg_types::domain_address::{Domain, DomainAddress};
use frame_support::{
derive_impl,
Expand All @@ -12,6 +12,7 @@ use sp_runtime::{traits::IdentityLookup, DispatchError};

use crate::pallet as pallet_liquidity_pools_forwarder;

pub type RouterId = u32;
const SOURCE_CHAIN_ID: u64 = 1;
const FORWARDER_CHAIN_ID: u64 = 42;
pub const SOURCE_DOMAIN: Domain = Domain::Evm(SOURCE_CHAIN_ID);
Expand All @@ -20,7 +21,8 @@ pub const FORWARDER_DOMAIN_ADDRESS: DomainAddress =
DomainAddress::Evm(FORWARDER_CHAIN_ID, FORWARDER_ADAPTER_ADDRESS);
pub const FORWARD_CONTRACT: H160 = H160::repeat_byte(2);

pub const ROUTER_ID: RouterId = RouterId(1);
pub const ROUTER_ID: RouterId = 1;
const UNCONFIGURED_ROUTER_ID: RouterId = 2;
const FORWARD_SERIALIZED_MESSAGE_BYTES: [u8; 1] = [0x42];
const NON_FORWARD_SERIALIZED_MESSAGE_BYTES: [u8; 1] = [0x43];
pub const ERROR_NESTING: DispatchError = DispatchError::Other("Nesting forward msg not allowed");
Expand Down Expand Up @@ -100,8 +102,18 @@ impl LpMessage for Message {
}
}

#[derive(Default, Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, MaxEncodedLen, Hash)]
pub struct RouterId(pub u32);
pub struct TestRouterProvider;

impl RouterProvider<Domain> for TestRouterProvider {
type RouterId = RouterId;

fn routers_for_domain(domain: Domain) -> Vec<Self::RouterId> {
match domain {
Domain::Centrifuge => vec![],
Domain::Evm(_) => vec![ROUTER_ID, UNCONFIGURED_ROUTER_ID],
}
}
}

frame_support::construct_runtime!(
pub enum Runtime {
Expand Down Expand Up @@ -131,5 +143,6 @@ impl pallet_liquidity_pools_forwarder::Config for Runtime {
type MessageReceiver = MockSenderReceiver;
type MessageSender = MockSenderReceiver;
type RouterId = RouterId;
type RouterProvider = TestRouterProvider;
type RuntimeEvent = RuntimeEvent;
}
16 changes: 0 additions & 16 deletions pallets/liquidity-pools-forwarder/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,6 @@ mod receive_message {
});
}

#[test]
fn with_failed_unwrapping() {
System::externalities().execute_with(|| {
config_mocks(true);

assert_noop!(
LiquidityPoolsForwarder::receive(
ROUTER_ID,
FORWARDER_DOMAIN_ADDRESS,
Message::NonForward
),
Error::<Runtime>::UnwrappingFailed
);
});
}

#[test]
fn forward_with_message_receiver_err() {
System::externalities().execute_with(|| {
Expand Down
1 change: 1 addition & 0 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,7 @@ impl pallet_liquidity_pools_forwarder::Config for Runtime {
type MessageReceiver = LiquidityPoolsGateway;
type MessageSender = MessageSerializer<RouterDispatcher<Runtime>, LiquidityPoolsForwarder>;
type RouterId = RouterId;
type RouterProvider = LPGatewayRouterProvider;
type RuntimeEvent = RuntimeEvent;
}

Expand Down
1 change: 1 addition & 0 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,7 @@ impl pallet_liquidity_pools_forwarder::Config for Runtime {
type MessageReceiver = LiquidityPoolsGateway;
type MessageSender = MessageSerializer<RouterDispatcher<Runtime>, LiquidityPoolsForwarder>;
type RouterId = RouterId;
type RouterProvider = LPGatewayRouterProvider;
type RuntimeEvent = RuntimeEvent;
}

Expand Down
1 change: 1 addition & 0 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,7 @@ impl pallet_liquidity_pools_forwarder::Config for Runtime {
type MessageReceiver = LiquidityPoolsGateway;
type MessageSender = MessageSerializer<RouterDispatcher<Runtime>, LiquidityPoolsForwarder>;
type RouterId = RouterId;
type RouterProvider = LPGatewayRouterProvider;
type RuntimeEvent = RuntimeEvent;
}

Expand Down

0 comments on commit cd72182

Please sign in to comment.