Skip to main content

tari_comms/peer_manager/
peer_features.rs

1// Copyright 2019, The Tari Project
2//
3// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4// following conditions are met:
5//
6// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7// disclaimer.
8//
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10// following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13// products derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
21// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23use std::fmt;
24
25use bitflags::bitflags;
26use serde::{Deserialize, Serialize};
27
28bitflags! {
29    /// Peer feature flags. These advertised the capabilities of peer nodes.
30    #[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
31    pub struct PeerFeatures: u32 {
32        /// No capabilities
33        const NONE = 0b0000_0000;
34        /// Node is able to propagate messages
35        const MESSAGE_PROPAGATION = 0b0000_0001;
36        /// Legacy compatibility for nodes that used to offer store and forward functionality; this is no longer used,
37        /// but it will change the node's public key if it is not present.
38        const DHT_STORE_FORWARD = 0b0000_0010;
39        /// Node is a communication node (typically a base layer node)
40        const COMMUNICATION_NODE = Self::MESSAGE_PROPAGATION.bits() | Self::DHT_STORE_FORWARD.bits();
41        /// Node is a network client
42        const COMMUNICATION_CLIENT = Self::NONE.bits();
43    }
44}
45
46const UNKNOWN_PEER_FEATURE: i32 = -1;
47
48impl PeerFeatures {
49    /// Returns true if these flags represent a COMMUNICATION_CLIENT.
50    #[inline]
51    pub fn is_client(self) -> bool {
52        self == PeerFeatures::COMMUNICATION_CLIENT
53    }
54
55    /// Returns true if these flags represent a COMMUNICATION_NODE.
56    #[inline]
57    pub fn is_node(self) -> bool {
58        self == PeerFeatures::COMMUNICATION_NODE
59    }
60
61    /// Returns a human-readable string that represents these flags.
62    pub fn as_role_str(self) -> &'static str {
63        match self {
64            PeerFeatures::COMMUNICATION_NODE => "node",
65            PeerFeatures::COMMUNICATION_CLIENT => "client",
66            _ => "unknown",
67        }
68    }
69
70    /// Returns the bits of the PeerFeatures as an i32
71    pub fn to_i32(&self) -> i32 {
72        match *self {
73            PeerFeatures::MESSAGE_PROPAGATION => 1,
74            PeerFeatures::DHT_STORE_FORWARD => 2,
75            PeerFeatures::COMMUNICATION_NODE => 3,
76            PeerFeatures::COMMUNICATION_CLIENT => 0,
77            _ => UNKNOWN_PEER_FEATURE, // All cases must be handled explicitly,
78        }
79    }
80}
81
82impl Default for PeerFeatures {
83    fn default() -> Self {
84        PeerFeatures::NONE
85    }
86}
87
88impl fmt::Display for PeerFeatures {
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        write!(f, "{self:?}")
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn test_to_i32_conversion() {
100        for feature in PeerFeatures::all() {
101            assert_ne!(
102                feature.to_i32(),
103                UNKNOWN_PEER_FEATURE,
104                "Failed for feature: {feature:?}",
105            );
106            match feature {
107                PeerFeatures::NONE => assert_eq!(feature.to_i32(), 0),
108                PeerFeatures::MESSAGE_PROPAGATION => assert_eq!(feature.to_i32(), 1),
109                PeerFeatures::DHT_STORE_FORWARD => assert_eq!(feature.to_i32(), 2),
110                PeerFeatures::COMMUNICATION_NODE => assert_eq!(feature.to_i32(), 3),
111                _ => panic!("Unexpected feature: {feature:?}"),
112            }
113        }
114    }
115}