snarkos_node_network/
lib.rs

1// Copyright (c) 2019-2025 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 std::{net::SocketAddr, str::FromStr};
33use tracing::*;
34
35// Include the generated build information.
36pub mod built_info {
37    include!(concat!(env!("OUT_DIR"), "/built.rs"));
38}
39
40/// Returns the list of bootstrap peers.
41#[allow(clippy::if_same_then_else)]
42pub fn bootstrap_peers<N: Network>(is_dev: bool) -> Vec<SocketAddr> {
43    if cfg!(feature = "test") || is_dev {
44        // Development testing contains optional bootstrap peers loaded from the environment.
45        match std::env::var("TEST_BOOTSTRAP_PEERS") {
46            Ok(peers) => peers.split(',').map(|peer| SocketAddr::from_str(peer).unwrap()).collect(),
47            Err(err) => {
48                warn!("Failed to load bootstrap peers from environment: {err}");
49                vec![]
50            }
51        }
52    } else if N::ID == snarkvm::console::network::MainnetV0::ID {
53        // Mainnet contains the following bootstrap peers.
54        vec![
55            SocketAddr::from_str("35.231.67.219:4130").unwrap(),
56            SocketAddr::from_str("34.73.195.196:4130").unwrap(),
57            SocketAddr::from_str("34.23.225.202:4130").unwrap(),
58            SocketAddr::from_str("34.148.16.111:4130").unwrap(),
59        ]
60    } else if N::ID == snarkvm::console::network::TestnetV0::ID {
61        // TestnetV0 contains the following bootstrap peers.
62        vec![
63            SocketAddr::from_str("34.138.104.159:4130").unwrap(),
64            SocketAddr::from_str("35.231.46.237:4130").unwrap(),
65            SocketAddr::from_str("34.148.251.155:4130").unwrap(),
66            SocketAddr::from_str("35.190.141.234:4130").unwrap(),
67        ]
68    } else if N::ID == snarkvm::console::network::CanaryV0::ID {
69        // CanaryV0 contains the following bootstrap peers.
70        vec![
71            SocketAddr::from_str("34.139.88.58:4130").unwrap(),
72            SocketAddr::from_str("34.139.252.207:4130").unwrap(),
73            SocketAddr::from_str("35.185.98.12:4130").unwrap(),
74            SocketAddr::from_str("35.231.106.26:4130").unwrap(),
75        ]
76    } else {
77        // Unrecognized networks contain no bootstrap peers.
78        vec![]
79    }
80}
81
82/// Logs the peer's snarkOS repo SHA and how it compares to ours.
83pub fn log_repo_sha_comparison(peer_addr: SocketAddr, peer_sha: Option<&String>, ctx: &str) {
84    let our_sha = built_info::GIT_COMMIT_HASH.unwrap_or_default();
85    let unknown_sha = "unknown".to_owned();
86    let peer_sha = peer_sha.unwrap_or(&unknown_sha);
87    let sha_cmp = if peer_sha == &unknown_sha {
88        " with an unknown repo SHA".to_owned()
89    } else if peer_sha == our_sha {
90        format!("@{peer_sha} (same as us)")
91    } else if our_sha.is_empty() {
92        format!("@{peer_sha} (potentially different than us)")
93    } else {
94        format!("@{peer_sha} (different than us)")
95    };
96
97    debug!("{ctx} Peer '{peer_addr}' uses snarkOS{sha_cmp}");
98}