snarkvm_ledger_block/ratifications/
bytes.rs1use super::*;
17
18impl<N: Network> FromBytes for Ratifications<N> {
19 #[inline]
21 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
22 let version = u8::read_le(&mut reader)?;
24 if version != 1 {
26 return Err(error("Invalid ratifications version"));
27 }
28 let num_ratify: u32 = FromBytes::read_le(&mut reader)?;
30 if num_ratify as usize > Self::MAX_RATIFICATIONS {
32 return Err(error("Failed to read ratifications: too many ratifications"));
33 }
34 let ratifications = (0..num_ratify).map(|_| FromBytes::read_le(&mut reader)).collect::<Result<Vec<_>, _>>()?;
36 Self::try_from(ratifications).map_err(into_io_error)
38 }
39}
40
41impl<N: Network> ToBytes for Ratifications<N> {
42 #[inline]
44 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
45 1u8.write_le(&mut writer)?;
47 u32::try_from(self.ratifications.len()).map_err(error)?.write_le(&mut writer)?;
49 self.ratifications.values().try_for_each(|ratification| ratification.write_le(&mut writer))
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 const ITERATIONS: u32 = 100;
59
60 #[test]
61 fn test_bytes() -> Result<()> {
62 let rng = &mut TestRng::default();
63
64 for _ in 0..ITERATIONS {
65 let expected = crate::ratifications::test_helpers::sample_block_ratifications(rng);
66 let expected_bytes = expected.to_bytes_le()?;
68 assert_eq!(expected, Ratifications::read_le(&expected_bytes[..])?);
69 }
70 Ok(())
71 }
72}