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