snarkvm_ledger_block/ratifications/
serialize.rs1use super::*;
17
18impl<N: Network> Serialize for Ratifications<N> {
19 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
34impl<'de, N: Network> Deserialize<'de> for Ratifications<N> {
35 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)]
68mod 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 let expected_string = &expected.to_string();
83 let candidate_string = serde_json::to_string(&expected).unwrap();
84
85 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_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 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 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_bincode(crate::ratifications::test_helpers::sample_block_ratifications(rng));
114 }
115 }
116}