Skip to content

Commit

Permalink
add batch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 7, 2024
1 parent 8245ce2 commit 2414a5a
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 318 deletions.
34 changes: 32 additions & 2 deletions libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ 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>;

/// Compose this message with a new one
fn pack(&self, other: Self) -> Result<Self, DispatchError>;

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

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

#[cfg(any(test, feature = "std"))]
Expand All @@ -32,20 +45,37 @@ pub mod test_util {

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("unsupported message".into()),
None => Err("empty message".into()),
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!()
}
}
}

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
Loading

0 comments on commit 2414a5a

Please sign in to comment.