Skip to content

Commit

Permalink
tests: Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Jun 28, 2024
1 parent 48cad3c commit cdc56a2
Show file tree
Hide file tree
Showing 6 changed files with 172 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 @@ -211,7 +211,7 @@ pub mod pallet {
},

/// The router for a given domain was set.
DomainMultiRoutersSet {
DomainMultiRouterSet {
domain: Domain,
routers: BoundedVec<T::Router, T::MaxRouterCount>,
},
Expand Down Expand Up @@ -624,9 +624,9 @@ pub mod pallet {
}

/// Set routers for a particular domain.
#[pallet::weight(T::WeightInfo::set_domain_multi_routers())]
#[pallet::weight(T::WeightInfo::set_domain_multi_router())]
#[pallet::call_index(8)]
pub fn set_domain_multi_routers(
pub fn set_domain_multi_router(
origin: OriginFor<T>,
domain: Domain,
routers: BoundedVec<T::Router, T::MaxRouterCount>,
Expand All @@ -641,7 +641,7 @@ pub mod pallet {

<DomainMultiRouters<T>>::insert(domain.clone(), routers.clone());

Self::deposit_event(Event::DomainMultiRoutersSet { domain, routers });
Self::deposit_event(Event::DomainMultiRouterSet { domain, routers });

Ok(())
}
Expand Down Expand Up @@ -776,7 +776,7 @@ pub mod pallet {
actual_weight: Some(read_weight),
pays_fee: Pays::Yes,
},
error: Error::<T>::RouterNotFound.into(),
error: Error::<T>::MultiRouterNotFound.into(),
})?;

let mut post_dispatch_info = PostDispatchInfo {
Expand Down
174 changes: 161 additions & 13 deletions pallets/liquidity-pools-gateway/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,15 @@ mod set_domain_router {
router.clone(),
));

let storage_entry = DomainRouters::<Runtime>::get(domain.clone());
assert_eq!(storage_entry.unwrap(), router);
assert_eq!(
DomainRouters::<Runtime>::get(domain.clone()).unwrap(),
router
);

event_exists(Event::<Runtime>::DomainRouterSet { domain, router });
});
}

#[test]
fn router_init_error() {
new_test_ext().execute_with(|| {
Expand All @@ -180,6 +183,8 @@ mod set_domain_router {
),
Error::<Runtime>::RouterInitFailed,
);

assert!(DomainRouters::<Runtime>::get(domain).is_none());
});
}

Expand All @@ -198,8 +203,7 @@ mod set_domain_router {
BadOrigin
);

let storage_entry = DomainRouters::<Runtime>::get(domain);
assert!(storage_entry.is_none());
assert!(DomainRouters::<Runtime>::get(domain).is_none());
});
}

Expand All @@ -218,8 +222,95 @@ mod set_domain_router {
Error::<Runtime>::DomainNotSupported
);

let storage_entry = DomainRouters::<Runtime>::get(domain);
assert!(storage_entry.is_none());
assert!(DomainRouters::<Runtime>::get(domain).is_none());
});
}
}

mod set_domain_multi_router {
use super::*;

#[test]
fn success() {
new_test_ext().execute_with(|| {
let domain = Domain::EVM(0);
let routers = get_mock_routers();

assert_ok!(LiquidityPoolsGateway::set_domain_multi_router(
RuntimeOrigin::root(),
domain.clone(),
routers.clone(),
));

assert_eq!(
DomainMultiRouters::<Runtime>::get(domain.clone()).unwrap(),
routers
);

event_exists(Event::<Runtime>::DomainMultiRouterSet { domain, routers });
});
}
#[test]
fn router_init_error() {
new_test_ext().execute_with(|| {
let domain = Domain::EVM(0);
let router = RouterMock::<Runtime>::default();
router.mock_init(move || Err(DispatchError::Other("error")));

let routers = BoundedVec::try_from(vec![router]).unwrap();

assert_noop!(
LiquidityPoolsGateway::set_domain_multi_router(
RuntimeOrigin::root(),
domain.clone(),
routers,
),
Error::<Runtime>::RouterInitFailed,
);

assert!(DomainMultiRouters::<Runtime>::get(domain).is_none());
});
}

#[test]
fn bad_origin() {
new_test_ext().execute_with(|| {
let domain = Domain::EVM(0);
let router = RouterMock::<Runtime>::default();

let routers = BoundedVec::try_from(vec![router]).unwrap();

assert_noop!(
LiquidityPoolsGateway::set_domain_multi_router(
RuntimeOrigin::signed(get_test_account_id()),
domain.clone(),
routers,
),
BadOrigin
);

assert!(DomainMultiRouters::<Runtime>::get(domain).is_none());
});
}

#[test]
fn unsupported_domain() {
new_test_ext().execute_with(|| {
let domain = Domain::Centrifuge;
let router = RouterMock::<Runtime>::default();

let routers = BoundedVec::try_from(vec![router]).unwrap();

assert_noop!(
LiquidityPoolsGateway::set_domain_multi_router(
RuntimeOrigin::root(),
domain.clone(),
routers,
),
Error::<Runtime>::DomainNotSupported
);

assert!(DomainMultiRouters::<Runtime>::get(domain).is_none());
});
}
}
Expand Down Expand Up @@ -836,7 +927,7 @@ mod process_outbound_message {
router.mock_send(mock_fn.clone());
}

assert_ok!(LiquidityPoolsGateway::set_domain_multi_routers(
assert_ok!(LiquidityPoolsGateway::set_domain_multi_router(
RuntimeOrigin::root(),
domain.clone(),
routers,
Expand Down Expand Up @@ -878,14 +969,47 @@ mod process_outbound_message {
});
}

#[test]
fn multi_router_not_found() {
new_test_ext().execute_with(|| {
let domain = Domain::EVM(0);
let sender = get_test_account_id();
let msg = MessageMock::First;

let nonce = OutboundMessageNonce::one();

OutboundMessageQueue::<Runtime>::insert(
nonce,
(domain.clone(), sender.clone(), msg.clone()),
);

assert_ok!(LiquidityPoolsGateway::process_outbound_message(
RuntimeOrigin::signed(get_test_account_id()),
nonce,
));

let (stored_domain, stored_sender, stored_message, stored_error): (
_,
AccountId32,
MessageMock,
_,
) = FailedOutboundMessages::<Runtime>::get(nonce).unwrap();

assert_eq!(stored_domain, domain);
assert_eq!(stored_sender, sender);
assert_eq!(stored_message, msg);
assert_eq!(stored_error, Error::<Runtime>::MultiRouterNotFound.into());
});
}

#[test]
fn failure() {
new_test_ext().execute_with(|| {
let domain = Domain::EVM(0);

let routers = get_mock_routers();

assert_ok!(LiquidityPoolsGateway::set_domain_multi_routers(
assert_ok!(LiquidityPoolsGateway::set_domain_multi_router(
RuntimeOrigin::root(),
domain.clone(),
routers.clone(),
Expand Down Expand Up @@ -986,7 +1110,7 @@ mod process_failed_outbound_message {
router.mock_send(mock_fn.clone());
}

assert_ok!(LiquidityPoolsGateway::set_domain_multi_routers(
assert_ok!(LiquidityPoolsGateway::set_domain_multi_router(
RuntimeOrigin::root(),
domain.clone(),
routers,
Expand All @@ -1004,7 +1128,7 @@ mod process_failed_outbound_message {
nonce
));

assert!(!FailedOutboundMessages::<Runtime>::contains_key(nonce));
assert!(FailedOutboundMessages::<Runtime>::get(nonce).is_none());

event_exists(Event::<Runtime>::OutboundMessageExecutionSuccess {
nonce,
Expand All @@ -1028,14 +1152,38 @@ mod process_failed_outbound_message {
});
}

#[test]
fn multi_router_not_found() {
new_test_ext().execute_with(|| {
let domain = Domain::EVM(0);
let sender = get_test_account_id();
let msg = MessageMock::First;
let err = DispatchError::Unavailable;

let nonce = OutboundMessageNonce::one();

FailedOutboundMessages::<Runtime>::insert(
nonce,
(domain.clone(), sender.clone(), msg.clone(), err),
);

assert_ok!(LiquidityPoolsGateway::process_failed_outbound_message(
RuntimeOrigin::signed(get_test_account_id()),
nonce,
));

assert!(FailedOutboundMessages::<Runtime>::get(nonce).is_some());
});
}

#[test]
fn failure() {
new_test_ext().execute_with(|| {
let domain = Domain::EVM(0);

let routers = get_mock_routers();

assert_ok!(LiquidityPoolsGateway::set_domain_multi_routers(
assert_ok!(LiquidityPoolsGateway::set_domain_multi_router(
RuntimeOrigin::root(),
domain.clone(),
routers.clone(),
Expand Down Expand Up @@ -1114,7 +1262,7 @@ mod outbound_queue_impl {
>::try_from(vec![router])
.expect("can create multi-router vec");

assert_ok!(LiquidityPoolsGateway::set_domain_multi_routers(
assert_ok!(LiquidityPoolsGateway::set_domain_multi_router(
RuntimeOrigin::root(),
domain.clone(),
routers,
Expand Down Expand Up @@ -1158,7 +1306,7 @@ mod outbound_queue_impl {
}

#[test]
fn router_not_found() {
fn multi_router_not_found() {
new_test_ext().execute_with(|| {
let domain = Domain::EVM(0);
let sender = get_test_account_id();
Expand Down
4 changes: 2 additions & 2 deletions pallets/liquidity-pools-gateway/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub trait WeightInfo {
fn process_msg() -> Weight;
fn process_outbound_message() -> Weight;
fn process_failed_outbound_message() -> Weight;
fn set_domain_multi_routers() -> Weight;
fn set_domain_multi_router() -> Weight;
}

// NOTE: We use temporary weights here. `execute_epoch` is by far our heaviest
Expand Down Expand Up @@ -124,7 +124,7 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().writes(1))
}

fn set_domain_multi_routers() -> Weight {
fn set_domain_multi_router() -> Weight {
// TODO: BENCHMARK CORRECTLY
//
// NOTE: Reasonable weight taken from `PoolSystem::set_max_reserve`
Expand Down
6 changes: 2 additions & 4 deletions runtime/integration-tests/src/cases/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ mod utils {
token_name: BoundedVec::<
u8,
<T as pallet_pool_system::Config>::StringLimit,
>::try_from(
"A highly advanced tranche".as_bytes().to_vec()
)
>::try_from("A highly advanced tranche".as_bytes().to_vec())
.expect("Can create BoundedVec for token name"),
token_symbol: BoundedVec::<
u8,
Expand Down Expand Up @@ -247,7 +245,7 @@ mod utils {
let domain = Domain::EVM(evm_chain_id);

assert_ok!(
pallet_liquidity_pools_gateway::Pallet::<T>::set_domain_multi_routers(
pallet_liquidity_pools_gateway::Pallet::<T>::set_domain_multi_router(
<T as frame_system::Config>::RuntimeOrigin::root(),
domain,
vec![domain_router]
Expand Down
2 changes: 1 addition & 1 deletion runtime/integration-tests/src/cases/lp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ pub fn setup<T: Runtime, F: FnOnce(&mut <RuntimeEnv<T> as EnvEvmExtension<T>>::E
);

assert_ok!(
pallet_liquidity_pools_gateway::Pallet::<T>::set_domain_multi_routers(
pallet_liquidity_pools_gateway::Pallet::<T>::set_domain_multi_router(
RawOrigin::Root.into(),
Domain::EVM(EVM_DOMAIN_CHAIN_ID),
vec![DomainRouter::<T>::AxelarEVM(axelar_evm_router)]
Expand Down
2 changes: 1 addition & 1 deletion runtime/integration-tests/src/cases/routers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn environment_for_xcm<T: Runtime + FudgeSupport>() -> FudgeEnv<T> {
fn check_submission<T: Runtime>(mut env: impl Env<T>, routers: Vec<T::Router>) {
let expected_event = env.parachain_state_mut(|| {
assert_ok!(
pallet_liquidity_pools_gateway::Pallet::<T>::set_domain_multi_routers(
pallet_liquidity_pools_gateway::Pallet::<T>::set_domain_multi_router(
RawOrigin::Root.into(),
TEST_DOMAIN,
routers
Expand Down

0 comments on commit cdc56a2

Please sign in to comment.