amareleo_node_bft_events/
validators_response.rs

1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkOS 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
16use super::*;
17
18#[derive(Clone, Debug, PartialEq, Eq)]
19pub struct ValidatorsResponse<N: Network> {
20    pub validators: IndexMap<SocketAddr, Address<N>>,
21}
22
23impl<N: Network> EventTrait for ValidatorsResponse<N> {
24    /// Returns the event name.
25    #[inline]
26    fn name(&self) -> Cow<'static, str> {
27        "ValidatorsResponse".into()
28    }
29}
30
31impl<N: Network> ToBytes for ValidatorsResponse<N> {
32    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
33        // Write the number of validators.
34        u16::try_from(self.validators.len()).map_err(error)?.write_le(&mut writer)?;
35        // Write the validators.
36        for (socket_addr, address) in &self.validators {
37            socket_addr.write_le(&mut writer)?;
38            address.write_le(&mut writer)?;
39        }
40        Ok(())
41    }
42}
43
44impl<N: Network> FromBytes for ValidatorsResponse<N> {
45    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
46        // Read the number of validators.
47        let num_validators = u16::read_le(&mut reader)?;
48        // Read the validators.
49        let mut validators = IndexMap::with_capacity(num_validators as usize);
50        for _ in 0..num_validators {
51            let socket_addr = SocketAddr::read_le(&mut reader)?;
52            let address = Address::<N>::read_le(&mut reader)?;
53            validators.insert(socket_addr, address);
54        }
55        Ok(Self { validators })
56    }
57}
58
59#[cfg(test)]
60pub mod prop_tests {
61    use crate::{ValidatorsResponse, challenge_request::prop_tests::any_valid_address};
62
63    use bytes::{Buf, BufMut, BytesMut};
64    use indexmap::IndexMap;
65    use proptest::{
66        collection::hash_map,
67        prelude::{BoxedStrategy, Strategy, any},
68    };
69    use snarkvm::{
70        prelude::Address,
71        utilities::{FromBytes, ToBytes},
72    };
73    use std::net::{IpAddr, SocketAddr};
74    use test_strategy::proptest;
75
76    type CurrentNetwork = snarkvm::prelude::MainnetV0;
77
78    pub fn any_valid_socket_addr() -> BoxedStrategy<SocketAddr> {
79        any::<(IpAddr, u16)>().prop_map(|(ip_addr, port)| SocketAddr::new(ip_addr, port)).boxed()
80    }
81
82    pub fn any_index_map() -> BoxedStrategy<IndexMap<SocketAddr, Address<CurrentNetwork>>> {
83        hash_map(any_valid_socket_addr(), any_valid_address(), 0..50)
84            .prop_map(|map| map.iter().map(|(k, v)| (*k, *v)).collect())
85            .boxed()
86    }
87
88    pub fn any_validators_response() -> BoxedStrategy<ValidatorsResponse<CurrentNetwork>> {
89        any_index_map().prop_map(|map| ValidatorsResponse { validators: map }).boxed()
90    }
91
92    #[proptest]
93    fn validators_response_roundtrip(
94        #[strategy(any_validators_response())] validators_response: ValidatorsResponse<CurrentNetwork>,
95    ) {
96        let mut bytes = BytesMut::default().writer();
97        validators_response.write_le(&mut bytes).unwrap();
98        let decoded = ValidatorsResponse::<CurrentNetwork>::read_le(&mut bytes.into_inner().reader()).unwrap();
99        assert_eq![decoded, validators_response];
100    }
101}