Skip to main content

snarkos_node_network/
lib.rs

1// Copyright (c) 2019-2026 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
16#![forbid(unsafe_code)]
17
18pub mod node_type;
19pub use node_type::*;
20
21pub mod peer;
22pub use peer::*;
23
24pub mod peering;
25pub use peering::*;
26
27pub mod resolver;
28pub use resolver::*;
29
30use snarkvm::prelude::Network;
31
32use smol_str::SmolStr;
33use socket2::SockRef;
34use std::{env::VarError, io, net::SocketAddr, str::FromStr, time::Duration};
35use tokio::net::TcpStream;
36use tracing::*;
37
38// Include the generated build information.
39pub mod built_info {
40    include!(concat!(env!("OUT_DIR"), "/built.rs"));
41}
42
43/// Returns the list of bootstrap peers.
44#[allow(clippy::if_same_then_else)]
45pub fn bootstrap_peers<N: Network>(is_dev: bool) -> Vec<SocketAddr> {
46    if cfg!(feature = "test") || is_dev {
47        // Development testing contains optional bootstrap peers loaded from the environment.
48        match std::env::var("TEST_BOOTSTRAP_PEERS") {
49            Ok(peers) => peers.split(',').map(|peer| SocketAddr::from_str(peer).unwrap()).collect(),
50            Err(VarError::NotPresent) => {
51                // Return an empty list if the environment variable is not present.
52                vec![]
53            }
54            Err(err) => {
55                // Log other errors, e.g., invalid encoding.
56                warn!("Failed to load bootstrap peers from environment: {err}");
57                vec![]
58            }
59        }
60    } else if N::ID == snarkvm::console::network::MainnetV0::ID {
61        // Mainnet contains the following bootstrap peers.
62        vec![
63            SocketAddr::from_str("35.231.67.219:4130").unwrap(),
64            SocketAddr::from_str("34.73.195.196:4130").unwrap(),
65            SocketAddr::from_str("34.23.225.202:4130").unwrap(),
66            SocketAddr::from_str("34.148.16.111:4130").unwrap(),
67        ]
68    } else if N::ID == snarkvm::console::network::TestnetV0::ID {
69        // TestnetV0 contains the following bootstrap peers.
70        vec![
71            SocketAddr::from_str("34.138.104.159:4130").unwrap(),
72            SocketAddr::from_str("35.231.46.237:4130").unwrap(),
73            SocketAddr::from_str("34.148.251.155:4130").unwrap(),
74            SocketAddr::from_str("35.190.141.234:4130").unwrap(),
75        ]
76    } else if N::ID == snarkvm::console::network::CanaryV0::ID {
77        // CanaryV0 contains the following bootstrap peers.
78        vec![
79            SocketAddr::from_str("34.139.88.58:4130").unwrap(),
80            SocketAddr::from_str("34.139.252.207:4130").unwrap(),
81            SocketAddr::from_str("35.185.98.12:4130").unwrap(),
82            SocketAddr::from_str("35.231.106.26:4130").unwrap(),
83        ]
84    } else {
85        // Unrecognized networks contain no bootstrap peers.
86        vec![]
87    }
88}
89
90/// Get our SHA from the build information (or None if it is not set or does not 40 bytes long).
91pub fn get_repo_commit_hash() -> Option<[u8; 40]> {
92    built_info::GIT_COMMIT_HASH.and_then(|sha| sha.as_bytes().try_into().ok())
93}
94
95/// Logs the peer's snarkOS repo SHA and how it compares to ours.
96pub fn log_repo_sha_comparison(peer_addr: SocketAddr, peer_sha: &Option<[u8; 40]>, ctx: &str) {
97    let our_sha = get_repo_commit_hash();
98
99    // Generate a string representation for the peers hash.
100    let peer_sha_str: Option<&str> = peer_sha.as_ref().and_then(|h| str::from_utf8(h).ok());
101
102    let sha_cmp = match (&our_sha, peer_sha, peer_sha_str) {
103        // They sent no hash, or an invalid string.
104        (_, _, None) | (_, None, _) => " with an unknown repo SHA".to_owned(),
105        // Our hash cannot be retrieved.
106        (None, _, Some(theirs_str)) => format!("@{theirs_str} (potentially different than us)"),
107        // Both hashes are valid. Compare.
108        (Some(ours), Some(theirs), Some(theirs_str)) => {
109            if ours == theirs {
110                format!("@{theirs_str} (same as us)")
111            } else {
112                format!("@{theirs_str} (different than us)")
113            }
114        }
115    };
116
117    debug!("{ctx} Peer '{peer_addr}' uses snarkOS{sha_cmp}");
118}
119
120/// Shortens the commit SHA.
121pub fn shorten_snarkos_sha(sha: &Option<[u8; 40]>) -> SmolStr {
122    if let Some(full_sha) = sha.as_ref().and_then(|s| str::from_utf8(s).ok()) {
123        let end_idx = full_sha.char_indices()
124            .nth(7) // GitHub commit SHA shorthand.
125            .map(|(i, _)| i)
126            .unwrap_or(full_sha.len()); // Can't really fail.
127
128        SmolStr::from(&full_sha[..end_idx])
129    } else {
130        "unknown snarkOS SHA".into()
131    }
132}
133
134/// Adjusts the low-level socket settings for extra robustness.
135pub fn harden_socket(stream: &TcpStream) -> io::Result<()> {
136    let socket = SockRef::from(stream);
137
138    // Make OS-level disconnects immediate (no TIME_WAIT).
139    socket.set_linger(Some(Duration::from_secs(0)))?;
140
141    // Disable Nagle's algorithm for lower latency.
142    socket.set_tcp_nodelay(true)?;
143
144    // Disconnect if unacknowledged data stalls for 20s. This protects
145    // the kernel's retransmission queue.
146    #[cfg(target_os = "linux")]
147    socket.set_tcp_user_timeout(Some(Duration::from_secs(20)))?;
148
149    Ok(())
150}