From 8d30367bcda9b9fdcc4338dda86197a17a03fa60 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Fri, 17 May 2024 23:20:50 +0200 Subject: [PATCH 1/3] utils: add dedicated modules for serializing and deserializing arrays --- src/utils/mod.rs | 1 + src/utils/serialize_utils.rs | 353 +++++++++++++++++++++++++++++++++++ 2 files changed, 354 insertions(+) create mode 100644 src/utils/serialize_utils.rs diff --git a/src/utils/mod.rs b/src/utils/mod.rs index adb6987..d7b7f3e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -8,6 +8,7 @@ use crate::primitives::asset::TokenAmount; pub mod druid_utils; pub mod error_utils; pub mod script_utils; +pub mod serialize_utils; pub mod test_utils; pub mod transaction_utils; diff --git a/src/utils/serialize_utils.rs b/src/utils/serialize_utils.rs new file mode 100644 index 0000000..5d211e2 --- /dev/null +++ b/src/utils/serialize_utils.rs @@ -0,0 +1,353 @@ +use std::any::TypeId; +use std::convert::TryFrom; +use std::fmt::Formatter; +use std::marker::PhantomData; + +use bincode::config::*; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::de::{SeqAccess, Visitor}; +use serde::ser::{SerializeTuple}; + +pub fn bincode_default() -> WithOtherTrailing, RejectTrailing> { + DefaultOptions::new() + .with_fixint_encoding() + .reject_trailing_bytes() +} + +pub fn bincode_compact() -> WithOtherTrailing, RejectTrailing> { + DefaultOptions::new() + .with_varint_encoding() + .reject_trailing_bytes() +} + +/// A codec for fixed-size arrays. +pub mod fixed_array_codec { + use super::*; + + pub fn serialize( + values: &[T; N], + serializer: S, + ) -> Result { + if TypeId::of::() == TypeId::of::() && serializer.is_human_readable() { + // We're serializing a byte array for a human-readable format, make it a hex string + vec_codec::serialize(values, serializer) + } else { + // Serialize the array as a tuple, to avoid adding a length prefix + let mut tuple = serializer.serialize_tuple(N)?; + for e in values { + tuple.serialize_element(e)?; + } + tuple.end() + } + } + + pub fn deserialize<'de, T: Deserialize<'de> + 'static, D: Deserializer<'de>, const N: usize>( + deserializer: D, + ) -> Result<[T; N], D::Error> { + if TypeId::of::() == TypeId::of::() && deserializer.is_human_readable() { + // We're deserializing a byte array for a human-readable format, we'll accept two different + // representations: + // - A hexadecimal string + // - An array of byte literals (this format should never be produced by the serializer + // for human-readable formats, but it was in the past, so we'll still support reading + // it for backwards-compatibility). + vec_to_fixed_array(vec_codec::deserialize(deserializer)?) + } else { + // We're deserializing a binary format, read the array as a tuple + // (to avoid adding a length prefix) + + struct FixedArrayVisitor(PhantomData); + impl<'de, T: Deserialize<'de>, const N: usize> Visitor<'de> for FixedArrayVisitor { + type Value = [T; N]; + + fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result { + write!(formatter, "a sequence") + } + + fn visit_seq>(self, mut seq: A) -> Result { + let mut vec = Vec::with_capacity(N); + while let Some(val) = seq.next_element::()? { + vec.push(val) + } + vec_to_fixed_array(vec) + } + } + + deserializer.deserialize_tuple(N, FixedArrayVisitor(Default::default())) + } + } +} + +/// A codec for variable-length `Vec`s. +pub mod vec_codec { + use super::*; + + pub fn serialize( + values: &[T], + serializer: S, + ) -> Result { + if TypeId::of::() == TypeId::of::() && serializer.is_human_readable() { + // We're serializing a byte array for a human-readable format, make it a hex string + let bytes = unsafe { std::slice::from_raw_parts(values.as_ptr() as *const u8, values.len()) }; + serializer.serialize_str(&hex::encode(bytes)) + } else { + // Serialize the array as a length-prefixed sequence + values.serialize(serializer) + } + } + + pub fn deserialize<'de, T: Deserialize<'de> + 'static, D: Deserializer<'de>>( + deserializer: D, + ) -> Result, D::Error> { + if TypeId::of::() == TypeId::of::() && deserializer.is_human_readable() { + // We're deserializing a byte array for a human-readable format, we'll accept two different + // representations: + // - A hexadecimal string + // - An array of byte literals (this format should never be produced by the serializer + // for human-readable formats, but it was in the past, so we'll still support reading + // it for backwards-compatibility). + + struct HexStringOrBytesVisitor(); + impl<'de> Visitor<'de> for HexStringOrBytesVisitor { + type Value = Vec; + + fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result { + formatter.write_str("hex string or byte array") + } + + fn visit_str(self, value: &str) -> Result { + hex::decode(value).map_err(E::custom) + } + + fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de> { + let mut vec = Vec::new(); + while let Some(elt) = seq.next_element::()? { + vec.push(elt); + } + Ok(vec) + } + } + + Ok(deserializer.deserialize_any(HexStringOrBytesVisitor())?.into_iter() + // This is a hack to convert the Vec into a Vec, even though we already know + // that T = u8. This could be done in a much nicer way if trait specialization were + // a thing, but unfortunately it's still only available on nightly :( + .map(|b| unsafe { std::mem::transmute_copy::(&b) }) + .collect::>()) + } else { + // Read a length-prefixed sequence as a Vec + >::deserialize(deserializer) + } + } +} + +fn vec_to_fixed_array( + vec: Vec, +) -> Result<[T; N], E> { + <[T; N]>::try_from(vec) + .map_err(|vec| E::custom(format!("expected exactly {} elements, but read {}", N, vec.len()))) +} + +/*---- TESTS ----*/ + +#[cfg(test)] +mod tests { + use std::fmt::Debug; + use bincode::Options; + + use serde::{Deserialize, Serialize}; + use serde::de::DeserializeOwned; + use super::*; + + fn repeat(orig: &str, n: usize) -> String { + let mut res = String::with_capacity(orig.len() * n); + for _ in 0..n { + res.push_str(orig) + } + res + } + + fn test_bin_codec( + options: fn() -> O, + obj: T, + expect: &str, + ) { + let bytes = options().serialize(&obj).unwrap(); + assert_eq!(hex::encode(&bytes), expect); + assert_eq!(options().deserialize::(&bytes).unwrap(), obj); + } + + fn test_json_codec( + obj: T, + expect: &str, + ) { + let json = serde_json::to_string(&obj).unwrap(); + assert_eq!(json, expect); + assert_eq!(serde_json::from_str::(&json).unwrap(), obj); + } + + fn test_json_deserialize( + obj: T, + json: &str, + ) { + assert_eq!(serde_json::from_str::(&json).unwrap(), obj); + } + + macro_rules! test_fixed_array { + ($n:literal) => { + test_bin_codec(bincode_default, [VAL; $n], &repeat(HEX, $n)); + test_json_codec([VAL; $n], &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + macro_rules! test_fixed_array_wrapper { + ($e:ty, $t:ident, $n:literal) => { + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct $t([$e; $n]); + test_bin_codec(bincode_default, $t([VAL; $n]), &repeat(HEX, $n)); + test_json_codec($t([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + #[test] + fn test_fixed_u32_arrays() { + const VAL : u32 = 0xDEADBEEF; + const HEX : &str = "efbeadde"; + + test_fixed_array!(0); + test_fixed_array!(1); + test_fixed_array!(32); + + test_fixed_array_wrapper!(u32, FixedArrayWrapper0, 0); + test_fixed_array_wrapper!(u32, FixedArrayWrapper1, 1); + test_fixed_array_wrapper!(u32, FixedArrayWrapper32, 32); + + macro_rules! test_fixed_array_wrapper_codec { + ($t:ident, $n:literal) => { + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct $t(#[serde(with = "fixed_array_codec")] [u32; $n]); + test_bin_codec(bincode_default, $t([VAL; $n]), &repeat(HEX, $n)); + test_json_codec($t([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper0, 0); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper1, 1); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper32, 32); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper33, 33); + } + + #[test] + fn test_fixed_u8_arrays() { + const VAL : u8 = 123; + const HEX : &str = "7b"; + + test_fixed_array!(0); + test_fixed_array!(1); + test_fixed_array!(32); + + test_fixed_array_wrapper!(u8, FixedArrayWrapper0, 0); + test_fixed_array_wrapper!(u8, FixedArrayWrapper1, 1); + test_fixed_array_wrapper!(u8, FixedArrayWrapper32, 32); + + macro_rules! test_fixed_array_wrapper_codec { + ($t:ident, $n:literal) => { + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct $t(#[serde(with = "fixed_array_codec")] [u8; $n]); + test_bin_codec(bincode_default, $t([VAL; $n]), &repeat(HEX, $n)); + test_json_codec($t([VAL; $n]), &format!("\"{}\"", hex::encode(&[VAL; $n].to_vec()))); + test_json_deserialize($t([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper0, 0); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper1, 1); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper32, 32); + test_fixed_array_wrapper_codec!(CodecFixedArrayWrapper33, 33); + } + + fn size_to_hex_default(n: usize) -> String { + hex::encode(bincode_default().serialize(&n).unwrap()) + } + + macro_rules! test_vec { + ($n:literal) => { + test_bin_codec(bincode_default, [VAL; $n].to_vec(), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_json_codec([VAL; $n].to_vec(), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + macro_rules! test_vec_wrapper { + ($n:literal) => { + test_bin_codec(bincode_default, VecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_json_codec(VecWrapper([VAL; $n].to_vec()), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + #[test] + fn test_u32_vecs() { + const VAL : u32 = 0xDEADBEEF; + const HEX : &str = "efbeadde"; + + test_vec!(0); + test_vec!(1); + test_vec!(32); + test_vec!(33); + + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct VecWrapper(Vec); + + test_vec_wrapper!(0); + test_vec_wrapper!(1); + test_vec_wrapper!(32); + test_vec_wrapper!(33); + + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct CodecVecWrapper(#[serde(with = "vec_codec")] Vec); + macro_rules! test_vec_wrapper_codec { + ($n:literal) => { + test_bin_codec(bincode_default, CodecVecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_json_codec(CodecVecWrapper([VAL; $n].to_vec()), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + test_vec_wrapper_codec!(0); + test_vec_wrapper_codec!(1); + test_vec_wrapper_codec!(32); + test_vec_wrapper_codec!(33); + } + + #[test] + fn test_u8_vecs() { + const VAL : u8 = 123; + const HEX : &str = "7b"; + + test_vec!(0); + test_vec!(1); + test_vec!(32); + test_vec!(33); + + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct VecWrapper(Vec); + + test_vec_wrapper!(0); + test_vec_wrapper!(1); + test_vec_wrapper!(32); + test_vec_wrapper!(33); + + #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] + struct CodecVecWrapper(#[serde(with = "vec_codec")] Vec); + macro_rules! test_vec_wrapper_codec { + ($n:literal) => { + test_bin_codec(bincode_default, CodecVecWrapper([VAL; $n].to_vec()), &format!("{}{}", size_to_hex_default($n), repeat(HEX, $n))); + test_json_codec(CodecVecWrapper([VAL; $n].to_vec()), &format!("\"{}\"", hex::encode(&[VAL; $n].to_vec()))); + test_json_deserialize(CodecVecWrapper([VAL; $n].to_vec()), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + }; + } + + test_vec_wrapper_codec!(0); + test_vec_wrapper_codec!(1); + test_vec_wrapper_codec!(32); + test_vec_wrapper_codec!(33); + } +} From 7097e54d4342bcdc1123769240ea227b1bbfdb51 Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Wed, 29 May 2024 12:56:17 +0200 Subject: [PATCH 2/3] Add a struct for representing fixed-length byte arrays This allows other structs which wrap a fixed-length byte array to simply use it as a field, and not have to deal with any of the details of formatting or JSON hex (de)serialization. --- src/utils/serialize_utils.rs | 146 +++++++++++++++++++++++++++++++++-- 1 file changed, 141 insertions(+), 5 deletions(-) diff --git a/src/utils/serialize_utils.rs b/src/utils/serialize_utils.rs index 5d211e2..bd199fc 100644 --- a/src/utils/serialize_utils.rs +++ b/src/utils/serialize_utils.rs @@ -1,7 +1,9 @@ use std::any::TypeId; -use std::convert::TryFrom; -use std::fmt::Formatter; +use std::convert::{TryFrom, TryInto}; +use std::fmt; use std::marker::PhantomData; +use std::ops::{Deref, DerefMut}; +use std::str::FromStr; use bincode::config::*; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -20,6 +22,104 @@ pub fn bincode_compact() -> WithOtherTrailing( + #[serde(with = "fixed_array_codec")] + [u8; N], +); + +impl FixedByteArray { + pub fn new(arr: [u8; N]) -> Self { + Self(arr) + } +} + +impl AsRef<[u8]> for FixedByteArray { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} + +impl AsMut<[u8]> for FixedByteArray { + fn as_mut(&mut self) -> &mut [u8] { + &mut self.0 + } +} + +impl Deref for FixedByteArray { + type Target = [u8; N]; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for FixedByteArray { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl From<[u8; N]> for FixedByteArray { + fn from(value: [u8; N]) -> Self { + Self(value) + } +} + +impl TryFrom<&[u8]> for FixedByteArray { + type Error = std::array::TryFromSliceError; + + fn try_from(value: &[u8]) -> Result { + value.try_into().map(Self) + } +} + +impl TryFrom<&Vec> for FixedByteArray { + type Error = std::array::TryFromSliceError; + + fn try_from(value: &Vec) -> Result { + value.as_slice().try_into().map(Self) + } +} + +impl fmt::LowerHex for FixedByteArray { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // This is hacky because we can't make an array of type [u8; {N * 2}] due to + // generic parameters not being allowed in constant expressions on stable rust + assert_eq!(std::mem::size_of::<[u16; N]>(), std::mem::size_of::<[u8; N]>() * 2); + let mut buf = [0u16; N]; + let slice = unsafe { std::slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, N * 2) }; + hex::encode_to_slice(&self.0, slice).unwrap(); + f.write_str(std::str::from_utf8(slice).unwrap()) + } +} + +impl fmt::Display for FixedByteArray { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::LowerHex::fmt(self, f) + } +} + +impl fmt::Debug for FixedByteArray { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "FixedByteArray<{N}>({self:x})") + } +} + +impl FromStr for FixedByteArray { + type Err = hex::FromHexError; + + fn from_str(s: &str) -> Result { + let mut buf = [0u8; N]; + hex::decode_to_slice(s, &mut buf)?; + Ok(Self(buf)) + } +} + /// A codec for fixed-size arrays. pub mod fixed_array_codec { use super::*; @@ -60,7 +160,7 @@ pub mod fixed_array_codec { impl<'de, T: Deserialize<'de>, const N: usize> Visitor<'de> for FixedArrayVisitor { type Value = [T; N]; - fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!(formatter, "a sequence") } @@ -111,7 +211,7 @@ pub mod vec_codec { impl<'de> Visitor<'de> for HexStringOrBytesVisitor { type Value = Vec; - fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("hex string or byte array") } @@ -152,7 +252,7 @@ fn vec_to_fixed_array( #[cfg(test)] mod tests { - use std::fmt::Debug; + use std::fmt::{Debug, Display}; use bincode::Options; use serde::{Deserialize, Serialize}; @@ -193,6 +293,15 @@ mod tests { assert_eq!(serde_json::from_str::(&json).unwrap(), obj); } + fn test_display_fromstr( + obj: T, + expect: &str, + ) { + let string = obj.to_string(); + assert_eq!(string, expect); + assert_eq!(::from_str(&string).ok().unwrap(), obj); + } + macro_rules! test_fixed_array { ($n:literal) => { test_bin_codec(bincode_default, [VAL; $n], &repeat(HEX, $n)); @@ -350,4 +459,31 @@ mod tests { test_vec_wrapper_codec!(32); test_vec_wrapper_codec!(33); } + + #[test] + fn test_fixed_byte_array() { + const VAL : u8 = 123; + const HEX : &str = "7b"; + + macro_rules! test_fixed_byte_array { + ($n:literal) => { + test_bin_codec(bincode_default, FixedByteArray::<$n>([VAL; $n]), &repeat(HEX, $n)); + test_json_codec(FixedByteArray::<$n>([VAL; $n]), &format!("\"{}\"", repeat(HEX, $n))); + test_json_deserialize(FixedByteArray::<$n>([VAL; $n]), &serde_json::to_string(&[VAL; $n].to_vec()).unwrap()); + test_display_fromstr(FixedByteArray::<$n>([VAL; $n]), &repeat(HEX, $n)); + assert_eq!(format!("{:x}", FixedByteArray::<$n>([VAL; $n])), repeat(HEX, $n)); + assert_eq!( + format!("{:?}", FixedByteArray::<$n>([VAL; $n])), + format!("FixedByteArray<{}>({})", $n, repeat(HEX, $n))); + assert_eq!( + format!("{:x?}", FixedByteArray::<$n>([VAL; $n])), + format!("FixedByteArray<{}>({})", $n, repeat(HEX, $n))); + }; + } + + test_fixed_byte_array!(0); + test_fixed_byte_array!(1); + test_fixed_byte_array!(32); + test_fixed_byte_array!(33); + } } From 4be53f5a1ed14aac6a73ed340b2ee826799fd81f Mon Sep 17 00:00:00 2001 From: DaPorkchop_ Date: Wed, 29 May 2024 12:56:17 +0200 Subject: [PATCH 3/3] serialize_utils: implement From<&[u8; N]> for FixedByteArray --- src/utils/serialize_utils.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/utils/serialize_utils.rs b/src/utils/serialize_utils.rs index bd199fc..af72618 100644 --- a/src/utils/serialize_utils.rs +++ b/src/utils/serialize_utils.rs @@ -70,6 +70,12 @@ impl From<[u8; N]> for FixedByteArray { } } +impl From<&[u8; N]> for FixedByteArray { + fn from(value: &[u8; N]) -> Self { + Self(*value) + } +} + impl TryFrom<&[u8]> for FixedByteArray { type Error = std::array::TryFromSliceError;