Skip to content

Commit

Permalink
LPv2: Batch Message serialization (#1920)
Browse files Browse the repository at this point in the history
* custo serialize/deserialize for batch

* compiling solution

* serialization/deserialization working for batch message

* remove gmpf changes

* add batch nested protection

* faster serialization for submessages

* correct termination

* add tests for deserialize batch of batch

* add into_iter

* remove unused Box and add pack methods

* fix non_std compilation

* non_std

* fix max_encoded_len recursion issue

* fix submodules

* reduce batch limit
  • Loading branch information
lemunozm authored Jul 22, 2024
1 parent 976eebe commit 69af655
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 55 deletions.
17 changes: 7 additions & 10 deletions pallets/liquidity-pools/src/gmpf/error.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use scale_info::prelude::string::ToString;
use scale_info::prelude::string::{String, ToString};
use serde::{de, ser};
use sp_std::{
fmt::{self, Display},
vec::Vec,
};
use sp_std::fmt::{self, Display};

pub type Result<T> = sp_std::result::Result<T, Error>;

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
Message(Vec<u8>),
Message(String),
EnumSize,
Unimplemented,
UnknownSize,
Expand All @@ -18,20 +15,20 @@ pub enum Error {

impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string().into_bytes())
Error::Message(msg.to_string())
}
}

impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string().into_bytes())
Error::Message(msg.to_string())
}
}

impl Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let msg = match self {
Error::Message(msg) => sp_std::str::from_utf8(msg).unwrap_or(""),
Error::Message(msg) => msg,
Error::EnumSize => "enum variant size too large to serialize(> 255)",
Error::Unimplemented => "unimplemented serialization",
Error::UnknownSize => "sequence size is not known",
Expand Down
8 changes: 6 additions & 2 deletions pallets/liquidity-pools/src/gmpf/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@ impl<'a> ser::Serializer for &'a mut Serializer {
value.serialize(self)
}

fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
Err(Error::Unimplemented)
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
let len: u16 = len
.and_then(|len| len.try_into().ok())
.ok_or(Error::UnknownSize)?;
self.output.extend(&len.to_be_bytes());
Ok(self)
}

fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
Expand Down
2 changes: 2 additions & 0 deletions pallets/liquidity-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ mod gmpf {
mod ser;

pub use de::from_slice;
#[cfg(test)]
pub use error::Error;
pub use ser::to_vec;
}

Expand Down
Loading

0 comments on commit 69af655

Please sign in to comment.