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 serialization #1920

Merged
merged 16 commits into from
Jul 22, 2024
Merged
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
Loading