serialize.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the deltavm 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 PuzzleSolutions<N> { 19 /// Serializes the solutions 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 solutions = serializer.serialize_struct("PuzzleSolutions", 1)?; 24 solutions.serialize_field("solutions", &self.solutions.values().collect::<Vec<_>>())?; 25 solutions.end() 26 } 27 false => ToBytesSerializer::serialize_with_size_encoding(self, serializer), 28 } 29 } 30 } 31 32 impl<'de, N: Network> Deserialize<'de> for PuzzleSolutions<N> { 33 /// Deserializes the solutions from a JSON-string or buffer. 34 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 35 match deserializer.is_human_readable() { 36 true => { 37 let mut solutions = serde_json::Value::deserialize(deserializer)?; 38 Self::new(DeserializeExt::take_from_value::<D>(&mut solutions, "solutions")?).map_err(de::Error::custom) 39 } 40 false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "solutions"), 41 } 42 } 43 } 44 45 #[cfg(test)] 46 pub(super) mod tests { 47 use super::*; 48 use crate::PartialSolution; 49 use console::account::{Address, PrivateKey}; 50 51 type CurrentNetwork = console::network::MainnetV0; 52 53 pub(crate) fn sample_solutions(rng: &mut TestRng) -> PuzzleSolutions<CurrentNetwork> { 54 // Sample a new solutions. 55 let mut solutions = vec![]; 56 for _ in 0..rng.gen_range(1..CurrentNetwork::MAX_SOLUTIONS) { 57 let private_key = PrivateKey::<CurrentNetwork>::new(rng).unwrap(); 58 let address = Address::try_from(private_key).unwrap(); 59 60 let partial_solution = PartialSolution::new(rng.r#gen(), address, u64::rand(rng)).unwrap(); 61 let solution = Solution::new(partial_solution, u64::rand(rng)); 62 solutions.push(solution); 63 } 64 PuzzleSolutions::new(solutions).unwrap() 65 } 66 67 #[test] 68 fn test_serde_json() -> Result<()> { 69 let rng = &mut TestRng::default(); 70 71 // Sample a new solutions. 72 let expected = sample_solutions(rng); 73 74 // Serialize 75 let expected_string = &expected.to_string(); 76 let candidate_string = serde_json::to_string(&expected)?; 77 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 78 79 // Deserialize 80 assert_eq!(expected, PuzzleSolutions::from_str(expected_string)?); 81 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 82 83 Ok(()) 84 } 85 86 #[test] 87 fn test_bincode() -> Result<()> { 88 let rng = &mut TestRng::default(); 89 90 // Sample a new solutions. 91 let expected = sample_solutions(rng); 92 93 // Serialize 94 let expected_bytes = expected.to_bytes_le()?; 95 let expected_bytes_with_size_encoding = bincode::serialize(&expected)?; 96 assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]); 97 98 // Deserialize 99 assert_eq!(expected, PuzzleSolutions::read_le(&expected_bytes[..])?); 100 assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?); 101 102 Ok(()) 103 } 104 }