ant_protocol/
version.rs

1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use lazy_static::lazy_static;
10use std::sync::RwLock;
11
12lazy_static! {
13    /// The network_id is used to differentiate between different networks.
14    /// The default is set to 1 and it represents the mainnet.
15    pub static ref NETWORK_ID: RwLock<u8> = RwLock::new(1);
16
17    /// The node version used during Identify Behaviour.
18    pub static ref IDENTIFY_NODE_VERSION_STR: RwLock<String> =
19        RwLock::new(format!(
20            "ant/node/{}/{}",
21            get_truncate_version_str(),
22            *NETWORK_ID.read().expect("Failed to obtain read lock for NETWORK_ID"),
23        ));
24
25    /// The client version used during Identify Behaviour.
26    pub static ref IDENTIFY_CLIENT_VERSION_STR: RwLock<String> =
27        RwLock::new(format!(
28            "ant/client/{}/{}",
29            get_truncate_version_str(),
30            *NETWORK_ID.read().expect("Failed to obtain read lock for NETWORK_ID"),
31        ));
32
33    /// The req/response protocol version
34    pub static ref REQ_RESPONSE_VERSION_STR: RwLock<String> =
35        RwLock::new(format!(
36            "/ant/{}/{}",
37            get_truncate_version_str(),
38            *NETWORK_ID.read().expect("Failed to obtain read lock for NETWORK_ID"),
39        ));
40
41    /// The identify protocol version
42    pub static ref IDENTIFY_PROTOCOL_STR: RwLock<String> =
43        RwLock::new(format!(
44            "ant/{}/{}",
45            get_truncate_version_str(),
46            *NETWORK_ID.read().expect("Failed to obtain read lock for NETWORK_ID"),
47        ));
48}
49
50/// Update the NETWORK_ID. The other version strings will reference this value.
51/// By default, the network id is set to 1 which represents the mainnet.
52///
53/// This should be called before starting the node or client.
54/// The values will be read often and this can cause issues if the values are changed after the node is started.
55pub fn set_network_id(id: u8) {
56    info!("Setting network id to: {id}");
57    let mut network_id = NETWORK_ID
58        .write()
59        .expect("Failed to obtain write lock for NETWORK_ID");
60    *network_id = id;
61    info!("Network id set to: {id}");
62}
63
64/// Get the current NETWORK_ID as string.
65pub fn get_network_id() -> String {
66    format!(
67        "{}",
68        *NETWORK_ID
69            .read()
70            .expect("Failed to obtain read lock for NETWORK_ID")
71    )
72}
73
74// Protocol support shall be downward compatible for patch only version update.
75// i.e. versions of `A.B.X` or `A.B.X-alpha.Y` shall be considered as a same protocol of `A.B`
76pub fn get_truncate_version_str() -> String {
77    let version_str = env!("CARGO_PKG_VERSION");
78    let parts = version_str.split('.').collect::<Vec<_>>();
79    if parts.len() >= 2 {
80        format!("{}.{}", parts[0], parts[1])
81    } else {
82        panic!("Cannot obtain truncated version str for {version_str:?}: {parts:?}");
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn test_print_version_strings() -> Result<(), Box<dyn std::error::Error>> {
92        set_network_id(3);
93        println!(
94            "\nIDENTIFY_NODE_VERSION_STR: {}",
95            *IDENTIFY_NODE_VERSION_STR
96                .read()
97                .expect("Failed to obtain read lock for IDENTIFY_NODE_VERSION_STR")
98        );
99        println!(
100            "IDENTIFY_CLIENT_VERSION_STR: {}",
101            *IDENTIFY_CLIENT_VERSION_STR
102                .read()
103                .expect("Failed to obtain read lock for IDENTIFY_CLIENT_VERSION_STR")
104        );
105        println!(
106            "REQ_RESPONSE_VERSION_STR: {}",
107            *REQ_RESPONSE_VERSION_STR
108                .read()
109                .expect("Failed to obtain read lock for REQ_RESPONSE_VERSION_STR")
110        );
111        println!(
112            "IDENTIFY_PROTOCOL_STR: {}",
113            *IDENTIFY_PROTOCOL_STR
114                .read()
115                .expect("Failed to obtain read lock for IDENTIFY_PROTOCOL_STR")
116        );
117
118        // Test truncated version string
119        let truncated = get_truncate_version_str();
120        println!("\nTruncated version: {truncated}");
121
122        // Test network id string
123        let network_id = get_network_id();
124        println!("Network ID string: {network_id}");
125
126        Ok(())
127    }
128}