Skip to content

Commit

Permalink
add tests for deserialize batch of batch
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jul 18, 2024
1 parent e7e30a8 commit 9f99c02
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 76 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
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
120 changes: 54 additions & 66 deletions pallets/liquidity-pools/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ mod tests {
use cfg_primitives::{PoolId, TrancheId};
use cfg_types::fixed_point::Ratio;
use cfg_utils::vec_to_fixed_array;
use frame_support::assert_err;
use hex::FromHex;
use sp_runtime::{traits::One, FixedPointNumber};

Expand Down Expand Up @@ -585,17 +586,6 @@ mod tests {
);
}

#[test]
fn add_currency_zero() {
test_encode_decode_identity(
Message::AddAsset {
currency: 0,
evm_address: default_address_20(),
},
"09000000000000000000000000000000001231231231231231231231231231231231231231",
)
}

#[test]
fn add_currency() {
test_encode_decode_identity(
Expand All @@ -607,49 +597,11 @@ mod tests {
)
}

#[test]
fn add_pool_zero() {
test_encode_decode_identity(Message::AddPool { pool_id: 0 }, "0a0000000000000000")
}

#[test]
fn add_pool_long() {
test_encode_decode_identity(Message::AddPool { pool_id: POOL_ID }, "0a0000000000bce1a4")
}

#[test]
fn batch_messages() {
test_encode_decode_identity(
Message::Batch {
messages: BatchMessages::try_from(vec![
Message::AddPool { pool_id: 0 },
Message::AllowAsset {
currency: TOKEN_ID,
pool_id: POOL_ID,
},
])
.unwrap(),
},
concat!(
"04", // Batch index
"0009", // AddPool length
"0a0000000000000000", // AddPool content
"0019", // AddAsset length
"0c0000000000bce1a40000000000000000000000000eb5ec7b", // AllowAsset content
),
)
}

#[test]
fn batch_empty() {
test_encode_decode_identity(
Message::Batch {
messages: BatchMessages::default(),
},
concat!("04"),
)
}

#[test]
fn allow_asset() {
test_encode_decode_identity(
Expand All @@ -661,17 +613,6 @@ mod tests {
)
}

#[test]
fn allow_asset_zero() {
test_encode_decode_identity(
Message::AllowAsset {
currency: 0,
pool_id: 0,
},
"0c000000000000000000000000000000000000000000000000",
)
}

#[test]
fn add_tranche() {
test_encode_decode_identity(
Expand Down Expand Up @@ -942,18 +883,65 @@ mod tests {
)
}

#[test]
fn batch_empty() {
test_encode_decode_identity(
Message::Batch {
messages: BatchMessages::default(),
},
concat!("04"),
)
}

#[test]
fn batch_messages() {
test_encode_decode_identity(
Message::Batch {
messages: BatchMessages::try_from(vec![
Message::AddPool { pool_id: 0 },
Message::AllowAsset {
currency: TOKEN_ID,
pool_id: POOL_ID,
},
])
.unwrap(),
},
concat!(
"04", // Batch index
"0009", // AddPool length
"0a0000000000000000", // AddPool content
"0019", // AddAsset length
"0c0000000000bce1a40000000000000000000000000eb5ec7b", // AllowAsset content
),
)
}

#[test]
fn batch_of_batches_deserialization() {
// The message can not be created directly
let encoded = concat!(
"04", // Batch index
"000c", // Submessage length
"04", // Inner batch index
"0009", // AddPool length
"0a0000000000000000", // AddPool content
);

assert_err!(
gmpf::from_slice::<Message>(&hex::decode(encoded).unwrap()),
gmpf::Error::Message("A submessage can not be a batch".into()),
);
}

/// Verify the identity property of decode . encode on a Message value and
/// that it in fact encodes to and can be decoded from a given hex string.
fn test_encode_decode_identity(msg: Message, expected_hex: &str) {
let encoded = gmpf::to_vec(&msg).unwrap();
assert_eq!(hex::encode(encoded.clone()), expected_hex);

let decoded: Message = gmpf::from_slice(
&mut hex::decode(expected_hex)
.expect("Decode should work")
.as_slice(),
)
.expect("Deserialization should work");
let decoded: Message =
gmpf::from_slice(&hex::decode(expected_hex).expect("Decode should work"))
.expect("Deserialization should work");
assert_eq!(decoded, msg);
}

Expand Down

0 comments on commit 9f99c02

Please sign in to comment.