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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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 indexmap::IndexMap;
use std::borrow::Cow;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Ping<N: Network> {
    pub version: u32,
    pub node_type: NodeType,
    pub block_locators: Option<BlockLocators<N>>,
}

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

impl<N: Network> ToBytes for Ping<N> {
    fn write_le<W: io::Write>(&self, mut writer: W) -> io::Result<()> {
        self.version.write_le(&mut writer)?;
        self.node_type.write_le(&mut writer)?;
        if let Some(locators) = &self.block_locators {
            1u8.write_le(&mut writer)?;

            (locators.recents.len().min(u32::MAX as usize) as u32).write_le(&mut writer)?;
            for (height, hash) in locators.recents.iter() {
                height.write_le(&mut writer)?;
                hash.write_le(&mut writer)?;
            }

            (locators.checkpoints.len().min(u32::MAX as usize) as u32).write_le(&mut writer)?;
            for (height, hash) in locators.checkpoints.iter() {
                height.write_le(&mut writer)?;
                hash.write_le(&mut writer)?;
            }
        } else {
            0u8.write_le(&mut writer)?;
        }

        Ok(())
    }
}

impl<N: Network> FromBytes for Ping<N> {
    fn read_le<R: io::Read>(mut reader: R) -> io::Result<Self> {
        let version = u32::read_le(&mut reader)?;
        let node_type = NodeType::read_le(&mut reader)?;

        if u8::read_le(&mut reader)? == 0 {
            return Ok(Self { version, node_type, block_locators: None });
        }

        let mut recents = IndexMap::new();
        let num_recents = u32::read_le(&mut reader)?;
        for _ in 0..num_recents {
            let height = u32::read_le(&mut reader)?;
            let hash = N::BlockHash::read_le(&mut reader)?;
            recents.insert(height, hash);
        }

        let mut checkpoints = IndexMap::new();
        let num_checkpoints = u32::read_le(&mut reader)?;
        for _ in 0..num_checkpoints {
            let height = u32::read_le(&mut reader)?;
            let hash = N::BlockHash::read_le(&mut reader)?;
            checkpoints.insert(height, hash);
        }

        let block_locators = Some(BlockLocators { recents, checkpoints });

        Ok(Self { version, node_type, block_locators })
    }
}

impl<N: Network> Ping<N> {
    pub fn new(node_type: NodeType, block_locators: Option<BlockLocators<N>>) -> Self {
        Self { version: <Message<N>>::VERSION, node_type, block_locators }
    }
}

#[cfg(test)]
pub mod prop_tests {
    use crate::{challenge_request::prop_tests::any_node_type, Ping};
    use snarkos_node_sync_locators::{test_helpers::sample_block_locators, BlockLocators};
    use snarkvm::utilities::{FromBytes, ToBytes};

    use bytes::{Buf, BufMut, BytesMut};
    use proptest::prelude::{any, BoxedStrategy, Strategy};
    use test_strategy::proptest;

    type CurrentNetwork = snarkvm::prelude::Testnet3;

    pub fn any_block_locators() -> BoxedStrategy<BlockLocators<CurrentNetwork>> {
        any::<u32>().prop_map(sample_block_locators).boxed()
    }

    pub fn any_ping() -> BoxedStrategy<Ping<CurrentNetwork>> {
        (any::<u32>(), any_block_locators(), any_node_type())
            .prop_map(|(version, bls, node_type)| Ping { version, block_locators: Some(bls), node_type })
            .boxed()
    }

    #[proptest]
    fn ping_roundtrip(#[strategy(any_ping())] ping: Ping<CurrentNetwork>) {
        let mut bytes = BytesMut::default().writer();
        ping.write_le(&mut bytes).unwrap();
        let decoded = Ping::<CurrentNetwork>::read_le(&mut bytes.into_inner().reader()).unwrap();
        assert_eq!(ping, decoded);
    }
}