serialize.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use super::*; 17 18 impl<N: Network> Serialize for Header<N> { 19 /// Serializes the header to a JSON-string or buffer. 20 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { 21 match serializer.is_human_readable() { 22 true => { 23 let mut header = serializer.serialize_struct("Header", 7)?; 24 header.serialize_field("previous_state_root", &self.previous_state_root)?; 25 header.serialize_field("transactions_root", &self.transactions_root)?; 26 header.serialize_field("finalize_root", &self.finalize_root)?; 27 header.serialize_field("ratifications_root", &self.ratifications_root)?; 28 header.serialize_field("solutions_root", &self.solutions_root)?; 29 header.serialize_field("subdag_root", &self.subdag_root)?; 30 header.serialize_field("metadata", &self.metadata)?; 31 header.end() 32 } 33 false => ToBytesSerializer::serialize_with_size_encoding(self, serializer), 34 } 35 } 36 } 37 38 impl<'de, N: Network> Deserialize<'de> for Header<N> { 39 /// Deserializes the header from a JSON-string or buffer. 40 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 41 match deserializer.is_human_readable() { 42 true => { 43 let mut header = serde_json::Value::deserialize(deserializer)?; 44 Ok(Self::from( 45 DeserializeExt::take_from_value::<D>(&mut header, "previous_state_root")?, 46 DeserializeExt::take_from_value::<D>(&mut header, "transactions_root")?, 47 DeserializeExt::take_from_value::<D>(&mut header, "finalize_root")?, 48 DeserializeExt::take_from_value::<D>(&mut header, "ratifications_root")?, 49 DeserializeExt::take_from_value::<D>(&mut header, "solutions_root")?, 50 DeserializeExt::take_from_value::<D>(&mut header, "subdag_root")?, 51 DeserializeExt::take_from_value::<D>(&mut header, "metadata")?, 52 ) 53 .map_err(de::Error::custom)?) 54 } 55 false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "block header"), 56 } 57 } 58 } 59 60 #[cfg(test)] 61 mod tests { 62 use super::*; 63 64 #[test] 65 fn test_serde_json() -> Result<()> { 66 let rng = &mut TestRng::default(); 67 68 for expected in [crate::header::test_helpers::sample_block_header(rng)].into_iter() { 69 // Serialize 70 let expected_string = &expected.to_string(); 71 let candidate_string = serde_json::to_string(&expected)?; 72 73 // Deserialize 74 assert_eq!(expected, Header::from_str(expected_string)?); 75 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 76 } 77 Ok(()) 78 } 79 80 #[test] 81 fn test_bincode() -> Result<()> { 82 let rng = &mut TestRng::default(); 83 84 for expected in [crate::header::test_helpers::sample_block_header(rng)].into_iter() { 85 // Serialize 86 let expected_bytes = expected.to_bytes_le()?; 87 let expected_bytes_with_size_encoding = bincode::serialize(&expected)?; 88 assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]); 89 90 // Deserialize 91 assert_eq!(expected, Header::read_le(&expected_bytes[..])?); 92 assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?); 93 } 94 Ok(()) 95 } 96 }