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 Ratifications<N> { 19 /// Serializes the ratifications 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 ratifications = serializer.serialize_seq(Some(self.ratifications.len()))?; 24 for ratify in self.ratifications.values() { 25 ratifications.serialize_element(ratify)?; 26 } 27 ratifications.end() 28 } 29 false => ToBytesSerializer::serialize_with_size_encoding(self, serializer), 30 } 31 } 32 } 33 34 impl<'de, N: Network> Deserialize<'de> for Ratifications<N> { 35 /// Deserializes the ratifications from a JSON-string or buffer. 36 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 37 match deserializer.is_human_readable() { 38 true => { 39 use core::marker::PhantomData; 40 41 struct RatificationsDeserializer<N: Network>(PhantomData<N>); 42 43 impl<'de, N: Network> Visitor<'de> for RatificationsDeserializer<N> { 44 type Value = Vec<Ratify<N>>; 45 46 fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { 47 formatter.write_str("Vec<Ratify> sequence.") 48 } 49 50 fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> { 51 let mut ratifications = Vec::new(); 52 while let Some(ratify) = seq.next_element()? { 53 ratifications.push(ratify); 54 } 55 Ok(ratifications) 56 } 57 } 58 59 Self::try_from(deserializer.deserialize_seq(RatificationsDeserializer(PhantomData))?) 60 .map_err(de::Error::custom) 61 } 62 false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "ratifications"), 63 } 64 } 65 } 66 67 #[cfg(test)] 68 mod tests { 69 use super::*; 70 use console::network::MainnetV0; 71 72 type CurrentNetwork = MainnetV0; 73 74 const ITERATIONS: u32 = 100; 75 76 #[test] 77 fn test_serde_json() { 78 let rng = &mut TestRng::default(); 79 80 let check_serde_json = |expected: Ratifications<CurrentNetwork>| { 81 // Serialize 82 let expected_string = &expected.to_string(); 83 let candidate_string = serde_json::to_string(&expected).unwrap(); 84 85 // Deserialize 86 assert_eq!(expected, Ratifications::from_str(expected_string).unwrap()); 87 assert_eq!(expected, serde_json::from_str(&candidate_string).unwrap()); 88 }; 89 90 for _ in 0..ITERATIONS { 91 // Check the serialization. 92 check_serde_json(crate::ratifications::test_helpers::sample_block_ratifications(rng)); 93 } 94 } 95 96 #[test] 97 fn test_bincode() { 98 let rng = &mut TestRng::default(); 99 100 let check_bincode = |expected: Ratifications<CurrentNetwork>| { 101 // Serialize 102 let expected_bytes = expected.to_bytes_le().unwrap(); 103 let expected_bytes_with_size_encoding = bincode::serialize(&expected).unwrap(); 104 assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]); 105 106 // Deserialize 107 assert_eq!(expected, Ratifications::read_le(&expected_bytes[..]).unwrap()); 108 assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..]).unwrap()); 109 }; 110 111 for _ in 0..ITERATIONS { 112 // Check the serialization. 113 check_bincode(crate::ratifications::test_helpers::sample_block_ratifications(rng)); 114 } 115 } 116 }