From 80dbb0350dd02af646b7496cb2eb232d5fc50eec Mon Sep 17 00:00:00 2001 From: Cosmin Damian <17934949+cdamian@users.noreply.github.com> Date: Fri, 9 Aug 2024 19:52:50 +0300 Subject: [PATCH] lp-gateway: Unit tests WIP --- Cargo.lock | 12 + pallets/liquidity-pools-gateway/src/lib.rs | 126 +- pallets/liquidity-pools-gateway/src/mock.rs | 1 - pallets/liquidity-pools-gateway/src/tests.rs | 1171 ++++++++++-------- 4 files changed, 720 insertions(+), 590 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b5dfd2b257..ded0246947 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5476,6 +5476,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.11" @@ -8266,6 +8275,9 @@ dependencies = [ "frame-support", "frame-system", "hex", + "itertools 0.13.0", + "lazy_static", + "mock-builder", "orml-traits", "parity-scale-codec", "scale-info", diff --git a/pallets/liquidity-pools-gateway/src/lib.rs b/pallets/liquidity-pools-gateway/src/lib.rs index 7190b36424..424ea1d580 100644 --- a/pallets/liquidity-pools-gateway/src/lib.rs +++ b/pallets/liquidity-pools-gateway/src/lib.rs @@ -31,7 +31,7 @@ use core::fmt::Debug; use cfg_primitives::LP_DEFENSIVE_WEIGHT; use cfg_traits::liquidity_pools::{ InboundMessageHandler, LPEncoding, MessageProcessor, MessageQueue, OutboundMessageHandler, - Router as DomainRouter, + Proof, Router as DomainRouter, }; use cfg_types::domain_address::{Domain, DomainAddress}; use frame_support::{dispatch::DispatchResult, pallet_prelude::*}; @@ -40,7 +40,7 @@ use message::GatewayMessage; use orml_traits::GetByKey; pub use pallet::*; use parity_scale_codec::{EncodeLike, FullCodec}; -use sp_arithmetic::traits::BaseArithmetic; +use sp_arithmetic::traits::{BaseArithmetic, EnsureSub, One}; use sp_runtime::traits::EnsureAddAssign; use sp_std::{cmp::Ordering, convert::TryInto, vec::Vec}; @@ -63,14 +63,14 @@ mod tests; #[derive(Debug, Encode, Decode, Clone, Eq, MaxEncodedLen, PartialEq, TypeInfo)] #[scale_info(skip_type_params(T))] pub enum InboundEntry { - Message { - domain_address: DomainAddress, - message: T::Message, - expected_proof_count: u32, - }, - Proof { - current_count: u32, - }, + Message { + domain_address: DomainAddress, + message: T::Message, + expected_proof_count: u32, + }, + Proof { + current_count: u32, + }, } #[derive(Clone)] @@ -306,6 +306,27 @@ pub mod pallet { /// Invalid multi router. InvalidMultiRouter, + /// Inbound domain session not found. + InboundDomainSessionNotFound, + + /// The router that sent the inbound message is unknown. + UnknownInboundMessageRouter, + + /// The router that sent the message is not the first one. + MessageExpectedFromFirstRouter, + + /// The router that sent the proof should not be the first one. + ProofNotExpectedFromFirstRouter, + + /// A message was expected instead of a proof. + ExpectedMessageType, + + /// A message proof was expected instead of a message. + ExpectedMessageProofType, + + /// Pending inbound entry not found. + PendingInboundEntryNotFound, + /// Multi-router not found. MultiRouterNotFound, @@ -475,10 +496,6 @@ pub mod pallet { T::AdminOrigin::ensure_origin(origin)?; ensure!(domain != Domain::Centrifuge, Error::::DomainNotSupported); - ensure!( - routers.len() == T::MaxRouterCount::get() as usize, - Error::::InvalidMultiRouter - ); let mut router_hashes = Vec::new(); @@ -512,11 +529,6 @@ pub mod pallet { ) -> DispatchResult { T::AdminOrigin::ensure_origin(origin)?; - ensure!( - router_hashes.len() == T::MaxRouterCount::get() as usize, - Error::::InvalidMultiRouter - ); - let session_id = SessionIdStore::::try_mutate(|n| { n.ensure_add_assign(One::one())?; Ok::(*n) @@ -550,7 +562,6 @@ pub mod pallet { } impl Pallet { - //TODO(cdamian): Use safe math fn get_expected_proof_count(domain: &Domain) -> Result { let routers = InboundRouters::::get(domain).ok_or(Error::::MultiRouterNotFound)?; @@ -592,26 +603,23 @@ pub mod pallet { /// - messages are only sent by the first inbound router. /// - proofs are not sent by the first inbound router. fn validate_inbound_entry( - domain: Domain, + inbound_processing_info: &InboundProcessingInfo, router_hash: T::Hash, inbound_entry: &InboundEntry, ) -> DispatchResult { - let inbound_routers = - //TODO(cdamian): Add new error - InboundRouters::::get(domain).ok_or(Error::::InvalidMultiRouter)?; + let inbound_routers = inbound_processing_info.inbound_routers.clone(); ensure!( inbound_routers.iter().any(|x| x == &router_hash), - //TODO(cdamian): Add error - Error::::InvalidMultiRouter + Error::::UnknownInboundMessageRouter ); + //TODO(cdamian): Test with 2 routers match inbound_entry { InboundEntry::Message { .. } => { ensure!( inbound_routers.get(0) == Some(&router_hash), - //TODO(cdamian): Add error - Error::::InvalidMultiRouter + Error::::MessageExpectedFromFirstRouter ); Ok(()) @@ -619,8 +627,7 @@ pub mod pallet { InboundEntry::Proof { .. } => { ensure!( inbound_routers.get(0) != Some(&router_hash), - //TODO(cdamian): Add error - Error::::InvalidMultiRouter + Error::::ProofNotExpectedFromFirstRouter ); Ok(()) @@ -628,39 +635,6 @@ pub mod pallet { } } - fn update_storage_entry( - domain: Domain, - old: &mut InboundEntry, - new: InboundEntry, - ) -> DispatchResult { - match old { - InboundEntry::Message { - expected_proof_count: stored_expected_proof_count, - .. - } => match new { - InboundEntry::Message { .. } => { - let expected_message_proof_count = Self::get_expected_proof_count(&domain)?; - - stored_expected_proof_count - .ensure_add_assign(expected_message_proof_count)?; - - Ok(()) - } - //TODO(cdamian): Update error - InboundEntry::Proof { .. } => Err(Error::::InvalidMultiRouter.into()), - }, - InboundEntry::Proof { current_count } => match new { - InboundEntry::Proof { .. } => { - current_count.ensure_add_assign(1)?; - - Ok(()) - } - //TODO(cdamian): Update error - InboundEntry::Message { .. } => Err(Error::::InvalidMultiRouter.into()), - }, - } - } - fn update_pending_entry( session_id: T::SessionId, message_proof: Proof, @@ -689,8 +663,8 @@ pub mod pallet { .. } => old.ensure_add_assign(new).map_err(|e| e.into()), InboundEntry::Proof { .. } => { - // TODO(cdamian): Add new error. - Err(Error::::InvalidMultiRouter.into()) + //TODO(cdamian): Test with 2 routers + Err(Error::::ExpectedMessageType.into()) } }, InboundEntry::Proof { current_count: old } => match inbound_entry { @@ -698,8 +672,7 @@ pub mod pallet { old.ensure_add_assign(new).map_err(|e| e.into()) } InboundEntry::Message { .. } => { - // TODO(cdamian): Add new error. - Err(Error::::InvalidMultiRouter.into()) + Err(Error::::ExpectedMessageProofType.into()) } }, }, @@ -720,11 +693,7 @@ pub mod pallet { inbound_processing_info.expected_proof_count_per_message, ); - Self::validate_inbound_entry( - inbound_processing_info.domain_address.domain(), - router_hash, - &inbound_entry, - )?; + Self::validate_inbound_entry(&inbound_processing_info, router_hash, &inbound_entry)?; Self::update_pending_entry( inbound_processing_info.current_session_id, @@ -782,8 +751,7 @@ pub mod pallet { inbound_processing_info.current_session_id, (message_proof, inbound_router), |storage_entry| match storage_entry { - // TODO(cdamian): Add new error - None => Err(Error::::InvalidMultiRouter.into()), + None => Err(Error::::PendingInboundEntryNotFound.into()), Some(stored_inbound_entry) => match stored_inbound_entry { InboundEntry::Message { expected_proof_count, @@ -827,15 +795,13 @@ pub mod pallet { domain_address: DomainAddress, weight: &mut Weight, ) -> Result, DispatchError> { - let inbound_routers = - //TODO(cdamian): Add new error - InboundRouters::::get(domain_address.domain()).ok_or(Error::::InvalidMultiRouter)?; + let inbound_routers = InboundRouters::::get(domain_address.domain()) + .ok_or(Error::::MultiRouterNotFound)?; weight.saturating_accrue(T::DbWeight::get().reads(1)); - let current_session_id = - //TODO(cdamian): Add new error - InboundDomainSessions::::get(domain_address.domain()).ok_or(Error::::InvalidMultiRouter)?; + let current_session_id = InboundDomainSessions::::get(domain_address.domain()) + .ok_or(Error::::InboundDomainSessionNotFound)?; weight.saturating_accrue(T::DbWeight::get().reads(1)); diff --git a/pallets/liquidity-pools-gateway/src/mock.rs b/pallets/liquidity-pools-gateway/src/mock.rs index 428d21427d..332b56df33 100644 --- a/pallets/liquidity-pools-gateway/src/mock.rs +++ b/pallets/liquidity-pools-gateway/src/mock.rs @@ -149,7 +149,6 @@ impl pallet_liquidity_pools_gateway::Config for Runtime { type MaxRouterCount = MaxRouterCount; type Message = Message; type MessageQueue = MockLiquidityPoolsGatewayQueue; - type MultiRouterCount = MultiRouterCount; type Router = RouterMock; type RuntimeEvent = RuntimeEvent; type RuntimeOrigin = RuntimeOrigin; diff --git a/pallets/liquidity-pools-gateway/src/tests.rs b/pallets/liquidity-pools-gateway/src/tests.rs index a66cc89115..d38d5a532f 100644 --- a/pallets/liquidity-pools-gateway/src/tests.rs +++ b/pallets/liquidity-pools-gateway/src/tests.rs @@ -23,7 +23,13 @@ use super::{ origin::*, pallet::*, }; -use crate::GatewayMessage; +use crate::{GatewayMessage, InboundEntry}; + +lazy_static! { + static ref ROUTER_HASH_1: H256 = H256::from_low_u64_be(1); + static ref ROUTER_HASH_2: H256 = H256::from_low_u64_be(2); + static ref ROUTER_HASH_3: H256 = H256::from_low_u64_be(3); +} mod utils { use super::*; @@ -652,530 +658,675 @@ mod message_processor_impl { pub proof_count: u32, pub mock_called_times: u32, } + + pub fn gen_new(t: T, count: usize) -> Vec::Item>> + where + T: IntoIterator + Clone, + T::IntoIter: Clone, + T::Item: Clone, + { + std::iter::repeat(t.clone().into_iter()) + .take(count) + .multi_cartesian_product() + .collect::>() + } } use util::*; - mod combined_messages { + mod one_router { use super::*; - mod two_messages { - use super::*; - - lazy_static! { - static ref TEST_MAP: HashMap, ExpectedTestResult> = - HashMap::from([ - ( - vec![Message::Simple, Message::Simple], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 4 - )), - proof_count: 0, - mock_called_times: 0, - } - ), - ( - vec![Message::Proof(MESSAGE_PROOF), Message::Proof(MESSAGE_PROOF)], - ExpectedTestResult { - inbound_message: None, - proof_count: 2, - mock_called_times: 0, - } - ), - ( - vec![Message::Simple, Message::Proof(MESSAGE_PROOF)], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 2 - )), - proof_count: 1, - mock_called_times: 0, - } - ), - ( - vec![Message::Proof(MESSAGE_PROOF), Message::Simple], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 2 - )), - proof_count: 1, - mock_called_times: 0, - } - ), - ]); - } + #[test] + fn success() { + new_test_ext().execute_with(|| { + let message = Message::Simple; + let session_id = 1; + let domain_address = DomainAddress::EVM(1, [1; 20]); + let router_hash = *ROUTER_HASH_1; + let gateway_message = GatewayMessage::Inbound { + domain_address: domain_address.clone(), + message: message.clone(), + router_hash, + }; + + InboundRouters::::insert( + domain_address.domain(), + BoundedVec::<_, _>::try_from(vec![router_hash]).unwrap(), + ); + InboundDomainSessions::::insert(domain_address.domain(), session_id); + + let handler = MockLiquidityPools::mock_handle( + move |mock_domain_address, mock_message| { + assert_eq!(mock_domain_address, domain_address); + assert_eq!(mock_message, message); + + Ok(()) + }, + ); - #[test] - fn two_messages() { - let tests = generate_test_combinations(2) - .iter() - .map(|x| { - ( - x.clone(), - TEST_MAP - .get(x) - .expect(format!("test for {x:?} should be covered").as_str()), - ) - }) - .collect::>(); - - run_tests!(tests); - } + let (res, _) = LiquidityPoolsGateway::process(gateway_message); + assert_ok!(res); + assert_eq!(handler.times(), 1); + }); } - mod three_messages { - use super::*; - - lazy_static! { - static ref TEST_MAP: HashMap, ExpectedTestResult> = - HashMap::from([ - ( - vec![Message::Simple, Message::Simple, Message::Simple,], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 6 - )), - proof_count: 0, - mock_called_times: 0, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: None, - proof_count: 3, - mock_called_times: 0, - } - ), - ( - vec![ - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: None, - proof_count: 0, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: None, - proof_count: 0, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Simple, - ], - ExpectedTestResult { - inbound_message: None, - proof_count: 0, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Simple, - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 4 - )), - proof_count: 1, - mock_called_times: 0, - } - ), - ( - vec![ - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Simple, - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 4 - )), - proof_count: 1, - mock_called_times: 0, - } - ), - ( - vec![ - Message::Simple, - Message::Simple, - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 4 - )), - proof_count: 1, - mock_called_times: 0, - } - ) - ]); - } + #[test] + fn multi_router_not_found() { + new_test_ext().execute_with(|| { + let message = Message::Simple; + let domain_address = DomainAddress::EVM(1, [1; 20]); + let router_hash = *ROUTER_HASH_1; + let gateway_message = GatewayMessage::Inbound { + domain_address: domain_address.clone(), + message: message.clone(), + router_hash, + }; + + let (res, _) = LiquidityPoolsGateway::process(gateway_message); + assert_noop!(res, Error::::MultiRouterNotFound); + }); + } - #[test] - fn three_messages() { - let tests = generate_test_combinations(3) - .iter() - .map(|x| { - ( - x.clone(), - TEST_MAP - .get(x) - .expect(format!("test for {x:?} should be covered").as_str()), - ) - }) - .collect::>(); - - run_tests!(tests); - } + #[test] + fn inbound_domain_session_not_found() { + new_test_ext().execute_with(|| { + let message = Message::Simple; + let domain_address = DomainAddress::EVM(1, [1; 20]); + let router_hash = *ROUTER_HASH_1; + let gateway_message = GatewayMessage::Inbound { + domain_address: domain_address.clone(), + message: message.clone(), + router_hash, + }; + + InboundRouters::::insert( + domain_address.domain(), + BoundedVec::<_, _>::try_from(vec![router_hash]).unwrap(), + ); + + let (res, _) = LiquidityPoolsGateway::process(gateway_message); + assert_noop!(res, Error::::InboundDomainSessionNotFound); + }); } - mod four_messages { - use super::*; - - lazy_static! { - static ref TEST_MAP: HashMap, ExpectedTestResult> = - HashMap::from([ - ( - vec![ - Message::Simple, - Message::Simple, - Message::Simple, - Message::Simple, - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 8 - )), - proof_count: 0, - mock_called_times: 0, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: None, - proof_count: 4, - mock_called_times: 0, - } - ), - ( - vec![ - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: None, - proof_count: 1, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: None, - proof_count: 1, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: None, - proof_count: 1, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Simple, - ], - ExpectedTestResult { - inbound_message: None, - proof_count: 1, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Simple, - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 2 - )), - proof_count: 0, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 2 - )), - proof_count: 0, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Simple, - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 2 - )), - proof_count: 0, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Simple, - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 2 - )), - proof_count: 0, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Simple, - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 2 - )), - proof_count: 0, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Simple, - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 2 - )), - proof_count: 0, - mock_called_times: 1, - } - ), - ( - vec![ - Message::Simple, - Message::Simple, - Message::Simple, - Message::Proof(MESSAGE_PROOF), - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 6 - )), - proof_count: 1, - mock_called_times: 0, - } - ), - ( - vec![ - Message::Simple, - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Simple, - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 6 - )), - proof_count: 1, - mock_called_times: 0, - } - ), - ( - vec![ - Message::Simple, - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Simple, - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 6 - )), - proof_count: 1, - mock_called_times: 0, - } - ), - ( - vec![ - Message::Proof(MESSAGE_PROOF), - Message::Simple, - Message::Simple, - Message::Simple, - ], - ExpectedTestResult { - inbound_message: Some(( - DomainAddress::EVM(1, [1; 20]), - Message::Simple, - 6 - )), - proof_count: 1, - mock_called_times: 0, - } - ), - ]); - } + #[test] + fn unknown_inbound_message_router() { + new_test_ext().execute_with(|| { + let message = Message::Simple; + let session_id = 1; + let domain_address = DomainAddress::EVM(1, [1; 20]); + let router_hash = *ROUTER_HASH_1; + let gateway_message = GatewayMessage::Inbound { + domain_address: domain_address.clone(), + message: message.clone(), + // The router stored has a different hash, this should trigger the expected + // error. + router_hash: *ROUTER_HASH_2, + }; + + InboundRouters::::insert( + domain_address.domain(), + BoundedVec::<_, _>::try_from(vec![router_hash]).unwrap(), + ); + InboundDomainSessions::::insert(domain_address.domain(), session_id); + + let (res, _) = LiquidityPoolsGateway::process(gateway_message); + assert_noop!(res, Error::::UnknownInboundMessageRouter); + }); + } - #[test] - fn four_messages() { - let tests = generate_test_combinations(4) - .iter() - .filter(|x| TEST_MAP.get(x.clone()).is_some()) - .map(|x| { - ( - x.clone(), - TEST_MAP - .get(x) - .expect(format!("test for {x:?} should be covered").as_str()), - ) - }) - .collect::>(); - - run_tests!(tests); - } + #[test] + fn expected_message_proof_type() { + new_test_ext().execute_with(|| { + let message = Message::Simple; + let message_proof = message.to_message_proof().get_message_proof().unwrap(); + let session_id = 1; + let domain_address = DomainAddress::EVM(1, [1; 20]); + let router_hash = *ROUTER_HASH_1; + let gateway_message = GatewayMessage::Inbound { + domain_address: domain_address.clone(), + message: message.clone(), + router_hash, + }; + + InboundRouters::::insert( + domain_address.domain(), + BoundedVec::<_, _>::try_from(vec![router_hash]).unwrap(), + ); + InboundDomainSessions::::insert(domain_address.domain(), session_id); + PendingInboundEntries::::insert( + session_id, + (message_proof, router_hash), + InboundEntry::::Proof { current_count: 0 }, + ); + + let (res, _) = LiquidityPoolsGateway::process(gateway_message); + assert_noop!(res, Error::::ExpectedMessageProofType); + }); } } - #[test] - fn two_non_proof_and_four_proofs() { - let tests = generate_test_combinations(6) - .into_iter() - .filter(|x| { - let r = x.iter().counts_by(|c| c.clone()); - let non_proof_count = r.get(&Message::Simple); - let proof_count = r.get(&Message::Proof(MESSAGE_PROOF)); - - match (non_proof_count, proof_count) { - (Some(non_proof_count), Some(proof_count)) => { - *non_proof_count == 2 && *proof_count == 4 - } - _ => false, - } - }) - .map(|x| { - ( - x, - ExpectedTestResult { - inbound_message: None, - proof_count: 0, - mock_called_times: 2, - }, - ) - }) - .collect::>(); - - run_tests!(tests); - } + // mod combined_messages { + // use super::*; + // + // mod two_messages { + // use super::*; + // + // lazy_static! { + // static ref TEST_MAP: HashMap, ExpectedTestResult> = + // HashMap::from([ + // ( + // vec![Message::Simple, Message::Simple], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 4 + // )), + // proof_count: 0, + // mock_called_times: 0, + // } + // ), + // ( + // vec![Message::Proof(MESSAGE_PROOF), Message::Proof(MESSAGE_PROOF)], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 2, + // mock_called_times: 0, + // } + // ), + // ( + // vec![Message::Simple, Message::Proof(MESSAGE_PROOF)], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 2 + // )), + // proof_count: 1, + // mock_called_times: 0, + // } + // ), + // ( + // vec![Message::Proof(MESSAGE_PROOF), Message::Simple], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 2 + // )), + // proof_count: 1, + // mock_called_times: 0, + // } + // ), + // ]); + // } + // + // #[test] + // fn two_messages() { + // let tests = generate_test_combinations(2) + // .iter() + // .map(|x| { + // ( + // x.clone(), + // TEST_MAP + // .get(x) + // .expect(format!("test for {x:?} should be covered").as_str()), + // ) + // }) + // .collect::>(); + // + // run_tests!(tests); + // } + // } + // + // mod three_messages { + // use super::*; + // + // lazy_static! { + // static ref TEST_MAP: HashMap, ExpectedTestResult> = + // HashMap::from([ + // ( + // vec![Message::Simple, Message::Simple, Message::Simple,], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 6 + // )), + // proof_count: 0, + // mock_called_times: 0, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 3, + // mock_called_times: 0, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 0, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 0, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 0, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 4 + // )), + // proof_count: 1, + // mock_called_times: 0, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 4 + // )), + // proof_count: 1, + // mock_called_times: 0, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 4 + // )), + // proof_count: 1, + // mock_called_times: 0, + // } + // ) + // ]); + // } + // + // #[test] + // fn three_messages() { + // let tests = generate_test_combinations(3) + // .iter() + // .map(|x| { + // ( + // x.clone(), + // TEST_MAP + // .get(x) + // .expect(format!("test for {x:?} should be covered").as_str()), + // ) + // }) + // .collect::>(); + // + // run_tests!(tests); + // } + // } + // + // mod four_messages { + // use super::*; + // + // lazy_static! { + // static ref TEST_MAP: HashMap, ExpectedTestResult> = + // HashMap::from([ + // ( + // vec![ + // Message::Simple, + // Message::Simple, + // Message::Simple, + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 8 + // )), + // proof_count: 0, + // mock_called_times: 0, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 4, + // mock_called_times: 0, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 1, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 1, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 1, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 1, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 2 + // )), + // proof_count: 0, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 2 + // )), + // proof_count: 0, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 2 + // )), + // proof_count: 0, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 2 + // )), + // proof_count: 0, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 2 + // )), + // proof_count: 0, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 2 + // )), + // proof_count: 0, + // mock_called_times: 1, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Simple, + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 6 + // )), + // proof_count: 1, + // mock_called_times: 0, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 6 + // )), + // proof_count: 1, + // mock_called_times: 0, + // } + // ), + // ( + // vec![ + // Message::Simple, + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 6 + // )), + // proof_count: 1, + // mock_called_times: 0, + // } + // ), + // ( + // vec![ + // Message::Proof(MESSAGE_PROOF), + // Message::Simple, + // Message::Simple, + // Message::Simple, + // ], + // ExpectedTestResult { + // inbound_message: Some(( + // DomainAddress::EVM(1, [1; 20]), + // Message::Simple, + // 6 + // )), + // proof_count: 1, + // mock_called_times: 0, + // } + // ), + // ]); + // } + // + // #[test] + // fn four_messages() { + // let tests = generate_test_combinations(4) + // .iter() + // .filter(|x| TEST_MAP.get(x.clone()).is_some()) + // .map(|x| { + // ( + // x.clone(), + // TEST_MAP + // .get(x) + // .expect(format!("test for {x:?} should be covered").as_str()), + // ) + // }) + // .collect::>(); + // + // run_tests!(tests); + // } + // } + // } + // + // #[test] + // fn two_non_proof_and_four_proofs() { + // let tests = generate_test_combinations(6) + // .into_iter() + // .filter(|x| { + // let r = x.iter().counts_by(|c| c.clone()); + // let non_proof_count = r.get(&Message::Simple); + // let proof_count = r.get(&Message::Proof(MESSAGE_PROOF)); + // + // match (non_proof_count, proof_count) { + // (Some(non_proof_count), Some(proof_count)) => { + // *non_proof_count == 2 && *proof_count == 4 + // } + // _ => false, + // } + // }) + // .map(|x| { + // ( + // x, + // ExpectedTestResult { + // inbound_message: None, + // proof_count: 0, + // mock_called_times: 2, + // }, + // ) + // }) + // .collect::>(); + // + // run_tests!(tests); + // } #[test] fn inbound_message_handler_error() { @@ -1466,6 +1617,7 @@ mod batches { let (result, weight) = LiquidityPoolsGateway::process(GatewayMessage::Inbound { domain_address, message: Message::deserialize(&(1..=5).collect::>()).unwrap(), + router_hash: *ROUTER_HASH_1, }); assert_eq!(weight, LP_DEFENSIVE_WEIGHT * 5); @@ -1490,6 +1642,7 @@ mod batches { let (result, weight) = LiquidityPoolsGateway::process(GatewayMessage::Inbound { domain_address, message: Message::deserialize(&(1..=5).collect::>()).unwrap(), + router_hash: *ROUTER_HASH_1, }); // 2 correct messages and 1 failed message processed.