Skip to content

Commit

Permalink
added infrastructure to tests batches
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 7, 2024
1 parent 2414a5a commit 9c6e607
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 147 deletions.
43 changes: 0 additions & 43 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ use sp_std::vec::Vec;
/// An encoding & decoding trait for the purpose of meeting the
/// LiquidityPools General Message Passing Format
pub trait LPEncoding: Sized {
const MAX_PACKED_MESSAGES: u32;

fn serialize(&self) -> Vec<u8>;
fn deserialize(input: &[u8]) -> Result<Self, DispatchError>;

Expand All @@ -38,47 +36,6 @@ pub trait LPEncoding: Sized {
fn empty() -> Self;
}

#[cfg(any(test, feature = "std"))]
pub mod test_util {
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

use super::*;

pub const DECODING_ERR_MSG: &str = "decoding message error";
pub const EMPTY_ERR_MSG: &str = "empty message error error";

#[derive(Default, Debug, Eq, PartialEq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct Message;
impl LPEncoding for Message {
const MAX_PACKED_MESSAGES: u32 = 1;

fn serialize(&self) -> Vec<u8> {
vec![0x42]
}

fn deserialize(input: &[u8]) -> Result<Self, DispatchError> {
match input.first() {
Some(0x42) => Ok(Self),
Some(_) => Err(DECODING_ERR_MSG.into()),
None => Err(EMPTY_ERR_MSG.into()),
}
}

fn pack(&self, _: Self) -> Result<Self, DispatchError> {
unimplemented!()
}

fn unpack(&self) -> Vec<Self> {
vec![Self]
}

fn empty() -> Self {
unimplemented!()
}
}
}

/// The trait required for sending outbound messages.
pub trait Router {
/// The sender type of the outbound message.
Expand Down
5 changes: 2 additions & 3 deletions pallets/liquidity-pools-gateway/queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use cfg_mocks::pallet_mock_liquidity_pools_gateway;
use cfg_primitives::LPGatewayQueueMessageNonce;
use cfg_traits::liquidity_pools::test_util::Message as LPTestMessage;
use cfg_types::domain_address::Domain;
use frame_support::derive_impl;
use sp_runtime::traits::ConstU128;
Expand Down Expand Up @@ -46,11 +45,11 @@ impl pallet_balances::Config for Runtime {

impl pallet_mock_liquidity_pools_gateway::Config for Runtime {
type Destination = Domain;
type Message = LPTestMessage;
type Message = ();
}

impl Config for Runtime {
type Message = LPTestMessage;
type Message = ();
type MessageNonce = LPGatewayQueueMessageNonce;
type MessageProcessor = LPGatewayMock;
type RuntimeEvent = RuntimeEvent;
Expand Down
14 changes: 6 additions & 8 deletions pallets/liquidity-pools-gateway/queue/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use cfg_primitives::LPGatewayQueueMessageNonce;
use cfg_traits::liquidity_pools::{
test_util::Message as LPTestMessage, MessageQueue as MessageQueueT,
};
use cfg_traits::liquidity_pools::MessageQueue as MessageQueueT;
use frame_support::{assert_noop, assert_ok, dispatch::RawOrigin};
use sp_runtime::{
traits::{BadOrigin, One, Zero},
Expand Down Expand Up @@ -35,7 +33,7 @@ mod process_message {
#[test]
fn success() {
new_test_ext().execute_with(|| {
let message = LPTestMessage {};
let message = ();
let nonce = LPGatewayQueueMessageNonce::one();

MessageQueue::<Runtime>::insert(nonce, message.clone());
Expand Down Expand Up @@ -87,7 +85,7 @@ mod process_message {
#[test]
fn failure_message_processor() {
new_test_ext().execute_with(|| {
let message = LPTestMessage {};
let message = ();
let nonce = LPGatewayQueueMessageNonce::one();

MessageQueue::<Runtime>::insert(nonce, message.clone());
Expand Down Expand Up @@ -126,7 +124,7 @@ mod process_failed_message {
#[test]
fn success() {
new_test_ext().execute_with(|| {
let message = LPTestMessage {};
let message = ();
let nonce = LPGatewayQueueMessageNonce::one();
let error = DispatchError::Unavailable;

Expand Down Expand Up @@ -179,7 +177,7 @@ mod process_failed_message {
#[test]
fn failure_message_processor() {
new_test_ext().execute_with(|| {
let message = LPTestMessage {};
let message = ();
let nonce = LPGatewayQueueMessageNonce::one();
let error = DispatchError::Unavailable;

Expand Down Expand Up @@ -218,7 +216,7 @@ mod message_queue_impl {
#[test]
fn success() {
new_test_ext().execute_with(|| {
let message = LPTestMessage {};
let message = ();

assert_ok!(LPGatewayQueue::submit(message.clone()));

Expand Down
64 changes: 62 additions & 2 deletions pallets/liquidity-pools-gateway/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use cfg_mocks::{
pallet_mock_liquidity_pools, pallet_mock_liquidity_pools_gateway_queue, pallet_mock_routers,
RouterMock,
};
use cfg_traits::liquidity_pools::test_util::Message;
use cfg_traits::liquidity_pools::LPEncoding;
use cfg_types::domain_address::DomainAddress;
use frame_support::{derive_impl, weights::constants::RocksDbWeight};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use runtime_common::origin::EnsureAccountOrRoot;
use scale_info::TypeInfo;
use sp_core::{crypto::AccountId32, H256};
use sp_runtime::traits::IdentityLookup;
use sp_runtime::{traits::IdentityLookup, DispatchError};

use crate::{pallet as pallet_liquidity_pools_gateway, EnsureLocal, GatewayMessage};

Expand All @@ -20,6 +22,64 @@ pub const SOURCE_ADDRESS: [u8; LENGTH_SOURCE_ADDRESS] = [0u8; LENGTH_SOURCE_ADDR

pub const LP_ADMIN_ACCOUNT: AccountId32 = AccountId32::new([u8::MAX; 32]);

pub const MAX_PACKED_MESSAGES_ERR: &str = "packed limit error";
pub const MAX_PACKED_MESSAGES: usize = 10;

#[derive(Default, Debug, Eq, PartialEq, Clone, Encode, Decode, TypeInfo)]
pub enum Message {
#[default]
Simple,
Pack(Vec<Message>),
}

impl MaxEncodedLen for Message {
fn max_encoded_len() -> usize {
4 + MAX_PACKED_MESSAGES
}
}

impl LPEncoding for Message {
fn serialize(&self) -> Vec<u8> {
match self {
Self::Simple => vec![0x42],
Self::Pack(list) => list.iter().map(|_| 0x42).collect(),
}
}

fn deserialize(input: &[u8]) -> Result<Self, DispatchError> {
Ok(match input.len() {
0 => unimplemented!(),
1 => Self::Simple,
n => Self::Pack(sp_std::iter::repeat(Self::Simple).take(n).collect()),
})
}

fn pack(&self, other: Self) -> Result<Self, DispatchError> {
match self {
Self::Simple => Ok(Self::Pack(vec![Self::Simple, other])),
Self::Pack(list) if list.len() == MAX_PACKED_MESSAGES => {
Err(MAX_PACKED_MESSAGES_ERR.into())
}
Self::Pack(list) => {
let mut new_list = list.clone();
new_list.push(other);
Ok(Self::Pack(new_list))
}
}
}

fn unpack(&self) -> Vec<Self> {
match self {
Self::Simple => vec![Self::Simple],
Self::Pack(list) => list.clone(),
}
}

fn empty() -> Self {
Self::Pack(vec![])
}
}

frame_support::construct_runtime!(
pub enum Runtime {
System: frame_system,
Expand Down
Loading

0 comments on commit 9c6e607

Please sign in to comment.