Skip to content

Commit

Permalink
LPv2: Batch message logic (r2) (#1949)
Browse files Browse the repository at this point in the history
* add batch logic

* added infrastructure to tests batches

* fix common tests

* fix tests and some renamings
  • Loading branch information
lemunozm authored Aug 8, 2024
1 parent 8245ce2 commit b5178c2
Show file tree
Hide file tree
Showing 12 changed files with 341 additions and 446 deletions.
33 changes: 10 additions & 23 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,17 @@ use sp_std::vec::Vec;
pub trait LPEncoding: Sized {
fn serialize(&self) -> Vec<u8>;
fn deserialize(input: &[u8]) -> Result<Self, DispatchError>;
}

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

use super::*;

#[derive(Default, Debug, Eq, PartialEq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct Message;
impl LPEncoding for Message {
fn serialize(&self) -> Vec<u8> {
vec![0x42]
}

fn deserialize(input: &[u8]) -> Result<Self, DispatchError> {
match input.first() {
Some(0x42) => Ok(Self),
Some(_) => Err("unsupported message".into()),
None => Err("empty message".into()),
}
}
}
/// Extend this message with a new one
fn pack_with(&mut self, other: Self) -> DispatchResult;

/// Decompose the message into a list of messages
/// If the message is not decomposable, it returns the own message.
fn submessages(&self) -> Vec<Self>;

/// Creates an empty message.
/// It's the identity message for composing messages with pack_with
fn empty() -> Self;
}

/// The trait required for sending outbound messages.
Expand Down
20 changes: 20 additions & 0 deletions libs/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@
use parity_scale_codec::Encode;
use sp_std::cmp::min;

pub struct BufferReader<'a>(pub &'a [u8]);

impl<'a> BufferReader<'a> {
pub fn read_bytes(&mut self, bytes: usize) -> Option<&[u8]> {
if self.0.len() < bytes {
return None;
}

let (consumed, remaining) = self.0.split_at(bytes);
self.0 = remaining;
Some(consumed)
}

pub fn read_array<const N: usize>(&mut self) -> Option<&[u8; N]> {
let (consumed, remaining) = self.0.split_first_chunk::<N>()?;
self.0 = remaining;
Some(consumed)
}
}

/// Build a fixed-size array using as many elements from `src` as possible
/// without overflowing and ensuring that the array is 0 padded in the case
/// where `src.len()` is smaller than S.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ where
exit_status: ExitError::Other("account bytes mismatch for domain".into()),
})?;

match pallet_liquidity_pools_gateway::Pallet::<T>::process_msg(
match pallet_liquidity_pools_gateway::Pallet::<T>::receive_message(
pallet_liquidity_pools_gateway::GatewayOrigin::Domain(domain_address).into(),
msg,
)
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
Loading

0 comments on commit b5178c2

Please sign in to comment.