serialize.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphavm library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 use super::*; 20 21 impl<N: Network> Serialize for PuzzleSolutions<N> { 22 /// Serializes the solutions to a JSON-string or buffer. 23 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { 24 match serializer.is_human_readable() { 25 true => { 26 let mut solutions = serializer.serialize_struct("PuzzleSolutions", 1)?; 27 solutions.serialize_field("solutions", &self.solutions.values().collect::<Vec<_>>())?; 28 solutions.end() 29 } 30 false => ToBytesSerializer::serialize_with_size_encoding(self, serializer), 31 } 32 } 33 } 34 35 impl<'de, N: Network> Deserialize<'de> for PuzzleSolutions<N> { 36 /// Deserializes the solutions from a JSON-string or buffer. 37 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 38 match deserializer.is_human_readable() { 39 true => { 40 let mut solutions = serde_json::Value::deserialize(deserializer)?; 41 Self::new(DeserializeExt::take_from_value::<D>(&mut solutions, "solutions")?).map_err(de::Error::custom) 42 } 43 false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "solutions"), 44 } 45 } 46 } 47 48 #[cfg(test)] 49 pub(super) mod tests { 50 use super::*; 51 use crate::PartialSolution; 52 use console::account::{Address, PrivateKey}; 53 54 type CurrentNetwork = console::network::MainnetV0; 55 56 pub(crate) fn sample_solutions(rng: &mut TestRng) -> PuzzleSolutions<CurrentNetwork> { 57 // Sample a new solutions. 58 let mut solutions = vec![]; 59 for _ in 0..rng.gen_range(1..CurrentNetwork::MAX_SOLUTIONS) { 60 let private_key = PrivateKey::<CurrentNetwork>::new(rng).unwrap(); 61 let address = Address::try_from(private_key).unwrap(); 62 63 let partial_solution = PartialSolution::new(rng.r#gen(), address, u64::rand(rng)).unwrap(); 64 let solution = Solution::new(partial_solution, u64::rand(rng)); 65 solutions.push(solution); 66 } 67 PuzzleSolutions::new(solutions).unwrap() 68 } 69 70 #[test] 71 fn test_serde_json() -> Result<()> { 72 let rng = &mut TestRng::default(); 73 74 // Sample a new solutions. 75 let expected = sample_solutions(rng); 76 77 // Serialize 78 let expected_string = &expected.to_string(); 79 let candidate_string = serde_json::to_string(&expected)?; 80 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 81 82 // Deserialize 83 assert_eq!(expected, PuzzleSolutions::from_str(expected_string)?); 84 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 85 86 Ok(()) 87 } 88 89 #[test] 90 fn test_bincode() -> Result<()> { 91 let rng = &mut TestRng::default(); 92 93 // Sample a new solutions. 94 let expected = sample_solutions(rng); 95 96 // Serialize 97 let expected_bytes = expected.to_bytes_le()?; 98 let expected_bytes_with_size_encoding = bincode::serialize(&expected)?; 99 assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]); 100 101 // Deserialize 102 assert_eq!(expected, PuzzleSolutions::read_le(&expected_bytes[..])?); 103 assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?); 104 105 Ok(()) 106 } 107 }