snarkos_node_router_messages/
peer_response.rs

1// Copyright (c) 2019-2025 Provable Inc.
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
18use snarkvm::prelude::{FromBytes, ToBytes};
19
20use std::borrow::Cow;
21
22#[derive(Clone, Debug, PartialEq, Eq)]
23pub struct PeerResponse {
24    pub peers: Vec<SocketAddr>,
25}
26
27impl MessageTrait for PeerResponse {
28    /// Returns the message name.
29    #[inline]
30    fn name(&self) -> Cow<'static, str> {
31        "PeerResponse".into()
32    }
33}
34
35impl ToBytes for PeerResponse {
36    fn write_le<W: io::Write>(&self, mut writer: W) -> io::Result<()> {
37        // Return error if the number of peers exceeds the maximum.
38        if self.peers.len() > u8::MAX as usize {
39            return Err(io::Error::new(io::ErrorKind::InvalidInput, format!("Too many peers: {}", self.peers.len())));
40        }
41
42        (self.peers.len() as u8).write_le(&mut writer)?;
43        for peer in self.peers.iter() {
44            peer.write_le(&mut writer)?;
45        }
46        Ok(())
47    }
48}
49
50impl FromBytes for PeerResponse {
51    fn read_le<R: io::Read>(mut reader: R) -> io::Result<Self> {
52        let count = u8::read_le(&mut reader)?;
53        let mut peers = Vec::with_capacity(count as usize);
54        for _ in 0..count {
55            peers.push(SocketAddr::read_le(&mut reader)?);
56        }
57
58        Ok(Self { peers })
59    }
60}
61
62#[cfg(test)]
63pub mod prop_tests {
64    use crate::PeerResponse;
65    use snarkvm::utilities::{FromBytes, ToBytes};
66
67    use bytes::{Buf, BufMut, BytesMut};
68    use proptest::{
69        collection::vec,
70        prelude::{BoxedStrategy, Strategy, any},
71    };
72    use std::net::{IpAddr, SocketAddr};
73    use test_strategy::proptest;
74
75    pub fn any_valid_socket_addr() -> BoxedStrategy<SocketAddr> {
76        any::<(IpAddr, u16)>().prop_map(|(ip_addr, port)| SocketAddr::new(ip_addr, port)).boxed()
77    }
78
79    pub fn any_vec() -> BoxedStrategy<Vec<SocketAddr>> {
80        vec(any_valid_socket_addr(), 0..50).prop_map(|v| v).boxed()
81    }
82
83    pub fn any_peer_response() -> BoxedStrategy<PeerResponse> {
84        any_vec().prop_map(|peers| PeerResponse { peers }).boxed()
85    }
86
87    #[proptest]
88    fn peer_response_roundtrip(#[strategy(any_peer_response())] peer_response: PeerResponse) {
89        let mut bytes = BytesMut::default().writer();
90        peer_response.write_le(&mut bytes).unwrap();
91        let decoded = PeerResponse::read_le(&mut bytes.into_inner().reader()).unwrap();
92        assert_eq!(decoded, peer_response);
93    }
94}