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 use alphavm_utilities::DeserializeExt; 22 23 impl<N: Network> Serialize for HeaderLeaf<N> { 24 /// Serializes the leaf into string or bytes. 25 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { 26 match serializer.is_human_readable() { 27 true => { 28 let mut leaf = serializer.serialize_struct("HeaderLeaf", 2)?; 29 leaf.serialize_field("index", &self.index)?; 30 leaf.serialize_field("id", &self.id)?; 31 leaf.end() 32 } 33 false => ToBytesSerializer::serialize_with_size_encoding(self, serializer), 34 } 35 } 36 } 37 38 impl<'de, N: Network> Deserialize<'de> for HeaderLeaf<N> { 39 /// Deserializes the leaf from a string or bytes. 40 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 41 match deserializer.is_human_readable() { 42 true => { 43 // Parse the leaf from a string into a value. 44 let mut leaf = serde_json::Value::deserialize(deserializer)?; 45 // Recover the leaf. 46 Ok(Self::new( 47 // Retrieve the index. 48 DeserializeExt::take_from_value::<D>(&mut leaf, "index")?, 49 // Retrieve the id. 50 DeserializeExt::take_from_value::<D>(&mut leaf, "id")?, 51 )) 52 } 53 false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "header leaf"), 54 } 55 } 56 } 57 58 #[cfg(test)] 59 mod tests { 60 use super::*; 61 62 #[test] 63 fn test_serde_json() -> Result<()> { 64 let mut rng = TestRng::default(); 65 66 // Sample the leaf. 67 let expected = test_helpers::sample_leaf(&mut rng); 68 69 // Serialize 70 let expected_string = &expected.to_string(); 71 let candidate_string = serde_json::to_string(&expected)?; 72 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 73 74 // Deserialize 75 assert_eq!(expected, HeaderLeaf::from_str(expected_string)?); 76 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 77 78 Ok(()) 79 } 80 81 #[test] 82 fn test_bincode() -> Result<()> { 83 let mut rng = TestRng::default(); 84 85 // Sample the leaf. 86 let expected = test_helpers::sample_leaf(&mut rng); 87 88 // Serialize 89 let expected_bytes = expected.to_bytes_le()?; 90 let expected_bytes_with_size_encoding = bincode::serialize(&expected)?; 91 assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]); 92 93 // Deserialize 94 assert_eq!(expected, HeaderLeaf::read_le(&expected_bytes[..])?); 95 assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?); 96 97 Ok(()) 98 } 99 }