Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPv2: Batch message logic #1923

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libs/mocks/src/outbound_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ pub mod pallet {
fn submit(a: Self::Sender, b: Self::Destination, c: Self::Message) -> DispatchResult {
execute_call!((a, b, c))
}

fn weight() -> Weight {
Weight::zero()
}
}
}
35 changes: 34 additions & 1 deletion libs/traits/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,31 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use frame_support::dispatch::{DispatchResult, DispatchResultWithPostInfo};
use frame_support::{
dispatch::{DispatchResult, DispatchResultWithPostInfo},
pallet_prelude::Weight,
};
use sp_runtime::DispatchError;
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,6 +48,8 @@ pub mod test_util {
#[derive(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]
}
Expand All @@ -43,6 +61,18 @@ pub mod test_util {
None => Err("empty message".into()),
}
}

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

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

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

Expand Down Expand Up @@ -75,6 +105,9 @@ pub trait OutboundQueue {
destination: Self::Destination,
msg: Self::Message,
) -> DispatchResult;

/// Returns the required defensive weight to submit any message
fn weight() -> Weight;
}

/// The trait required for processing incoming 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,8 +287,9 @@ where
exit_status: ExitError::Other("account bytes mismatch for domain".into()),
})?;

match pallet_liquidity_pools_gateway::Pallet::<T>::process_msg(
pallet_liquidity_pools_gateway::GatewayOrigin::Domain(domain_address).into(),
match pallet_liquidity_pools_gateway::Pallet::<T>::receive_message(
pallet_liquidity_pools_gateway::GatewayOrigin::Domain(domain_address.clone()).into(),
pallet_liquidity_pools_gateway::GatewayOrigin::Domain(domain_address),
msg,
)
.map(|_| ())
Expand Down
Loading