1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkOS library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

use snarkvm::prelude::{FromBytes, ToBytes};

use std::borrow::Cow;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PeerResponse {
    pub peers: Vec<SocketAddr>,
}

impl MessageTrait for PeerResponse {
    /// Returns the message name.
    #[inline]
    fn name(&self) -> Cow<'static, str> {
        "PeerResponse".into()
    }
}

impl ToBytes for PeerResponse {
    fn write_le<W: io::Write>(&self, mut writer: W) -> io::Result<()> {
        // Restrict the maximum number of peers to share.
        (self.peers.len().min(u8::MAX as usize) as u8).write_le(&mut writer)?;
        for peer in self.peers.iter().take(u8::MAX as usize) {
            peer.write_le(&mut writer)?;
        }
        Ok(())
    }
}

impl FromBytes for PeerResponse {
    fn read_le<R: io::Read>(mut reader: R) -> io::Result<Self> {
        let count = u8::read_le(&mut reader)?;
        let mut peers = Vec::with_capacity(count as usize);
        for _ in 0..count {
            peers.push(SocketAddr::read_le(&mut reader)?);
        }

        Ok(Self { peers })
    }
}

#[cfg(test)]
pub mod prop_tests {
    use crate::PeerResponse;
    use snarkvm::utilities::{FromBytes, ToBytes};

    use bytes::{Buf, BufMut, BytesMut};
    use proptest::{
        collection::vec,
        prelude::{any, BoxedStrategy, Strategy},
    };
    use std::net::{IpAddr, SocketAddr};
    use test_strategy::proptest;

    pub fn any_valid_socket_addr() -> BoxedStrategy<SocketAddr> {
        any::<(IpAddr, u16)>().prop_map(|(ip_addr, port)| SocketAddr::new(ip_addr, port)).boxed()
    }

    pub fn any_vec() -> BoxedStrategy<Vec<SocketAddr>> {
        vec(any_valid_socket_addr(), 0..50).prop_map(|v| v).boxed()
    }

    pub fn any_peer_response() -> BoxedStrategy<PeerResponse> {
        any_vec().prop_map(|peers| PeerResponse { peers }).boxed()
    }

    #[proptest]
    fn peer_response_roundtrip(#[strategy(any_peer_response())] peer_response: PeerResponse) {
        let mut bytes = BytesMut::default().writer();
        peer_response.write_le(&mut bytes).unwrap();
        let decoded = PeerResponse::read_le(&mut bytes.into_inner().reader()).unwrap();
        assert_eq!(decoded, peer_response);
    }
}